'use client'; import { FC, forwardRef, useCallback, useState } from 'react'; import clsx from 'clsx'; import { useFormContext, useWatch } from 'react-hook-form'; export const Checkbox = forwardRef< null, { checked?: boolean; disableForm?: boolean; name?: string; className?: string; label?: string; onChange?: (event: { target: { name?: string; value: boolean; }; }) => void; variant?: 'default' | 'hollow'; } >((props, ref: any) => { const { checked, className, label, disableForm, variant } = props; const form = useFormContext(); const register = disableForm ? {} : form.register(props.name!); const watch = disableForm ? false : form.watch(props.name!); const val = watch || checked; const changeStatus = useCallback(() => { props?.onChange?.({ target: { name: props.name!, value: !val, }, }); if (!disableForm) { // @ts-ignore register?.onChange?.({ target: { name: props.name!, value: !val, }, }); } }, [val]); return (
{val && (
)}
{!!label &&
{label}
}
); });