[WIP] Improvement

This commit is contained in:
2022-08-29 12:00:49 +07:00
parent 10f99bfb9a
commit b942a7e468
30 changed files with 1395 additions and 328 deletions

View File

@@ -67,3 +67,49 @@ export function RHFMultiCheckbox({ name, options, ...other }: RHFMultiCheckboxPr
/>
);
}
// ----------------------------------------------------------------------
interface optionsCustomInterface {
value: string;
label: string;
}
interface RHFCustomMultiCheckboxProps {
name: string;
options: optionsCustomInterface[];
}
export function RHFCustomMultiCheckbox({ name, options, ...other }: RHFCustomMultiCheckboxProps) {
const { control } = useFormContext();
return (
<Controller
name={name}
control={control}
render={({ field }) => {
const onSelected = (option: optionsCustomInterface) =>
field.value.includes(option.value)
? field.value.filter((value: string) => value !== option.value)
: [...field.value, option.value];
return (
<FormGroup>
{options.map((option, index) => (
<FormControlLabel
key={index}
control={
<Checkbox
checked={field.value.includes(option.value)}
onChange={() => field.onChange(onSelected(option))}
/>
}
label={option.label}
{...other}
/>
))}
</FormGroup>
);
}}
/>
);
}