import React, { useEffect, useState } from 'react'; import classnames from 'classnames'; import IReactComponentProps from '../../../common/structures/IReactComponentProps'; import styles from './IconCheckbox.scss'; interface ISvgProps { className?: string; } interface IProps extends IReactComponentProps { checked?: boolean; onChange?: (checked: boolean) => void; Icon: React.FC; disabled?: boolean; name?: string; label?: string | undefined; /** Whether to override svg icon path colors * */ useIconColorsOnChecked?: boolean; useIconColorsOnCheckedHover?: boolean; useIconColorsOnUnchecked?: boolean; useIconColorsOnUncheckedHover?: boolean; } export const IconCheckbox: React.FC = ({ checked, disabled, Icon, name, label, onChange, className, id, style, useIconColorsOnChecked, useIconColorsOnCheckedHover, useIconColorsOnUnchecked, useIconColorsOnUncheckedHover, ...restProps }) => { const [isChecked, setIsChecked] = useState(checked); // state to track temporarily disabling hover to make the checked/unchecked effect play nicer with hover const [disableHoverStyles, setDisableHoverStyles] = useState(checked); // to allow updates to the state via prop changes, an effect much be used useEffect(() => { setIsChecked(checked); }, [checked]); const handleChange = () => { setDisableHoverStyles(true); setIsChecked((prev) => { onChange?.(!prev); return !prev; }); }; const onMouseLeave = () => { if (disableHoverStyles) { setDisableHoverStyles(false); } }; return (
); }; export default IconCheckbox;