import React, { FC, memo, MouseEventHandler, useEffect, useRef } from 'react'; import { Description, Icon } from '../../../'; import { cn } from '../../util/bem'; import { FormSizesType, SizeType } from '../../util/global-props'; import './checkbox.component.scss'; export type CheckboxPropsType = { className?: string; label?: React.ReactNode; disabled?: boolean; checked?: boolean; defaultChecked?: boolean; autoFocus?: boolean; height?: FormSizesType; margin?: SizeType; size?: 'sm' | 'md' | 'lg'; style?: React.CSSProperties; flex?: number; onChange?: (checked: boolean) => void; onClick?: MouseEventHandler; onMouseUp?: MouseEventHandler; tabIndex?: number; } const classNames = cn('checkbox'); export const Checkbox: FC = memo((props) => { const node = useRef(null); useEffect(() => { if (node.current && props.autoFocus && !props.disabled) { node.current.focus(); } }, [ props.autoFocus, props.disabled ]); const handleClick = () => { if (props.disabled || props.defaultChecked) { return; } if (props.onChange) { props.onChange(!props.checked); } }; const isChecked = props.checked || props.defaultChecked; return (
{ props.label ? ( { props.label } ) : null }
); }); Checkbox.defaultProps = { defaultChecked: false, disabled: false, autoFocus: false, size: 'sm' };