Having this React component:
import RadioButton from './radio-button';
import { Label, Horizontal, Vertical } from './radio-button.styles';
import { RadioOption, OptionGroup } from './radio-button.types';
const RenderedOptions = ({ label, options, onChange }: OptionGroup) => {
return (
<>
{options.map(({ value, name, disabled }: RadioOption, index) => {
const optionLabel = label.replace(/ +/g, '-');
const optionId = `${optionLabel}-${index}`;
return (
<RadioButton
value={value}
label={value}
key={optionId}
id={optionId}
name={name}
disabled={disabled}
defaultChecked={index === 0}
onChange={onChange}
/>
);
})}
</>
);
};
export const RadioButtonGroup = ({ label, options, horizontal, onChange }: OptionGroup) => {
const renderedOptions = horizontal ? (
<Horizontal>
<RenderedOptions label={label} options={options} onChange={onChange} />
</Horizontal>
) : (
<Vertical>
<RenderedOptions label={label} options={options} onChange={onChange} />
</Vertical>
);
return (
<>
<Label>{label}</Label>
{renderedOptions}
</>
);
};
export default RadioButtonGroup;
As the name says, it's a reusable Radio Group component which works fine but I have the feeling that it could be done better/cleaner.
Any suggestions?