import { ChangeEvent, FocusEvent, useCallback } from 'react'; import { ariaDescribedByIds, enumOptionValueDecoder, enumOptionValueEncoder, enumOptionsDeselectValue, enumOptionsIsSelected, enumOptionsSelectValue, getOptionValueFormat, optionId, FormContextType, WidgetProps, RJSFSchema, StrictRJSFSchema, } 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 */ function CheckboxesWidget({ id, disabled, options, value, autofocus = false, readonly, onChange, onBlur, onFocus, htmlName, }: WidgetProps) { const { inline = false, enumOptions, enumDisabled, emptyValue } = options; const optionValueFormat = getOptionValueFormat(options); const checkboxesValues = Array.isArray(value) ? value : [value]; const handleBlur = useCallback( ({ target }: FocusEvent) => onBlur(id, enumOptionValueDecoder(target && target.value, enumOptions, optionValueFormat, emptyValue)), [onBlur, id, enumOptions, emptyValue, optionValueFormat], ); const handleFocus = useCallback( ({ target }: FocusEvent) => onFocus(id, enumOptionValueDecoder(target && target.value, enumOptions, optionValueFormat, emptyValue)), [onFocus, id, enumOptions, emptyValue, optionValueFormat], ); 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 = ( {option.label} ); return inline ? ( ) : (
); })}
); } export default CheckboxesWidget;