@rjsf/antd
Version:
Ant Design theme, fields and widgets for react-jsonschema-form
28 lines • 2.45 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { ariaDescribedByIds, enumOptionSelectedValue, enumOptionValueDecoder, enumOptionValueEncoder, getOptionValueFormat, optionId, } from '@rjsf/utils';
import { Checkbox } from 'antd';
/** 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({ autofocus, disabled, registry, id, htmlName, onBlur, onChange, onFocus, options, readonly, value, }) {
const { formContext } = registry;
const { readonlyAsDisabled = true } = formContext;
const { enumOptions, enumDisabled, inline, emptyValue } = options;
const optionValueFormat = getOptionValueFormat(options);
const handleChange = (nextValue) => onChange(enumOptionValueDecoder(nextValue, enumOptions, optionValueFormat, emptyValue));
const handleBlur = ({ target }) => onBlur(id, enumOptionValueDecoder(target.value, enumOptions, optionValueFormat, emptyValue));
const handleFocus = ({ target }) => onFocus(id, enumOptionValueDecoder(target.value, enumOptions, optionValueFormat, emptyValue));
// Antd's typescript definitions do not contain the following props that are actually necessary and, if provided,
// they are used, so hacking them in via by spreading `extraProps` on the component to avoid typescript errors
const extraProps = {
id,
onBlur: !readonly ? handleBlur : undefined,
onFocus: !readonly ? handleFocus : undefined,
};
const selectValue = enumOptionSelectedValue(value, enumOptions, true, optionValueFormat, []);
return Array.isArray(enumOptions) && enumOptions.length > 0 ? (_jsx(Checkbox.Group, { disabled: disabled || (readonlyAsDisabled && readonly), name: htmlName || id, onChange: !readonly ? handleChange : undefined, value: selectValue, ...extraProps, "aria-describedby": ariaDescribedByIds(id), children: Array.isArray(enumOptions) &&
enumOptions.map((option, i) => (_jsxs("span", { children: [_jsx(Checkbox, { id: optionId(id, i), name: htmlName || id, autoFocus: i === 0 ? autofocus : false, disabled: Array.isArray(enumDisabled) && enumDisabled.includes(option.value), value: enumOptionValueEncoder(option.value, i, optionValueFormat), children: option.label }), !inline && _jsx("br", {})] }, i))) })) : null;
}
//# sourceMappingURL=index.js.map