import { IOption } from '@websolutespa/bom-core'; import { FormControl, useControl, valueToString } from '@websolutespa/bom-mixer-forms'; import { useLabel } from '@websolutespa/bom-mixer-hooks'; import { ChangeEvent, FocusEvent, useId, useState } from 'react'; import { Field, FieldProps, Label, Radio } from '../forms'; import { FieldError } from './field-error'; type FieldRadioProps = { control: FormControl; }; export function FieldRadio({ control, ...props }: FieldRadioProps & FieldProps) { const uid = useId(); const label = useLabel(); const id = useId(); // const uniqueName = `${control.name}-${uid}`; const uniqueName = id; const getUniqueName = (option: IOption) => { return `${uniqueName}-${option.id}`; }; const [state, setValue, setTouched] = useControl(control); const onChange = (event: ChangeEvent) => { // console.log('onChange', event.target.checked); const option = control.options?.find(x => String(x.id) === event.target.value); setValue(option || null); }; const [focus, setFocus] = useState(false); const onBlur = (_: FocusEvent) => { setTouched(); setFocus(false); }; const onFocus = (_: FocusEvent) => { setFocus(true); }; const controlLabel = control.label && label(control.label); return ( state.flags.hidden ? ( ) : ( {controlLabel && ( {controlLabel} )} {control.options?.map((option) => ( ))} ) ); }