1 | import { useFormContext, Controller } from 'react-hook-form';
|
---|
2 | // @mui
|
---|
3 | import Checkbox from '@mui/material/Checkbox';
|
---|
4 | import FormGroup from '@mui/material/FormGroup';
|
---|
5 | import FormLabel from '@mui/material/FormLabel';
|
---|
6 | import FormControl from '@mui/material/FormControl';
|
---|
7 | import FormHelperText from '@mui/material/FormHelperText';
|
---|
8 | import FormControlLabel, {
|
---|
9 | FormControlLabelProps,
|
---|
10 | formControlLabelClasses,
|
---|
11 | } from '@mui/material/FormControlLabel';
|
---|
12 |
|
---|
13 | // ----------------------------------------------------------------------
|
---|
14 |
|
---|
15 | interface RHFCheckboxProps extends Omit<FormControlLabelProps, 'control'> {
|
---|
16 | name: string;
|
---|
17 | helperText?: React.ReactNode;
|
---|
18 | }
|
---|
19 |
|
---|
20 | export function RHFCheckbox({ name, helperText, ...other }: RHFCheckboxProps) {
|
---|
21 | const { control } = useFormContext();
|
---|
22 |
|
---|
23 | return (
|
---|
24 | <Controller
|
---|
25 | name={name}
|
---|
26 | control={control}
|
---|
27 | render={({ field, fieldState: { error } }) => (
|
---|
28 | <div>
|
---|
29 | <FormControlLabel control={<Checkbox {...field} checked={field.value} />} {...other} />
|
---|
30 |
|
---|
31 | {(!!error || helperText) && (
|
---|
32 | <FormHelperText error={!!error}>{error ? error?.message : helperText}</FormHelperText>
|
---|
33 | )}
|
---|
34 | </div>
|
---|
35 | )}
|
---|
36 | />
|
---|
37 | );
|
---|
38 | }
|
---|
39 |
|
---|
40 | // ----------------------------------------------------------------------
|
---|
41 |
|
---|
42 | interface RHFMultiCheckboxProps extends Omit<FormControlLabelProps, 'control' | 'label'> {
|
---|
43 | name: string;
|
---|
44 | options: { label: string; value: any }[];
|
---|
45 | row?: boolean;
|
---|
46 | label?: string;
|
---|
47 | spacing?: number;
|
---|
48 | helperText?: React.ReactNode;
|
---|
49 | }
|
---|
50 |
|
---|
51 | export function RHFMultiCheckbox({
|
---|
52 | row,
|
---|
53 | name,
|
---|
54 | label,
|
---|
55 | options,
|
---|
56 | spacing,
|
---|
57 | helperText,
|
---|
58 | sx,
|
---|
59 | ...other
|
---|
60 | }: RHFMultiCheckboxProps) {
|
---|
61 | const { control } = useFormContext();
|
---|
62 |
|
---|
63 | const getSelected = (selectedItems: string[], item: string) =>
|
---|
64 | selectedItems.includes(item)
|
---|
65 | ? selectedItems.filter((value) => value !== item)
|
---|
66 | : [...selectedItems, item];
|
---|
67 |
|
---|
68 | return (
|
---|
69 | <Controller
|
---|
70 | name={name}
|
---|
71 | control={control}
|
---|
72 | render={({ field, fieldState: { error } }) => (
|
---|
73 | <FormControl component="fieldset">
|
---|
74 | {label && (
|
---|
75 | <FormLabel component="legend" sx={{ typography: 'body2' }}>
|
---|
76 | {label}
|
---|
77 | </FormLabel>
|
---|
78 | )}
|
---|
79 |
|
---|
80 | <FormGroup
|
---|
81 | sx={{
|
---|
82 | ...(row && {
|
---|
83 | flexDirection: 'row',
|
---|
84 | }),
|
---|
85 | [`& .${formControlLabelClasses.root}`]: {
|
---|
86 | '&:not(:last-of-type)': {
|
---|
87 | mb: spacing || 0,
|
---|
88 | },
|
---|
89 | ...(row && {
|
---|
90 | mr: 0,
|
---|
91 | '&:not(:last-of-type)': {
|
---|
92 | mr: spacing || 2,
|
---|
93 | },
|
---|
94 | }),
|
---|
95 | },
|
---|
96 | ...sx,
|
---|
97 | }}
|
---|
98 | >
|
---|
99 | {options.map((option) => (
|
---|
100 | <FormControlLabel
|
---|
101 | key={option.value}
|
---|
102 | control={
|
---|
103 | <Checkbox
|
---|
104 | checked={field.value.includes(option.value)}
|
---|
105 | onChange={() => field.onChange(getSelected(field.value, option.value))}
|
---|
106 | />
|
---|
107 | }
|
---|
108 | label={option.label}
|
---|
109 | {...other}
|
---|
110 | />
|
---|
111 | ))}
|
---|
112 | </FormGroup>
|
---|
113 |
|
---|
114 | {(!!error || helperText) && (
|
---|
115 | <FormHelperText error={!!error} sx={{ mx: 0 }}>
|
---|
116 | {error ? error?.message : helperText}
|
---|
117 | </FormHelperText>
|
---|
118 | )}
|
---|
119 | </FormControl>
|
---|
120 | )}
|
---|
121 | />
|
---|
122 | );
|
---|
123 | }
|
---|