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

Commit f307c6c

Browse files
태재영Matt Goo
태재영
authored and
Matt Goo
committed
docs(chips): fix InputChips example error (#960)
1 parent 00f2ef1 commit f307c6c

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

packages/chips/README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ Input chips are a variant of chips which enable user input by converting text in
106106
class MyInputChips extends React.Component {
107107
state = {
108108
chips: [
109-
{label: 'Jane Smith', id: 'janesmith'},
110-
{label: 'John Doe', id: 'johndoe'}
109+
{label: 'Jane Smith', id: 'JaneSmith'},
110+
{label: 'John Doe', id: 'JohnDoe'},
111111
],
112112
};
113113

@@ -119,11 +119,15 @@ class MyInputChips extends React.Component {
119119
// Create a new chips array to ensure that a re-render occurs.
120120
// See: https://reactjs.org/docs/state-and-lifecycle.html#do-not-modify-state-directly
121121
const chips = [...this.state.chips];
122-
chips.push({label, id});
123-
this.setState({chips});
124-
e.target.value = '';
122+
if (chips.some((v) => v.id === id)) {
123+
console.error('There is already a chip which has same key.');
124+
} else {
125+
chips.push({label, id});
126+
this.setState({chips});
127+
e.target.value = '';
128+
}
125129
}
126-
}
130+
};
127131

128132
render() {
129133
return (
@@ -135,6 +139,7 @@ class MyInputChips extends React.Component {
135139
>
136140
{this.state.chips.map((chip) =>
137141
<Chip
142+
id={chip.id}
138143
key={chip.id} // The chip's key cannot be its index, because its index may change.
139144
label={chip.label}
140145
trailingIcon={<MaterialIcon icon='cancel' />}

0 commit comments

Comments
 (0)