import React, { useCallback } from 'react'; import { ariaDescribedByIds, enumOptionsIndexForValue, enumOptionsValueForIndex, optionId, FormContextType, RJSFSchema, StrictRJSFSchema, WidgetProps, } from '@rjsf/utils'; import { Radio, Flex } from '@mantine/core'; import { cleanupOptions } from '../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( props: WidgetProps ) { const { id, value, required, disabled, readonly, autofocus, label, hideLabel, rawErrors, options, onChange, onBlur, onFocus, } = props; const { enumOptions, enumDisabled, inline, emptyValue } = options; const themeProps = cleanupOptions(options); const handleChange = useCallback( (nextValue: any) => { if (!disabled && !readonly && onChange) { onChange(enumOptionsValueForIndex(nextValue, enumOptions, emptyValue)); } }, [onChange, disabled, readonly, enumOptions, emptyValue] ); const handleBlur = useCallback( ({ target }: React.FocusEvent) => { if (onBlur) { onBlur(id, enumOptionsValueForIndex(target && target.value, enumOptions, emptyValue)); } }, [onBlur, id, enumOptions, emptyValue] ); const handleFocus = useCallback( ({ target }: React.FocusEvent) => { if (onFocus) { onFocus(id, enumOptionsValueForIndex(target && target.value, enumOptions, emptyValue)); } }, [onFocus, id, enumOptions, emptyValue] ); const selected = enumOptionsIndexForValue(value, enumOptions) as string; return ( 0 ? rawErrors.join('\n') : undefined} aria-describedby={ariaDescribedByIds(id)} {...themeProps} > {Array.isArray(enumOptions) ? ( {enumOptions.map((option, i) => ( ))} ) : null} ); }