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;

View file

@ -2,8 +2,6 @@ import React, { useMemo } from 'react';
import styled from 'styled-components';
import v4 from 'uuid/v4';
import classNames from 'classnames';
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
import { Field, FieldProps } from 'formik';
const ToggleContainer = styled.div`
${tw`relative select-none w-12 leading-normal`};
@ -36,49 +34,44 @@ const ToggleContainer = styled.div`
}
`;
interface Props {
export interface SwitchProps {
name: string;
description?: string;
label: string;
enabled?: boolean;
description?: string;
defaultChecked?: boolean;
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
children?: React.ReactNode;
}
const Switch = ({ name, label, description }: Props) => {
const Switch = ({ name, label, description, defaultChecked, onChange, children }: SwitchProps) => {
const uuid = useMemo(() => v4(), []);
return (
<FormikFieldWrapper name={name}>
<div className={'flex items-center'}>
<ToggleContainer className={'mr-4 flex-none'}>
<Field name={name}>
{({ field, form }: FieldProps) => (
<input
id={uuid}
name={name}
type={'checkbox'}
onChange={() => {
form.setFieldTouched(name);
form.setFieldValue(field.name, !field.value);
}}
defaultChecked={field.value}
/>
)}
</Field>
<label htmlFor={uuid}/>
</ToggleContainer>
<div className={'w-full'}>
<label
className={classNames('input-dark-label cursor-pointer', { 'mb-0': !!description })}
htmlFor={uuid}
>{label}</label>
{description &&
<p className={'input-help'}>
{description}
</p>
}
</div>
<div className={'flex items-center'}>
<ToggleContainer className={'mr-4 flex-none'}>
{children
|| <input
id={uuid}
name={name}
type={'checkbox'}
onChange={e => onChange && onChange(e)}
defaultChecked={defaultChecked}
/>
}
<label htmlFor={uuid}/>
</ToggleContainer>
<div className={'w-full'}>
<label
className={classNames('input-dark-label cursor-pointer', { 'mb-0': !!description })}
htmlFor={uuid}
>{label}</label>
{description &&
<p className={'input-help'}>
{description}
</p>
}
</div>
</FormikFieldWrapper>
</div>
);
};