Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/mdc-chips/chip-set/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ export class MDCChipSetFoundation extends MDCFoundation<MDCChipSetAdapter> {
this.adapter_.removeChipAtIndex(index);
const maxIndex = this.adapter_.getChipListCount() - 1;
const nextIndex = Math.min(index, maxIndex);

// Do not throw error if chip set is empty
if (nextIndex < 0) {
return;
}

this.removeFocusFromChipsExcept_(nextIndex);
// After removing a chip, we should focus the trailing action for the next chip.
this.adapter_.focusChipTrailingActionAtIndex(nextIndex);
Expand Down
9 changes: 9 additions & 0 deletions test/unit/mdc-chips/mdc-chip-set.foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ test('#handleChipRemoval gives focus to the next chip', () => {
td.verify(mockAdapter.focusChipTrailingActionAtIndex(1));
});

test('#handleChipRemoval does not throw error if chip set is left empty', () => {
const {foundation, mockAdapter} = setupTest();
td.when(mockAdapter.getChipListCount()).thenReturn(0);
td.when(mockAdapter.getIndexOfChipById('chipA')).thenReturn(0);

foundation.handleChipRemoval('chipA');
td.verify(mockAdapter.focusChipTrailingActionAtIndex(-1), {times: 0});
});

function setupChipNavigationTest(chipIds, isRTL=false) {
const {foundation, mockAdapter} = setupTest();
td.when(mockAdapter.getIndexOfChipById(td.matchers.isA(String))).thenDo((id) => {
Expand Down