Skip to content

Commit 411728d

Browse files
authored
fix: apply combo-box filter before opening overlay on input (#12987)
1 parent 094e1df commit 411728d

5 files changed

Lines changed: 58 additions & 12 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ export const ComboBoxBaseMixin = (superClass) =>
180180
this._updateScroller();
181181
}
182182

183-
if (props.has('_highlightState')) {
183+
// An item can be highlighted while the overlay is closed, e.g. when filtering
184+
// on input. Reference it once the overlay is opened.
185+
if (props.has('_highlightState') || (props.has('_overlayOpened') && this._overlayOpened)) {
184186
this._updateActiveDescendant();
185187
}
186188
}

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,34 +205,27 @@ export const ComboBoxItemsMixin = (superClass) =>
205205
}
206206

207207
/**
208-
* Override an event listener from `ComboBoxBaseMixin` to handle
209-
* batched setting of both `opened` and `filter` properties.
208+
* Override an event listener from `ComboBoxBaseMixin` to update
209+
* the filter and open the dropdown on user input.
210210
* @param {!Event} event
211211
* @protected
212212
* @override
213213
*/
214214
_onInput(event) {
215215
const filter = this._inputElementValue;
216216

217-
// When opening dropdown on user input, both `opened` and `filter` properties are set.
218-
// Perform a batched property update instead of relying on sync property observers.
219-
// This is necessary to avoid an extra data-provider request for loading first page.
220-
const props = {};
221-
222217
if (this.filter === filter) {
223218
// Filter and input value might get out of sync, while keyboard navigating for example.
224219
// Afterwards, input value might be changed to the same value as used in filtering.
225220
// In situation like these, we need to make sure all the filter changes handlers are run.
226221
this._filterChanged(this.filter);
227222
} else {
228-
props.filter = filter;
223+
this.filter = filter;
229224
}
230225

231226
if (!this.opened && !this._isClearButton(event) && !this.autoOpenDisabled) {
232-
props.opened = true;
227+
this.opened = true;
233228
}
234-
235-
this.setProperties(props);
236229
}
237230

238231
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ export const ComboBoxScrollerMixin = (superClass) =>
284284
}
285285

286286
this.requestContentUpdate();
287+
288+
// The focused index can change while closed, when scrolling to it is skipped
289+
if (this.focusedIndex >= 0 && !this.loading) {
290+
this.scrollIntoView(this.focusedIndex);
291+
}
287292
return;
288293
}
289294

‎packages/combo-box/test/data-provider-filtering.test.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,39 @@ describe('data provider filtering', () => {
139139
expect(comboBox.$.overlay.opened).to.be.false;
140140
});
141141
});
142+
143+
describe('opening on input', () => {
144+
let overlay, openedChangedSpy;
145+
146+
function syncDataProvider(params, callback) {
147+
const items = ['Item 1', 'Item 2', 'Item 3'].filter((item) => item.includes(params.filter));
148+
callback(items, items.length);
149+
}
150+
151+
beforeEach(async () => {
152+
comboBox = fixtureSync('<vaadin-combo-box></vaadin-combo-box>');
153+
await nextRender();
154+
overlay = comboBox.$.overlay;
155+
openedChangedSpy = sinon.spy();
156+
overlay.addEventListener('opened-changed', openedChangedSpy);
157+
comboBox.inputElement.focus();
158+
});
159+
160+
it('should not open overlay when no items match the filter with sync data provider', () => {
161+
comboBox.dataProvider = syncDataProvider;
162+
setInputValue(comboBox, 'no match');
163+
expect(comboBox.opened).to.be.true;
164+
expect(overlay.opened).to.be.false;
165+
expect(openedChangedSpy).to.be.not.called;
166+
});
167+
168+
it('should open overlay while loading with async data provider', () => {
169+
// Data provider that does not respond, like a pending server request
170+
comboBox.dataProvider = sinon.spy();
171+
setInputValue(comboBox, 'Item');
172+
expect(comboBox.dataProvider).to.be.calledOnce;
173+
expect(comboBox.loading).to.be.true;
174+
expect(overlay.opened).to.be.true;
175+
});
176+
});
142177
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ describe('internal filtering', () => {
148148
expect(comboBox.filteredItems).to.eql(['bar', 'baz']);
149149
});
150150

151+
it('should not open overlay on input when no items match the filter', () => {
152+
const openedChangedSpy = sinon.spy();
153+
overlay.addEventListener('opened-changed', openedChangedSpy);
154+
155+
setInputValue(comboBox, 'qux');
156+
157+
expect(comboBox.opened).to.be.true;
158+
expect(overlay.opened).to.be.false;
159+
expect(openedChangedSpy).to.be.not.called;
160+
});
161+
151162
it('should filter out all items with a invalid filter', () => {
152163
setInputValue(comboBox, 'qux');
153164

0 commit comments

Comments
 (0)