0.15.0 • Published 5 years ago

@material/react-chips v0.15.0

Weekly downloads
997
License
MIT
Repository
github
Last release
5 years ago

React Chips

A React version of an MDC Chips.

Installation

npm install @material/react-chips

Usage

Styles

with Sass:

import '@material/react-chips/index.scss';

with CSS:

import "@material/react-chips/dist/chips.css";

Javascript Instantiation

import React, {Component} from 'react';
import {ChipSet, Chip} from '@material/react-chips';

class MyApp extends Component {
  render() {
    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

class MyChoiceChips extends React.Component {
  state = {
    selectedChipIds: ['chip2'],
  };

  render() {
    return (
      <ChipSet
        choice
        selectedChipIds={this.state.selectedChipIds}
        handleSelect={(selectedChipIds) => this.setState({selectedChipIds})}
      >
        <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.

class MyFilterChips extends React.Component {
  state = {
    selectedChipIds: ['chip1', 'chip2'],
  };

  render() {
    return (
      <ChipSet
        filter
        selectedChipIds={this.state.selectedChipIds}
        handleSelect={(selectedChipIds) => this.setState({selectedChipIds})}
      >
        <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.

class MyInputChips extends React.Component {
  state = {
    chips: [
      {label: 'Jane Smith', id: 'JaneSmith'},
      {label: 'John Doe', id: 'JohnDoe'},
    ],
  };

  handleKeyDown = (e) => {
    // If you have a more complex input, you may want to store the value in the state.
    const label = e.target.value;
    if (!!label && e.key === 'Enter') {
      const id = label.replace(/\s/g,'');
      // 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 = [...this.state.chips];
      if (chips.some((v) => v.id === id)) {
        console.error('There is already a chip which has same key.');
      } else {
        chips.push({label, id});
        this.setState({chips});
        e.target.value = '';
      }
    }
  };

  render() {
    return (
      <div>
        <input type="text" onKeyDown={this.handleKeyDown} />
        <ChipSet
          input
          updateChips={(chips) => this.setState({chips})}
        >
          {this.state.chips.map((chip) =>
            <Chip
              id={chip.id}
              key={chip.id} // The chip's key cannot be its index, because its index may change.
              label={chip.label}
              trailingIcon={<MaterialIcon icon='cancel' />}
            />
          )}
        </ChipSet>
      </div>
    );
  }
}

Props

Chip Set

Prop NameTypeDescription
classNameStringClasses to be applied to the chip set element
selectedChipIdsArrayArray of ids of chips that are selected
handleSelectFunction(selectedChipIds: string[]) => voidCallback when Chips are Selected
updateChipsFunction(chips: Array{chipProps}) => voidCallback when the ChipSet updates its chips
choiceBooleanIndicates that the chips in the set are choice chips, which allow single selection from a set of options
filterBooleanIndicates that the chips in the set are filter chips, which allow multiple selection from a set of options
inputBooleanIndicates that the chips in the set are input chips, where chips can be added or removed

Chip

Prop NameTypeDescription
classNameStringClasses to be applied to the chip element
idNumberRequired. Unique identifier for the chip
labelStringText to be shown on the chip
leadingIconElementAn icon element that appears as the leading icon.
trailingIconElementAn icon element that appears as the remove icon. Clicking on it should remove the chip.
selectedBooleanIndicates whether the chip is selected
handleSelectFunction(id: string, selected: boolean) => voidCallback for selecting the chip with the given id
handleInteractionFunction(id: string) => voidCallback for interaction of chip (onClickonKeyDown)
handleTrailingIconInteractionFunction(id: string) => voidCallback for interaction with trailing icon
shouldRemoveOnTrailingIconClickBooleanindicates if interaction with trailing icon should remove chip. defaults to true

Note: handleTrailingIconInteraction will execute before handleRemove. Without explicitly setting shouldRemoveOnTrailingIconClick to false both callbacks will fire on trailingIcon interaction

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.

Advanced Sass Mixins