Skip to content

Commit 3a3101f

Browse files
authored
fix: do not jump to focused item after scroll (#3855)
1 parent 97fbfd5 commit 3a3101f

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

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

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

974-
const inputValue = this._inputElementValue;
975-
if (inputValue === undefined || inputValue === this._getItemLabel(this.selectedItem)) {
976-
// When the input element value is the same as the current value or not defined,
977-
// set the focused index to the item that matches the value.
978-
this._focusedIndex = this.$.dropdown.indexOfLabel(this._getItemLabel(this.selectedItem));
979-
} else {
980-
// When the user filled in something that is different from the current value = filtering is enabled,
981-
// set the focused index to the item that matches the filter query.
982-
this._focusedIndex = this.$.dropdown.indexOfLabel(this.filter);
974+
// Do not update `_focusedIndex` to from `-1` to old value during scroll.
975+
// Otherwise, the scroll would jump back to the previously focused item.
976+
if (!this.loading) {
977+
const inputValue = this._inputElementValue;
978+
if (inputValue === undefined || inputValue === this._getItemLabel(this.selectedItem)) {
979+
// When the input element value is the same as the current value or not defined,
980+
// set the focused index to the item that matches the value.
981+
this._focusedIndex = this.$.dropdown.indexOfLabel(this._getItemLabel(this.selectedItem));
982+
} else {
983+
// When the user filled in something that is different from the current value = filtering is enabled,
984+
// set the focused index to the item that matches the filter query.
985+
this._focusedIndex = this.$.dropdown.indexOfLabel(this.filter);
986+
}
983987
}
984988
}
985989
}

‎packages/combo-box/test/lazy-loading.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,28 @@ describe('lazy loading', () => {
410410
enterKeyDown(comboBox.inputElement);
411411
expect(comboBox.value).to.eql('custom value');
412412
});
413+
414+
it('should not jump back to focused item after scroll', async () => {
415+
comboBox.size = SIZE;
416+
comboBox.dataProvider = spyAsyncDataProvider;
417+
comboBox.opened = true;
418+
// Wait for the async data provider to respond
419+
await aTimeout(0);
420+
421+
comboBox.value = 'item 8';
422+
comboBox.close();
423+
424+
comboBox.opened = true;
425+
comboBox.$.dropdown._scrollIntoView(50);
426+
// Wait for the async data provider to respond
427+
await aTimeout(0);
428+
429+
// Wait for the timeout in __loadingChanged
430+
await aTimeout(0);
431+
432+
const items = getViewportItems(comboBox);
433+
expect(items.some((item) => item.index === 50)).to.be.true;
434+
});
413435
});
414436

415437
describe('changing dataProvider', () => {

0 commit comments

Comments
 (0)