import React, { DetailedHTMLProps, HTMLAttributes, InputHTMLAttributes, ReactNode, } from "react"; import { twMerge } from "tailwind-merge"; import CheckMarkSVG from "../svgs/CheckMarkSVG"; export type CheckboxProps = DetailedHTMLProps< InputHTMLAttributes, HTMLInputElement > & { name: string; wrapperProps?: DetailedHTMLProps< HTMLAttributes, HTMLDivElement >; label?: string | ReactNode; labelProps?: DetailedHTMLProps< HTMLAttributes, HTMLLabelElement >; defaultChecked?: boolean; wrapperClassName?: string; setChecked?: React.Dispatch>; }; /** * # Checkbox Component * @className twui-checkbox * @className twui-checkbox-checked * @className twui-checkbox-unchecked */ export default function Checkbox({ wrapperProps, label, labelProps, size, name, wrapperClassName, defaultChecked, setChecked, ...props }: CheckboxProps) { const finalSize = size || 20; const [internalChecked, setInternalChecked] = React.useState( defaultChecked || false ); const checkMarkRef = React.useRef(); return (
{ checkMarkRef.current?.click(); wrapperProps?.onClick?.(e); }} className={twMerge( "flex items-center gap-2", wrapperClassName, wrapperProps?.className )} > { setInternalChecked(e.target.checked); setChecked?.(e.target.checked); }} ref={checkMarkRef as any} />
{internalChecked && }
{label && }
); }