import { ChangeEvent, FocusEvent } from 'react'; import Checkbox from '@material-ui/core/Checkbox'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import FormGroup from '@material-ui/core/FormGroup'; import FormLabel from '@material-ui/core/FormLabel'; import { ariaDescribedByIds, enumOptionsDeselectValue, enumOptionsIsSelected, enumOptionsSelectValue, enumOptionsValueForIndex, labelValue, 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 */ export default function CheckboxesWidget< T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any >({ label, hideLabel, id, disabled, options, value, autofocus, readonly, required, onChange, onBlur, onFocus, }: WidgetProps) { const { enumOptions, enumDisabled, inline, emptyValue } = options; const checkboxesValues = Array.isArray(value) ? value : [value]; const _onChange = (index: number) => ({ target: { checked } }: ChangeEvent) => { if (checked) { onChange(enumOptionsSelectValue(index, checkboxesValues, enumOptions)); } else { onChange(enumOptionsDeselectValue(index, checkboxesValues, enumOptions)); } }; const _onBlur = ({ target: { value } }: FocusEvent) => onBlur(id, enumOptionsValueForIndex(value, enumOptions, emptyValue)); const _onFocus = ({ target: { value } }: FocusEvent) => onFocus(id, enumOptionsValueForIndex(value, enumOptions, emptyValue)); return ( <> {labelValue( {label || undefined} , hideLabel )} {Array.isArray(enumOptions) && enumOptions.map((option, index: number) => { const checked = enumOptionsIsSelected(option.value, checkboxesValues); const itemDisabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1; const checkbox = ( (id)} /> ); return ; })} ); }