Make switches not reliant on Formik

This commit is contained in:
Dane Everitt 2020-04-25 17:37:03 -07:00
parent a10191a120
commit 67c6be9f6f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 59 additions and 39 deletions

View file

@ -0,0 +1,27 @@
import React from 'react';
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
import { Field, FieldProps } from 'formik';
import Switch, { SwitchProps } from '@/components/elements/Switch';
const FormikSwitch = ({ name, label, ...props }: SwitchProps) => {
return (
<FormikFieldWrapper name={name}>
<Field name={name}>
{({ field, form }: FieldProps) => (
<Switch
name={name}
label={label}
onChange={() => {
form.setFieldTouched(name);
form.setFieldValue(field.name, !field.value);
}}
defaultChecked={field.value}
{...props}
/>
)}
</Field>
</FormikFieldWrapper>
);
};
export default FormikSwitch;