@rjsf/mui
Version:
Material UI 7 theme, fields and widgets for react-jsonschema-form
38 lines • 3.14 kB
JavaScript
import { createElement as _createElement } from "react";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import Checkbox from '@mui/material/Checkbox';
import FormControlLabel from '@mui/material/FormControlLabel';
import FormGroup from '@mui/material/FormGroup';
import FormLabel from '@mui/material/FormLabel';
import { ariaDescribedByIds, enumOptionValueDecoder, enumOptionsDeselectValue, enumOptionsIsSelected, enumOptionsSelectValue, getOptionValueFormat, labelValue, optionId, } from '@rjsf/utils';
import { getMuiProps } from '../util.js';
/** 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(props) {
const { label, hideLabel, id, htmlName, disabled, options, value, autofocus, readonly, required, onChange, onBlur, onFocus, } = props;
const { enumOptions, enumDisabled, inline, emptyValue } = options;
const optionValueFormat = getOptionValueFormat(options);
const checkboxesValues = Array.isArray(value) ? value : [value];
const _onChange = (index) => ({ target: { checked } }) => {
if (checked) {
onChange(enumOptionsSelectValue(index, checkboxesValues, enumOptions));
}
else {
onChange(enumOptionsDeselectValue(index, checkboxesValues, enumOptions));
}
};
const _onBlur = ({ target }) => onBlur(id, enumOptionValueDecoder(target && target.value, enumOptions, optionValueFormat, emptyValue));
const _onFocus = ({ target }) => onFocus(id, enumOptionValueDecoder(target && target.value, enumOptions, optionValueFormat, emptyValue));
const { rjsfSlotProps: muiSlotProps, ...otherMuiProps } = getMuiProps(options);
return (_jsxs(_Fragment, { children: [labelValue(_jsx(FormLabel, { required: required, htmlFor: id, children: label || undefined }), hideLabel), _jsx(FormGroup, { ...otherMuiProps, ...muiSlotProps === null || muiSlotProps === void 0 ? void 0 : muiSlotProps.formGroup, id: id, row: !!inline, children: Array.isArray(enumOptions) &&
enumOptions.map((option, index) => {
const checked = enumOptionsIsSelected(option.value, checkboxesValues);
const itemDisabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1;
const checkbox = (_jsx(Checkbox, { ...muiSlotProps === null || muiSlotProps === void 0 ? void 0 : muiSlotProps.checkbox, id: optionId(id, index), name: htmlName || id, checked: checked, disabled: disabled || itemDisabled || readonly, autoFocus: autofocus && index === 0, onChange: _onChange(index), onBlur: _onBlur, onFocus: _onFocus, "aria-describedby": ariaDescribedByIds(id) }));
return (_createElement(FormControlLabel, { ...muiSlotProps === null || muiSlotProps === void 0 ? void 0 : muiSlotProps.formControlLabel, control: checkbox, key: index, label: option.label }));
}) })] }));
}
//# sourceMappingURL=CheckboxesWidget.js.map