Closed
Description
Hi - I'm trying to use MultiValueContainer (same problem with MultiValueLabel) to render an icon image alongside the selected text.
However the props passed to MultiValueContainer don't contain the Option object that it's responsible for for drawing. By traversing through the children I could find the label, but the icon url is already part of my Option object.
MultiValueContainer = (props) => {
console.log(props)
return (
<components.MultiValueContainer {...props}>
<span>icon here</span>
{props.children}
</components.MultiValueContainer>
)
}
Any help appreciated.
cheers, jamie
Activity
jechlin commentedon Apr 24, 2018
I see that you need MultiValue - that wasn't clear to me from the docs but was obvious when I implemented it for SingleValue.
Please close, sorry for the noise.
gwyneplaine commentedon Apr 26, 2018
No worries @jechlin, glad you got to the bottom of it
canda commentedon May 1, 2018
I came exactly into the same error by mistake.
I see that the selected option is coming as
data
in the props, but it's not documented.What do you think about reducing the burden in the docs?
https://deploy-preview-2289--react-select.netlify.com/props#components
Perhaps removing some components. For example, having
MultiValue
I'm not sure if it is necessary to have alsoMultiValueContainer
,MultiValueLabel
, andMultiValueRemove
.Perhaps collapsing (hiding) these less important components on the docs.
Perhaps grouping all common props into a collapsable element that is collapsed (hidden) by default.
And I it would be nice to have some description for what each component represents. (A screenshot would be fantastic).
Again this are just a few suggestions, take whatever you feel to.
Great library! Much thanks for your hard work
webmobiles commentedon May 18, 2018
hello,
anyone know an example how tu add the icon to the option?
thanks
canda commentedon May 18, 2018
Just override the Option component with yours and you will have full control on what you want to render.
See Option on https://deploy-preview-2289--react-select.netlify.com/components
Perhaps something like
And then you apply it to your Select component
TroyWolf commentedon Jan 16, 2019
@canda showed how to implement an icon with your option labels. Works perfect with v2. Anyone know how to accomplish similar with v1.2.1? I'm stuck in the past. ☕
AbhaysinghBhosale commentedon Feb 19, 2019
Did the same solutions and working fine. but for the selected option its not showing the icons. It should work like the example 'Custom Placeholder' on this page https://jedwatson.github.io/react-select/
I am using react-select 2.0
Rall3n commentedon Feb 19, 2019
@AbhaysinghBhosale You might have to also overwrite the
SingleValue
component to achieve the same effect as with the options.@TroyWolf For v1.x check out the example
Custom Placeholder, Option, Value, and Arrow Components
on the v1 page https://v1.react-select.com. There is also a working link to the source code of the example.meetajhu commentedon Mar 30, 2019
@canda You are legend sir!
AbhaysinghBhosale commentedon May 15, 2019
I am using version 2.0 and its not working for the selected option
AbhaysinghBhosale commentedon May 15, 2019
@Rall3n Thanks for pointing its worked after using Single Value componentas follows
const { Option, SingleValue } = components; const IconOption = (props) => ( <Option {...props}> <img className='img-responsive img-flex img-filter' src={require(
../../Images/${props.data.Type}.png)} alt="" /> {" "+props.data.label} </Option> ); const ValueOption = (props) => ( <SingleValue {...props}> <img className='img-responsive img-flex img-filter' src={require(
../../Images/${props.data.Type}.png)} alt="" /> {" "+props.data.label} </SingleValue> );
and passing to select as
<Select components={{ Option: IconOption, SingleValue : ValueOption }} classNamePrefix="filter" className={this.state.selectClass} value={selectedOption} defaultValue={selectedOption} label="Single select" options={options} onChange={this.setfunciton} />
Ku4rtz commentedon Jul 20, 2019
If your Icon is image, how should I be able to do :
The fact that I use require(...) gives me a bug cause react can't reach module props.data.image as path, even if the path exists and work if I type it as string. Any solution?
tapas100 commentedon Oct 11, 2019
can you plz share your version and code sample ... i am also using 2.0 but unable to achieve the result
ahessami commentedon Feb 5, 2020
@tapas100 - I don't know if you're still looking for this code sample or not... please see below full example... I used the example from @AbhaysinghBhosale along with insight provided by @Rall3n and @canda and came up with this which is basically the same thing that @AbhaysinghBhosale has provided:
Thx @canda, @Rall3n and @AbhaysinghBhosale!
1 remaining item