Home

Awesome

Support for MUI v4 or MUI v5

After maintaining this project for over 3 years, we chose not to add support for MUI v4+ since it would require significant changes and there is already a built-in chip input. The same component is also available for MUI v5, and there are some alternatives like mui-chips-input.


material-ui-chip-input

Build Status Coverage Status

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@next

Note: This is the version for Material-UI 1.0.0 or later. If you are still using Material-UI 0.x, you can use our legacy version.

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.

import ChipInput from 'material-ui-chip-input'

// uncontrolled input
<ChipInput
  defaultValue={['foo', 'bar']}
  onChange={(chips) => handleChange(chips)}
/>

// controlled input
<ChipInput
  value={yourChips}
  onAdd={(chip) => handleAddChip(chip)}
  onDelete={(chip, index) => handleDeleteChip(chip, index)}
/>

Properties

NameTypeDefaultDescription
allowDuplicatesboolfalseAllows duplicate chips if set to true.
alwaysShowPlaceholderboolIf true, the placeholder will always be visible.
blurBehaviorenum'clear'Behavior when the chip input is blurred: 'clear' clears the input, 'add' creates a chip, 'add-or-clear' either creates a chip or clears the input on failure (see: onBeforeAdd), and 'ignore' keeps the input.
chipRendererfuncA function of the type ({ value, text, chip, isFocused, isDisabled, isReadOnly, handleClick, handleDelete, className }, 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.
clearInputValueOnChangeboolfalseWhether the input value should be cleared if the value prop is changed.
dataSourcearrayData source for auto complete. This should be an array of strings or objects.
dataSourceConfigshapeConfig 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.
defaultValuearrayThe chips to display by default (for uncontrolled mode).
delayBeforeAddboolfalseUse setTimeout 150ms delay before adding chips in case other input callbacks like onSelection need to fire first.
disabledboolDisables the chip input if set to true.
disableUnderlineboolfalseIf true, the input will not have an underline.
FormHelperTextPropsobjectProps to pass through to the FormHelperText component.
fullWidthboolIf true, the chip input will fill the available width.
fullWidthInputboolIf 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.
helperTextnodeHelper text that is displayed below the input.
InputLabelPropsobjectProps to pass through to the InputLabel.
InputPropsobjectProps to pass through to the Input.
inputReffuncUse this property to pass a ref callback to the native input component.
inputValuestringThe input value (enables controlled mode for the text input if set).
labelnodeThe content of the floating label.
newChipKeyCodesarrayOf[13]The key codes (KeyboardEvent.keyCode) used to determine when to create a new chip.
newChipKeysarrayOf['Enter']The keys (KeyboardEvent.key) used to determine when to create a new chip.
onAddfuncCallback function that is called when a new chip was added (in controlled mode).
onBeforeAddfuncCallback function that is called with the chip to be added and should return true to add the chip or false to prevent the chip from being added without clearing the text input.
onChangefuncCallback function that is called when the chips change (in uncontrolled mode).
onDeletefuncCallback function that is called when a new chip was removed (in controlled mode).
onUpdateInputfuncCallback function that is called when the input changes.
placeholderstringA placeholder that is displayed if the input has no values.
readOnlyboolMakes the chip input read-only if set to true.
valuearrayThe chips to display (enables controlled mode if set).
variantenum'standard'Sets the variant for the input. Values 'standard', 'outlined', 'filled'

Any other properties supplied will be spread to the root element. The properties of <FormControl /> are also available.

CSS API

You can customize the class names used by ChipInput with the classes property. It accepts the following keys:

Have a look at this guide for more detail.

Credits

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

License

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