import React, { InputHTMLAttributes, PropsWithChildren, forwardRef, useEffect, useRef, useState, } from "react"; import classNames from "classnames"; import { Field, FieldProps } from ":/components/Forms/Field"; export type CheckboxProps = InputHTMLAttributes & FieldProps & { indeterminate?: boolean; label?: string; }; export const Checkbox = forwardRef( ( { indeterminate, className = "", checked, label, ...props }: CheckboxProps, ref, ) => { const inputRef = useRef(); const [value, setValue] = useState(!!checked); useEffect(() => { setValue(!!checked); }, [checked]); useEffect(() => { if (inputRef.current) { inputRef.current.indeterminate = !!indeterminate; } }, [indeterminate]); return ( { setValue(e.target.checked); props.onChange?.(e); }} checked={value} ref={(checkboxRef) => { if (typeof ref === "function") { ref(checkboxRef); } inputRef.current = checkboxRef || undefined; }} /> {label && {label}} ); }, ); export const CheckboxGroup = ({ children, ...props }: PropsWithChildren & FieldProps) => { return ( {children} ); }; const Checkmark = () => ( ); const Indeterminate = () => ( );