import { FormEvent, FocusEvent } from 'react'; import { ChoiceGroup, IChoiceGroupOption, IChoiceGroupProps } from '@fluentui/react'; import { ariaDescribedByIds, enumOptionsIndexForValue, enumOptionsValueForIndex, labelValue, optionId, FormContextType, RJSFSchema, StrictRJSFSchema, WidgetProps, } from '@rjsf/utils'; import _pick from 'lodash/pick'; const allowedProps: (keyof IChoiceGroupProps)[] = [ 'componentRef', 'options', 'defaultSelectedKey', 'selectedKey', 'onChange', 'label', /* Backward compatibility with fluentui v7 */ 'onChanged' as any, 'theme', 'styles', 'ariaLabelledBy', ]; export default function RadioWidget({ id, options, value, required, label, hideLabel, onChange, onBlur, onFocus, disabled, readonly, }: WidgetProps) { const { enumOptions, enumDisabled, emptyValue } = options; function _onChange(_ev?: FormEvent, option?: IChoiceGroupOption): void { if (option) { onChange(enumOptionsValueForIndex(option.key, 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 newOptions = Array.isArray(enumOptions) ? enumOptions.map((option, index) => ({ key: String(index), name: id, id: optionId(id, index), text: option.label, disabled: Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1, 'aria-describedby': ariaDescribedByIds(id), })) : []; const selectedIndex = enumOptionsIndexForValue(value, enumOptions) as string; const uiProps = _pick((options.props as object) || {}, allowedProps); return ( ); }