import React, { FC, memo, MouseEventHandler, useEffect, useRef } from 'react'; import { cn } from '../../util/bem'; import { FormSizesType, SizeType, TextColorType } from '../../util/global-props'; import { Description } from '../description/description.component'; import './switch.component.scss'; export type SwitchPropsType = { label?: React.ReactNode; disabled?: boolean; checked?: boolean; autoFocus?: boolean; size?: 'md' | 'lg'; descriptionSize?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'; height?: FormSizesType; margin?: SizeType; color?: TextColorType; style?: React.CSSProperties; flex?: number; onChange?: (checked: boolean) => void; onClick?: MouseEventHandler; onMouseUp?: MouseEventHandler; tabIndex?: number; } const classNames = cn('switch'); export const Switch: 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) { return; } if (props.onChange) { props.onChange(!props.checked); } }; return (
{ props.label && ( { props.label } ) }
); }); Switch.defaultProps = { size: 'md', disabled: false, autoFocus: false };