Skip to content

Limit selected options showing in multi-select #4653

Answered by ebonow
vaibhavarora14 asked this question in Q&A
Discussion options

You must be logged in to vote

Personally, I prefer to use the MultiValue index prop to avoid mapping and filtering React.children of the ValueContainer

Here is a working codesandbox demo

const VALUE_LIMIT = 3;

const MultiValue = (props) => {
  const { index, getValue } = props;
  const hiddenLength = getValue().length - VALUE_LIMIT;

  if (index < VALUE_LIMIT) {
    return <components.MultiValue {...props} />
  }
  if (index === VALUE_LIMIT) {
    return <div className="option-label">{`+${hiddenLength} selected`}</div>
  }
  return  null;
};
  
<Select isMulti components={{ MultiValue }} /> 

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@vaibhavarora14
Comment options

Answer selected by vaibhavarora14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants