Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/components/elements/Checkbox.tsx
7461 views
1
import React from 'react';
2
import { Field, FieldProps } from 'formik';
3
import Input from '@/components/elements/Input';
4
5
interface Props {
6
name: string;
7
value: string;
8
className?: string;
9
}
10
11
type OmitFields = 'ref' | 'name' | 'value' | 'type' | 'checked' | 'onClick' | 'onChange';
12
13
type InputProps = Omit<JSX.IntrinsicElements['input'], OmitFields>;
14
15
const Checkbox = ({ name, value, className, ...props }: Props & InputProps) => (
16
<Field name={name}>
17
{({ field, form }: FieldProps) => {
18
if (!Array.isArray(field.value)) {
19
console.error('Attempting to mount a checkbox using a field value that is not an array.');
20
21
return null;
22
}
23
24
return (
25
<Input
26
{...field}
27
{...props}
28
className={className}
29
type={'checkbox'}
30
checked={(field.value || []).includes(value)}
31
onClick={() => form.setFieldTouched(field.name, true)}
32
onChange={(e) => {
33
const set = new Set(field.value);
34
set.has(value) ? set.delete(value) : set.add(value);
35
36
field.onChange(e);
37
form.setFieldValue(field.name, Array.from(set));
38
}}
39
/>
40
);
41
}}
42
</Field>
43
);
44
45
export default Checkbox;
46
47