mirror of
https://github.com/ershisan99/flashcards-example-project.git
synced 2025-12-16 12:33:18 +00:00
37 lines
781 B
TypeScript
37 lines
781 B
TypeScript
import { FieldValues, UseControllerProps, useController } from 'react-hook-form'
|
|
|
|
import { Checkbox, CheckboxProps } from '../../'
|
|
|
|
export type ControlledCheckboxProps<TFieldValues extends FieldValues> =
|
|
UseControllerProps<TFieldValues> & Omit<CheckboxProps, 'id' | 'onChange' | 'value'>
|
|
|
|
export const ControlledCheckbox = <TFieldValues extends FieldValues>({
|
|
control,
|
|
defaultValue,
|
|
name,
|
|
rules,
|
|
shouldUnregister,
|
|
...checkboxProps
|
|
}: ControlledCheckboxProps<TFieldValues>) => {
|
|
const {
|
|
field: { onChange, value },
|
|
} = useController({
|
|
control,
|
|
defaultValue,
|
|
name,
|
|
rules,
|
|
shouldUnregister,
|
|
})
|
|
|
|
return (
|
|
<Checkbox
|
|
{...{
|
|
checked: value,
|
|
id: name,
|
|
onChange,
|
|
...checkboxProps,
|
|
}}
|
|
/>
|
|
)
|
|
}
|