Skip to content

Commit 00a4f01

Browse files
authored
fix: focus selected item set before filtered items (#3769)
1 parent 8653fc2 commit 00a4f01

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎packages/combo-box/src/vaadin-combo-box-mixin.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,11 @@ export const ComboBoxMixin = (subclass) =>
971971
this._selectItemForValue(this.value);
972972
}
973973

974-
if (this._inputElementValue === undefined || this._inputElementValue === this.value) {
974+
const inputValue = this._inputElementValue;
975+
if (inputValue === undefined || inputValue === this._getItemLabel(this.selectedItem)) {
975976
// When the input element value is the same as the current value or not defined,
976977
// set the focused index to the item that matches the value.
977-
this._focusedIndex = this._indexOfValue(this.value, this.filteredItems);
978+
this._focusedIndex = this.$.dropdown.indexOfLabel(this._getItemLabel(this.selectedItem));
978979
} else {
979980
// When the user filled in something that is different from the current value = filtering is enabled,
980981
// set the focused index to the item that matches the filter query.

‎packages/combo-box/test/external-filtering.test.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,31 @@ describe('external filtering', () => {
227227
expect(comboBox.value).to.equal('custom');
228228
});
229229
});
230+
231+
describe('selectedItem is set before', () => {
232+
const items = [
233+
{ label: 'Item 0', value: '0' },
234+
{ label: 'Item 1', value: '1' },
235+
{ label: 'Item 2', value: '2' },
236+
];
237+
238+
beforeEach(() => {
239+
comboBox = fixtureSync(`<vaadin-combo-box></vaadin-combo-box>`);
240+
comboBox.selectedItem = items[0];
241+
comboBox.filteredItems = items;
242+
});
243+
244+
it('should set component value based on selected item', () => {
245+
expect(comboBox.value).to.equal('0');
246+
});
247+
248+
it('should set input value based on selected item', () => {
249+
expect(comboBox.inputElement.value).to.equal('Item 0');
250+
});
251+
252+
it('should have the correct item focused when opened', () => {
253+
comboBox.open();
254+
expect(getFocusedItemIndex(comboBox)).to.equal(0);
255+
});
256+
});
230257
});

0 commit comments

Comments
 (0)