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 { iconMap } from '../icon/icon.library'; import './radio.component.scss'; export type RadioPropsType = { className?: string; label?: React.ReactNode; disabled?: boolean; checked?: boolean; defaultChecked?: boolean; value?: string; iconMargin?: SizeType; autoFocus?: boolean; height?: FormSizesType; margin?: SizeType; size?: 'sm' | 'md' | 'lg'; style?: React.CSSProperties; viewType?: 'block' | 'radio'; theme?: 'primary' | 'primary-green' | 'primary-white' | 'secondary' | 'dark' | 'transparent' | 'alert'; icon?: keyof typeof iconMap; flex?: number; onChange?: (val: string | boolean) => void; onClick?: MouseEventHandler; onMouseUp?: MouseEventHandler; tabIndex?: number; children?: React.ReactNode; } const classNames = cn('radio'); export const Radio: FC = memo((props) => { const node = useRef(null); const getIconColor = () => { if (props.theme === 'primary' || props.theme === 'alert' || props.theme === 'dark') { return 'white'; } return 'dark'; }; 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.value && props.onChange) { props.onChange(props.value); return; } if (props.onChange) { props.onChange(!props.checked); } }; const isChecked = props.checked || props.defaultChecked; return (
{ props.viewType === 'block' && ( <> { props.icon && } { props.children } ) } { props.label && props.viewType === 'radio' ? ( { props.label } ) : null }
); }); Radio.defaultProps = { defaultChecked: false, disabled: false, iconMargin: 'xs', autoFocus: false, size: 'sm', viewType: 'radio', theme: 'primary' };