Khan / react-multi-select

A multiple select component for React
MIT License
184 stars 97 forks source link

Is there a way to limit number of selections? #62

Open pankajsoni19 opened 4 years ago

liamdiprose commented 4 years ago

In your onSelectedChanged handler function, you can decide whether to update the state object or not:

const maxSelected = 3;
const { options, selected } = this.state;
<MultiSelect
      options={options}
      selected={selected}
      onSelectedChanged={(newSelected) => {
          if (newSelected.length <= maxSelected) {
              // Number of Selections is OK, save to state...
              this.setState({selected: newSelected})
          } else {
              console.log("Too many selected");
          }
       }}
   />