import React, { ComponentProps, DetailedHTMLProps, HTMLAttributes, ReactNode, } from "react"; import { twMerge } from "tailwind-merge"; import CheckMarkSVG from "../svgs/CheckMarkSVG"; import Stack from "../layout/Stack"; import Row from "../layout/Row"; import { Info } from "lucide-react"; import Span from "../layout/Span"; export type CheckboxProps = React.DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement > & { wrapperProps?: DetailedHTMLProps< HTMLAttributes, HTMLDivElement >; label?: string | ReactNode; labelProps?: React.DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; defaultChecked?: boolean; wrapperClassName?: string; setChecked?: React.Dispatch>; checked?: boolean; readOnly?: boolean; size?: number; changeHandler?: (value: boolean) => void; info?: string | ReactNode; wrapperWrapperProps?: ComponentProps; }; /** * # Checkbox Component * @className twui-checkbox * @className twui-checkbox-checked * @className twui-checkbox-unchecked */ export default function Checkbox({ wrapperProps, label, labelProps, size, wrapperClassName, defaultChecked, setChecked: externalSetChecked, readOnly, checked: externalChecked, changeHandler, info, wrapperWrapperProps, ...props }: CheckboxProps) { const finalSize = size || 20; const [checked, setChecked] = React.useState( defaultChecked || externalChecked || false ); const finalTitle = props.title ? props.title : `Checkbox-${Math.round(Math.random() * 100000)}`; React.useEffect(() => { if (typeof externalChecked == "undefined") return; setChecked(externalChecked); }, [externalChecked]); React.useEffect(() => { changeHandler?.(checked); }, [checked]); return (
{ setChecked(!checked); externalSetChecked?.(!checked); }} >
{checked && }
{label || finalTitle}
{info && ( {info} )}
); }