Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 - display icon in selected option #2553

Closed
jechlin opened this issue Apr 24, 2018 · 14 comments
Closed

v2 - display icon in selected option #2553

jechlin opened this issue Apr 24, 2018 · 14 comments

Comments

@jechlin
Copy link

jechlin commented Apr 24, 2018

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

@jechlin
Copy link
Author

jechlin commented 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
Copy link
Collaborator

No worries @jechlin, glad you got to the bottom of it

@canda
Copy link

canda commented 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 also MultiValueContainer, MultiValueLabel, and MultiValueRemove.
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
Copy link

hello,
anyone know an example how tu add the icon to the option?
thanks

@canda
Copy link

canda commented 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

import { components } from 'react-select';
const { Option } = components;
const IconOption = (props) => (
    <Option {...props}>
      <SomeIcon />
      {props.data.label}
    </Option>
);

And then you apply it to your Select component

import Select from 'react-select';
[...]
  <Select
    [...]
    components={{ Option: IconOption }}
  />

@TroyWolf
Copy link

@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
Copy link

AbhaysinghBhosale commented Feb 19, 2019

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

import { components } from 'react-select';
const { Option } = components;
const IconOption = (props) => (
    <Option {...props}>
      <SomeIcon />
      {props.data.label}
    </Option>
);

And then you apply it to your Select component

import Select from 'react-select';
[...]
  <Select
    [...]
    components={{ Option: IconOption }}
  />

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
Copy link
Collaborator

Rall3n commented 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
Copy link

@canda You are legend sir!

@AbhaysinghBhosale
Copy link

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

I am using version 2.0 and its not working for the selected option

@AbhaysinghBhosale
Copy link

@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
Copy link

Ku4rtz commented Jul 20, 2019

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

import { components } from 'react-select';
const { Option } = components;
const IconOption = (props) => (
    <Option {...props}>
      <SomeIcon />
      {props.data.label}
    </Option>
);

And then you apply it to your Select component

import Select from 'react-select';
[...]
  <Select
    [...]
    components={{ Option: IconOption }}
  />

If your Icon is image, how should I be able to do :

const IconOption = (props) => (

''
);

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
Copy link

tapas100 commented Oct 11, 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} />

can you plz share your version and code sample ... i am also using 2.0 but unable to achieve the result

@ahessami
Copy link

ahessami commented Feb 5, 2020

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

can you plz share your version and code sample ... i am also using 2.0 but unable to achieve the result

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

import React from "react";
import Select, {components} from 'react-select';
import {avatarOptions} from '../helpers/ddOptions';

class MyApp extends React.Component {

    // Change event handler for React-Select component.
    handleOnChangeReactSelect = (selectValue) => {
        // Update the current stopwatch.
        // Take a copy of the current stopwatch.
        const updatedStopwatch = {
            ...this.props.stopwatch,
            "avatar": selectValue
        };
        
        this.props.updateStopwatch(this.props.stopwatchindex, updatedStopwatch);
    }

    render() {

        // React Select mods.
        const { Option, SingleValue } = components;
        const IconOption = (props) => (
            <Option {...props}>
                <img className="maybeUseClassName" src={props.data.label} style={{width:30}} alt="" />
            </Option>
        );
        const ValueOption = (props) => ( 
            <SingleValue {...props}> 
                <img className="maybeUseClassName" src={props.data.label} style={{width:30}} alt="" /> 
            </SingleValue> 
        );

        return (
                <div className="name-container">
                    <Select 
                        name="avatar"
                        value={avatar}
                        onChange={this.handleOnChangeReactSelect}
                        options={avatarOptions()}
                        components={{ Option: IconOption, SingleValue: ValueOption }}
                    />
                </div>
        )
    }
}

export default MyApp;

Thx @canda, @Rall3n and @AbhaysinghBhosale!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests