Skip to content

[switch][checkbox] Prevent input state changes in readOnly mode - #4551

Merged
atomiks merged 4 commits into
mui:masterfrom
tsbehlman:readonly-in-label
Apr 8, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
tsbehlman:readonly-in-label

Conversation

@tsbehlman

Copy link
Copy Markdown
Contributor

Switch and Checkbox are currently able to be toggled by clicking their paired label element even in readOnly mode, whether the label encloses the control or is arranged as a sibling element:

read-only.control.labels.mp4

It's possible to work around this issue using a nativeButton configuration, which causes the label to target the button element instead of the hidden <input type="checkbox" />. This PR aims to fix the default configuration by marking the hidden input as disabled in read-only mode. I chose this over the built-in readonly attribute as readonly does not prevent a checkbox's checked state from changing.

@pkg-pr-new

pkg-pr-new Bot commented Apr 7, 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@4551
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4551
    

commit: 2f6fb10

@mui-bot

mui-bot commented Apr 7, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+66B(+0.01%) 🔺+21B(+0.01%)

Details of bundle changes


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

@netlify

netlify Bot commented Apr 7, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 2f6fb10
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69d5f51e0d69f70008e49122
😎 Deploy Preview https://deploy-preview-4551--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.

{
checked,
disabled,
disabled: disabled || readOnly,

@mj12albert mj12albert Apr 7, 2026 •

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.

This will cause readOnly fields to be incorrectly excluded from form submission, I think a proper fix could be to preventDefault in the click handler

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.

Excluding the field the field from the form submission is an issue, thanks for pointing that out.

Unfortunately the control element's click handler isn't called when the label is clicked. It looks like calling preventDefault in the input's change handler works, I can see that it prevents the input's checked state from changing.

@mj12albert mj12albert added the type: bug It doesn't behave as expected. label Apr 7, 2026

@atomiks atomiks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the contribution @tsbehlman

@atomiks atomiks added component: switch Changes related to the switch component. component: checkbox Changes related to the checkbox component. labels Apr 8, 2026
@atomiks
atomiks merged commit 5b9fd60 into mui:master Apr 8, 2026
9 checks passed
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. component: switch Changes related to the switch component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants