Skip to content

[checkbox group] Fix uncontrolled default initialization - #4535

Merged
atomiks merged 5 commits into
mui:masterfrom
atomiks:codex/default-init-fixes
Apr 7, 2026
Merged

atomiks merged 5 commits into
mui:masterfrom
atomiks:codex/default-init-fixes

Conversation

@atomiks

@atomiks atomiks commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

An uncontrolled CheckboxGroup without value or defaultValue should behave like an empty selected list, but it could fall back to per-checkbox local state instead of maintaining a shared group value. This initializes the uncontrolled group value to [] so the checkbox group state behaves consistently from the first render.

Changes

  • Initialize uncontrolled CheckboxGroup state to an empty array when no defaultValue is provided.
  • Preserve the existing checkbox-group update flow while making the uncontrolled state concrete.
  • Add a regression test covering the omitted-defaultValue checkbox-group case.
  • Tighten the checkbox-group state typing so type inference matches the runtime fix.

@atomiks atomiks added type: bug It doesn't behave as expected. component: checkbox Changes related to the checkbox component. component: collapsible Changes related to the collapsible component. labels Apr 6, 2026 — with ChatGPT Codex Connector
@atomiks atomiks removed the component: collapsible Changes related to the collapsible component. label Apr 6, 2026
@mui-bot

mui-bot commented Apr 6, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+58B(+0.01%) 🔺+30B(+0.02%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@pkg-pr-new

pkg-pr-new Bot commented Apr 6, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4535
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4535
    

commit: 4702675

@netlify

netlify Bot commented Apr 6, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 4702675
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69d4fd37fe95db00082054d5
😎 Deploy Preview https://deploy-preview-4535--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks marked this pull request as ready for review April 6, 2026 09:20
const disabled = fieldDisabled || disabledProp;

const [value, setValueUnwrapped] = useControlled({
const defaultValue = React.useMemo<string[] | undefined>(() => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that work?

Suggested change
const defaultValue = React.useMemo<string[] | undefined>(() => {
const defaultValue = externalValue === undefined
? (defaultValueProp ?? EMPTY_ARRAY)
: undefined

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used as a dependency, so it has to be wrapped in memo (EMPTY_ARRAY is not really safe to use here)

@atomiks
atomiks force-pushed the codex/default-init-fixes branch from 1791569 to 70c2aa8 Compare April 7, 2026 07:06

atomiks commented Apr 7, 2026

Copy link
Copy Markdown
Contributor Author

Codex Review (GPT-5.4)

I reviewed the full branch diff against a freshly fetched upstream/master. This is a bug-fix PR with secondary tests and public-api implications, and the current branch looks consistent with the reported checkbox-group failure mode.

1. Bugs / Issues (None)

Root Cause & Patch Assessment

The main fix in packages/react/src/checkbox-group/CheckboxGroup.tsx addresses the actual root cause rather than only the symptom: uncontrolled CheckboxGroup no longer starts from runtime undefined when defaultValue is omitted, so the group path now initializes as an empty array and child checkboxes stay attached to shared group state.

The follow-up change in packages/react/src/checkbox/root/CheckboxRoot.tsx also resolves the regression risk introduced by routing parent-group updates through the resolved group state. With !isGroupedWithParent added to the generic setGroupValue(...) branch, parent-enabled groups now have a single update owner instead of two competing mutation paths.

Pattern Consistency Assessment

The final shape is consistent with the rest of the controlled/uncontrolled patterns in this codebase: initialize the uncontrolled source once in CheckboxGroup, then let children derive their checked state from group context instead of falling back to per-checkbox local state.

I also checked the nearby parent-checkbox flow in packages/react/src/checkbox-group/useCheckboxGroupParent.ts. The PR does not widen that hook’s contract; it just ensures the hook now receives the resolved group value in uncontrolled mode, which is the missing piece for the existing logic to work as intended.

Test Coverage Assessment

Coverage is proportionate to the risk. The new regression in packages/react/src/checkbox-group/CheckboxGroup.test.tsx covers the originally broken plain uncontrolled case with omitted defaultValue, and the added case in packages/react/src/checkbox-group/useCheckboxGroupParent.test.tsx covers the adjacent uncontrolled allValues / parent-checkbox path that the first revision had put at risk.

I also ran pnpm typescript and pnpm test:jsdom CheckboxGroup --no-watch, both of which passed on the current branch.

Recommendation

Approve ✅

I do not see any actionable issues remaining in the current branch. The patch now fixes the uncontrolled initialization bug at the correct layer, preserves the parent-checkbox ownership model, and includes targeted regression coverage for both affected paths.

@atomiks
atomiks merged commit 0cf10b8 into mui:master Apr 7, 2026
7 checks passed
@atomiks
atomiks deleted the codex/default-init-fixes branch April 7, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: checkbox Changes related to the checkbox component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants