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

Add class to default value component

Open SeanSilke opened this issue 9 years ago • 7 comments

Hello recently i been working with multi select and needed to highlight the value that had been clicked. I accomplished it by using default Value component as valueComponent and adding class to it. But only way to add class it to mutate props.value. Is there better way to add class to default value component?

Here my code.

import Value from 'react-select/src/Value';

  ValueComponent(props){
    if (props.value.value === this.props.highlighted){
      props.value.className = 'Select-value-highlighted';
    }
    return (<Value {...props}/>);
  }
      <Select
        ...
        valueComponent = {this.ValueComponent}
        />

One other way is to wrap component with div that have class, but this div will be redundant and css will look not so clean.

Also will it be bette if you expose Value directly, so it can be imported like following? `import {Value} from 'react-select';``

SeanSilke avatar May 17 '17 23:05 SeanSilke

Hi @SeanSilke you can accomplish this by using the valueComponent. You can see how this is done here: https://github.com/JedWatson/react-select/blob/master/examples/src/components/CustomComponents.js#L55

agirton avatar May 18 '17 04:05 agirton

Thanks @agirton. I actually done it with valueComponent. The code above is working fine. screenshot at may 18 13-00-49 My concerns is how i done it.

As you can see i need only a small change in standard Value component. And only way to add class to standard Value component was the mutation of props.value.

  ValueComponent(props){
    if (props.value.value === this.props.highlighted){
      props.value.className = 'Select-value-highlighted';
    }
    return (<Value {...props}/>);
  }

Is there better way to add class to standard Value component?

May be it is good idea to add class modifying props to library's Value Component? So now in render function the is

<div className={classNames('Select-value', this.props.value.className)} 
 ...
</div>

And if wee add some props say customClass we can use it like this

<div className={
    classNames('Select-value', this.props.value.className, this.props.customClass )
    } 
     ...
</div>

SeanSilke avatar May 18 '17 10:05 SeanSilke

You can deep-clone props with underscore or lodash before mutating value.className. Or use ramda`s assocPath, which will essentially do the same but with less code.

cbergmiller avatar Jun 01 '17 20:06 cbergmiller

@cbergmiller thanks. Yes deep copying is a way to avoid mutation. But there is still question. Is there better way to add style to standard Value component? And if there is no better way. Is it possible to extend Value component and add this feature to it? @agirton @JedWatson ?

SeanSilke avatar Jun 02 '17 17:06 SeanSilke

+1 @SeanSilke, did you find any better way?

dspoonia7 avatar Sep 13 '17 05:09 dspoonia7

@dspoonia7 No I didn't. I end up with modifying props.value.className as mentioned earlier.

SeanSilke avatar Sep 13 '17 08:09 SeanSilke

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

bladey avatar May 27 '20 06:05 bladey