Skip to content

Commit 781a02a

Browse files
fix: sync top group if necessary even when dropdown is opened (#9095) (#9255)
Co-authored-by: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com>
1 parent c8969ce commit 781a02a

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/multi-select-combo-box/src/vaadin-multi-select-combo-box-mixin.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,26 @@ export const MultiSelectComboBoxMixin = (superClass) =>
758758
__updateTopGroup(selectedItemsOnTop, selectedItems, opened) {
759759
if (!selectedItemsOnTop) {
760760
this._topGroup = [];
761-
} else if (!opened) {
761+
} else if (!opened || this.__needToSyncTopGroup()) {
762762
this._topGroup = [...selectedItems];
763763
}
764764
}
765765

766+
/** @private */
767+
__needToSyncTopGroup() {
768+
// Only sync for object items
769+
if (!this.itemIdPath) {
770+
return false;
771+
}
772+
return (
773+
this._topGroup &&
774+
this._topGroup.some((item) => {
775+
const selectedItem = this.selectedItems[this._findIndex(item, this.selectedItems, this.itemIdPath)];
776+
return selectedItem && item !== selectedItem;
777+
})
778+
);
779+
}
780+
766781
/** @private */
767782
__createChip(item) {
768783
const chip = document.createElement('vaadin-multi-select-combo-box-chip');

packages/multi-select-combo-box/test/selecting-items.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,22 @@ describe('selecting items', () => {
301301
beforeEach(() => {
302302
comboBox.itemIdPath = 'id';
303303
comboBox.items = items;
304-
comboBox.selectedItems = items.slice(1, 3);
305304
});
306305

307306
it('should show selected items at the top of the overlay', () => {
307+
comboBox.selectedItems = items.slice(1, 3);
308308
comboBox.opened = true;
309309
expectItems(['banana', 'lemon', 'apple', 'orange', 'pear']);
310310
});
311+
312+
it('should synchronize selected item state when overlay is opened', async () => {
313+
comboBox.selectedItems = [{ id: '1', label: 'banana' }];
314+
comboBox.opened = true;
315+
const itemReference = getFirstItem(comboBox).item;
316+
comboBox.selectedItems = [{ id: '1', label: 'banana' }];
317+
await nextRender();
318+
expect(getFirstItem(comboBox).item).to.not.equal(itemReference);
319+
});
311320
});
312321

313322
describe('dataProvider', () => {

0 commit comments

Comments
 (0)