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

Commit 4642324

Browse files
author
Matt Goo
authored
fix(chips): copy selectedChipIds before calling setState (#645)
1 parent 3e0e8b6 commit 4642324

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/chips/ChipSet.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default class ChipSet extends React.Component<ChipSetProps, ChipSetState>
9999
return {
100100
hasClass: (className: string) => this.classes.split(' ').includes(className),
101101
setSelected: () => {
102-
const selectedChipIds = this.state.foundation.getSelectedChipIds();
102+
const selectedChipIds = this.state.foundation.getSelectedChipIds().slice();
103103
this.setState({selectedChipIds}, () => {
104104
this.props.handleSelect(selectedChipIds);
105105
});

test/unit/chips/ChipSet.test.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,51 @@ test('#handleSelect calls foundation.handleChipSelection with selectedChipId and
143143
td.verify(handleChipSelection('1', false), {times: 1});
144144
});
145145

146+
test('#handleSelect calls updates parent component with selectedChipIds correctly for filter chips', () => {
147+
type HandleSelectMethod = (prevSelectedChipIds: string[], selectedChipId: string[]) => void;
148+
type Props = {
149+
handleSelect: HandleSelectMethod
150+
};
151+
type State = {selectedChipIds: string[]};
152+
153+
class TestChipParentComponent extends React.Component<Props, State> {
154+
state = {selectedChipIds: []};
155+
componentDidUpdate(_: Props, pState: State) {
156+
if (pState.selectedChipIds !== this.state.selectedChipIds) {
157+
this.props.handleSelect(pState.selectedChipIds, this.state.selectedChipIds);
158+
}
159+
}
160+
handleSelect = (selectedChipIds: string[]) => {
161+
this.setState({selectedChipIds});
162+
}
163+
render() {
164+
const Chip1 = <Chip id='chip1'/>;
165+
const Chip2 = <Chip id='chip2'/>;
166+
return (
167+
<ChipSet
168+
filter
169+
selectedChipIds={this.state.selectedChipIds}
170+
handleSelect={this.handleSelect}
171+
>
172+
{Chip1}
173+
{Chip2}
174+
</ChipSet>
175+
);
176+
}
177+
}
178+
179+
const handleChipSelection = coerceForTesting<HandleSelectMethod>(td.func());
180+
const wrapper = mount<TestChipParentComponent>(
181+
<TestChipParentComponent handleSelect={handleChipSelection}/>
182+
);
183+
const chipSet = wrapper.childAt(0);
184+
chipSet.children().children().first().children().simulate('click');
185+
td.verify(handleChipSelection([], ['chip1']), {times: 1});
186+
chipSet.children().children().last().children().simulate('click');
187+
td.verify(handleChipSelection(['chip1'], ['chip1', 'chip2']), {times: 1});
188+
});
189+
190+
146191
test('#handleInteraction calls #foundation.handleChipInteraction', () => {
147192
const handleChipInteraction = td.func();
148193
const foundation = {handleChipInteraction};

0 commit comments

Comments
 (0)