import type { ChangeEvent, FocusEvent } from 'react'; import { Label, Checkbox } from '@fluentui/react-components'; import { Flex } from '@fluentui/react-migration-v0-v9'; import type { FormContextType, WidgetProps, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils'; import { ariaDescribedByIds, enumOptionValueDecoder, enumOptionsDeselectValue, enumOptionsIsSelected, enumOptionsSelectValue, getOptionValueFormat, labelValue, optionId, } 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, >({ label, hideLabel, id, htmlName, disabled, options, value, autofocus, readonly, required, onChange, onBlur, onFocus, }: WidgetProps) { const { enumOptions, enumDisabled, inline, emptyValue } = options; const optionValueFormat = getOptionValueFormat(options); const checkboxesValues = Array.isArray(value) ? value : [value]; const handleChange = (index: number) => ({ target: { checked } }: ChangeEvent) => { if (checked) { onChange(enumOptionsSelectValue(index, checkboxesValues, enumOptions)); } else { onChange(enumOptionsDeselectValue(index, checkboxesValues, enumOptions)); } }; const handleBlur = ({ target }: FocusEvent) => onBlur(id, enumOptionValueDecoder(target?.value, enumOptions, optionValueFormat, emptyValue)); const handleFocus = ({ target }: FocusEvent) => onFocus(id, enumOptionValueDecoder(target?.value, enumOptions, optionValueFormat, emptyValue)); return ( <> {labelValue( , hideLabel, )} {Array.isArray(enumOptions) && enumOptions.map((option, index: number) => { const checked = enumOptionsIsSelected(option.value, checkboxesValues); const itemDisabled = Array.isArray(enumDisabled) && enumDisabled.includes(option.value); return ( ); })} ); }