import { ChangeEvent, MouseEvent, useRef, useState } from 'react'; import styles from './styles/rt-checkbox.module.css'; import config from './styles/component-values-checkbox.module.css'; import { RtFrame } from '@dpa/react-frame-intro'; import classnames from 'classnames/bind'; import { RtCheckboxProps } from './rt-checkbox.types'; import { getIcon } from './utils/get-icon'; import { forwardRef, useMemo } from 'react'; import { RtBaseProps, useRtBaseProps } from '@dpa/react-hoc'; const cn = classnames.bind(styles); export const RtCheckbox = forwardRef( ( props: RtCheckboxProps & RtBaseProps, ref: React.ForwardedRef ) => { const { name, value, use, modifier, size = 'sm', width = 'min-content', height = 'min-content', isActive = false, isIntermediate = false, radius = 'primary', isLoading = false, disabled = false, onChange, ariaLabel, ariaLabelledBy, } = props; const { onMouseEnter, onMouseOut, 'data-size-scheme': dataSizeScheme, ...baseProps } = useRtBaseProps(props, '0.2.2'); const [isHover, setIsHover] = useState(false); const inputRef = useRef(null); const mouseOverHandle = (event: MouseEvent) => { setIsHover(true); onMouseEnter?.(event); }; const mouseOutHandle = (event: MouseEvent) => { setIsHover(false); onMouseOut?.(event); }; const handleChange = (event: ChangeEvent) => { const { checked: value } = event.currentTarget; if (onChange) { onChange({ event, value, name: name || '' }); } }; const Icon = useMemo(() => { // TODO: Skeleton // if (isLoading) { // return } // } if (isHover && !disabled && (isActive || isIntermediate)) { return getIcon('hover', size); } if (isIntermediate) { return getIcon('intermediate', size); } if (isActive) { return getIcon('checked', size); } }, [isActive, disabled, isHover, isIntermediate, size]); const state = disabled ? 'disabled' : isLoading ? 'loading' : undefined; return ( { if (e.code === 'Space') { inputRef.current?.click(); } }} > { e.stopPropagation(); }} aria-label={ariaLabel} aria-labelledby={ariaLabelledBy} /> {Icon && } ); } ); RtCheckbox.displayName = 'RtCheckbox';