React-select: Max number of options

Created on 28 Nov 2016  路  4Comments  路  Source: JedWatson/react-select

Hello!
Could I set max number of selected options for multi select?
Thanks!

Most helpful comment

It would be nice to have it built in, but it is also easy to do by yourself:

For example:

let newOptions = this.state.currentOptions;

if (this.state.selected != null && this.state.selected.length === MAX) {
  newOptions = [];
}

return (
<Select
  multi
  value={this.state.selected}
  options={newOptions}/>
);

All 4 comments

It would be nice to have it built in, but it is also easy to do by yourself:

For example:

let newOptions = this.state.currentOptions;

if (this.state.selected != null && this.state.selected.length === MAX) {
  newOptions = [];
}

return (
<Select
  multi
  value={this.state.selected}
  options={newOptions}/>
);

@caiolopes That method doesn't quite work. Upon hitting MAX, the options already selected disappear ostensibly because the values in the array passed to the value prop are no longer founder in options.

Please disregard, I had been passing in an array of strings, e.g. ["apple", "banana"] for the value property.

I was not aware I could also pass in [{label: "Apple", value: "apple"}, {label: "Banana", value: "banana"}]. Of course that is perfectly reasonable sense that's the format returned in onChange :)

Hello -

In an effort to sustain the react-select project going forward, we're closing old issues / pull requests.

We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our limited efforts to maintain the latest version.

If you aren't using the latest version of react-select please consider upgrading to see if it resolves any issues you're having.

If you feel this issue / pull request is still relevant and you'd like us to review it, please leave a comment and we'll do our best to get back to you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

coder-guy22296 picture coder-guy22296  路  3Comments

ericj17 picture ericj17  路  3Comments

juliensnz picture juliensnz  路  3Comments

AchinthaReemal picture AchinthaReemal  路  3Comments

pablote picture pablote  路  3Comments