react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Add the ability to show the number of selections and not the tokens as there are lot of selections

Open erezLieberman opened this issue 8 years ago • 11 comments

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: gbzdl 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.

erezLieberman avatar Jul 11 '18 11:07 erezLieberman

Hi @erezLieberman,

I'm looking for the same solution. Please let me know if you found something.

leonzinger avatar Jul 22 '18 13:07 leonzinger

@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">&nbsp;</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}
        />

    );
}

erezLieberman avatar Jul 23 '18 09:07 erezLieberman

Hi @erezLieberman Thank you! Found another solution that worked for me. Just use CSS nth-child(n+1) {display: none}

leonzinger avatar Jul 24 '18 06:07 leonzinger

@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} />

Sawan1994 avatar Sep 28 '18 10:09 Sawan1994

@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.

wesleywong avatar Nov 22 '18 07:11 wesleywong

@leonzinger How do you add that css? I'm facing the same problem...

hrafaelveloso avatar Feb 05 '19 12:02 hrafaelveloso

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 }}
  />

CodeSanbox

Rall3n avatar Feb 05 '19 13:02 Rall3n

{React.cloneElement(children[1])} crashes my app because it gets rendered for every option...

hrafaelveloso avatar Feb 05 '19 14:02 hrafaelveloso

@hrafaelveloso you might be doing something wrong, because:

  1. The example works fine.
  2. The ValueContainer component should only be rendered once, so it being there for every option is pretty strange. children[1] should be the Input component that is being passed to the ValueContainer.

Rall3n avatar Feb 05 '19 18:02 Rall3n

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.

hrafaelveloso avatar Nov 21 '19 10:11 hrafaelveloso

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!

bladey avatar May 28 '20 05:05 bladey