Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit f978109

Browse files
authored
fix(chips): Do not throw error if chip set becomes empty (#5290)
1 parent b8bc4a2 commit f978109

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/mdc-chips/chip-set/foundation.ts

+6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ export class MDCChipSetFoundation extends MDCFoundation<MDCChipSetAdapter> {
110110
this.adapter_.removeChipAtIndex(index);
111111
const maxIndex = this.adapter_.getChipListCount() - 1;
112112
const nextIndex = Math.min(index, maxIndex);
113+
114+
// Early exit if there are no other chips in the set
115+
if (nextIndex < 0) {
116+
return;
117+
}
118+
113119
this.removeFocusFromChipsExcept_(nextIndex);
114120
// After removing a chip, we should focus the trailing action for the next chip.
115121
this.adapter_.focusChipTrailingActionAtIndex(nextIndex);

test/unit/mdc-chips/mdc-chip-set.foundation.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ test('#handleChipRemoval gives focus to the next chip', () => {
245245
td.verify(mockAdapter.focusChipTrailingActionAtIndex(1));
246246
});
247247

248+
test('#handleChipRemoval does not throw error if chip set is left empty', () => {
249+
const {foundation, mockAdapter} = setupTest();
250+
td.when(mockAdapter.getChipListCount()).thenReturn(0);
251+
td.when(mockAdapter.getIndexOfChipById('chipA')).thenReturn(0);
252+
253+
foundation.handleChipRemoval('chipA');
254+
td.verify(mockAdapter.focusChipTrailingActionAtIndex(-1), {times: 0});
255+
});
256+
248257
function setupChipNavigationTest(chipIds, isRTL=false) {
249258
const {foundation, mockAdapter} = setupTest();
250259
td.when(mockAdapter.getIndexOfChipById(td.matchers.isA(String))).thenDo((id) => {

0 commit comments

Comments
 (0)