You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: treat explicit checked={undefined} as uncontrolled - #318
<Checkbox checked={undefined} /> is the usual React way to say "uncontrolled" (for example checked={isControlled ? value : undefined}). The value state already behaves that way: useControlledState(defaultChecked, checked) runs uncontrolled when checked is undefined. But the change handler decided with 'checked' in props, which is true for an explicit checked={undefined}, so setRawValue was skipped and clicking the checkbox did nothing: it stayed in its defaultChecked state forever (the native <input type="checkbox" checked={undefined}> toggles).
The handler now checks checked === undefined, the same condition useControlledState uses for uncontrolled mode. Nothing changes when checked is a boolean, or when the prop is not passed at all.
Test: works in uncontrolled mode when checked prop is explicitly undefined in tests/index.test.tsx fails on master (Expected: true, Received: false) and passes now. npm test (1 suite, 25 tests), npm run tsc, npm run lint and prettier on the changed test file pass.
Related: #221 (2022, still open) also deals with checked={undefined} together with defaultChecked, but changes the old src/index.jsx and the demo; this PR is against the current src/index.tsx.
This fixes a checkbox that did not toggle when checked was explicitly undefined alongside defaultChecked. Controlled and omitted-checked behavior is unchanged, and a test covers the fix. There is no remaining merge risk.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: 3e9fcf28-bd4a-47bf-897e-0c8680a7dcfb
Commits
Reviewing files that changed from the base of the PR and between 02ce122 and c65afbc.
Files selected for processing: 2
src/index.tsx
tests/index.test.tsx
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
This branch has not been deployed
No deployments
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.
<Checkbox checked={undefined} />is the usual React way to say "uncontrolled" (for examplechecked={isControlled ? value : undefined}). The value state already behaves that way:useControlledState(defaultChecked, checked)runs uncontrolled whencheckedisundefined. But the change handler decided with'checked' in props, which istruefor an explicitchecked={undefined}, sosetRawValuewas skipped and clicking the checkbox did nothing: it stayed in itsdefaultCheckedstate forever (the native<input type="checkbox" checked={undefined}>toggles).The handler now checks
checked === undefined, the same conditionuseControlledStateuses for uncontrolled mode. Nothing changes whencheckedis a boolean, or when the prop is not passed at all.Test:
works in uncontrolled mode when checked prop is explicitly undefinedintests/index.test.tsxfails onmaster(Expected: true,Received: false) and passes now.npm test(1 suite, 25 tests),npm run tsc,npm run lintand prettier on the changed test file pass.Related: #221 (2022, still open) also deals with
checked={undefined}together withdefaultChecked, but changes the oldsrc/index.jsxand the demo; this PR is against the currentsrc/index.tsx.Summary by CodeRabbit
checked={undefined}时复选框无法正常切换的问题;点击可选中,再次点击可取消选中。