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

Would be nice to be able to set label and value to custom properties

Open minimaximus opened this issue 11 years ago • 18 comments

Right now, I have to make sure my array contains elements with label/value pairs, which requires modifications on the server. It would be really nice if react-select would take arbitrary elements as label/value fields. So for example, if I have [{name:'Alex', id: 1}, {name:'Jane', id:2}], I could tell react-select that the label field is 'name' and the value field is 'id'.

Thanks.

minimaximus avatar May 18 '15 21:05 minimaximus

@minimaximus why? Can you not transform your data prior to usage? This is just pushing complexity lower, we should be lifting complexity out.

dcousens avatar May 18 '15 23:05 dcousens

Duplicate of https://github.com/JedWatson/react-select/issues/58 Related to https://github.com/JedWatson/react-select/pull/95

dcousens avatar May 18 '15 23:05 dcousens

Also want this. Why bother transforming when the library could just use the correct keys as provided by my db? Less ops overall.

adamscybot avatar Jun 10 '15 09:06 adamscybot

Can tell my use case. I use http request for getting filtered data and my backend responded me with id, name fields. id will be used as value and name as label. That's why I need my settings for label and value fields.

imevro avatar Jun 25 '15 19:06 imevro

+1

malonecj avatar Jul 23 '15 14:07 malonecj

+1

mmillet avatar Aug 03 '15 05:08 mmillet

+1

PetrSnobelt avatar Aug 05 '15 08:08 PetrSnobelt

In real use almost never we have { value, label } objects. It means that everyone who uses that library have to implement mapping from real data format to library acceptable format. So why not provide the getters for value and label as it done in other libraries?

oluckyman avatar Sep 29 '15 13:09 oluckyman

@oluckyman what other libraries?

dcousens avatar Sep 29 '15 13:09 dcousens

d3 is heavily using accessors functions. But now I have looked among autocomplete libs. Some of them provides custom getter and some of them no. jQuery autocompleter is like react-select, it doesn’t give alternatives to { label, value } format.

oluckyman avatar Sep 29 '15 14:09 oluckyman

If you try to use react select on existing api (which you can't modify), it is pain without ability to set label and value. Some api use Id and Name, some key and value etc...

PetrSnobelt avatar Sep 29 '15 16:09 PetrSnobelt

I'm happy to add labelKey and valueKey props. Let's do this.

JedWatson avatar Sep 30 '15 13:09 JedWatson

I already implemented this in my wrapper class, so I can do this for react-select too

Ilyá Belsky

Sent from my iPad

On 30 сент. 2015 г., at 15:39, Jed Watson [email protected] wrote:

I'm happy to add labelKey and valueKey props. Let's do this.

— Reply to this email directly or view it on GitHub.

oluckyman avatar Sep 30 '15 15:09 oluckyman

@oluckyman thanks for the offer, I jumped in and implemented this but would love some tests and a change review if you're up for it!

JedWatson avatar Sep 30 '15 16:09 JedWatson

@JedWatson thx for this neat abstraction! Do you think it'd make sense to add support for nested accessors?

const data = { foo: { bar : 'label' }, baz: { qux: 'value' } }

<Select valueKey='foo.bar' labelKey='bar.qux' ... />

Would be trivial to implement using e.g. lodash.get but not sure if you want to add another dependency to the project just for this (especially if you wouldn't need it elsewhere in the codebase) :smirk:

mmrko avatar Oct 18 '15 20:10 mmrko

Would love to have nested accessors as well! Thanks.

nitinja avatar Nov 28 '16 13:11 nitinja

For those, who found this issue, but can't find out how to get same functionality in newer versions: take a look at getOptionLabel prop.

mudrila avatar Jul 12 '19 20:07 mudrila

Just to extend @mudrila 's answer, you can use getOptionLabel like this:

<Select
    options={myArrayOfObjects}
    getOptionLabel={(option) => option['myCustomLabel']}
/>

flowfelis avatar Apr 03 '20 13:04 flowfelis