import React from 'react' const CheckboxComponent = ( { label, id, name, onChange, checked, disabled, indeterminate, ...props }: CheckboxProps, ref: React.ForwardedRef ) => { const inputRef = React.useRef(null) const inputId = id || name const [isChecked, setIsChecked] = React.useState(!!checked || false) const [isIndeterminate, setIsIndeterminate] = React.useState( !!indeterminate || false ) React.useEffect(() => { setIsChecked(!!checked) if (checked) setIsIndeterminate(false) }, [checked]) React.useEffect(() => { setIsIndeterminate(!!indeterminate) if (indeterminate) setIsChecked(false) if (inputRef.current) inputRef.current.indeterminate = !!indeterminate }, [indeterminate]) const handleChange = (event: React.ChangeEvent) => { if (disabled) return setIsChecked(event.target.checked) setIsIndeterminate(false) onChange && onChange(event) } const checkboxIconClasses = `transition duration-200 ease-in-out ${ isChecked || isIndeterminate ? 'scale-100' : 'scale-0' }` const checkboxIconContainerClasses = `border border-card-stroke transition duration-200 ease-in-out ${ disabled ? 'bg-base-4' : isChecked || isIndeterminate ? 'bg-primary border-primary' : 'bg-base-1' } rounded w-4 h-4 flex justify-center items-center m-px` const checkboxLabelClasses = `ml-1 input-label text-f6 tracking-4 leading-6 ${ disabled ? 'text-inverted-2' : '' }` return ( ) } const CheckboxWithFowardRef = React.forwardRef(CheckboxComponent) export const Checkbox = React.memo(CheckboxWithFowardRef) export interface CheckboxProps extends React.InputHTMLAttributes { label?: string | React.ReactNode indeterminate?: boolean }