import { FocusEvent } from 'react'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import FormLabel from '@material-ui/core/FormLabel'; import Radio from '@material-ui/core/Radio'; import RadioGroup from '@material-ui/core/RadioGroup'; import { ariaDescribedByIds, enumOptionsIndexForValue, enumOptionsValueForIndex, labelValue, optionId, FormContextType, RJSFSchema, StrictRJSFSchema, WidgetProps, } from '@rjsf/utils'; /** The `RadioWidget` is a widget for rendering a radio group. * It is typically used with a string property constrained with enum options. * * @param props - The `WidgetProps` for this component */ export default function RadioWidget({ id, options, value, required, disabled, readonly, label, hideLabel, onChange, onBlur, onFocus, }: WidgetProps) { const { enumOptions, enumDisabled, emptyValue } = options; const _onChange = (_: any, value: any) => onChange(enumOptionsValueForIndex(value, enumOptions, emptyValue)); const _onBlur = ({ target: { value } }: FocusEvent) => onBlur(id, enumOptionsValueForIndex(value, enumOptions, emptyValue)); const _onFocus = ({ target: { value } }: FocusEvent) => onFocus(id, enumOptionsValueForIndex(value, enumOptions, emptyValue)); const row = options ? options.inline : false; const selectedIndex = enumOptionsIndexForValue(value, enumOptions) ?? null; return ( <> {labelValue( {label || undefined} , hideLabel )} (id)} > {Array.isArray(enumOptions) && enumOptions.map((option, index) => { const itemDisabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1; const radio = ( } label={option.label} value={String(index)} key={index} disabled={disabled || itemDisabled || readonly} /> ); return radio; })} ); }