react-select: onChange returns the selected value, not the complete event
onChange simply returns the new value and label and not the whole event.
I understand we can use the hidden input field to fire an onChange but this does not happen automatically and while we can manually fire an onChange for hidden input field, we can also wrap an event around the Select. Here is how I am doing it following #520:
<Select
name="form-field-name"
value={val}
options={options}
onChange={(val)=> {onChangeFunc({target: { name:'revision_id', value: val.value }})}}
/>
Creating an object myself and passing it to my onChange handler. Is this the right way to do it or you can suggest a better way around it?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 97
- Comments: 46 (3 by maintainers)
Commits related to this issue
- [DXDP-154] Select with react-select wrapper (#1512) ## Issues - No way to access the entire `onChange` event: https://github.com/JedWatson/react-select/issues/1631 so we're "simulating it" https://g... — committed to auth0/cosmos by deleted user 5 years ago
- [DXDP-154] Select with react-select wrapper (#1512) ## Issues - No way to access the entire `onChange` event: https://github.com/JedWatson/react-select/issues/1631 so we're "simulating it" https://g... — committed to auth0/cosmos by deleted user 5 years ago
@ashnur I’m going to address your comment for the benefit of anyone else who is seeking to understand the reasoning here, but I also want to make it clear that your language and tone is not welcome or acceptable.
First of all, this isn’t about retaining the synthetic event, I know how to do that - it’s about getting an event in the first place.
It’s not possible to create a
selectcomponent and capture anonChangeevent by changing itsvalue. The event requires user interaction to fire. Faking the interaction would be significantly less predictable than choosing not to try and conform to an API that we can’t correctly implement.There. Isn’t. Such. An. Event. There is for native
selectcomponents, and if they do what you need, I wouldn’t recommend using this library.react-selectis one giant non-standard workaround. It fills a gap left by the HTML spec, because native select controls are insufficient for a range of use cases; that hasn’t changed in two decades, and this is far from the first library to deal with it (prior art includes Selectize and Select2 for jQuery, you can go further back to YUI and other frameworks if you like)There’s no such thing as a “proper API”. This project is not pretending to be a native form control. If anything, the API has been designed to prevent implementation details (such as unpredictable source events) from leaking through.
Anyway, I’m done with this issue, hope the explanation and workaround helps people who come across this in the future, but I don’t maintain this project to be called dishonest or obnoxious.
@JedWatson
From the React docs:
The HTML element called
selectdoes have such an event, being a HTML element. You could use the original or could create a new one.If you call this a select library, that is to be used in place of that select, it is highly dishonest to say that there isn’t such an event. We all used it for quite a long time, it exists. It’s your implementation details that are leaking through and blocking a proper API. And I find it obnoxious that you suggest further propagating said leak of internals by way of one of, non-standard workarounds.
Another workaround: you can use
thisto access the name of the select (or any of its other attributes). Example:I remember this one time the internet gave me close to 100 thumbs down in a GitHub issue. So I doubled-down on a being jerk to open-source maintainers and it really changed the way everyone saw things.
Any update on this issue?
This library really needs to conform to the expected behavior of input elements. That is, send the original event instead of an object containing label and value.
Has anyone taken a look at Material UI’s example for React Select and their onChange function? They recommend using React Select for an auto-complete example. It helped me solve the issue of an on change event. This is their sandbox https://codesandbox.io/s/5kvmpwpo9k . For easy reference this is their onChange example and their Select component. I hope it gets someone else started on a solution that works in their project.
I wasn’t familiar with multiple arrows in a function. This is a curried function and this had a nice explanation https://stackoverflow.com/questions/32782922/what-do-multiple-arrow-functions-mean-in-javascript
Any chance of ever getting resolution on this issue?
Sorry for not providing an official response to this earlier, I’m jumping in because it was asked on Twitter and that drew my attention to this issue (for some reason I thought we’d answered this before)
As @Rall3n said, the reason for the design of the current
onChangearguments is that there is no reliable event we can pass. As there is no browser-nativechangeevent we can pass, you probably don’t want to receive the underlying browser event that triggered the change (click,touch,keypress, etc) because that wouldn’t be consistent with other form components and interacting with the event wouldn’t necessarily have the expected effect (e.g if you were topreventDefaulton it)In fact from memory, for various conditions we don’t even always have an event (or there may not just be one, because of internal debouncing).
It seems better to consistently provide a predictable API, which can be relied upon in all conditions, rather than faking an API which can’t be properly implemented. And given that, it also made sense not to even make the API look like something it’s not (by mimicing
event.currentTargetetc).The
onChangeAPI provides two things: the updated value, and the event meta that describes how the change happened, as well as other useful data like the name property. As far as I know this is everything we can reliably provide.The only reason I can think of to fake the native
eventAPI shape is if you’re passing a generic handler to multiple form components that include Select and native inputs… which can be solved by extending the Select component, if that’s what you need:Hope this clears up the reasoning and workaround.
Would love to see this feature implemented, you can do so much from getting the original event!
👍 I’ve found that can cause issues with
redux-form’s field component, it would be great to get the original event passed as the second argument on the onChange callback.I didn’t realise this until now and I feel I wasted my time a bit using in my project. This is so common that don’t expect it to be not here.
I made it work by encapsulting the onChange function with another function that pass the Select name :
@bobort have you tried https://github.com/paypal/downshift ? 😃
Actually this was already a previous issue, that was marked as closed in v2, but it’s not resolved at all: https://github.com/JedWatson/react-select/issues/272
pinging @gwyneplaine on that as he’s the one who closed it.
I am not sure who are you talking to but the “we” in this case doesn’t apply to me. I don’t want to add additional markup to deal with inadequate APIs. Throwing code at problems only leads to problems down the road.
I don’t see how this solves anything related to this issue.
I wouldn’t go so far as to blame people. 😃 Let’s be honest, this is still something used for all kinds of meaningful purposes, no matter its drawbacks. There is no reason to try to question the people who I am sure had good reasons for choosing the abstractions they chose.
https://react-select.com/upgrade-guide Seems like there is now a second argument
actionMetaforonChange.nameis accessible from there.well, @github really needs to fix their autocomplete 😂
a good alternative which I’ve been following is downshift, but it’s more barebones rather than a plug-and-play kind of approach.
Wow! I would have expected that by now some work-around would have been available for this though. Hopefully soon.
I’ve run into this issue, passing a value to an onChange handler passed from one of
redux-form’s field component, as @jonathanchrisp mentioned, causes the string to be spread operated by this line: https://github.com/erikras/redux-form/blob/master/src/ConnectedField.js#L103For now the only solution I could think of was writing a single edge case if statement in the container for this component.
I feel like it’s kind of a standard using events in onX handlers, so I’m all in favor for introducing that standard here. To maintain compatibility with people using the onChange handler as is, a simple wrapper could be introduced, and exposed through the props? The proposed solution for adding the event as a secondary argument would probably be better.
this should be documented. is it yet? Should be in the minimal example on the readme
Encapsulation and normalization is great, but please expose the default browser implementation for those that need it. Should be a minimum when building components around browser UI elements.
Dear Mr Eric Bonow, I would like to say a big thank you for your support and I would like to take your facebook account or any other social media you are using to take the advantage in learning new things. Thank you and best regards, Trong Anh
Vào Th 7, 19 thg 6, 2021 vào lúc 07:40 Eric Bonow < @.***> đã viết:
https://react-select.com/advanced#action-meta
The documentation is even wrong. It states that action is a string, but it is an object with the properties action, option and name. It solves the issue with getting the name of the target at least.
However, I understand your frustration. This behavior is not intuitive and even worse, not documented correctly. There are other things wrong with this library. For instance, when you define the options as value-label pairs, I would expect it would be enough to pass the value as prop to have a controlled input. But the event only gives me the value, then I have to built the object with the value-label pair again and pass it to the element. This results in a lot of boilerplate, which makes this lib basically a pain in the ass. It is a shame that there is so much work put into this and then it does not even handle the most basic stuff by just following the html standards.
It seems to make the whole issue even worse, @ashnur . If the implementation is ever corrected, now developers who used that second argument will have to refactor their code even more. I expected so much more from this project. I’m just now diving into ReactJS, and I thought I was missing some major concept. Nope. The developers of this particular package just didn’t follow standard procedures.
This is what I came up with: https://gist.github.com/cyrilf/109fc97a22cd080abcc5068a30976696 Not perfect and a bit hacky, but it solved my use-case where I really needed an
eventHope it helps ✌️
They have a good reason to prefer this pattern. The call to the function bound to the
onChangeprop is not a result of achangeevent, but it is the result of akeydown,clickortouch. The event has noinputelement as the target, therefore no name, no value.This pattern at least provides you with the selected option as the value and (thanks to a previous update) the name of the select (provided by the
nameprop) in the action meta as a second argument.Well, the person replying to every comment could have stopped all this by just giving an example that it’s okay to return the value “only” instead of even, as a result of handleChange.
If we want to get
event, we can easily do so by wrapping the select in a div and let the event bubble. Therefore parents’ div can have a handleChange instead and useevent.currentTarget.valueto retrieve the value.I remember even I commented on it a while back just to know my statement was incorrect but he seems to reply to everyone in same fashion as he did to my comment. Are you really a representative of this package?
Nice hack. Why not just use vanillajs then?
@kushalmahajan it’s quite an interesting fact that react-select became the only viable/usable option for a customized select input control, despite all the drawbacks.