We are using Formik for form entry on react web and mobile apps.
Issue with Formik checkbox was
input-field.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{field.type === 'checkbox' && ( | |
<MyCheckbox | |
checked={ | |
(Array.isArray(value) && value.length > 0) || value === field.value | |
} | |
value={field.value} | |
label={field.label} | |
name={field.name} | |
/> | |
)} |
mycheck.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MyCheckbox = ({ label, labelPlacement, ...props }) => { | |
const [field, meta] = useField({ ...props, type: 'checkbox' }) | |
return ( | |
<> | |
<FormControlLabel | |
control={<Checkbox {...field} {...props} color='primary' />} | |
label={label} | |
labelPlacement={labelPlacement ? labelPlacement : 'end'} | |
/> | |
{meta.touched && meta.error ? <div>{meta.error}</div> : null} | |
</> | |
) | |
} | |
export default MyCheckbox |
product.dialog.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const onSubmit = (values, resetForm) => { | |
setStatus({ type: 'pending' }) | |
const { id, createdAt, __typename, hotdeal, ...rest } = values | |
let hotdealValue = 'no' | |
if (Array.isArray(hotdeal)) { | |
if (hotdeal.length > 0) hotdealValue = 'yes' | |
} else hotdealValue = hotdeal | |
const productInput = { hotdeal: hotdealValue, ...rest } | |
... | |
} |
Comments
Post a Comment