Skip to content

Commit c4eb6bb

Browse files
authored
fix: clear selection when using clear() method (#4140)
1 parent bc8286a commit c4eb6bb

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎packages/multi-select-combo-box/src/vaadin-multi-select-combo-box.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ declare class MultiSelectComboBox<TItem = ComboBoxDefaultItem> extends HTMLEleme
303303
*/
304304
clearCache(): void;
305305

306+
/**
307+
* Clears the selected items.
308+
*/
309+
clear(): void;
310+
306311
/**
307312
* Requests an update for the content of items.
308313
* While performing the update, it invokes the renderer (passed in the `renderer` property) once an item.

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,15 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
510510
return this.required && !this.readonly ? this._hasValue : true;
511511
}
512512

513+
/**
514+
* Clears the selected items.
515+
*/
516+
clear() {
517+
this.__updateSelection([]);
518+
519+
announce(this.i18n.cleared);
520+
}
521+
513522
/**
514523
* Clears the cached pages and reloads data from data provider when needed.
515524
*/
@@ -895,9 +904,7 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
895904
_onClearButtonClick(event) {
896905
event.stopPropagation();
897906

898-
this.__updateSelection([]);
899-
900-
announce(this.i18n.cleared);
907+
this.clear();
901908
}
902909

903910
/**

‎packages/multi-select-combo-box/test/accessibility.test.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ describe('accessibility', () => {
170170
expect(region.textContent).to.equal('Selection cleared');
171171
});
172172

173+
it('should announce when using clear() method', () => {
174+
comboBox.selectedItems = [apple, banana, lemon];
175+
comboBox.clearButtonVisible = true;
176+
177+
comboBox.clear();
178+
179+
clock.tick(150);
180+
181+
expect(region.textContent).to.equal('Selection cleared');
182+
});
183+
173184
it('should announce when focusing a chip with keyboard', async () => {
174185
comboBox.selectedItems = [apple];
175186

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ describe('selecting items', () => {
104104
await sendKeys({ down: 'Tab' });
105105
expect(comboBox.selectedItems).to.deep.equal(['apple']);
106106
});
107+
108+
it('should un-select item when using clear() method', () => {
109+
comboBox.selectedItems = ['orange'];
110+
comboBox.clear();
111+
expect(comboBox.selectedItems).to.deep.equal([]);
112+
});
107113
});
108114

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

0 commit comments

Comments
 (0)