Add class to default value component
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';``
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
Thanks @agirton. I actually done it with valueComponent. The code above is working fine.
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>
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 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 ?
+1 @SeanSilke, did you find any better way?
@dspoonia7 No I didn't.
I end up with modifying props.value.className as mentioned earlier.
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.