import React from 'react'; import { type RadioButtonGroupProps } from '@carbon/react/lib/components/RadioButtonGroup/RadioButtonGroup'; import { type Control, Controller, type FieldValues } from 'react-hook-form'; import { RadioButtonGroup, RadioButton } from '@carbon/react'; import { type RadioOption } from '../../../stock-items/add-stock-item/stock-item-details/stock-item-details.resource'; interface ControlledRadioButtonGroupProps extends Omit { controllerName: string; name: string; control: Control; options: RadioOption[]; id?: string; onChange?: (selectedValue: string, name: string, event: React.ChangeEvent) => void; } const ControlledRadioButtonGroup = (props: ControlledRadioButtonGroupProps) => { const { controllerName, name, control, options, onChange: onChangeProp, id: propsId, ...radioGroupProps } = props; return ( ( ) => { const matchedOption = options.find((option) => String(option.value) === selectedValue); onChange(matchedOption ? matchedOption.value : selectedValue); // Fire prop change if (onChangeProp) { onChangeProp(selectedValue, name, event); } }} id={name} ref={ref} defaultSelected={propsId} valueSelected={value != null ? String(value) : value} > {options.map((option, index) => ( ))} )} /> ); }; export default ControlledRadioButtonGroup;