| 1 |
import { ChangeEvent, FocusEvent } from 'react'; |
| 2 |
import Checkbox from '@mui/material/Checkbox'; |
| 3 |
import FormControlLabel from '@mui/material/FormControlLabel'; |
| 4 |
import FormGroup from '@mui/material/FormGroup'; |
| 5 |
import FormLabel from '@mui/material/FormLabel'; |
| 6 |
import { |
| 7 |
ariaDescribedByIds, |
| 8 |
enumOptionsDeselectValue, |
| 9 |
enumOptionsIsSelected, |
| 10 |
enumOptionsSelectValue, |
| 11 |
enumOptionsValueForIndex, |
| 12 |
labelValue, |
| 13 |
optionId, |
| 14 |
FormContextType, |
| 15 |
WidgetProps, |
| 16 |
RJSFSchema, |
| 17 |
StrictRJSFSchema, |
| 18 |
} from '@rjsf/utils'; |
| 19 |
|
| 20 |
/** The `CheckboxesWidget` is a widget for rendering checkbox groups. |
| 21 |
* It is typically used to represent an array of enums. |
| 22 |
* |
| 23 |
* @param props - The `WidgetProps` for this component |
| 24 |
*/ |
| 25 |
export default function CheckboxesWidget< |
| 26 |
T = any, |
| 27 |
S extends StrictRJSFSchema = RJSFSchema, |
| 28 |
F extends FormContextType = any |
| 29 |
>({ |
| 30 |
label, |
| 31 |
hideLabel, |
| 32 |
id, |
| 33 |
disabled, |
| 34 |
options, |
| 35 |
value, |
| 36 |
autofocus, |
| 37 |
readonly, |
| 38 |
required, |
| 39 |
onChange, |
| 40 |
onBlur, |
| 41 |
onFocus, |
| 42 |
}: WidgetProps<T, S, F>) { |
| 43 |
const { enumOptions, enumDisabled, inline, emptyValue } = options; |
| 44 |
const checkboxesValues = Array.isArray(value) ? value : [value]; |
| 45 |
|
| 46 |
const _onChange = |
| 47 |
(index: number) => |
| 48 |
({ target: { checked } }: ChangeEvent<HTMLInputElement>) => { |
| 49 |
if (checked) { |
| 50 |
onChange(enumOptionsSelectValue(index, checkboxesValues, enumOptions)); |
| 51 |
} else { |
| 52 |
onChange(enumOptionsDeselectValue(index, checkboxesValues, enumOptions)); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
const _onBlur = ({ target: { value } }: FocusEvent<HTMLButtonElement>) => |
| 57 |
onBlur(id, enumOptionsValueForIndex<S>(value, enumOptions, emptyValue)); |
| 58 |
const _onFocus = ({ target: { value } }: FocusEvent<HTMLButtonElement>) => |
| 59 |
onFocus(id, enumOptionsValueForIndex<S>(value, enumOptions, emptyValue)); |
| 60 |
|
| 61 |
return ( |
| 62 |
<> |
| 63 |
{labelValue( |
| 64 |
<FormLabel required={required} htmlFor={id}> |
| 65 |
{label || undefined} |
| 66 |
</FormLabel>, |
| 67 |
hideLabel |
| 68 |
)} |
| 69 |
<FormGroup id={id} row={!!inline}> |
| 70 |
{Array.isArray(enumOptions) && |
| 71 |
enumOptions.map((option, index: number) => { |
| 72 |
const checked = enumOptionsIsSelected<S>(option.value, checkboxesValues); |
| 73 |
const itemDisabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1; |
| 74 |
const checkbox = ( |
| 75 |
<Checkbox |
| 76 |
id={optionId(id, index)} |
| 77 |
name={id} |
| 78 |
checked={checked} |
| 79 |
disabled={disabled || itemDisabled || readonly} |
| 80 |
autoFocus={autofocus && index === 0} |
| 81 |
onChange={_onChange(index)} |
| 82 |
onBlur={_onBlur} |
| 83 |
onFocus={_onFocus} |
| 84 |
aria-describedby={ariaDescribedByIds<T>(id)} |
| 85 |
/> |
| 86 |
); |
| 87 |
return <FormControlLabel control={checkbox} key={index} label={option.label} />; |
| 88 |
})} |
| 89 |
</FormGroup> |
| 90 |
</> |
| 91 |
); |
| 92 |
} |