UNPKG

@astarte-platform/react-bootstrap

Version:

React Bootstrap theme, fields and widgets for react-jsonschema-form, powered by react-bootstrap, this is a package from a fork of official project

35 lines 2.25 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import FormSelect from 'react-bootstrap/FormSelect'; import { ariaDescribedByIds, enumOptionsIndexForValue, enumOptionsValueForIndex, } from '@rjsf/utils'; export default function SelectWidget({ schema, id, options, required, disabled, readonly, value, multiple, autofocus, onChange, onBlur, onFocus, placeholder, rawErrors = [], }) { const { enumOptions, enumDisabled, emptyValue: optEmptyValue } = options; const emptyValue = multiple ? [] : ''; function getValue(event, multiple) { if (multiple) { return [].slice .call(event.target.options) .filter((o) => o.selected) .map((o) => o.value); } else { return event.target.value; } } const selectedIndexes = enumOptionsIndexForValue(value, enumOptions, multiple); return (_jsxs(FormSelect, { id: id, name: id, value: typeof selectedIndexes === 'undefined' ? emptyValue : selectedIndexes, required: required, multiple: multiple, disabled: disabled || readonly, autoFocus: autofocus, className: rawErrors.length > 0 ? 'is-invalid' : '', onBlur: onBlur && ((event) => { const newValue = getValue(event, multiple); onBlur(id, enumOptionsValueForIndex(newValue, enumOptions, optEmptyValue)); }), onFocus: onFocus && ((event) => { const newValue = getValue(event, multiple); onFocus(id, enumOptionsValueForIndex(newValue, enumOptions, optEmptyValue)); }), onChange: (event) => { const newValue = getValue(event, multiple); onChange(enumOptionsValueForIndex(newValue, enumOptions, optEmptyValue)); }, "aria-describedby": ariaDescribedByIds(id), children: [!multiple && schema.default === undefined && _jsx("option", { value: '', children: placeholder }), enumOptions.map(({ value, label }, i) => { const disabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(value) != -1; return (_jsx("option", { id: label, value: String(i), disabled: disabled, children: label }, i)); })] })); } //# sourceMappingURL=SelectWidget.js.map