import React, { ChangeEvent, ElementType, forwardRef, PropsWithChildren, useCallback, useMemo } from 'react'; import cn from 'classnames'; import { PaletteColors } from '../styles'; import { baseClasses, iconClasses, layerClasses, variantClasses } from './checkbox_styles'; import { palette } from '../index'; interface Props { component?: string | ElementType; checked: boolean; partialCheck?: boolean; disabled?: boolean; color?: PaletteColors; defaultColor?: string; className?: string; inputClassName?: string; containerProps?: any; variant?: 'raised' | 'outlined'; isRadio?: Boolean; size?: 'regular' | 'small'; classes?: { container?: string; input?: string; }; } export type CheckboxProps = PropsWithChildren, 'color' | 'size'> & Props>; const CheckboxComponent = forwardRef( ( { component: Component = 'div', checked, disabled, color, defaultColor: propsDefaultColor = palette?.primary[400], className, inputClassName, containerProps, onChange, variant = 'outlined', isRadio, classes = {}, partialCheck, size = 'regular', ...other }, ref ) => { const handleChange = useCallback( (event: ChangeEvent) => { if (disabled) { return; } if (typeof onChange === 'function') { onChange(event); } }, [disabled, onChange] ); return (
); } ); const DEFAULT_ICON_PROPS = { scale: 0.5, opacity: 0, }; const CHECKED_ICON_PROPS = { scale: 1, opacity: 1, }; const CheckIcon: React.FC<{ checked: boolean; partialCheck: boolean; classes: { checkIcon: string } }> = ({ checked: propsChecked, partialCheck, classes, }) => { const checked = propsChecked || partialCheck; const spring = useMemo(() => (checked ? CHECKED_ICON_PROPS : DEFAULT_ICON_PROPS), [checked]); return ( {propsChecked && } {!propsChecked && partialCheck && } ); }; export const Checkbox = CheckboxComponent;