Skip to content

Repository files navigation

material-ui-chip-input

This project provides a chip input field for Material-UI. It is inspired by Angular Material's chip input.

Demo

If you want to try the component yourself instead of watching a gif, head over to the storybook for a live demo!

Installation

npm i --save material-ui-chip-input

Usage

The 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.

importChipInputfrom'material-ui-chip-input'// uncontrolled input<ChipInputdefaultValue={['foo','bar']}onChange={(chips)=>handleChange(chips)}/>// controlled input<ChipInputvalue={yourChips}onRequestAdd={(chip)=>handleAddChip(chip)}onRequestDelete={(chip,index)=>handleDeleteChip(chip,index)}/>

Properties

NameTypeDefaultDescription
allowDuplicatesboolfalseAllows duplicate chips if set to true.
chipContainerStyleobjectOverride the inline-styles of the chip container element.
chipRendererfunctionA function of the type ({ value, text, chip, 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 chip, 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. chip is always the raw value from dataSource, either an object or a string.
clearOnBlurbooltrueIf true, clears the input value after the component loses focus.
dataSourcearrayData source for auto complete. This should be an array of strings or objects.
dataSourceConfigobjectConfig 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.
defaultValuestring[]The chips to display by default (for uncontrolled mode).
disabledboolfalseDisables the chip input if set to true.
errorTextnodeThe error text to display.
floatingLabelTextnodeThe content of the floating label.
fullWidthboolfalseIf true, the chip input will fill the available width.
fullWidthInputboolfalseIf 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.
hintTextnodeThe hint text to display.
idstringa unique idThe id prop for the text field, should be set to some deterministic value if you use server-side rendering.
newChipKeyCodesnumber[][13] (enter key)The key codes used to determine when to create a new chip.
onBlurfunctionCallback function that is called with event when the input loses focus, where event.target.value is the current value of the text input.
onChangefunctionCallback function that is called when the chips change (in uncontrolled mode).
onClickfunctionCallback function that is called when text input is clicked.
onRequestAddfunctionCallback function that is called when a new chip was added (in controlled mode).
onRequestDeletefunctionCallback function that is called when a new chip was removed (in controlled mode).
onUpdateInputfunctionCallback function that is called when the input changes (useful for auto complete).
openOnFocusboolfalseOpens the auto complete list on focus if set to true.
styleobjectOverride the inline-styles of the root element.
valuestring[]The chips to display (enables controlled mode if set).

Additionally, most other properties of Material UI's Auto Complete and Text Field should be supported. Please open an issue if something is missing or does not work as expected.

Credits

The code for the input component was adapted from Material UI's Auto Complete and Text Field that we all know and love.

License

The files included in this repository are licensed under the MIT license.

About

A chip input field using Material-UI.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages