-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
onChange returns the selected value, not the complete event #1631
Comments
👍 I've found that can cause issues with |
Currently there is no way to get the original event, at least I didn't found one and there no mention of original event in docs. |
Would love to see this feature implemented, you can do so much from getting the original event! |
Another workaround: you can use
|
I've run into this issue, passing a value to an onChange handler passed from one of For 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. |
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. |
I'm also looking for this feature, because either it's impossible right now to change the validation message when the field is required or I don't know how to do it. Here is a CodeSandbox that implements this solution to change the message, which doesn't work on Select due to the missing event. Edit: Nevermind, I found this. |
Any chance of ever getting resolution on this issue? |
@gvincentlh this is just a guess, but I think this function should be changed so that it saves the event https://github.com/JedWatson/react-select/blob/master/src/Select.js#L452-L461 Then the event could be passed optionally instead of the value (there is a simpleValue option already, this could be similar I think) |
@JedWatson do you have an opinion on this that you care to share here? |
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. |
@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. |
This is what I came up with: https://gist.github.com/cyrilf/109fc97a22cd080abcc5068a30976696 Hope it helps ✌️ |
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 |
I made it work by encapsulting the onChange function with another function that pass the Select name :
|
Nice hack. Why not just use vanillajs then? |
It's the method documented by the react.js team : https://reactjs.org/docs/handling-events.html#passing-arguments-to-event-handlers |
no, I mean, the variable that seems to be a string, selectName, is actually an object with an .id property.. sure, I could just pass the whole state there, but that doesn't mean I get my original event object unless I somehow recreate a live reference to some dom element, which you seem to be doing here |
The method mentioned by bflorat returns the mouse click event on the react-select rendered div if doing a selection. It definitely would be nice to see this implemented, but can work around it for now. (edited to clarify). |
Wow This is not implemented? Accept the PR? Im worried about backwards compatibility. Others have wrote custom logic because the wired problems |
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. |
https://react-select.com/upgrade-guide |
this should be documented. is it yet? Should be in the minimal example on the readme |
I don't see how this solves anything related to this issue. |
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. |
@bobort have you tried https://github.com/paypal/downshift ? :) |
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 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? |
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. |
So, what was the design process for not passing the event but only the value. I'm curious? Also, what problems do you think this simple wrap will contain. Let's say I build a React component like this. Why it would be a problem if library authors haven't provided that option? Thoughts! |
Dear @kushalmahajan I have the same exact questions :)
It's not about problems. I mean, problems can be stated with additional markup, especially if someone wants the code to follow specific semantics. But the bigger question is, why would a library push its users to such hacks? |
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. |
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. |
They have a good reason to prefer this pattern. The call to the function bound to the 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 |
Wow! I would have expected that by now some work-around would have been available for this though. |
## Issues - No way to access the entire `onChange` event: JedWatson/react-select#1631 so we're "simulating it" https://github.com/auth0/cosmos/pull/1512/files#diff-92e315380bbd9b642236e410bd014289R147 - It does not seem to output a real `<select>` component so: - We cannot use our current automation attributes. - We cannot add a custom id to the select because there is no select. API Compatibility: **No breaking changes**. In this PR I include some "adapters" for react-select that may slow down the performance of the component, we could make our API closer to react-select's one in order to remove this adapters and improve performance.
Actually this was already a previous issue, that was marked as closed in v2, but it's not resolved at all: pinging @gwyneplaine on that as he's the one who closed it. |
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 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 The The only reason I can think of to fake the native import ReactSelect from 'react-select';
export const Select = ({ onChange, name, ...props }) => {
const patchedOnChange = (value) => {
onChange({ currentTarget: { value, name }});
}
return <ReactSelect onChange={patchedOnChange} name={name} {...props} />
} Hope this clears up the reasoning and workaround. |
From the React docs:
The HTML element called 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. |
@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
There. Isn't. Such. An. Event. There is for native
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. |
|
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. |
## Issues - No way to access the entire `onChange` event: JedWatson/react-select#1631 so we're "simulating it" https://github.com/auth0/cosmos/pull/1512/files#diff-92e315380bbd9b642236e410bd014289R147 - It does not seem to output a real `<select>` component so: - We cannot use our current automation attributes. - We cannot add a custom id to the select because there is no select. API Compatibility: **No breaking changes**. In this PR I include some "adapters" for react-select that may slow down the performance of the component, we could make our API closer to react-select's one in order to remove this adapters and improve performance.
I know this might not be the ideal solution, but a workaround would be to set the value={{ "valueOne": object.id, "valueTwo": "Another value" }} And then onChange={(event) => onChangeMethod(event.target.value.valueOne, event.taget.value.valueTwo)}
// ^ or you could send the whole event and extract the values in the method Hope it's useful for somebody else! |
i want to get value , not object
you will get |
Do we have any way to save the data in react-select? I mean that when we got the data already and we press F5 to reload it, it can't be lost. |
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:
… @tronganhnguyenthanh <https://github.com/tronganhnguyenthanh> ,
You would need to implement a client-side storage solution if you want
data to persist beyond the lifecycle scope of the application.
There's <https://usehooks.com/useLocalStorage/>
a <https://github.com/beautifulinteractions/>
plethora <https://github.com/hacknlove/usestorage>
of <https://github.com/ugenc/useStorage>
different <https://github.com/alex-cory/use-react-storage>
solutions <https://github.com/capacitor-community/react-hooks>
out there for this kind of thing...
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1631 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHLDLXCYRCKAEXTTT3Y34N3TTPRQFANCNFSM4DFPOY6Q>
.
|
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:
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?
The text was updated successfully, but these errors were encountered: