import { forwardRef, ReactNode } from 'react'; import classnames from 'classnames'; import { useRadioGroupContext } from './Context'; import styles from './RadioButton.css'; import Badge from '../Badge'; import Box from '../Box'; import Flex from '../Flex'; import Label from '../Label'; import FormHelperText from '../sharedSubcomponents/FormHelperText'; import Text from '../Text'; import useFocusVisible from '../useFocusVisible'; import useInteractiveStates from '../utils/useInteractiveStates'; import useTapScaleAnimation from '../utils/useTapScaleAnimation'; type BadgeType = { text: string; type?: | 'info' | 'error' | 'warning' | 'success' | 'neutral' | 'recommendation' | 'darkWash' | 'lightWash'; }; type Props = { badge?: BadgeType; checked?: boolean; disabled?: boolean; id: string; image?: ReactNode; label?: string; name?: string; onChange: (arg1: { event: React.ChangeEvent; checked: boolean }) => void; ref?: HTMLInputElement; // eslint-disable-line react/no-unused-prop-types, size?: 'sm' | 'md'; helperText?: string; value: string; }; const RadioGroupButtonWithForwardRef = forwardRef(function RadioButton( { checked, disabled, id, image, label, name, onChange, helperText, value, badge, size }: Props, ref, ) { const { isFocusVisible } = useFocusVisible(); const tapScaleAnimation = useTapScaleAnimation(); const { handleOnMouseEnter, handleOnMouseLeave, handleOnBlur, handleOnFocus, handleOnMouseDown, handleOnMouseUp, isFocused, isHovered, isActive: isPressed, } = useInteractiveStates(); const { parentName } = useRadioGroupContext(); if (parentName !== 'RadioGroup') { throw new Error( `RadioGroup.RadioButton must be used within a [RadioGroup](https://gestalt.pinterest.systems/web/radiogroup).`, ); } return (
{/* OUTER CONTAINER */} {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
tapScaleAnimation.handleMouseDown()} onMouseUp={() => tapScaleAnimation.handleMouseUp()} > {/* RADIO */}
{/* INPUT */} onChange({ checked: event.target.checked, event })} onFocus={handleOnFocus} onMouseDown={handleOnMouseDown} onMouseEnter={handleOnMouseEnter} onMouseLeave={handleOnMouseLeave} onMouseUp={handleOnMouseUp} type="radio" value={value} />
{Boolean(image) && {image}} {label && ( )} {badge && ( )} {label && helperText ? : null}
); }); RadioGroupButtonWithForwardRef.displayName = 'RadioGroup.RadioButton'; export default RadioGroupButtonWithForwardRef;