Skip to content

fix: treat explicit checked={undefined} as uncontrolled - #318

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/checked-undefined-uncontrolled
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/checked-undefined-uncontrolled

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Dec 1, 2026 •

Copy link
Copy Markdown

<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.

Summary by CodeRabbit

  • Bug 修复
    • 修复了显式设置 checked={undefined} 时复选框无法正常切换的问题;点击可选中,再次点击可取消选中。

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

当 checked 为 undefined 时,Checkbox 现在会更新内部值。新增测试验证显式传入 checked={undefined} 时,点击可选中和取消选中。

Changes

Checkbox 非受控值更新

Layer / File(s) Summary
内部值更新与交互验证
src/index.tsx, tests/index.test.tsx
handleChange 在 checked === undefined 时更新内部值。新增测试验证显式传入 checked={undefined} 后,点击可选中,再次点击可取消选中。

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix

Suggested reviewers: wangxingkang

Merge Risk: Minimal · up to c65af

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: treat explicit checked={undefined} as uncontrolled”描述了 PR 的主要变更:显式传入 checked={undefined} 时,Checkbox 以非受控模式运行。变更文件 src/index.tsx 中的处理逻辑从检查 props 是否包含 checked 改为检查 checked === undefined,测试文件新增了对…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

标题“fix: treat explicit checked={undefined} as uncontrolled”描述了 PR 的主要变更:显式传入 checked={undefined} 时,Checkbox 以非受控模式运行。变更文件 src/index.tsx 中的处理逻辑从检查 props 是否包含 checked 改为检查 checked === undefined,测试文件新增了对应用例。标题与变更内容一致,具体且简洁。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • 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.

Share

兔子轻点复选框,
空值也能把状态改。
点一下,勾选亮起来,
再一点,勾选轻轻离开。
新测试守着这来回,
胡萝卜旁,兔子笑开怀。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant