Skip to content

Commit

Permalink
fix: focus selected item set before filtered items (#3769) (#3784)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaadin-bot committed May 5, 2022
1 parent b40e522 commit 4e5893d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/combo-box/src/vaadin-combo-box-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,11 @@ export const ComboBoxMixin = (subclass) =>
this._selectItemForValue(this.value);
}

if (this._inputElementValue === undefined || this._inputElementValue === this.value) {
const inputValue = this._inputElementValue;
if (inputValue === undefined || inputValue === this._getItemLabel(this.selectedItem)) {
// When the input element value is the same as the current value or not defined,
// set the focused index to the item that matches the value.
this._focusedIndex = this._indexOfValue(this.value, this.filteredItems);
this._focusedIndex = this.$.dropdown.indexOfLabel(this._getItemLabel(this.selectedItem));
} else {
// When the user filled in something that is different from the current value = filtering is enabled,
// set the focused index to the item that matches the filter query.
Expand Down
27 changes: 27 additions & 0 deletions packages/combo-box/test/external-filtering.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,31 @@ describe('external filtering', () => {
expect(comboBox.value).to.equal('custom');
});
});

describe('selectedItem is set before', () => {
const items = [
{ label: 'Item 0', value: '0' },
{ label: 'Item 1', value: '1' },
{ label: 'Item 2', value: '2' }
];

beforeEach(() => {
comboBox = fixtureSync(`<vaadin-combo-box></vaadin-combo-box>`);
comboBox.selectedItem = items[0];
comboBox.filteredItems = items;
});

it('should set component value based on selected item', () => {
expect(comboBox.value).to.equal('0');
});

it('should set input value based on selected item', () => {
expect(comboBox.inputElement.value).to.equal('Item 0');
});

it('should have the correct item focused when opened', () => {
comboBox.open();
expect(getFocusedItemIndex(comboBox)).to.equal(0);
});
});
});

0 comments on commit 4e5893d

Please sign in to comment.