-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why is the value of a checkbox using useCheckbox
''
instead of 'on'
?
#3519
Comments
Hi, I would like to contribute here. Can I start working on this? |
Thanks for volunteering, go for it! |
@snowystinger I did confirm the issue is caused by the The idea is to return the So, instead of Let me know if this is the right way to go. |
I was kinda thinking it would have to be something like this. |
yeah, I'm not sure if there is a better way of doing it. |
Hey! Thanks for starting to look into it, this is such a strange bug. Undefined should be omitted from the dom. With your observations, I found both that the React Team doesn't intend to fix this, and that MUI also works around this. I think we can do it for all of useToggle, we don't need to special case it to checkboxes. I see this affects our React Spectrum Switch component as well https://react-spectrum.adobe.com/react-spectrum/Switch.html |
@snowystinger Hi, thanks for the references. Passing null in the value attribute logs a warning in the console by default. So, we omit only the undefined value in useToggle in the same way as MUI even if it's passed intentionally? Something like this Let me know if this is the right way, so that I can raise a PR for the changes in useToggle. |
Since I originally reported this, I went ahead and created a PR that fixes it over here. |
Discussed in #3516
Originally posted by brandonpittman September 14, 2022
Ran into this today and was wondering why is the value of a checkbox using
useCheckbox
''
instead of'on'
?I'm not passing it a value, but it's getting assigned to an empty string.
A checkbox's default behavior when it doesn't have an explicit value is to send
'on'
when submitted in a form.From MDN:
Also, this example you shared creates a checkbox with a
value
attribute that has no value.The
useCheckbox
examples in the docs also present with this problem.So the server pulls
''
out of theFormData
. The expected behavior is<input type="checkbox" >
shouldn't have avalue
attribute in the DOM.In the following example, you'll see that
useCheckbox
applies an emptyvalue
attribute. The default checkbox doesn't have that. When this usually occurs is when you applyvalue={null || undefined}
—which I now believeuseToggle
is doing here.If you expect the browser's default behavior and submit a form normally, this is a problem.
The text was updated successfully, but these errors were encountered: