Git Product home page Git Product logo

react-chips's Introduction

React Chips npm package Build Status

A controlled React input for arrays of data. Example

Chip

A chip is a component used to represent an arbitrary data object.

Getting Started

npm install --save react-chips
import React, { Component } from 'react';
import Chips, { Chip } from '../src'

class YourComponent extends Component {

  constructor(props) {
    super(props);
    this.state = {
      chips: []
    }
  }

  onChange = chips => {
    this.setState({ chips });
  }

  render() {
    return (
      <div>
        <Chips
          value={this.state.chips}
          onChange={this.onChange}
          suggestions={["Your", "Data", "Here"]}

        />
      </div>
    );
  }
}

Chips

Property Type Required Description
value Array An array of data that represents the value of the chips
onChange Function A function called when the value of chips changes, passes the chips value as an argument.
placeholder String The placeholder to populate the input with
theme Object A react-themeable theme
suggestions Array Data to fill the autocomplete list with
fetchSuggestions Function Delegate expecting to recive autocomplete suggestions (callback or promise)
fetchSuggestionsThrushold Number Maximum calls to fetchSuggestions per-second
fromSuggestionsOnly Boolean Only allow chips to be added from the suggestions list
uniqueChips Boolean Only allow one chip for each object
renderChip Function For custom chip usage. A function that passes the value of the chip as an argument, must return an element that will be rendered as each chip.
suggestionsFilter Function A function that is passed an autoCompleteData item, and the current input value as arguments. Must return a boolean for if the item should be shown.
getChipValue Function A function used to change the value that is passed into each chip.
createChipKeys Array An array of keys/keyCodes that will create a chip with the current input value when pressed. (Will not work of fromSuggestionsOnly is true).
getSuggestionValue Function The value to show in the input when a suggestion is selected
renderSuggestion Function For custom autocomplete list item usage. A function that passes the value as an argument, must return an element to render for each list item.
shouldRenderSuggestions Function See AutoSuggest
alwaysRenderSuggestions Boolean See AutoSuggest
highlightFirstSuggestion Boolean See AutoSuggest
focusInputOnSuggestionClick Boolean See AutoSuggest
multiSection Boolean See AutoSuggest
renderSectionTitle Function ✓ when multiSection={true} See AutoSuggest
getSectionSuggestions Function ✓ when multiSection={true} See AutoSuggest

Styles

This project uses react-themeable and Radium for styling. The Chips, and default Chip components both accept a theme prop. The theme structure, and default theme can be found here

Custom Chip Component

You may use a custom chip component, simply return the custom component to the renderChip prop function. This component will receive the following additional props from the Chips component.

<Chips
  ...
  renderChip={value => <CustomChip>{value}</CustomChip>}
/>
Property Type Description
selected bool A boolean that tells the chip if it is currently selected.
onRemove func A function to be invoked when the chip should be removed

Async Suggestions

To fetch asynchronous suggestions use fetchSuggestions.

<Chips
  ...
  fetchSuggestions={(value, callback) => {
    someAsynCall(callback)
  }}
/>

// or with a Promise

<Chips
  ...
  fetchSuggestions={(value) => someAsyncCallThatReturnsPromise}
/>

react-chips's People

Contributors

gregchamberlain avatar dmitrydodzin avatar andresolivero avatar

Stargazers

Roman avatar

Watchers

Dmytro Sadovnychyi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.