React Chips
A React version of an MDC Chips.
Installation
npm install @material/react-chips
Usage
Styles
with Sass:
;
with CSS:
;
Javascript Instantiation
;; { return <ChipSet> <Chip id='summer' label='Summer'/> <Chip id='winter' label='Winter'/> </ChipSet> ; }
Variants
Selection
There are two types of chips that allow for selection: choice chips for single selection, and filter chips for multiple selection. You can indicate a Chip
is selected by adding the selected
prop. Due to React's uni-directional data flow, you are expected write your own selection logic and pass a callback to the Chip
through the handleSelect
prop.
Choice chips
Component state = selectedChipIds: 'chip2' ; { return <ChipSet choice selectedChipIds=thisstateselectedChipIds handleSelect= this > <Chip id='chip1' label='Small' /> <Chip id='chip2' label='Medium' /> <Chip id='chip3' label='Large' /> </ChipSet> ; }
Filter chips
Filter chips include a leading checkmark to indicate selection. To define a set of chips as filter chips, add the filter
prop to the ChipSet
.
Component state = selectedChipIds: 'chip1' 'chip2' ; { return <ChipSet filter selectedChipIds=thisstateselectedChipIds handleSelect= this > <Chip id='chip1' label='Tops' /> <Chip id='chip2' label='Bottoms' /> <Chip id='chip3' label='Shoes' /> </ChipSet> ; }
Input chips
Input chips are a variant of chips which enable user input by converting text into chips. Chips may be dynamically added and removed from the chip set. To define a set of chips as input chips, add the input
prop to the ChipSet
. When a chip is removed, the component will notify you through the updateChips
prop callback. updateChips
will pass an array of props representing the specified chip data. The example below shows you how to use this.
NOTE: We recommend you store an array of chip labels and their respective IDs in the
state
to manage adding/removing chips. Do NOT use the chip's index as its ID or key, because its index may change due to the addition/removal of other chips.
Component state = chips: label: 'Jane Smith' id: 'janesmith' label: 'John Doe' id: 'johndoe' ; { // If you have a more complex input, you may want to store the value in the state. const label = etargetvalue; if !!label && ekey === 'Enter' const id = label; // Create a new chips array to ensure that a re-render occurs. // See: https://reactjs.org/docs/state-and-lifecycle.html#do-not-modify-state-directly const chips = ...thisstatechips; chips; this; etargetvalue = ''; } { return <div> <input type="text" onKeyDown=thishandleKeyDown /> <ChipSet input updateChips= this > thisstatechips </ChipSet> </div> ; }
Props
Chip Set
Prop Name | Type | Description |
---|---|---|
className | String | Classes to be applied to the chip set element |
selectedChipIds | Array | Array of ids of chips that are selected |
handleSelect | Function(id: string) => void | Callback for selecting the chip with the given id |
updateChips | Function(chips: Array{chipProps}) => void | Callback when the ChipSet updates its chips |
choice | Boolean | Indicates that the chips in the set are choice chips, which allow single selection from a set of options |
filter | Boolean | Indicates that the chips in the set are filter chips, which allow multiple selection from a set of options |
input | Boolean | Indicates that the chips in the set are input chips, where chips can be added or removed |
Chip
Prop Name | Type | Description |
---|---|---|
className | String | Classes to be applied to the chip element |
id | Number | Required. Unique identifier for the chip |
label | String | Text to be shown on the chip |
leadingIcon | Element | An icon element that appears as the leading icon. |
removeIcon | Element | An icon element that appears as the remove icon. Clicking on it should remove the chip. |
selected | Boolean | Indicates whether the chip is selected |
handleSelect | Function(id: string) => void | Callback for selecting the chip with the given id |
handleRemove | Function(id: string) => void | Callback for removing the chip with the given id |
Sass Mixins
Sass mixins may be available to customize various aspects of the Components. Please refer to the MDC Web repository for more information on what mixins are available, and how to use them.