import { IOption } from '@websolutespa/bom-core'; import { FormControl, useControl } from '@websolutespa/bom-mixer-forms'; import { useLabel } from '@websolutespa/bom-mixer-hooks'; import { ChangeEvent, FocusEvent, useId, useState } from 'react'; import { Field, FieldProps, Label, RadioCard } from '../forms'; import { FieldError } from './field-error'; type FieldCardProps = { control: FormControl; }; export function FieldCard({ control, ...props }: FieldCardProps & FieldProps) { const uid = useId(); const label = useLabel(); const uniqueName = `${control.name}-${uid}`; const [state, setValue, setTouched] = useControl(control); const onChange = (event: ChangeEvent) => { // console.log('onChange', event.target.value); const option = control.options?.find(x => x.id.toString() === 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 && ( )} {control.options?.map(option => ( {option.name} {option.abstract && } ))} ) ); }