@rjsf/material-ui
Version:
Material UI 4 theme, fields and widgets for react-jsonschema-form
34 lines • 2.84 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import MenuItem from '@material-ui/core/MenuItem';
import TextField from '@material-ui/core/TextField';
import { ariaDescribedByIds, enumOptionsIndexForValue, enumOptionsValueForIndex, labelValue, } from '@rjsf/utils';
/** The `SelectWidget` is a widget for rendering dropdowns.
* It is typically used with string properties constrained with enum options.
*
* @param props - The `WidgetProps` for this component
*/
export default function SelectWidget({ schema, id, name, // remove this from textFieldProps
options, label, hideLabel, required, disabled, readonly, placeholder, value, multiple, autofocus, onChange, onBlur, onFocus, rawErrors = [], registry, uiSchema, hideError, formContext, ...textFieldProps }) {
const { enumOptions, enumDisabled, emptyValue: optEmptyVal } = options;
multiple = typeof multiple === 'undefined' ? false : !!multiple;
const emptyValue = multiple ? [] : '';
const isEmpty = typeof value === 'undefined' || (multiple && value.length < 1) || (!multiple && value === emptyValue);
const _onChange = ({ target: { value } }) => onChange(enumOptionsValueForIndex(value, enumOptions, optEmptyVal));
const _onBlur = ({ target }) => onBlur(id, enumOptionsValueForIndex(target && target.value, enumOptions, optEmptyVal));
const _onFocus = ({ target }) => onFocus(id, enumOptionsValueForIndex(target && target.value, enumOptions, optEmptyVal));
const selectedIndexes = enumOptionsIndexForValue(value, enumOptions, multiple);
const showPlaceholderOption = !multiple && schema.default === undefined;
return (_jsxs(TextField, { id: id, name: id, label: labelValue(label, hideLabel || !label, false), value: !isEmpty && typeof selectedIndexes !== 'undefined' ? selectedIndexes : emptyValue, required: required, disabled: disabled || readonly, autoFocus: autofocus, placeholder: placeholder, error: rawErrors.length > 0, onChange: _onChange, onBlur: _onBlur, onFocus: _onFocus, ...textFieldProps, select // Apply this and the following props after the potential overrides defined in textFieldProps
: true, InputLabelProps: {
...textFieldProps.InputLabelProps,
shrink: !isEmpty,
}, SelectProps: {
...textFieldProps.SelectProps,
multiple,
}, "aria-describedby": ariaDescribedByIds(id), children: [showPlaceholderOption && _jsx(MenuItem, { value: '', children: placeholder }), Array.isArray(enumOptions) &&
enumOptions.map(({ value, label }, i) => {
const disabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1;
return (_jsx(MenuItem, { value: String(i), disabled: disabled, children: label }, i));
})] }));
}
//# sourceMappingURL=SelectWidget.js.map