vaadin / flow-components

Java counterpart of Vaadin Web Components
101 stars 66 forks source link

Value selected in Combobox can change when leaving the component #5184

Open Marczeeee opened 1 year ago

Marczeeee commented 1 year ago

Description of the bug

In a Combobox if the items list contains the same textual values differ only in their case (like 'dr.' and 'Dr.'), when selecting the later one in the list and leaving the Combobox component the selected item is changed to the which is first within the items list.

Expected behavior

If I select an item, it should be remain selected after leaving the Combobox component, even if there is an another item with the same text (but different casing).

Minimal reproducible example

Attached combobox-sample.zip with a project containing a page with only a Combobox on it, populated with items using different cases. If one selects 'Dr.' or 'prof' and then clicks outside the component, the selected value will be changed. Use mvn clean install -production command to build a war file of the project, deploy it in a vanilla Tomcat and open the main page of the application.

Versions

mcollovati commented 1 year ago

It seems like that the correct item is selected, but when moving the focus out of the component, the _commitValue() client function is invoked and then the wrong item is selected because in vaadin-combo-box-mixin the following method does acase insensitive match.

    /** 
     * Returns the first item that matches the provided label.
     * Labels are matched against each other case insensitively.
     *
     * @private
     */
    __getItemIndexByLabel(items, label) {
      if (!items || !label) {
        return -1;
      }

      return findItemIndex(items, (item) => {
        return this._getItemLabel(item).toString().toLowerCase() === label.toString().toLowerCase();
      });
    }

I'm uncertain if this is a bug or an expected behavior, so I'll transfer this issue to the flow-components repository, and the team can provide additional information.

yuriy-fix commented 1 year ago

Unfortunately, at the moment component doesn't support case-sensitive matching for the items. Enabling that one could cause issues for existing applications, so this need to be considered as opt-in enhancement and would require a separate API.