import { useState } from 'react'; import Radio from '../radio'; import { RadioProps } from '../radio/Radio'; import { useInputAttributes, WithInputAttributesProps } from '../inputs/contexts'; export type RadioGroupRadio = Omit< RadioProps, 'name' | 'checked' | 'onChange' | 'className' >; export interface RadioGroupProps { name: string; radios: readonly RadioGroupRadio[]; selectedValue?: T; // TODO: `NoInfer` from TypeScript 5.4 onChange: NonNullable['onChange']>; UNSAFE_inputAttributes?: WithInputAttributesProps['inputAttributes']; } export default function RadioGroup({ name, radios, selectedValue: controlledValue, onChange, UNSAFE_inputAttributes, }: RadioGroupProps) { const inputAttributes = useInputAttributes({ nonLabelable: true }); const [uncontrolledValue, setUncontrolledValue] = useState(controlledValue); return radios.length > 0 ? (
{radios.map(({ value = '' as T, ...restProps }, index) => ( { setUncontrolledValue(nextValue); onChange(nextValue); }} /> ))}
) : null; }