redux-form: checkbox doesn't set `checked`, changes value between true/false
I’m a little confused by how checkboxes and radio buttons work in the example. On the simple example: https://erikras.github.io/redux-form/#/examples/simple
Checking Employed
checkbox just sets the input’s value="true"
. Unchecking sets it to value="false"
. I believe it should leave value
alone and instead add the checked
property to the input. On the radio buttons, checked
is never set.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 7
- Comments: 28 (3 by maintainers)
sex value is always true
The
checked
is being set in the code.You might have a good argument against setting
value
for a checkbox, butredux-form
can’t know what kind of input it is until it receives an event. However, is there really a use case for needing a non-booleanvalue
on a checkbox?This is how I handled this with
material-ui
checkbox element:For semantic-ui-react, what I have done is
The
values
change totrue
orfalse
state.Thank you @erikras 😃 I’d like to bring attention on this line:
Because before, I used:
It was working well in development mode, but not in production mode (minimified code, no webpack server, with webpack dll plugin). Still don’t know why, But using
props.input.checked
andOnChange
binding made it work… I think more investigation is needed here…Also +1 for the answer from @dbismut. In case anyone was interested, I wrapped the Material UI checkbox by my own component:
You can use this component the same way like other inputs:
I found those two issues #2922 #1993 and added the attribute
normalize={value => !!value}
to myField
tag and it seems to solve it.I agree that checking boxes should flip the checked property, not the value. In normal HTML posts, having a value of true or false has no bearing on if the form is submitted. Its the checked property that determines this.
Also groups of checkboxes are pretty common. Instead of each checkbox having its own name, they should be able to share a name. (group:option1, group:option2 over option1:true, option2:false). This can already be done with radio groups. But since the value is overwritten with true/false, this is impossible.
Finally, using true/false to determine checked/unchecked makes using the intermediate property awkward.
Keep in mind that you have complete control over how the values are passed from the redux-form field to your input component. i.e. you don’t have to do
{...myField}
, but you can pass each attribute separately, as long as you passvalue
andonChange
, that’s all that redux-form cares about. For example, say you had a bizarre checkbox component that insisted on using the string values'yes'
and'no'
, but you wanted a boolean in Redux, you could doHello everybody. Thank you @tobice for your comment! I’ve manage to successfully make it work with v6 API. For people interested:
And use it in your form like that:
Here’s is what I did with Semantic UI checkbox.
Maybe MaterialCheckbox component has the function of onCheck to change the value, but other’s DIY Checkbox does’t work.
for example, like semantic-ui-react
the checked’s value should change