Add the ability to show the number of selections and not the tokens as there are lot of selections
Hi,
In our app the react select items should have a lot of options selected and we start to see a lot of items that look like this:
We thought about a solution that if there is only x selected it will be like now and if there is one than x it will remove the tags and show text like "selected: x"
by the way i asked about the right UX solution here.
Hi @erezLieberman,
I'm looking for the same solution. Please let me know if you found something.
@leonzinger we made a wrapper component that wraps the select component and adds this feature. the code looks something like that:
makeHiddenItemsItem(length) {
return (
<div className="Select-value more-than-allowed">
<span className="Select-value-label" role="option" aria-selected="true">
{(length)} More selected
<span className="Select-aria-only"> </span>
</span>
</div>
);
}
makeValueItem(isLimitVisibleItems, totalVisibleItems, items) {
const totalHiddenItems = items.length - totalVisibleItems;
if (!isLimitVisibleItems || totalHiddenItems === 0) return undefined;
const isVisibleItem = value => items.indexOf(value) <= totalVisibleItems - 1;
const isFirstHidden = value => items.indexOf(value) === totalVisibleItems;
return valueProps => {
if (isVisibleItem(valueProps.value)) {
return <Value {...valueProps} />;
} else if (isFirstHidden(valueProps.value)) {
return this.makeHiddenItemsItem(totalHiddenItems)
}
return null;
}
}
render() {
const valueItem = this.makeValueItem(this.props.limitVisibleItems, this.props.numberOfVisibleItems, this.props.value);
return (
<Select
{...this.props}
onChange={this.handleChange}
removeSelected={false}
valueComponent={valueItem}
/>
);
}
Hi @erezLieberman Thank you!
Found another solution that worked for me. Just use CSS
nth-child(n+1) {display: none}
@erezLieberman @leonzinger I found a way without adding wrapper and using feature provided by react-select itself.
You can use (components) and assign below code to it:
const MultiValue = props => {
return (<components.SingleValue {...props}>
<span>{"selected " + props.selectProps.value.length}</span>
</components.SingleValue>);
}
<Select
components={{ MultiValue }}
isMulti
closeMenuOnSelect={false}
hideSelectedOptions={false}
options={options}
/>
@erezLieberman @leonzinger I found a way without adding wrapper and using feature provided by react-select itself.
You can use (components) and assign below code to it:
const MultiValue = props => { return (<components.SingleValue {...props}> <span>{"selected " + props.selectProps.value.length}</span> </components.SingleValue>); }
<Select components={{ MultiValue }} isMulti closeMenuOnSelect={false} hideSelectedOptions={false} options={options} />
Hi @Sawan1994 your method actually is to created new SingleValue markup each time you select the option and the element keep add-on new markup but not remove the previous one. open developer tools to inspect it.
Need to find a way to erase the whole markup and replace with SingleValue each time selected an option.
@leonzinger How do you add that css? I'm facing the same problem...
Here is an example using the components framework. I'm overwriting the ValueContainer component to display the text.
const ValueContainer = ({ children, getValue, ...props }) => {
var length = getValue().length;
return (
<components.ValueContainer {...props}>
{!props.selectProps.menuIsOpen &&
`${length} Item${length != 1 ? "s" : ""} selected`}
{React.cloneElement(children[1])}
</components.ValueContainer>
);
};
<Select
{ ... }
isMulti
hideSelectedOptions={false}
components={{ ValueContainer }}
/>
{React.cloneElement(children[1])} crashes my app because it gets rendered for every option...
@hrafaelveloso you might be doing something wrong, because:
- The example works fine.
- The
ValueContainercomponent should only be rendered once, so it being there for every option is pretty strange.children[1]should be theInputcomponent that is being passed to theValueContainer.
This is a workaround that I'm using, and I'll put it here:
const SpecialMultiValue = props => {
const indexInValue = props.selectProps.value.findIndex(x => x === props.data);
const valueLength = props.selectProps.value.length;
if (indexInValue < 3) {
// Total # of selections is less than 3
return (
<span>
{indexInValue !== 0 ? ',' : null} {props.children}
</span>
);
}
if (indexInValue === valueLength - 1) {
// It's the last option in value, return something
return (
<span style={{ paddingLeft: '5px' }}>
(+ {valueLength - 3} users)
</span>
);
}
return null;
}
I wanted to show something like:
User1, User2, User3 (+X users)
You should change the returns to fit your needs.
Hello -
In an effort to sustain the react-select project going forward, we're closing old issues.
We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our efforts towards the current major 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.
However, if you feel this issue 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!