How do I pass options to preview images in select. I have country select and I would like to preview country flags. I don't know how to pass the "options" object in this case.
var countries =
[
{value: 'me', label: 'Montenegro'},
{value:'rs',label: 'Serbia' }
];
<Select name={"nationality_" + passenger.passengerId}
value={passenger.nationality}
options={countries}/>
To create a dropdown select in React, use the react-select library. The react-select library has features dynamic search/filter, async option loading, accessibility, and fast render times. In addition, it has a flexible and beautiful Select Input control for ReactJS with multi-select, autocomplete, and ajax support.
q=html+select+image. as a general solution: if you can, get your icons together as SVGs, import them into a font of your choice into the personal Unicode range, use that font in your <option> s; supported by everything up from IE 8.0, small and simple.
Gaurav's answer works but you lose the ability to search an option by its label.
Instead, use the prop formatOptionLabel
which allows you to change the default format:
const countries = [
{ value: 'me', label: 'Montenegro', image: '…' },
{ value:'rs', label: 'Serbia', image: '…' }
];
<ReactSelect
value={passenger.nationality}
options={countries}
formatOptionLabel={country => (
<div className="country-option">
<img src={country.image} alt="country-image" />
<span>{country.label}</span>
</div>
)}
/>
From the doc:
formatOptionLabel
Formats option labels in the menu and control as React components
https://react-select.com/props#select-props
Change the label in countries to html.
const options = [
{ value: 'chocolate', label: <div><img src={copyIcon} height="30px" width="30px"/>Chocolate </div> },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' },
];
Like this add this will add image in options for select.
In my case, when I want to render Country flags, I just add third value to single options, look below:
var countries =
[
{value: 'me', label: 'Montenegro', flagPath: <relative-path-to-flag>},
{value:'rs',label: 'Serbia', flagPath: <relative-path-to-flag> }
];
And if You want to render flag in dropdown options use 'component.Option', but it wouldn't display flag in selected option, for this you have to use 'component.SingleValue', for example:
singleOption = (props: OptionProps<any>) => (
<components.Option {...props}>
{props.data.flagPath? DO SOMETHING WITH YOUR ICON HERE : null}
{props.label}
</components.Option>)
singleValue = (props: any) => (
<components.SingleValue {...props}>
{props.data.flagPath?DO SOMETHING WITH YOUR ICON HERE: null}
</components.SingleValue>
)
Then You must use components inside Select, like this:
<Select
...
components={{
Option: this.singleOption,
SingleValue: this.singleValue
}}
/>
Hope it will help someone to resolve this problem :)
I think you're wanting your select to behave like last but one (Custom Placeholder, Option, Value, and Arrow Components) in this demo. Click here to find out the way they've designed their ones for demo
This may not be the best solution for everybody, but it was the best for me. I was trying to figure out how to do this and this post was great a help. Props to Canda.
1. Define your options as normal, but add an extra key, called image
. This should contain an image URL.
2. Modify the react-select components so that both selected and non-selected options will have images next to them.
3. Add some custom styles to the components to ensure the images are displayed properly.
4. Use the Select
component with some awesome images!
// Step 1
const options = [
{
label: 'Option 1',
value: 0,
image: 'https://example.com/amazing-image-1.png',
},
{
label: 'Option 2',
value: 1,
image: 'https://example.com/amazing-image-2.png',
}
];
// Step 2
import { components } from 'react-select';
const { SingleValue, Option } = components;
const IconSingleValue = (props) => (
<SingleValue {...props}>
<img src={props.data.image} style={{ height: '30px', width: '30px', borderRadius: '50%', marginRight: '10px' }}/>
{props.data.label}
</SingleValue>
);
const IconOption = (props) => (
<Option {...props}>
<img src={props.data.image} style={{ height: '30px', width: '30px', borderRadius: '50%', marginRight: '10px' }}/>
{props.data.label}
</Option>
);
// Step 3
const customStyles = {
option: (provided) => ({
...provided,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
}),
singleValue: (provided) => ({
...provided,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
}),
}
// Step 4
import Select from 'react-select';
[...]
<Select
styles={customStyles}
components={SingleValue: IconSingleValue, Option: IconOption }
options={options}
/>
[...]
I'm by no means an expert with this library. I just wanted to share what I found to be a pretty simple solution to what seems to be a common problem. If something can be done better, please let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With