Repository navigation
[radio group] Fix disabled selected form submission - #4926
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,416.95 ms +122.44 ms(+9.5%) | Renders: 50 (+0) | Paint: 2,119.75 ms +163.85 ms(+8.4%) No significant changes — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
flaviendelangle
left a comment
There was a problem hiding this comment.
PR #4926 Review Summary
PR: [radio group] Fix disabled selected form submission — by @atomiks
Scope: 5 files, narrow & focused. Fixes form-data parity for disabled radios + widens Radio.Indicator.State / Radio.Root context types to match runtime.
Critical Issues
None. All five reviewers concur: no blocking concerns.
Important Issues
1. Silent value divergence when controlled value doesn't match any rendered radio
File: packages/react/src/radio-group/RadioGroup.tsx:148-153
Source: silent-failure-hunter
Pre-PR: <RadioGroup value="missing"> submits "missing" (a signal).
Post-PR: same code submits null silently — because input?.checked is false. Same null is reported for "no input registered", "disabled input", and "controlled/DOM desync". A dev warning when checkedValue != null && !input.checked && !input.disabled would catch this.
⚠️ Counter-evidence from code-reviewer: the validation path still usescheckedValue(viagetRegistrationValuepreferringregistration.valueover the override atuseFieldControlRegistration.ts:49), sorequiredvalidation isn't affected. Only the form-submit payload changes. Worth confirming this is the intended trade-off, not a regression.
2. Drop it.skipIf(isJSDOM) on the two new tests
File: packages/react/src/radio-group/RadioGroup.test.tsx:1035, 1072
Source: pr-test-analyzer (verified locally — both tests pass in jsdom ~10ms)
The neighboring "submits null to onFormSubmit when no radio is selected" test at line 1038 uses the same fireEvent.click(submit) + handleSubmit.mock.calls[0][0] pattern and is not skipped. jsdom 27.4 correctly returns null from FormData(form).get('group') for disabled radios. The skip needlessly halves coverage at default test runs.
3. Remove the two redundant test comments
File: packages/react/src/radio-group/RadioGroup.test.tsx:1062, 1093
Source: comment-analyzer
// Native form submission excludes disabled inputs.
expect(new FormData(form).get('group')).toBe(null);The test name already says "to match native form data" and the FormData(...) call itself self-documents. Per AGENTS.md ("default to writing no comments"), remove these.
Suggestions
4. Clarify the ternary in getFormValue with explicit guards
File: packages/react/src/radio-group/RadioGroup.tsx:152
return input?.checked && !input.disabled ? (checkedValue ?? null) : null;Four distinct outcomes are collapsed into one ternary. An early-return form (if (!input || input.disabled || !input.checked) return null;) survives future edits (e.g., adding the dev warning from #1) without rewriting.
5. Add a "disabled → re-enabled" transition test
Test 1 toggles disabled=true once; nothing covers the reverse transition. A future refactor of registerInputRef swap logic could silently keep returning null after re-enable.
6. Add a test for group-level <RadioGroup disabled> form submission
RadioGroup.tsx:155 passes !disabled as the enabled flag, so a group-level disabled never registers the field — yielding {} instead of { test: null }. This distinct path is uncovered.
7. Note Radio.Indicator.State widening in the changelog
Source: type-design-analyzer
Public type extends from 2 fields to 10. Render-prop consumers and Pick/Omit users are safe; consumers doing exact structural construction (const s: RadioIndicatorState = {checked:true, transitionStatus:'idle'}) get TS2741. Trivial migration, but worth a one-line release note.
8. Pre-existing staleness in groupInputRef.current (follow-up)
RadioRoot.tsx:108-123 never nulls groupInputRef.current on unmount. The new getFormValue actively reads from this potentially-detached node where the prior code didn't. Out of scope for this PR — flag for follow-up.
Strengths
- Correct fix. Cross-verified by code-reviewer:
groupInputRef.currentis fresh throughuseStableCallback+ ref read; on disable-toggle, React commitsdisabledto the DOM before the effect re-registers, soinput.disabledistruewhengetFormValueruns. ✅ - Brings Radio into alignment with Checkbox/Switch — both already use
type RootContext = RootStateandIndicatorState extends RootState. Radio was the outlier; PR fixes the inconsistency. - Type widening matches what the provider already passes —
RadioRoot.tsx:243already setcontextValue = state(full memoized state), so this is type-as-truth cleanup, not a runtime broadening. useStableCallback+ ref-read pattern is idiomatic and AGENTS.md-compliant. NouseTimeout/useIsoLayoutEffect/shadow-DOM regressions.- The new source comment in
RadioGroup.tsx:149-150earns its place — explains a non-obvious native-parity decision (kept per comment-analyzer). - Existing
data-*attribute test atRadioGroup.test.tsx:172-198indirectly verifies the newRadioIndicatorStatefields propagate correctly throughstateAttributesMapping. No additional indicator tests needed.
Recommended Action
- Decide on #1: add a dev warning for the controlled/DOM desync case, or document the new "missing value silently becomes null" behavior. This is the only finding where reviewers materially disagreed.
- Drop
it.skipIf(isJSDOM)on the two new tests (#2). Quick win. - Remove the two redundant test comments (#3). Quick win.
- (Optional) Apply #4 (clarify ternary) and add the missing test cases (#5, #6).
- (Optional) Add a changelog note for the type widening (#7).
- (Follow-up) File an issue for the
groupInputRefunmount staleness (#8).
The PR is ship-ready as a bug fix; items 1-3 would strengthen it before merge.
68f45f5 to
a451ecf
Compare
a451ecf to
b4a30d7
Compare
Radio groups now match native form submission when the selected radio is disabled, and the indicator state type reflects the root state it already receives.
Changes
nullfrom form values when the tracked selected radio is disabled.Radio.Indicator.Statedocs and types.Original findings addressed
FormDatabut still reported byonFormSubmit.RadioIndicatorStateonly declaredcheckedeven though it receives root field state at runtime.