Repository navigation
[switch][checkbox] Prevent input state changes in readOnly mode - #4551
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| { | ||
| checked, | ||
| disabled, | ||
| disabled: disabled || readOnly, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
atomiks
left a comment
There was a problem hiding this comment.
Thanks for the contribution @tsbehlman
SwitchandCheckboxare currently able to be toggled by clicking their pairedlabelelement even inreadOnlymode, 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
nativeButtonconfiguration, which causes thelabelto target thebuttonelement instead of the hidden<input type="checkbox" />. This PR aims to fix the default configuration by marking the hidden input asdisabledin read-only mode. I chose this over the built-inreadonlyattribute asreadonlydoes not prevent a checkbox'scheckedstate from changing.