-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathFormCheckboxListField.tsx
More file actions
55 lines (51 loc) · 1.36 KB
/
Copy pathFormCheckboxListField.tsx
File metadata and controls
55 lines (51 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {
Checkbox,
FormControl,
FormControlLabel,
FormGroup,
FormHelperText,
FormLabel
} from '@mui/material';
import { type ReactNode } from 'react';
import { useFormContext, type FieldValues, type Path } from 'react-hook-form';
type FormCheckboxListFieldProps<Values extends FieldValues> = {
name: Path<Values>;
label: ReactNode;
helperText?: string;
options: { key: string; label: ReactNode; value: string }[];
labelPlacement: 'bottom' | 'end' | 'start' | 'top';
fullWidth?: boolean;
required?: boolean;
};
const FormCheckboxListField = <Values extends FieldValues>({
name,
label,
fullWidth,
helperText,
labelPlacement = 'end',
options = []
}: FormCheckboxListFieldProps<Values>) => {
const { register } = useFormContext<Values>();
const field = register(name);
return (
<FormControl fullWidth={fullWidth}>
<FormLabel component="legend" required={field.required}>
{label}
</FormLabel>
<FormGroup>
{options.map(option => (
<FormControlLabel
{...field}
key={option.key}
control={<Checkbox />}
label={option.label}
labelPlacement={labelPlacement}
value={option.value}
/>
))}
</FormGroup>
<FormHelperText>{helperText}</FormHelperText>
</FormControl>
);
};
export default FormCheckboxListField;