import React from 'react'; import { useController, useFormContext, type FieldValues, type Path } from 'react-hook-form'; import type { FormFieldPropsBase } from './types'; import { Checkbox } from '../../../chakra/checkbox'; import type { CheckboxProps } from '../../../chakra/checkbox'; export interface FormFieldCheckboxProps< FormFields extends FieldValues, Name extends Path = Path, > extends Pick, 'rules' | 'name' | 'onChange' | 'readOnly' | 'controllerProps'>, Omit { label: string; } const FormFieldCheckboxContent = < FormFields extends FieldValues, Name extends Path = Path, >({ name, label, rules, onChange, readOnly, controllerProps, ...rest }: FormFieldCheckboxProps) => { const { control } = useFormContext(); const { field, formState } = useController({ control, name, rules, ...controllerProps, }); const isDisabled = formState.isSubmitting; const handleChange: typeof field.onChange = React.useCallback(({ checked }: { checked: boolean }) => { field.onChange(checked); onChange?.(); }, [ field, onChange ]); return ( { label } ); }; export const FormFieldCheckbox = React.memo(FormFieldCheckboxContent) as typeof FormFieldCheckboxContent;