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

Commit 9841e18

Browse files
mgr34Matt Goo
authored and
Matt Goo
committed
fix(chips): ChipSet no longer overwrites handle[Interaction|Select|Re… (#651)
1 parent f9238ef commit 9841e18

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

packages/chips/ChipSet.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,24 @@ export default class ChipSet extends React.Component<ChipSetProps, ChipSetState>
163163
const {filter} = this.props;
164164
const {selectedChipIds} = this.state;
165165
const selected = selectedChipIds.indexOf(chip.props.id) > -1;
166+
const {handleInteraction, handleSelect, handleRemove, ...chipProps} = chip.props;
166167
const props = Object.assign(
167168
{},
168-
...chip.props,
169+
...chipProps,
169170
{
170171
selected,
171-
handleSelect: this.handleSelect,
172-
handleInteraction: this.handleInteraction,
173-
handleRemove: this.handleRemove,
172+
handleSelect: (id: string, selected: boolean): void => {
173+
handleSelect!(id, selected);
174+
this.handleSelect(id, selected);
175+
},
176+
handleInteraction: (id: string): void => {
177+
handleInteraction!(id);
178+
this.handleInteraction(id);
179+
},
180+
handleRemove: (id: string): void => {
181+
handleRemove!(id);
182+
this.handleRemove(id);
183+
},
174184
chipCheckmark: filter ? (
175185
<ChipCheckmark ref={this.setCheckmarkWidth} />
176186
) : null,

packages/chips/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Prop Name | Type | Description
155155
--- | --- | ---
156156
className | String | Classes to be applied to the chip set element
157157
selectedChipIds | Array | Array of ids of chips that are selected
158-
handleSelect | Function(id: string) => void | Callback for selecting the chip with the given id
158+
handleSelect | Function(selectedChipIds: string[]) => void | Callback when Chips are Selected
159159
updateChips | Function(chips: Array{chipProps}) => void | Callback when the ChipSet updates its chips
160160
choice | Boolean | Indicates that the chips in the set are choice chips, which allow single selection from a set of options
161161
filter | Boolean | Indicates that the chips in the set are filter chips, which allow multiple selection from a set of options
@@ -172,8 +172,10 @@ label | String | Text to be shown on the chip
172172
leadingIcon | Element | An icon element that appears as the leading icon.
173173
removeIcon | Element | An icon element that appears as the remove icon. Clicking on it should remove the chip.
174174
selected | Boolean | Indicates whether the chip is selected
175-
handleSelect | Function(id: string) => void | Callback for selecting the chip with the given id
175+
handleSelect | Function(id: string, selected: boolean) => void | Callback for selecting the chip with the given id
176176
handleRemove | Function(id: string) => void | Callback for removing the chip with the given id
177+
handleInteraction | Function(id: string) => void | Callback for interaction of chip (`onClick` | `onKeyDown`)
178+
177179

178180
## Sass Mixins
179181

test/unit/chips/ChipSet.test.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ test('#adapter.setSelected adds selectedChipId to state', () => {
8888
td.verify(getSelectedChipIds(), {times: 1});
8989
});
9090

91+
test('#chip.props.setSelected calls both #chip.props.handleSelect and #chipSet.props.handleSelect', () => {
92+
const chipHandleSelect = coerceForTesting<(id: string, selected: boolean) => void>(td.func());
93+
const wrapper = mount<ChipSet>(<ChipSet><Chip /></ChipSet>);
94+
wrapper.setProps({children: <Chip id='1' handleSelect={chipHandleSelect} />});
95+
wrapper.instance().handleSelect = coerceForTesting<(chipIds: string, selected: boolean) => void>(td.func());
96+
const chip = wrapper.children().props().children[0];
97+
chip.props.handleSelect('1', true);
98+
td.verify(wrapper.instance().handleSelect('1', true), {times: 1});
99+
td.verify(chipHandleSelect('1', true), {times: 1});
100+
});
101+
91102
// this is bad
92103
test('#adapter.setSelected removes selectedChipId from state', () => {
93104
const wrapper = shallow<ChipSet>(
@@ -187,6 +198,16 @@ test('#handleSelect calls updates parent component with selectedChipIds correctl
187198
td.verify(handleChipSelection(['chip1'], ['chip1', 'chip2']), {times: 1});
188199
});
189200

201+
test('#chip.props.handleInteraction calls both #chip.handleInteraction calls #foundation.handleChipInteraction', () => {
202+
const handleChipInteraction = coerceForTesting<(id: string) => void>(td.func());
203+
const wrapper = mount<ChipSet>(<ChipSet><Chip /></ChipSet>);
204+
wrapper.instance().handleInteraction = coerceForTesting<(chipId: string) => void>(td.func());
205+
wrapper.setProps({children: <Chip id='1' handleInteraction={handleChipInteraction} />});
206+
const chip = wrapper.children().props().children[0];
207+
chip.props.handleInteraction('1');
208+
td.verify(wrapper.instance().handleInteraction('1'), {times: 1});
209+
td.verify(handleChipInteraction('1'), {times: 1});
210+
});
190211

191212
test('#handleInteraction calls #foundation.handleChipInteraction', () => {
192213
const handleChipInteraction = td.func();
@@ -336,6 +357,17 @@ test('chip is rendered with handleRemove method', () => {
336357
td.verify(wrapper.instance().handleRemove('1'), {times: 1});
337358
});
338359

360+
test('#chip.props.handleRemove calls both #chipSet.handleRemove and #chip.props.handleRemove', () => {
361+
const handleChipRemove = coerceForTesting<(id: string) => void>(td.func());
362+
const wrapper = mount<ChipSet>(<ChipSet><Chip /></ChipSet>);
363+
wrapper.instance().handleRemove = coerceForTesting<(chipId: string) => void>(td.func());
364+
wrapper.setProps({children: <Chip id='1' handleRemove={handleChipRemove} />});
365+
const chip = wrapper.children().props().children[0];
366+
chip.props.handleRemove('1');
367+
td.verify(wrapper.instance().handleRemove('1'), {times: 1});
368+
td.verify(handleChipRemove('1'), {times: 1});
369+
});
370+
339371
test('chip is rendered ChipCheckmark if is filter variants', () => {
340372
const wrapper = mount(
341373
<ChipSet filter>

0 commit comments

Comments
 (0)