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

Ability to set style for each item?

Open searene opened this issue 8 years ago • 5 comments

This is a feature request.

I'm trying to create a fontFamily dropdown, something like this (https://nardeas.github.io/react-font-picker/).

react-font-picker 2018-01-18 10-02-42

As you can see, the font family of each item is different, so the user could be able to see each font's style. But I cannot find a way to achieve this in react-select. Is it possible to set each item's style? Something like this.

<Select
  name="form-field-name"
  value={value}
  onChange={this.handleChange}
  options={[
    { value: 'one', label: 'One', style={fontFamily: 'Arial'} },
    { value: 'two', label: 'Two', style={fontFamily: 'Times New Roman'} },
  ]}
/>

searene avatar Jan 18 '18 02:01 searene

Well, I found that this was actually supported. Thanks for making such an amazing component!

searene avatar Jan 20 '18 05:01 searene

How did you do it? @searene 🙏

stphnlngdncoding avatar May 22 '18 23:05 stphnlngdncoding

@stphnlngdncoding Exactly what I showed in the example.

searene avatar May 23 '18 15:05 searene

@searene are you sure? because object cannot have a property set with '=', you have to use ':' but even setting it like that did not work for me.

Dror-Bar avatar Jul 07 '20 08:07 Dror-Bar

It's a bit tricky, but the documentation covers it in "styles" section. I did it like this:

<Select
styles={{ option: (provided, state) => ({ ...provided, fontFamily: state.value })}}
  name="form-field-name"
  value={value}
  onChange={this.handleChange}
  options={[
    { value: 'Arial', label: 'Arial' },
    { value: 'Times New Roman', label: 'Times New Roman' },
  ]}
/>

Dror-Bar avatar Jul 07 '20 09:07 Dror-Bar