Giter Club home page Giter Club 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
chipTheme Object A react-themeable theme that will override the default chip 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

andresolivero avatar dmitrydodzin avatar gregchamberlain avatar one89 avatar sadovnychyi avatar serjsmor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-chips's Issues

focusFirstSuggestion doesn't clear input value

Hey, first of all thanks for the library. I'm already using react-autosuggest and this fits perfectly next to it! 🤗

However, when using focusFirstSuggestion (basically focuses the first selection), and hitting [enter] the value is selected, however the value isn't cleared. Here is an example: http://d.pr/v/LcOR

openOnFocus props

Hello.
Now <Autosuggest/> does not expand if input is focued and value is empty string.

Could you provide a new prop openOnFocus on Chips component and open <AutoSuggest> even if input value is empty?

p.s. Thanks a lot for react-chips. Simple to use.

Not able to add Background Color to Individual Chips

Hi , I am Unable to add background color to individual chips.Can you provide any attribute in your component to add unique Key or unique id.
We are passing only Array of values instead of that can you provide option of sending in Array of objects.So that we can easily modify the chips.
Please help in applying background color to individual Chips.

this.setState with asynchronous problem

in addChips(..)
you call this.props.onChange(this.state.chips) after this.setState(..)
but the new state is not send to onChange because of asynchronous.
So you have to call this.props.onChange with chips variable because it is the new state.

Position of suggestions

Good day, and congrats for this great package.

The suggestion selector always opens down, if the chips selector is near to bottom of the page or the footer, this is a inconvenient to choose a suggestion far bellow.

How can I modify this behavior?

Chip background color

How can I set the background color of each chip individually? Ideally this could be an easy option, or at least something added to the docs. Thanks for a useful react component!

Possibility to Return event onChange

Maybe I didn't see any solution in the docs but, when I have multiple chips instances in a same class component for example its very difficult to identify which one was updated to pass on setState so probably we can have access to the event object to get name of the current chip

[Suggestion] Give ID's or class names to divs

For example, the <div> that takes the input keeps dynamically widening when a chip is added.

This can be stopped by adding a width property.

Normally, I can just override the css elements in my own custom.css, but because the styling appears to be inline, I cannot do so.

Would we be able to add names to divs? This would at the very least make customization a bit easier.

I've highlighted in the screenshot which <div> I wish to customize.

Screen Shot 2019-07-15 at 1 19 55 PM

Currently, the workaround is to make another empty div that wraps around the chip, and use that to apply width properties.

This is fine, but applying names to div would be easier to customize (IMO)

Chip add on blur

Is there a way in which I can handle the blur event for the Chips component to create a last chip when the user blurs the element?, for example

[food x] [drinks x] soda -> blur event -> [food x] [drinks x] [soda x]

It's a bit counter-intuitive that the last item is lost if I don't press enter or tab before submitting as part of a form, am I missing something here?

Make the key to create a chip customizable.

It took looking at the code to realize creating a non-suggested chip requires hitting the tab key. Would it be possible to have this be customizable from the html tag level?

I've used createChipKeys props to add ',' comma key to make a chip. not working in Android devices

I've used createChipKeys prop to make both Enter and comma key to create chips.

Both are working on windows and Linux OS but not in Android device. Enter key is creating chips on Android but not comma key.
I've add these ASCII keys. Please suggest, if i'm wrong.

<Chips
placeholder={t("DOMAIN_NAME")}
variant="outlined"
onChange={handleChange}
value={selectedUser}
createChipKeys={[13, 188]}
/>

TypeError: Cannot read property 'prepareStyles' of undefined with README code

I use this versions.

 "material-ui": "^0.20.0"
"material-ui-chip-input": "^0.18.8"

and I try to use chips following README sample code.

I got Title's error.

mamorio_office

I resoleved this using MuiThemeProvider.

import { MuiThemeProvider } from 'material-ui/styles';
<MuiThemeProvider>
    <ChipInput
        defaultValue={['foo', 'bar']}
        onChange={(chips) => console.log(chips)}
    />
</MuiThemeProvider>

It may need to change sample code.
thanks,

not returning synthetic event in callback function.

hi there,
i want the synthetic react event created on the selection of chip.
but i am unable to find any...
is there any way to get there???
like i am calling a function on chip selection.
and want there the synthetic and general way of event.target to get the dynamic name i assign to the component.
like
<Chips
value={this.state.user.medicines}
onChange={this.changemedicines}
name={"medicine"+item}
suggestions={this.state.medicines}
placeholder="click to add medicines"
/>
but it doesn't returns any event but only a chip...
what i can do???

How to style

I don't get how we are supposed to style it, I'm trying to use it with material-ui v1 and make it look like material design, would you be able to provide a small example of how to style it?

Thank you!

Only create the last word if you paste a sentence with a valid delimiter

If you paste a sentence like 'this, is, a, test' it will create only the word test as a chip.

 addChip = (value) => {
    if (this.props.uniqueChips && this.props.value.indexOf(value) !== -1) {
      this.setState({value: ""});
      return;
    }
    let chips = [...this.props.value, value]
    this.props.onChange(chips);
    this.setState({ value: "" })
  }

I belive that is caused by addChip being called on a loop and this.props.onChange(chips) is been executed asyncronous by the component using this component, so the props value does not change in time for the next iteration.

Change the placeholder style?

Hi, I'm not able to change the placeholder style. I want to change its color, but it doesn't let me. Does anyone know how to do it?

Also the property "focus" doesn't let me to change the border color of the input.

Feature request: Add a 'chipTheme' prop to 'Chips'

When I've used this awesome project, I've felt the lack of providing a chipTheme 'prop' to affect how a singe chip will be styled. The default chip style was very convenient to me up to the point of the background color which I really wanted to change, without the need to create a 'CustomChip' through a render function.

This addition will contribute to a faster prototyping experience as to my own experience. #25

Fix React 16

All that needs to be done is updating the radium dependency to be at least 0.19.0, as 0.18 has react.proptypes and breaks in react 16

Limit the number of chips ?

Interested to know if this functionality exists or if there're plans for it's implementation.

To limit the number of chips before the input is disabled.

Update to use React 17/18

Getting peer dependency issues when using react 17/18.

Would be great to update, similar to what was done with React 16: #27

breaks with radium v0.18.2

With radium 0.18.2 the following error appears:
Uncaught TypeError: Super expression must either be null or a function, not undefined
Works, when radium is rolled back to 0.18.1 :)

FromSugesstionsOnly

Even when putting fromsuggestiononly input to false i cannot enter chip without selecting from suggestion box.

Remove promise/callback from prop

With the use of sagas or observables you don't need any async code in your containers and components.

I recommend you consider adding a loading-prop and change fetchSuggestions to be just a handler without async support to better support these new patterns.

createChipKeys - comma still remains as separator

Hi,
Im trying to user Chips and send it a createChipKeys with array of separators,
but although I didn't include comma as a separator, when am pressing it, it enter to "onChange".

is there any way to remove it from the chip keys?

thanks.

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.