-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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 handler for radio buttons does not fire according to spec. #1471
Comments
I believe it's intentional that it doesn't fire for true->false (because you really only want one event) but the true->true should not be counted. Essentially you're getting a change event for the radio group rather than for the individual radio button. |
I guess that makes some sense (I'm not sure it's ideal, but it's what the DOM does too...), but in that case the docs aren't accurate (and the true->true issue remains). |
Agree. If someone wants to tackle this, it would be nice if "changing" from checked to checked didn't trigger an onChange event. I'm also happy to accept a docs PR to make the expected behavior clearer. |
@gaearon Interested in taking a look at this one? Right now ChangeEventPlugin fires an onChange event for every |
@spicyj Yup, I'm interested. I can take a look closer to the weekend, OK? |
Sure, no rush. |
Onchange is not triggered when there is a change from true->false |
@spicyj can't we just add
in |
@itrelease No – at the point that handleChange is called, the |
…the reliable thing to get on a onChange event (see facebook/react#1471)
Hi, I encountered this issue as well - it still happens. jsfiddle- native html, vanilla js: https://jsfiddle.net/etai/yfsmqsqo/2/ on each, select a radio, then select one which is already selected. Any idea when this will be fixed? This forces us to abort the onChange when 'this' radio is the selected value, which is just ugly.. |
@spicyj Can you give a starting point to dive into this? I'd like to try this as my first PR for React. |
@gyzerok I have a fix for this, but unfortunately it's nearly impossible to write tests for react. It was a nightmare, I spent over 12 hours just trying to get it working, even with help of some people on the react IRC channel, with no success. |
@EtaiG Sorry for the delay. If the change is simple, we can take it without tests and if it's complex, maybe I can write some. Do you want to post a PR with your code as it is now? |
@spicyj Thanks for answering. |
It Looks like I deleted my changes in my local react clone since. Though I still have my test written (even though I can't run it) :) Anwyay, I merged react's master locally and checked the code again, so I'll explain the problem and the possible solutions (PR later if you want). The problem is caused by the fact that react uses the native click event to listen to user interaction with the radio, and then calls the onchange for the input. The reason for this (at least according to the comment in the code) is because IE8 does not fire the change event until blur, so in order to be 'compatible' with IE8, the click event is used. There is a tradeoff- supporting IE8 for a normal radio, while losing the W3 standard for radio, and also causing anyone who uses react and wants to specifically know when the radio selection has changed, to have to write more stateful code. So the possible solutions to this problem:
2 will work even if 'checked' is not passed to any radio, and 3 will work otherwise and is probably the cleanest solution (with the smallest amount of code). It's not possible to try and check the DOM for the value, since the 'click' event arrives after the change already occurred. I personally like 3 the most, but 2 is good too. Maybe there's something else I didn't think of. So... what do you think? |
For what it's worth, we're running into this, too. I saw some code that relied on Thanks for being on this, y'all :) |
My vague thoughts:
|
And, I guess if we get out of sync, it's not that bad. If we only use the stored state to decide whether the click event is really a change, then we won't, like, deadlock the checkbox and make it ignore inputs. Worst-case scenario, if someone screws with the state underneath us, we miss an (And, even then, I think programmatic changes fire |
@matchu I didn't write it up as a PR yet, though I tried all of the versions here to see what's possible. I'll write them up this weekend and post them here, and then maybe make a PR out of one or all of them. |
Sweet! Thanks, @EtaiG :) Incidentally, today I discovered a new issue in synthetic events for radio buttons: #4854. That might be an interesting follow-up project since you've already explored React's radio button event system... or you might just be tired of it all, which is fine, too ;P Our app is gonna patch over it for the time being, anyway. |
This kind of seems like it might be a breaking change for some people; especially since the behavior's been the same for all versions of React up until 15.6 :-/ should this have waited for v16? |
@ljharb What specifically are you concerned about? This seems like maybe nit the issue you meant to comment on? |
@jquense this is the correct issue - new in React 15.6 (not in 15.5 and below), |
Ah ok, onChange firing twice for double clicks or for already selected radios was a bug, it wasn't ever the intended behavior. I'd place it in the same category as the other issues fixed by the PR, such as range inputs not firing consistently or inputs missing events in IE. |
Just adding, if you think we missed a case we're the old behavior is more "correct" in some sense or for a use case we'd be glad chat about it, but might be better to open another issue since this one has a ton history at this point. I'm also sorry if we inadvertently moved your cheese :/ the input events code is hairy and the tension between semver breaking changes and bug fixes is particularly tough to navigate here! |
@jquense perhaps, but it was a "bug" for fifteen major versions of React. I definitely agree that this change ends up with the correct/better behavior - but we're having to make changes in Airbnb's codebase to account for it pre-upgrade (that would have broken with the upgrade, but worked with 0.13, v14, through v15.4) - so I think it probably would have been better to consider it semver-major, and I hope that in the future, React will err further on the side of caution in non-major releases. |
I agree that it is a breaking change and in our app it changes behaviour. The original behaviour was more convenient: We were able to improve a Radio Button Group -> after selecting the same radio item, it was unselected. Without having to introduce an extra button to unselect all – which is needed sometimes. Now there is no event, we could use to unselect already selected radio group. |
I'm also having this problem. Radio
Also, |
If you have a problem please post a new issue. It’s impossible to keep track of old closed issues. |
As described on http://facebook.github.io/react/docs/forms.html, the onChange handler of an input should fire when the checked state changes. However, it actually fires when the radio button is clicked,
In other words, it fails to fire when a checked radio button is unchecked (by checking a different radio button), and it fires even without a state change when a checked radio button is clicked.
In short, it's missing checked transtions true->false, and it's reporting spurious transitions true->true.
The text was updated successfully, but these errors were encountered: