import { FocusEvent } from 'react'; import { Checkbox } from 'antd'; import { ariaDescribedByIds, enumOptionsIndexForValue, enumOptionsValueForIndex, optionId, FormContextType, WidgetProps, RJSFSchema, StrictRJSFSchema, GenericObjectType, } from '@rjsf/utils'; /** The `CheckboxesWidget` is a widget for rendering checkbox groups. * It is typically used to represent an array of enums. * * @param props - The `WidgetProps` for this component */ export default function CheckboxesWidget< T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any, >({ autofocus, disabled, registry, id, htmlName, onBlur, onChange, onFocus, options, readonly, value, }: WidgetProps) { const { formContext } = registry; const { readonlyAsDisabled = true } = formContext as GenericObjectType; const { enumOptions, enumDisabled, inline, emptyValue } = options; const handleChange = (nextValue: any) => onChange(enumOptionsValueForIndex(nextValue, enumOptions, emptyValue)); const handleBlur = ({ target }: FocusEvent) => onBlur(id, enumOptionsValueForIndex(target.value, enumOptions, emptyValue)); const handleFocus = ({ target }: FocusEvent) => onFocus(id, enumOptionsValueForIndex(target.value, enumOptions, emptyValue)); // Antd's typescript definitions do not contain the following props that are actually necessary and, if provided, // they are used, so hacking them in via by spreading `extraProps` on the component to avoid typescript errors const extraProps = { id, onBlur: !readonly ? handleBlur : undefined, onFocus: !readonly ? handleFocus : undefined, }; const selectedIndexes = enumOptionsIndexForValue(value, enumOptions, true) as string[]; return Array.isArray(enumOptions) && enumOptions.length > 0 ? ( <> {Array.isArray(enumOptions) && enumOptions.map((option, i) => ( {option.label} {!inline &&
}
))}
) : null; }