Skip to content

Commit 67c6be9

Browse files
committed
Make switches not reliant on Formik
1 parent a10191a commit 67c6be9

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
3+
import { Field, FieldProps } from 'formik';
4+
import Switch, { SwitchProps } from '@/components/elements/Switch';
5+
6+
const FormikSwitch = ({ name, label, ...props }: SwitchProps) => {
7+
return (
8+
<FormikFieldWrapper name={name}>
9+
<Field name={name}>
10+
{({ field, form }: FieldProps) => (
11+
<Switch
12+
name={name}
13+
label={label}
14+
onChange={() => {
15+
form.setFieldTouched(name);
16+
form.setFieldValue(field.name, !field.value);
17+
}}
18+
defaultChecked={field.value}
19+
{...props}
20+
/>
21+
)}
22+
</Field>
23+
</FormikFieldWrapper>
24+
);
25+
};
26+
27+
export default FormikSwitch;

resources/scripts/components/elements/Switch.tsx

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import React, { useMemo } from 'react';
22
import styled from 'styled-components';
33
import v4 from 'uuid/v4';
44
import classNames from 'classnames';
5-
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
6-
import { Field, FieldProps } from 'formik';
75

86
const ToggleContainer = styled.div`
97
${tw`relative select-none w-12 leading-normal`};
@@ -36,49 +34,44 @@ const ToggleContainer = styled.div`
3634
}
3735
`;
3836

39-
interface Props {
37+
export interface SwitchProps {
4038
name: string;
41-
description?: string;
4239
label: string;
43-
enabled?: boolean;
40+
description?: string;
41+
defaultChecked?: boolean;
42+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
43+
children?: React.ReactNode;
4444
}
4545

46-
const Switch = ({ name, label, description }: Props) => {
46+
const Switch = ({ name, label, description, defaultChecked, onChange, children }: SwitchProps) => {
4747
const uuid = useMemo(() => v4(), []);
4848

4949
return (
50-
<FormikFieldWrapper name={name}>
51-
<div className={'flex items-center'}>
52-
<ToggleContainer className={'mr-4 flex-none'}>
53-
<Field name={name}>
54-
{({ field, form }: FieldProps) => (
55-
<input
56-
id={uuid}
57-
name={name}
58-
type={'checkbox'}
59-
onChange={() => {
60-
form.setFieldTouched(name);
61-
form.setFieldValue(field.name, !field.value);
62-
}}
63-
defaultChecked={field.value}
64-
/>
65-
)}
66-
</Field>
67-
<label htmlFor={uuid}/>
68-
</ToggleContainer>
69-
<div className={'w-full'}>
70-
<label
71-
className={classNames('input-dark-label cursor-pointer', { 'mb-0': !!description })}
72-
htmlFor={uuid}
73-
>{label}</label>
74-
{description &&
75-
<p className={'input-help'}>
76-
{description}
77-
</p>
78-
}
79-
</div>
50+
<div className={'flex items-center'}>
51+
<ToggleContainer className={'mr-4 flex-none'}>
52+
{children
53+
|| <input
54+
id={uuid}
55+
name={name}
56+
type={'checkbox'}
57+
onChange={e => onChange && onChange(e)}
58+
defaultChecked={defaultChecked}
59+
/>
60+
}
61+
<label htmlFor={uuid}/>
62+
</ToggleContainer>
63+
<div className={'w-full'}>
64+
<label
65+
className={classNames('input-dark-label cursor-pointer', { 'mb-0': !!description })}
66+
htmlFor={uuid}
67+
>{label}</label>
68+
{description &&
69+
<p className={'input-help'}>
70+
{description}
71+
</p>
72+
}
8073
</div>
81-
</FormikFieldWrapper>
74+
</div>
8275
);
8376
};
8477

resources/scripts/components/server/schedules/EditScheduleModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Schedule } from '@/api/server/schedules/getServerSchedules';
33
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
44
import Field from '@/components/elements/Field';
55
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
6-
import Switch from '@/components/elements/Switch';
6+
import FormikSwitch from '@/components/elements/FormikSwitch';
77
import createOrUpdateSchedule from '@/api/server/schedules/createOrUpdateSchedule';
88
import { ServerContext } from '@/state/server';
99
import { httpErrorToHuman } from '@/api/http';
@@ -56,7 +56,7 @@ const EditScheduleModal = ({ schedule, ...props }: Omit<Props, 'onScheduleUpdate
5656
running. Use the fields above to specify when these tasks should begin running.
5757
</p>
5858
<div className={'mt-6 bg-neutral-700 border border-neutral-800 shadow-inner p-4 rounded'}>
59-
<Switch
59+
<FormikSwitch
6060
name={'enabled'}
6161
description={'If disabled, this schedule and it\'s associated tasks will not run.'}
6262
label={'Enabled'}

0 commit comments

Comments
 (0)