import React from 'react' import { type RadioProps, RadioSize } from './types' import { getRadioTextProps, getRadioLabelStyles } from './utils' import { StyledRadioInput } from './StyledRadio' import Block from '../Primitives/Block/Block' import PrimitiveText from '../Primitives/PrimitiveText/PrimitiveText' import { Tooltip } from '../Tooltip/Tooltip' import type { RadioTokensType } from './radio.token' import { useResponsiveTokens } from '../../hooks/useResponsiveTokens' import { FOUNDATION_THEME } from '../../tokens' import { useErrorShake } from '../common/useErrorShake' import { getErrorShakeStyle } from '../common/error.animations' import { getTruncatedText } from '../../global-utils/GlobalUtils' export const Radio = ({ id, checked, defaultChecked, onChange, disabled = false, required = false, error = false, size = RadioSize.MEDIUM, children, subtext, slot, name, value, maxLength, ...rest }: RadioProps) => { const radioTokens = useResponsiveTokens('RADIO') const generatedId = React.useId() const uniqueId = id || generatedId const shouldShake = useErrorShake(error) const labelMaxLength = maxLength?.label const subtextMaxLength = maxLength?.subtext const subtextId = subtext ? `${uniqueId}-subtext` : undefined const customAriaDescribedBy = (rest as { 'aria-describedby'?: string })[ 'aria-describedby' ] const ariaDescribedBy = subtextId && customAriaDescribedBy ? `${customAriaDescribedBy} ${subtextId}` : subtextId || customAriaDescribedBy const isControlled = checked !== undefined const checkedProps = isControlled ? { checked } : { defaultChecked: defaultChecked ?? false } return ( ) } const RadioContent: React.FC<{ uniqueId: string disabled: boolean error: boolean required: boolean size: RadioSize children?: React.ReactNode subtext?: React.ReactNode radioTokens: RadioTokensType slot?: React.ReactNode labelMaxLength?: number subtextMaxLength?: number subtextId?: string }> = ({ uniqueId, disabled, error, required, size, children, subtext, radioTokens, slot, labelMaxLength, subtextMaxLength, subtextId, }) => { const labelStyles = getRadioLabelStyles(radioTokens, disabled) const textProps = getRadioTextProps(radioTokens, size, disabled, error) const subtextProps = getRadioTextProps( radioTokens, size, disabled, error, true ) const isStringLabel = typeof children === 'string' || typeof children === 'number' const labelTruncation = isStringLabel ? getTruncatedText(String(children), labelMaxLength) : null const subtextIsString = typeof subtext === 'string' || typeof subtext === 'number' const subtextTruncation = subtextIsString ? getTruncatedText(String(subtext), subtextMaxLength) : null const labelContent = children ? ( ) : null const labelWithTooltip = labelTruncation?.isTruncated && labelContent ? ( {labelContent} ) : ( labelContent ) const subtextContent = subtext ? ( {subtextTruncation?.isTruncated ? subtextTruncation.truncatedValue : subtext} ) : null const subtextWithTooltip = subtextTruncation?.isTruncated && subtextContent ? ( {subtextContent} ) : ( subtextContent ) return ( {labelWithTooltip && ( {labelWithTooltip} {slot && ( {slot} )} )} {subtextWithTooltip} ) } Radio.displayName = 'Radio' export default Radio