UNPKG

4.96 kBMarkdownView Raw
1# material-ui-chip-input
2This project provides a [chip input field][chipspec] for [Material-UI][mui]. It is inspired by [Angular Material's chip input][angular-chips].
3
4![Demo](demo.gif)
5
6If you want to try the component yourself instead of watching a gif, head over to [the storybook][gh-pages] for a live demo!
7
8## Installation
9```shell
10npm i --save material-ui-chip-input
11```
12
13## Usage
14The component supports either controlled or uncontrolled input mode. If you use the controlled mode (by setting the `value` attribute), the `onChange` callback won't be called.
15
16```jsx
17import ChipInput from 'material-ui-chip-input'
18
19// uncontrolled input
20<ChipInput
21 defaultValue={['foo', 'bar']}
22 onChange={(chips) => handleChange(chips)}
23/>
24
25// controlled input
26<ChipInput
27 value={yourChips}
28 onRequestAdd={(chip) => handleAddChip(chip)}
29 onRequestDelete={(chip, index) => handleDeleteChip(chip, index)}
30/>
31```
32
33## Properties
34| Name | Type | Default | Description |
35| --- | --- | --- | --- |
36| allowDuplicates | `bool` | `false` | Allows duplicate chips if set to true. |
37| chipContainerStyle | `object` | | Override the inline-styles of the chip container element. |
38| 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. Each item in the `dataSource` array will be passed to `chipRenderer` as arguments `value` and `text`. If `dataSource` is an array of objects and `dataSourceConfig` is present, then `value` and `text` will instead correspond to the object values defined in `dataSourceConfig`. If `dataSourceConfig` is not set and `dataSource` is an array of objects, then a custom `chipRenderer` must be set.|
39| clearOnBlur | `bool` | `true` | If true, clears the input value after the component loses focus. |
40| dataSource | `array` | | Data source for auto complete. This should be an array of strings or objects.|
41| dataSourceConfig | `object` | | Config for objects list dataSource, e.g. `{ text: 'text', value: 'value' }`. If not specified, the `dataSource` must be a flat array of strings or a custom `chipRenderer` must be set to handle the objects. |
42| defaultValue | `string[]` | | The chips to display by default (for uncontrolled mode). |
43| disabled | `bool` | `false` | Disables the chip input if set to true. |
44| errorText | `node` | | The error text to display. |
45| floatingLabelText | `node` | | The content of the floating label. |
46| fullWidth | `bool` | `false` | If true, the chip input will fill the available width. |
47| 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. |
48| hintText | `node` | | The hint text to display. |
49| 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. |
50| newChipKeyCodes | `number[]` | `[13]` (enter key) | The key codes used to determine when to create a new chip. |
51| 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. |
52| onChange | `function` | | Callback function that is called when the chips change (in uncontrolled mode). |
53| onRequestAdd | `function` | | Callback function that is called when a new chip was added (in controlled mode). |
54| onRequestDelete | `function` | | Callback function that is called when a new chip was removed (in controlled mode). |
55| onTouchTap | `function` | | Callback function that is called when text input is clicked. |
56| onUpdateInput | `function` | | Callback function that is called when the input changes (useful for auto complete). |
57| openOnFocus | `bool` | `false` | Opens the auto complete list on focus if set to true. |
58| style | `object` | | Override the inline-styles of the root element. |
59| value | `string[]` | | The chips to display (enables controlled mode if set). |
60
61
62Additionally, most other properties of Material UI's [Auto Complete][mui-auto-complete] and [Text Field][mui-text-field] should be supported. Please open an issue if something is missing or does not work as expected.
63
64## Credits
65The code for the input component was adapted from Material UI's [Auto Complete][mui-auto-complete] and [Text Field][mui-text-field] that we all know and love.
66
67## License
68The files included in this repository are licensed under the MIT license.
69
70[chipspec]: https://material.google.com/components/chips.html#chips-behavior
71[mui]: http://www.material-ui.com/#/
72[angular-chips]: https://material.angularjs.org/latest/demo/chips
73[mui-text-field]: http://www.material-ui.com/#/components/text-field
74[mui-auto-complete]: http://www.material-ui.com/#/components/auto-complete
75[gh-pages]: https://teamwertarbyte.github.io/material-ui-chip-input/