import React, { useCallback } from 'react'; import { ariaDescribedByIds, enumOptionsValueForIndex, enumOptionsIndexForValue, optionId, titleId, FormContextType, WidgetProps, RJSFSchema, StrictRJSFSchema, } from '@rjsf/utils'; import { Checkbox, Flex, Input } from '@mantine/core'; import { cleanupOptions } from '../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 >(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.value, enumOptions, emptyValue)); } }, [onBlur, id, enumOptions, emptyValue] ); const handleFocus = useCallback( ({ target }: React.FocusEvent) => { if (onFocus) { onFocus(id, enumOptionsValueForIndex(target.value, enumOptions, emptyValue)); } }, [onFocus, id, enumOptions, emptyValue] ); const selectedIndexes = enumOptionsIndexForValue(value, enumOptions, true) as string[]; return Array.isArray(enumOptions) && enumOptions.length > 0 ? ( <> {!hideLabel && !!label && ( (id)} required={required}> {label} )} 0 ? rawErrors.join('\n') : undefined} aria-describedby={ariaDescribedByIds(id)} {...themeProps} > {Array.isArray(enumOptions) ? ( {enumOptions.map((option, i) => ( ))} ) : null} ) : null; }