fix(Radio): support uncontrolled mode by using defaultChecked when no value prop#303
Open
GziXnine wants to merge 2 commits intochicagopcdc:devfrom
Open
fix(Radio): support uncontrolled mode by using defaultChecked when no value prop#303GziXnine wants to merge 2 commits intochicagopcdc:devfrom
GziXnine wants to merge 2 commits intochicagopcdc:devfrom
Conversation
… value prop
The Radio component always set checked={value !== undefined && option.value === value},
which forced fully-controlled behavior even when no value prop was provided. In
uncontrolled mode (no value prop), all radio buttons were permanently unchecked
because the checked attribute always evaluated to false, overriding native HTML
radio selection behavior.
Use conditional props: when a value prop is provided, use checked for controlled
mode; when no value prop is provided, use defaultChecked to allow native browser
radio selection to work correctly.
This fixes the failing 'updates on click (uncontrolled)' test.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The Radio component always set
checked={value !== undefined && option.value === value}, which forced fully-controlled behavior even when novalueprop was provided.Problem
In uncontrolled mode (no
valueprop), all radio buttons were permanently unchecked because thecheckedattribute always evaluated tofalse, overriding native HTML radio selection behavior. Clicking a radio button had no visible effect.Fix
Use conditional props:
valueprop is provided: usecheckedfor controlled modevalueprop is provided: usedefaultCheckedto allow native browser radio selectionFixes #276
Testing
'updates on click (uncontrolled)'test inRadio.test.tsx