import { type FormContextType, type RJSFSchema, type StrictRJSFSchema, type WidgetProps, ariaDescribedByIds, enumOptionsDeselectValue, enumOptionsIsSelected, enumOptionsSelectValue, enumOptionsValueForIndex, optionId } from "@rjsf/utils"; import { type ChangeEvent, type FocusEvent, useCallback } from "react"; /** 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 */ function CheckboxesWidget< T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any >({ id, disabled, options: { inline = false, enumOptions, enumDisabled, emptyValue }, value, autofocus = false, readonly, onChange, onBlur, onFocus }: WidgetProps) { const checkboxesValues = Array.isArray(value) ? value : [value]; const handleBlur = useCallback( ({ target }: FocusEvent) => onBlur(id, enumOptionsValueForIndex(target?.value, enumOptions, emptyValue)), [onBlur, id] ); const handleFocus = useCallback( ({ target }: FocusEvent) => onFocus(id, enumOptionsValueForIndex(target?.value, enumOptions, emptyValue)), [onFocus, id] ); return (
{Array.isArray(enumOptions) && enumOptions.map((option, index) => { const checked = enumOptionsIsSelected(option.value, checkboxesValues); const itemDisabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1; const disabledCls = disabled || itemDisabled || readonly ? "disabled" : ""; const handleChange = (event: ChangeEvent) => { if (event.target.checked) { onChange(enumOptionsSelectValue(index, checkboxesValues, enumOptions)); } else { onChange(enumOptionsDeselectValue(index, checkboxesValues, enumOptions)); } }; const checkbox = ( // biome-ignore lint/correctness/useJsxKeyInIterable: it's wrapped (id)} /> {option.label} ); return inline ? ( ) : (
); })}
); } export default CheckboxesWidget;