Files
flashcards-example-project/src/components/ui/controlled/controlled-checkbox/controlled-checkbox.tsx
2023-07-29 20:06:25 +02:00

37 lines
722 B
TypeScript

import { FieldValues, useController, UseControllerProps } from 'react-hook-form'
import { Checkbox, CheckboxProps } from '@/components'
type Props<T extends FieldValues> = UseControllerProps<T> &
Omit<CheckboxProps, 'checked' | 'onValueChange' | 'id'>
export const ControlledCheckbox = <T extends FieldValues>({
name,
rules,
shouldUnregister,
control,
defaultValue,
...checkboxProps
}: Props<T>) => {
const {
field: { onChange, value },
} = useController({
name,
rules,
shouldUnregister,
control,
defaultValue,
})
return (
<Checkbox
{...{
onValueChange: onChange,
checked: value,
id: name,
...checkboxProps,
}}
/>
)
}