Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit 6eecce6

Browse files
DanBeardleMaik
authored andcommitted
Allow Duplicates (#108)
* Added option to allow duplicate chips. Changed deletion to be based on index rather than value to support duplicate chip deletion * Added duplicate chips to the story to show it off * Fixed readme and stories with @leMaik 's feedback * Created a copy of this.state.chips before modifying it on chip delete
1 parent be1ad3a commit 6eecce6

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
lib
2+
lib
3+
.idea

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import ChipInput from 'material-ui-chip-input'
3333
## Properties
3434
| Name | Type | Default | Description |
3535
| --- | --- | --- | --- |
36+
| allowDuplicates | `bool` | `false` | If true allows duplicate chips. |
3637
| chipRenderer | `function` | | A function of the type `({ value, text, isFocused, isDisabled, handleClick, handleRequestDelete, defaultStyle }, key) => node` that returns a chip based on the given properties. This can be used to customize chip styles. |
3738
| clearOnBlur | `bool` | `true` | If true, clears the input value after the component loses focus. |
3839
| dataSource | `array` | | Data source for auto complete. |
@@ -44,7 +45,7 @@ import ChipInput from 'material-ui-chip-input'
4445
| fullWidth | `bool` | `false` | If true, the chip input will fill the available width. |
4546
| fullWidthInput | `bool` | `false` | If true, the input field will always be below the chips and fill the available space. By default, it will try to be beside the chips. |
4647
| hintText | `node` | | The hint text to display. |
47-
| id | `string` | _a unique id_ | The id prop for the text field, should be set to some deteministic value if you use server-side rendering. |
48+
| id | `string` | _a unique id_ | The id prop for the text field, should be set to some deterministic value if you use server-side rendering. |
4849
| newChipKeyCodes | `number[]` | `[13]` (enter key) | The key codes used to determine when to create a new chip. |
4950
| onBlur | `function` | | Callback function that is called with `event` when the input loses focus, where `event.target.value` is the current value of the text input. |
5051
| onChange | `function` | | Callback function that is called when the chips change (in uncontrolled mode). |

src/ChipInput.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class ChipInput extends React.Component {
306306
}
307307
}
308308

309-
if (!chips.some((c) => c[this.props.dataSourceConfig.value] === chip[this.props.dataSourceConfig.value])) {
309+
if (this.props.allowDuplicates || !chips.some((c) => c[this.props.dataSourceConfig.value] === chip[this.props.dataSourceConfig.value])) {
310310
if (this.props.value) {
311311
if (this.props.onRequestAdd) {
312312
this.props.onRequestAdd(chip)
@@ -320,7 +320,7 @@ class ChipInput extends React.Component {
320320
}
321321
} else {
322322
if (chip.trim().length > 0) {
323-
if (chips.indexOf(chip) === -1) {
323+
if (this.props.allowDuplicates || chips.indexOf(chip) === -1) {
324324
if (this.props.value) {
325325
if (this.props.onRequestAdd) {
326326
this.props.onRequestAdd(chip)
@@ -343,8 +343,9 @@ class ChipInput extends React.Component {
343343
}
344344
} else {
345345
if (this.props.dataSourceConfig) {
346-
const chips = this.state.chips.filter((c) => c[this.props.dataSourceConfig.value] !== chip)
347-
if (chips.length !== this.state.chips.length) {
346+
const chips = this.state.chips.slice();
347+
let changed = chips.splice(i,1) // remove the chip at index i
348+
if (changed) {
348349
this.setState({
349350
chips,
350351
focusedChip: this.state.focusedChip && this.state.focusedChip[this.props.dataSourceConfig.value] === chip ? null : this.state.focusedChip
@@ -354,8 +355,9 @@ class ChipInput extends React.Component {
354355
}
355356
}
356357
} else {
357-
const chips = this.state.chips.filter((c) => c !== chip)
358-
if (chips.length !== this.state.chips.length) {
358+
const chips = this.state.chips.slice();
359+
let changed = chips.splice(i,1) // remove the chip at index i
360+
if (changed) {
359361
this.setState({
360362
chips,
361363
focusedChip: this.state.focusedChip === chip ? null : this.state.focusedChip
@@ -421,7 +423,8 @@ class ChipInput extends React.Component {
421423
onRequestDelete, // eslint-disable-line no-unused-vars
422424
chipRenderer = defaultChipRenderer,
423425
newChipKeyCodes, // eslint-disable-line no-unused-vars
424-
...other
426+
allowDuplicates, // eslint-disable-line no-unused-vars
427+
...other,
425428
} = this.props
426429

427430
const {prepareStyles} = this.context.muiTheme

stories/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,9 @@ storiesOf('ChipInput', module)
214214
/>
215215
</form>
216216
))
217+
.add('with duplicates allowed', () => themed(
218+
<ChipInput
219+
defaultValue={['foo','bar','foo','bar']}
220+
allowDuplicates
221+
/>
222+
))

0 commit comments

Comments
 (0)