import { forwardRef, ReactElement, ReactNode, useEffect, useImperativeHandle, useRef } from 'react'; import classnames from 'classnames'; import styles from './InternalCheckbox.css'; import Box from '../Box'; import IconCompact from '../IconCompact'; import Label from '../Label'; import FormErrorMessage from '../sharedSubcomponents/FormErrorMessage'; import FormHelperText from '../sharedSubcomponents/FormHelperText'; import Text from '../Text'; import useFocusVisible from '../useFocusVisible'; import useInteractiveStates from '../utils/useInteractiveStates'; import useTapScaleAnimation from '../utils/useTapScaleAnimation'; type Props = { checked: boolean; disabled: boolean; errorMessage?: string; helperText?: string; id: string; image?: ReactNode; indeterminate: boolean; label?: string; labelDisplay: 'visible' | 'hidden'; name?: string; onChange?: (arg1: { event: React.ChangeEvent; checked: boolean }) => void; onClick?: (arg1: { event: React.ChangeEvent; checked: boolean }) => void; readOnly?: boolean; ref?: ReactElement; // eslint-disable-line react/no-unused-prop-types, size: 'sm' | 'md'; style?: { borderColor?: string; backgroundColor?: string; }; }; const InternalCheckboxWithForwardRef = forwardRef(function Checkbox( { checked, disabled, errorMessage, helperText, id, image, indeterminate, label, labelDisplay, name, onChange, onClick, readOnly, size, style, }: Props, ref, ) { const innerRef = useRef(null); // When using both forwardRef and innerRef, React.useimperativehandle() allows a parent component // that renders to call inputRef.current.focus() // @ts-expect-error - TS2322 - Type 'HTMLInputElement | null' is not assignable to type 'HTMLInputElement'. useImperativeHandle(ref, () => innerRef.current); useEffect(() => { if (innerRef && innerRef.current) { innerRef.current.indeterminate = indeterminate; } }, [indeterminate]); const handleChange = (event: React.ChangeEvent) => { onChange?.({ event, checked: event.target.checked }); }; const handleClick = (event: React.ChangeEvent) => { onClick?.({ event, checked: event.currentTarget.checked }); }; const { handleOnMouseEnter, handleOnMouseLeave, handleOnBlur, handleOnFocus, handleOnMouseDown, handleOnMouseUp, isFocused, isHovered, isActive, } = useInteractiveStates(); const { isFocusVisible } = useFocusVisible(); const tapScaleAnimation = useTapScaleAnimation(); let ariaDescribedby; if (errorMessage) { ariaDescribedby = `${id}-error`; } if (label && helperText) { ariaDescribedby = `${id}-helperText`; } const iconSizes = { sm: 10, md: 12, }; const unchecked = !(checked || indeterminate); return (
) => void' is not assignable to type 'MouseEventHandler'. onClick={handleClick} onFocus={handleOnFocus} onMouseDown={() => { handleOnMouseDown(); tapScaleAnimation.handleMouseDown(); }} onMouseEnter={handleOnMouseEnter} onMouseLeave={handleOnMouseLeave} onMouseUp={() => { handleOnMouseUp(); tapScaleAnimation.handleMouseUp(); }} type="checkbox" />
{Boolean(image) &&
{image}
} {label && (
{helperText ? ( ) : null} {errorMessage ? ( ) : null}
)}
); }); InternalCheckboxWithForwardRef.displayName = 'InternalCheckbox'; export default InternalCheckboxWithForwardRef;