"use client"; import React, { useState, useEffect, forwardRef } from 'react'; import classNames from 'classnames'; import { Flex, Icon, InteractiveDetails, InteractiveDetailsProps } from '.'; import styles from './Checkbox.module.scss'; interface CheckboxProps extends Omit { style?: React.CSSProperties; className?: string; isChecked?: boolean; isIndeterminate?: boolean; onToggle?: () => void; } const generateId = () => `checkbox-${Math.random().toString(36).substring(2, 9)}`; const Checkbox: React.FC = forwardRef(({ style, className, isChecked: controlledIsChecked, isIndeterminate = false, onToggle, ...interactiveDetailsProps }, ref) => { const [isChecked, setIsChecked] = useState(controlledIsChecked || false); const [checkboxId] = useState(generateId()); useEffect(() => { if (controlledIsChecked !== undefined) { setIsChecked(controlledIsChecked); } }, [controlledIsChecked]); const toggleItem = () => { if (onToggle) { onToggle(); } else { setIsChecked(!isChecked); } }; const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); toggleItem(); } }; return ( {(controlledIsChecked !== undefined ? controlledIsChecked : isChecked) && !isIndeterminate && ( )} {isIndeterminate && ( )} ); }); Checkbox.displayName = "Checkbox"; export { Checkbox }; export type { CheckboxProps };