import { useRef, useState, type CSSProperties, type ChangeEvent } from 'react'; import { Checkbox as MuiCheckbox, FormControlLabel, type CheckboxProps } from '@mui/material'; import clsx from 'clsx'; import './rds-checkbox.scss'; export interface RdsCheckboxProps extends Omit { labeltext?: string; isDisabled?: boolean; style?: 'square' | 'circular'; state?: 'default' | 'disabled' | 'hover'; status?: 'checked' | 'unchecked' | 'indeterminate'; showText?: boolean; cssStyle?: CSSProperties; } const RdsCheckbox = ({ labeltext, isDisabled = false, disabled, style = 'square', state = 'default', status, showText = true, className, cssStyle, color, onChange, ...props }:RdsCheckboxProps) => { const isCheckboxDisabled = Boolean(disabled) || isDisabled || state === 'disabled'; const [internalChecked, setInternalChecked] = useState(status === 'checked'); const prevStatusRef = useRef(status); if (status !== prevStatusRef.current) { prevStatusRef.current = status; if (status !== undefined) { setInternalChecked(status === 'checked'); } } const checked = internalChecked; const currentIndeterminate = status === 'indeterminate'; const handleChange = (event: ChangeEvent, value: boolean) => { setInternalChecked(value); if (onChange) onChange(event, value); }; const checkboxClassName = clsx( 'rds-checkbox', style && `rds-checkbox__${style}`, checked && 'rds-checkbox__checked', isCheckboxDisabled && 'rds-checkbox__disabled', currentIndeterminate && 'rds-checkbox__indeterminate', state && state !== 'default' && `rds-checkbox__${state}`, color && color !== 'default' && `rds-checkbox__${color}`, labeltext && !showText && 'rds-checkbox__text-hidden', status && `rds-checkbox__${status}`, className, ); const checkbox = ( ); if (labeltext) { return (
); } return (
{checkbox}
); }; RdsCheckbox.displayName = 'RdsCheckbox'; export default RdsCheckbox;