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
Fix default value for checkbox group #7912
Fix default value for checkbox group #7912
Conversation
@@ -254,6 +254,9 @@ export function createFormControl< | |||
); | |||
|
|||
isUndefined(defaultValue) || | |||
(isUndefined(defaultValue) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i will try with check undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm i think we need to look a bit further than doing === false check, as undefined check failed.
i think instead of introducing this fix (which i appreciated your time to look into), maybe we can simply apply a |
If you have a look the test in this merge request, we init the form with string[] type for field test:
When submit button is clicked, the value returned from hook form is It returns |
Probably not, but like what I have suggested above, there is a workaround to achieve what is desired behavior. The nature of not knowing what's in the render (without defaultValue) is the trouble here, which presented below with your fix -> first checkbox does not check ok default I would like to avoid this above, as this is troublesome as it depends on the number of the checkbox to determine defaultValue while it's more predictable for the user to simplify supply with a default value as an empty array for the lib to fallback. |
Right, I can see your point. Determine it's checkbox group or not by the default value instead of number of elements. I do have this kind of requirement. There is only one option in the checkbox group. We still want the return value to be string[]. At the moment, my workaround is adding a hidden input. I would like to avoid this workaround. Will you consider to support this? |
could you provide a |
Have a look this one: https://codesandbox.io/s/react-hook-form-js-forked-bz9j1g I will prefer to tell hook form |
I think you want to build a controlled component for your requirement and wrapped with |
|
for the second one https://codesandbox.io/s/react-hook-form-js-forked-1b5njd?file=/src/App.js |
For checkbox group, the default value should be
[]
instead offalse
.https://codesandbox.io/s/react-hook-form-js-forked-6hd9uc