import React, { ChangeEvent, KeyboardEvent } from 'react'; import classnames from 'classnames'; import { BackgroundColor, BorderColor, BorderRadius, IconColor, Display, AlignItems, } from '../../../helpers/constants/design-system'; import type { PolymorphicRef } from '../box'; import { Box, Icon, IconName, Text } from '..'; import { CheckboxProps, CheckboxComponent } from './checkbox.types'; export const Checkbox: CheckboxComponent = React.forwardRef( ( { id, isChecked, isIndeterminate, isDisabled, isReadOnly, isRequired, onChange, className = '', iconProps, inputProps, inputRef, title, name, label, ...props }: CheckboxProps, ref?: PolymorphicRef, ) => { const handleCheckboxKeyDown = (event: KeyboardEvent) => { if (event.key === 'Enter') { onChange?.(event as unknown as ChangeEvent); } }; // If no title is provided, use the label as the title only if the label is a string const sanitizedTitle = !title && typeof label === 'string' ? label : title || id; return ( ) => { if (isReadOnly) { event.preventDefault(); } else { onChange?.(event); } }} onKeyDown={handleCheckboxKeyDown} margin={0} marginRight={label ? 2 : 0} backgroundColor={ isChecked || isIndeterminate ? BackgroundColor.primaryDefault : BackgroundColor.transparent } borderColor={ isChecked || isIndeterminate ? BorderColor.primaryDefault : BorderColor.borderDefault } borderRadius={BorderRadius.SM} borderWidth={2} display={Display.Flex} ref={inputRef} {...inputProps} className={classnames( 'mm-checkbox__input', inputProps?.className ?? '', { 'mm-checkbox__input--checked': Boolean(isChecked), 'mm-checkbox__input--indeterminate': Boolean(isIndeterminate), 'mm-checkbox__input--readonly': Boolean(isReadOnly), }, )} /> {(isChecked || isIndeterminate) && ( )} {label ? {label} : null} ); }, );