leonardocnalves / material-ui-chip-input

A chip input field using Material-UI.

Home Page:https://teamwertarbyte.github.io/material-ui-chip-input/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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-rc.0 or later. If you are using Material-UI 1.0.0-beta, you should update to the latest version. 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

Name Type Default Description
allowDuplicates bool false Allows duplicate chips if set to true.
chipRenderer function A function of the type ({ value, text, chip, isFocused, isDisabled, 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.
classes object Extends the styles applied to this component.
clearOnBlur bool true If true, clears the input value after the component loses focus.
dataSource array Data source for auto complete. This should be an array of strings or objects.
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.
defaultValue string[] The chips to display by default (for uncontrolled mode).
disabled bool false Disables the chip input if set to true.
helperText node The helper text to display.
inputRef function Use that property to pass a ref callback to the native input component.
fullWidth bool false If true, the chip input will fill the available width.
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.
label node The content of the floating label.
newChipKeyCodes number[] [13] (enter key) The key codes used to determine when to create a new chip.
onBeforeAdd function Callback 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.
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.
onChange function Callback function that is called when the chips change (in uncontrolled mode).
onAdd function Callback function that is called when a new chip was added (in controlled mode).
onDelete function Callback function that is called when a new chip was removed (in controlled mode).
onUpdateInput function Callback function that is called when the input changes.
placeholder node A short placeholder that is displayed if the input has no values.
value string[] The chips to display (enables controlled mode if set).

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:

  • root
  • inputRoot
  • input
  • chipContainer
  • label
  • helperText
  • chip

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.

About

A chip input field using Material-UI.

https://teamwertarbyte.github.io/material-ui-chip-input/

License:MIT License


Languages

Language:JavaScript 99.6%Language:HTML 0.4%