Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

MultiSelect max limit for options #60

Closed
karankumar89 opened this issue Sep 4, 2018 · 1 comment
Closed

MultiSelect max limit for options #60

karankumar89 opened this issue Sep 4, 2018 · 1 comment
Labels

Comments

@karankumar89
Copy link

I was trying to figure out how to limit the number of selections in the multiselect option. For example: if my total number of options are 6, but the user should not select more than 3.

@Aidurber
Copy link
Owner

Aidurber commented Sep 5, 2018

Hi @karankumar89
You can prevent the selection of additional items in the onChange callback. Just don't update your state if the length is greater than a certain amount:

  selectMultipleOption(value) {
    console.log("Val", value);
    if (value.length <= 3) {
      this.setState({ arrayValue: value });
    }
  }

https://codesandbox.io/s/5wmqw8m24n
There is a little weirdness, if you look at the above link the value that is console logged doesn't reflect the number of items selected. This is because the component maintains its own internal state. However, you should still be able to do this, just use your own state as your single source of truth.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants