import { type ForwardRefRenderFunction, forwardRef } from 'react'; import { Fieldset, type FieldsetProps, Label, type LabelProps, Radio, type RadioProps, } from '../../../atoms'; import { LabelledField } from '../labelled-field'; import styles from './radio-group.module.scss'; export type RadioGroupItem = { /** * The item id. */ id: string; /** * Should the item be disabled? */ isDisabled?: boolean; /** * The item label. */ label: LabelProps['children']; /** * The item value. */ value: string; }; export type RadioGroupProps = Omit & { /** * Should we display the radio buttons inlined? * * @default false */ isInline?: boolean; /** * The radio group name. */ name: string; /** * A function to handle selection change. */ onSwitch?: RadioProps['onChange']; /** * The options. */ options: RadioGroupItem[]; /** * The selected value. It should match a RadioGroupItem value or be undefined. */ value?: RadioGroupItem['value']; }; const RadioGroupWithRef: ForwardRefRenderFunction< HTMLFieldSetElement, RadioGroupProps > = ( { className = '', isInline = false, name, onSwitch, options, value, ...props }, ref ) => { const layoutModifier = isInline ? styles['group--inline'] : ''; const groupClass = `${layoutModifier} ${className}`; return (
{options.map((option) => ( } isInline isReversedOrder key={option.id} label={} /> ))}
); }; /** * RadioGroup component * * Render a group of labelled radio buttons. */ export const RadioGroup = forwardRef(RadioGroupWithRef);