-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(radio): allow native form onChange to fire for Radio selection #10441
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
Open
starboyvarun
wants to merge
1
commit into
adobe:main
Choose a base branch
from
starboyvarun:fix/radio-native-form-onchange
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+55
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern appears to be coming from some places like:
react/react#10135
https://gist.github.com/kentcdodds/5e54cdbadf1b08ed7003c33fc00bab4c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this approach, what else did you consider?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you caught me ,
I lifted that from the
setNativeValuepattern floating around in those RTL/gist threads. Fair call, it shouldn't be in library code. It leans on React's internal value tracker, and now that I look again thenew MouseEvent()is wrong too, since there's agetOwnerWindowcall literally ten lines above it for iframe support.Things I tried before landing on that one:-
First was just dispatching a bubbling
changeevent. Cleaner, but it doesn't actually work — React usesclickfor radio/checkbox change detection (shouldUseClickEvent), so a native listener would see it but an ancestor React<form onChange>still wouldn't fire. Half a fix, so I dropped it.Then I went down the tracker route, which is what you're looking at. It works, but yeah, I don't love it either now.
What I should have done from the start is just call
nextElem.click():The browser sets checkedness through its own activation behavior instead of the
checkedsetter, so the tracker goes stale on its own and React fires onChange down the normal path.useRadio's onChange then callssetSelectedValuefor us, so that line goes away too. Nothing poking at React internals.Ran it locally — RAC RadioGroup 74/74, v3 Radio 57/57, S2 RadioGroup 4/4. I also went and checked read-only/disabled, since
setSelectedValueno-ops in those cases and I'm deleting the call: read-only arrow keys, read-only click, and disabled arrow keys all still leave the selection alone and never fire onChange. React restores the controlledcheckedafter the click, so it holds up.One other thing I noticed while measuring all this — my PR description is wrong. I said the
stopPropagation()removal was about native events bubbling to the form. It isn't. If you attach a realform.addEventListener('change', ...), that already works on main for mouse clicks; it was only ever broken for keyboard. WhatstopPropagation()actually killed was the synthetic event reaching an ancestor ReactonChange. I'll rewrite the description and add a test with an actual native listener, because right now both my tests use React'sonChangeon the form and don't cover that case at all.Before I push anything though — is
.click()the direction you'd want here, or is there an approach you'd rather I take? I'm also fine splitting this up and landing just thestopPropagationremoval first, with the keyboard side as a separate PR if you think that's worth discussing on its own. Happy to go whichever way you prefer rather than guessing again.