Skip to content

fix(RAC): relax inputRef type on Checkbox, Switch, and Radio to accept callback refs - #10451

Open
timges wants to merge 2 commits into
adobe:mainfrom
timges:relax-checkbox-switch-radio-inputref-type
Open

fix(RAC): relax inputRef type on Checkbox, Switch, and Radio to accept callback refs#10451
timges wants to merge 2 commits into
adobe:mainfrom
timges:relax-checkbox-switch-radio-inputref-type

Conversation

@timges

@timges timges commented Aug 11, 2026

Copy link
Copy Markdown

Closes #10319

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

CheckboxProps/CheckboxFieldProps, SwitchProps/SwitchFieldProps, and RadioProps/RadioFieldProps all typed inputRef as RefObject<HTMLInputElement | null>, which meant a ref callback could not be passed even though mergeRefs already accepts one and forwards it down to the underlying <input>.

This PR:

  • Widens inputRef on all six prop interfaces from RefObject<HTMLInputElement | null> to Ref<HTMLInputElement | null>.
  • Widens the internal useCheckboxAria helper's userProvidedInputRef parameter to match (its return type, the already-merged/normalized ref, stays RefObject since that's what the underlying useCheckbox/useCheckboxGroupItem hooks require).
  • Fixes a latent bug found while testing this: Switch/SwitchField built the merged ref inline on every render instead of memoizing it like Checkbox/Radio do, so a callback ref would be torn down and re-invoked on every re-render rather than only on mount/unmount. Wrapped it in useMemo to match.

Added a "should support callback ref" test to Checkbox.test.js, Switch.test.js, and RadioGroup.test.js for both the deprecated and Field variants, asserting the callback is invoked with the input node on mount and its cleanup function is called exactly once on unmount.

Run:

yarn jest packages/react-aria-components/test/Checkbox.test.js packages/react-aria-components/test/Switch.test.js packages/react-aria-components/test/RadioGroup.test.js
yarn check-types

🧢 Your Project:

Spectrum Metrics

…lback refs

CheckboxProps, SwitchProps, and RadioProps (and their Field variants) typed
inputRef as RefObject, so a ref callback couldn't be passed even though
mergeRefs already supports one. Widen inputRef to Ref on all six, widen
useCheckboxAria's internal parameter to match, and memoize Switch's ref
merge like Checkbox/Radio already do so a callback ref only tears down on
unmount instead of every render.

Closes adobe#10319
@timges timges changed the title Relax inputRef type on Checkbox, Switch, and Radio to accept callback refs fix(RAC): relax inputRef type on Checkbox, Switch, and Radio to accept callback refs Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Relax type of CheckboxFieldProps["inputRef"] from RefObject to Ref

1 participant