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

Commit 73ab5a0

Browse files
author
Matt Goo
authored
feat(chips): make deselect and toggleSelect private. Update handleChipInteraction/Removal API (#3617)
BREAKING CHANGE: deselect and toggleSelect are private methods. handleChipInteraction and handleChipRemoval now accept chipId instead of an event.
1 parent 313618a commit 73ab5a0

File tree

5 files changed

+55
-86
lines changed

5 files changed

+55
-86
lines changed

packages/mdc-chips/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,9 @@ Method Signature | Description
380380
--- | ---
381381
`getSelectedChipIds() => boolean` | Returns an array of the IDs of all selected chips
382382
`select(chipId: string) => void` | Selects the chip with the given id
383-
`deselect(chipId: string) => void` | Deselects the chip with the given id
384-
`toggleSelect(chipId: string) => void` | Toggles selection of the chip with the given id
385-
`handleChipInteraction(evt: Event) => void` | Handles a custom `MDCChip:interaction` event on the root element
386-
`handleChipSelection(evt: Event) => void` | Handles a custom `MDCChip:selection` event on the root element
387-
`handleChipRemoval(evt: Event) => void` | Handles a custom `MDCChip:removal` event on the root element
383+
`handleChipInteraction(chipId: string) => void` | Handles a custom `MDCChip:interaction` event on the root element
384+
`handleChipSelection(chipId: string, selected: boolean) => void` | Handles a custom `MDCChip:selection` event on the root element
385+
`handleChipRemoval(chipId: string) => void` | Handles a custom `MDCChip:removal` event on the root element
388386

389387
#### `MDCChipSetFoundation` Event Handlers
390388

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ class MDCChipSetFoundation extends MDCFoundation {
7878

7979
/**
8080
* Toggles selection of the chip with the given id.
81+
* @private
8182
* @param {string} chipId
8283
*/
83-
toggleSelect(chipId) {
84+
toggleSelect_(chipId) {
8485
if (this.selectedChipIds_.indexOf(chipId) >= 0) {
85-
this.deselect(chipId);
86+
this.deselect_(chipId);
8687
} else {
8788
this.select(chipId);
8889
}
@@ -108,9 +109,10 @@ class MDCChipSetFoundation extends MDCFoundation {
108109

109110
/**
110111
* Deselects the chip with the given id.
112+
* @private
111113
* @param {string} chipId
112114
*/
113-
deselect(chipId) {
115+
deselect_(chipId) {
114116
const index = this.selectedChipIds_.indexOf(chipId);
115117
if (index >= 0) {
116118
this.selectedChipIds_.splice(index, 1);
@@ -120,36 +122,34 @@ class MDCChipSetFoundation extends MDCFoundation {
120122

121123
/**
122124
* Handles a chip interaction event
123-
* @param {!MDCChipInteractionEventType} evt
125+
* @param {string} chipId
124126
*/
125-
handleChipInteraction(evt) {
126-
const {chipId} = evt.detail;
127+
handleChipInteraction(chipId) {
127128
if (this.adapter_.hasClass(cssClasses.CHOICE) || this.adapter_.hasClass(cssClasses.FILTER)) {
128-
this.toggleSelect(chipId);
129+
this.toggleSelect_(chipId);
129130
}
130131
}
131132

132133
/**
133134
* Handles a chip selection event, used to handle discrepancy when selection state is set directly on the Chip.
134-
* @param {!MDCChipSelectionEventType} evt
135+
* @param {string} chipId
136+
* @param {boolean} selected
135137
*/
136-
handleChipSelection(evt) {
137-
const {chipId, selected} = evt.detail;
138+
handleChipSelection(chipId, selected) {
138139
const chipIsSelected = this.selectedChipIds_.indexOf(chipId) >= 0;
139140
if (selected && !chipIsSelected) {
140141
this.select(chipId);
141142
} else if (!selected && chipIsSelected) {
142-
this.deselect(chipId);
143+
this.deselect_(chipId);
143144
}
144145
}
145146

146147
/**
147148
* Handles the event when a chip is removed.
148-
* @param {!MDCChipRemovalEventType} evt
149+
* @param {string} chipId
149150
*/
150-
handleChipRemoval(evt) {
151-
const {chipId} = evt.detail;
152-
this.deselect(chipId);
151+
handleChipRemoval(chipId) {
152+
this.deselect_(chipId);
153153
this.adapter_.removeChip(chipId);
154154
}
155155
}

packages/mdc-chips/chip-set/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ class MDCChipSet extends MDCComponent {
7777
}
7878
});
7979

80-
this.handleChipInteraction_ = (evt) => this.foundation_.handleChipInteraction(evt);
81-
this.handleChipSelection_ = (evt) => this.foundation_.handleChipSelection(evt);
82-
this.handleChipRemoval_ = (evt) => this.foundation_.handleChipRemoval(evt);
80+
this.handleChipInteraction_ = (evt) => this.foundation_.handleChipInteraction(evt.detail.chipId);
81+
this.handleChipSelection_ = (evt) => this.foundation_.handleChipSelection(evt.detail.chipId, evt.detail.selected);
82+
this.handleChipRemoval_ = (evt) => this.foundation_.handleChipRemoval(evt.detail.chipId);
8383
this.root_.addEventListener(
8484
MDCChipFoundation.strings.INTERACTION_EVENT, this.handleChipInteraction_);
8585
this.root_.addEventListener(

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

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ test('in filter chips, #select does nothing if chip is already selected', () =>
106106
assert.equal(foundation.getSelectedChipIds().length, 1);
107107
});
108108

109-
test('in filter chips, #deselect deselects selected chips', () => {
109+
test('in filter chips, #handleChipInteraction deselects chip if in selectedChipId', () => {
110110
const {foundation, mockAdapter} = setupTest();
111111
td.when(mockAdapter.hasClass(cssClasses.FILTER)).thenReturn(true);
112-
foundation.select('chipA');
113-
foundation.select('chipB');
112+
foundation.handleChipInteraction('chipA');
113+
foundation.handleChipInteraction('chipB');
114114
assert.equal(foundation.getSelectedChipIds().length, 2);
115115

116-
foundation.deselect('chipB');
116+
foundation.handleChipInteraction('chipB');
117117
td.verify(mockAdapter.setSelected('chipB', false));
118118
assert.equal(foundation.getSelectedChipIds().length, 1);
119119

120-
foundation.deselect('chipA');
120+
foundation.handleChipInteraction('chipA');
121121
td.verify(mockAdapter.setSelected('chipA', false));
122122
assert.equal(foundation.getSelectedChipIds().length, 0);
123123
});
@@ -127,11 +127,7 @@ test('#handleChipInteraction selects chip if the chip set is a filter chip set',
127127
td.when(mockAdapter.hasClass(cssClasses.CHOICE)).thenReturn(false);
128128
td.when(mockAdapter.hasClass(cssClasses.FILTER)).thenReturn(true);
129129

130-
foundation.handleChipInteraction({
131-
detail: {
132-
chipId: 'chipA',
133-
},
134-
});
130+
foundation.handleChipInteraction('chipA');
135131
td.verify(mockAdapter.setSelected('chipA', true));
136132
});
137133

@@ -140,11 +136,7 @@ test('#handleChipInteraction selects chip if the chip set is a choice chip set',
140136
td.when(mockAdapter.hasClass(cssClasses.CHOICE)).thenReturn(true);
141137
td.when(mockAdapter.hasClass(cssClasses.FILTER)).thenReturn(false);
142138

143-
foundation.handleChipInteraction({
144-
detail: {
145-
chipId: 'chipA',
146-
},
147-
});
139+
foundation.handleChipInteraction('chipA');
148140
td.verify(mockAdapter.setSelected('chipA', true));
149141
});
150142

@@ -153,11 +145,7 @@ test('#handleChipInteraction does nothing if the chip set is neither choice nor
153145
td.when(mockAdapter.hasClass(cssClasses.CHOICE)).thenReturn(false);
154146
td.when(mockAdapter.hasClass(cssClasses.FILTER)).thenReturn(false);
155147

156-
foundation.handleChipInteraction({
157-
detail: {
158-
chipId: 'chipA',
159-
},
160-
});
148+
foundation.handleChipInteraction('chipA');
161149
td.verify(mockAdapter.setSelected('chipA', true), {times: 0});
162150
});
163151

@@ -166,12 +154,7 @@ test('#handleChipSelection selects an unselected chip if selected is true', () =
166154

167155
foundation.selectedChipIds_ = [];
168156
foundation.select = td.func();
169-
foundation.handleChipSelection({
170-
detail: {
171-
chipId: 'chipA',
172-
selected: true,
173-
},
174-
});
157+
foundation.handleChipSelection('chipA', true);
175158
td.verify(foundation.select('chipA'));
176159
});
177160

@@ -180,50 +163,29 @@ test('#handleChipSelection does nothing if selected is true and the chip is alre
180163

181164
foundation.selectedChipIds_ = ['chipA'];
182165
foundation.select = td.func();
183-
foundation.handleChipSelection({
184-
detail: {
185-
chipId: 'chipA',
186-
selected: true,
187-
},
188-
});
166+
foundation.handleChipSelection('chipA', true);
189167
td.verify(foundation.select('chipA'), {times: 0});
190168
});
191169

192170
test('#handleChipSelection deselects a selected chip if selected is false', () => {
193171
const {foundation} = setupTest();
194172

195173
foundation.selectedChipIds_ = ['chipA'];
196-
foundation.deselect = td.func();
197-
foundation.handleChipSelection({
198-
detail: {
199-
chipId: 'chipA',
200-
selected: false,
201-
},
202-
});
203-
td.verify(foundation.deselect('chipA'));
174+
foundation.handleChipSelection('chipA', false);
175+
assert.equal(foundation.selectedChipIds_.length, 0);
204176
});
205177

206178
test('#handleChipSelection does nothing if selected is false and the chip is not selected', () => {
207179
const {foundation} = setupTest();
208180

209-
foundation.selectedChipIds_ = [];
210-
foundation.deselect = td.func();
211-
foundation.handleChipSelection({
212-
detail: {
213-
chipId: 'chipA',
214-
selected: false,
215-
},
216-
});
217-
td.verify(foundation.deselect('chipA'), {times: 0});
181+
foundation.selectedChipIds_ = ['chipB'];
182+
foundation.handleChipSelection('chipA', false);
183+
assert.equal(foundation.selectedChipIds_.length, 1);
218184
});
219185

220186
test('#handleChipRemoval removes chip', () => {
221187
const {foundation, mockAdapter} = setupTest();
222188

223-
foundation.handleChipRemoval({
224-
detail: {
225-
chipId: 'chipA',
226-
},
227-
});
189+
foundation.handleChipRemoval('chipA');
228190
td.verify(mockAdapter.removeChip('chipA'));
229191
});

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,24 @@ test('#destroy cleans up child chip components', () => {
8989

9090
test('#initialSyncWithDOM sets up event handlers', () => {
9191
const {root, mockFoundation} = setupMockFoundationTest();
92-
93-
domEvents.emit(root, MDCChipFoundation.strings.INTERACTION_EVENT);
94-
td.verify(mockFoundation.handleChipInteraction(td.matchers.anything()), {times: 1});
95-
96-
domEvents.emit(root, MDCChipFoundation.strings.SELECTION_EVENT);
97-
td.verify(mockFoundation.handleChipSelection(td.matchers.anything()), {times: 1});
98-
99-
domEvents.emit(root, MDCChipFoundation.strings.REMOVAL_EVENT);
100-
td.verify(mockFoundation.handleChipRemoval(td.matchers.anything()), {times: 1});
92+
const {INTERACTION_EVENT, REMOVAL_EVENT, SELECTION_EVENT} = MDCChipFoundation.strings;
93+
const evtData = {
94+
chipId: 'chipA', selected: true,
95+
};
96+
const evt1 = document.createEvent('CustomEvent');
97+
const evt2 = document.createEvent('CustomEvent');
98+
const evt3 = document.createEvent('CustomEvent');
99+
evt1.initCustomEvent(INTERACTION_EVENT, true, true, evtData);
100+
evt2.initCustomEvent(REMOVAL_EVENT, true, true, evtData);
101+
evt3.initCustomEvent(SELECTION_EVENT, true, true, evtData);
102+
103+
root.dispatchEvent(evt1);
104+
root.dispatchEvent(evt2);
105+
root.dispatchEvent(evt3);
106+
107+
td.verify(mockFoundation.handleChipInteraction('chipA'), {times: 1});
108+
td.verify(mockFoundation.handleChipSelection('chipA', true), {times: 1});
109+
td.verify(mockFoundation.handleChipRemoval('chipA'), {times: 1});
101110
});
102111

103112
test('#destroy removes event handlers', () => {

0 commit comments

Comments
 (0)