import React, { ChangeEvent } from "react"; import CheckboxIndeterminateIcon from "PUBLIC_DIR/images/checkbox.indeterminate.react.svg"; import CheckboxCheckedIcon from "PUBLIC_DIR/images/checkbox.checked.react.svg"; import CheckboxIcon from "PUBLIC_DIR/images/checkbox.react.svg"; import { Text } from "../text"; import { StyledLabel, HiddenInput } from "./Checkbox.styled"; import { CheckboxProps } from "./Checkbox.types"; const RenderCheckboxIcon = ({ isChecked, isIndeterminate, tabIndex, }: { isChecked: boolean; isIndeterminate: boolean; tabIndex: number; }) => { return isIndeterminate ? ( ) : isChecked ? ( ) : ( ); }; const CheckboxPure = ({ id, className, style, label, value, title, truncate, hasError, onChange, isChecked, isIndeterminate, isDisabled, name, tabIndex, helpButton, ...rest }: CheckboxProps) => { const [checked, setChecked] = React.useState(isChecked); const ref = React.useRef(null); const prevProps = React.useRef({ indeterminate: false, prevChecked: isChecked, }); React.useEffect(() => { if (prevProps.current.indeterminate !== isIndeterminate && ref.current) { prevProps.current.indeterminate = isIndeterminate || false; ref.current.indeterminate = isIndeterminate || false; } if (prevProps.current.prevChecked !== isChecked) { setChecked(isChecked); prevProps.current.prevChecked = isChecked; } }, [isIndeterminate, isChecked, checked]); const onInputChange = (e: ChangeEvent) => { if (isDisabled) e.preventDefault(); e.stopPropagation(); setChecked(e.target.checked); onChange?.(e); }; const onClick = (e: React.MouseEvent) => { return e.preventDefault(); }; return (
{label && ( {label} )} {helpButton && ( {helpButton} )}
); }; CheckboxPure.defaultProps = { isChecked: false, truncate: false, tabIndex: -1, hasError: false, }; CheckboxPure.displayName = "CheckboxPure"; const Checkbox = React.memo(CheckboxPure); export { Checkbox, CheckboxPure };