fix: call onChange for clicks on detached checkboxes and radios #118
main fromrefs/pull/118/head·opened Refs #115. Stacked on #116; this covers its last "Not covered" item. Until #116 merges, this diff includes its commits; review only the last one (c425e2a).
Browsers fire no change when a checkbox or radio outside the document is clicked, e.g. input.click() in a container never appended. Redact maps their onChange to change, so neither the handler nor #116's restore ran. React reads them from click (shouldUseClickEvent), which reaches detached elements, and fires onChange when its value tracker sees checked change.
What changes#
- A checkbox's or radio's
onChangealso listens forclick, but only acts while the element is detached. Attached controls keepchange, so nothing changes for them. - A detached radio fires only if it was unchecked, judged by its
checkedprop, else the state the last restore saw, else its default. The restore then undoes a rejected click. - As in React, a detached click that
onClickprevents still callsonChange.
Switching attached controls to click too would match React there as well (onChange despite a prevented click, none for a dispatched change), but it would change event order and restore timing for every app to fix a detached-only case.
Size#
+65 to +97 B gzip against #116; three entries go over budget. DETACHED_CHANGE_SIZE_ALLOWANCE holds the measured increments and needs your approval. If that's too much for a detached-only gap, I'm happy to close this.
Not covered#
All detached only:
- Listeners run in prop order, so
onChangewritten beforeonClickruns first; React always runsonClickfirst. - An uncontrolled radio takes its previous state from the last restore, so it misses
onChangeafter a handler-less sibling was checked, and repeats it for two clicks with no await between. - The restore stays asynchronous, as in #116; React's is synchronous.
Testing#
Removing the fix fails 10 of the 17 new jsdom tests; the other 7 check cases where nothing may fire or change. The browser test confirms browsers fire no change for detached controls, and its detached cases fail without the fix in Chromium, Firefox and WebKit. pnpm test:pr passes on Node 24.22.0, and the actions browser suite passes in all three browsers.
I used Claude Code to write the fix and tests, and had it run the suites and a separate review pass.
- 1
Important
Draft PR not reviewed#
Draft PRs are not automatically reviewed by default.
- Trigger a manual review
To automatically review draft PRs, update your CodeRabbit configuration:
reviews: auto_review: drafts: true- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands.
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: c425e2ac639630ee5294aec3e5aa30c54228b9ad
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR