I think this is bug
If create adapter component for Field with type="checkbox" and set value attribute. When checking the box will set the value to true, and unchecking the checkbox will set value to false.
Checking the box will add the value to the array, and unchecking the checkbox will remove the value from the array.
https://codesandbox.io/s/mmjkqqwy0y
react-final-form 3.4.0
react 16.3.2
I think this feature would essentially work if the FieldProps "value" prop was made to work not only for checkboxes and ratio buttons: https://github.com/final-form/react-final-form#value-any .
To achieve the true false expected behavior here @madspark is correct:
Checkboxes:
valueis specified: the checkbox will be checked if the value given in value is contained in the array that is the value for the field for the form. Checking the box will add the value to the array, and unchecking the checkbox will remove the value from the array.- no
valueis specified: the checkbox will be checked if the value is truthy. Checking the box will set the value to true, and unchecking the checkbox will set the value to false.
This is my workaround for now. I'm passing input.onChange() an object that closely resembles an event.
<Field
name="someArray"
type="checkbox"
value="someValue"
component={({ input }) => (
<div onClick={() => input.onChange({
target: {
type: "checkbox",
checked: !input.checked
}
})}>
{"The value is" + input.checked}
</div>
)}
/>
It would be nice if input.onChange() took a boolean directly. However this solution is still shorter than operating on the array myself, but it feels a bit hacky!
Most helpful comment
This is my workaround for now. I'm passing
input.onChange()an object that closely resembles an event.<Field name="someArray" type="checkbox" value="someValue" component={({ input }) => ( <div onClick={() => input.onChange({ target: { type: "checkbox", checked: !input.checked } })}> {"The value is" + input.checked} </div> )} />It would be nice if
input.onChange()took a boolean directly. However this solution is still shorter than operating on the array myself, but it feels a bit hacky!