@rjsf/mui
Version:
Material UI 5 theme, fields and widgets for react-jsonschema-form
27 lines • 2.28 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import FormControlLabel from '@mui/material/FormControlLabel/index.js';
import FormLabel from '@mui/material/FormLabel/index.js';
import Radio from '@mui/material/Radio/index.js';
import RadioGroup from '@mui/material/RadioGroup/index.js';
import { ariaDescribedByIds, enumOptionsIndexForValue, enumOptionsValueForIndex, labelValue, optionId, } from '@rjsf/utils';
/** The `RadioWidget` is a widget for rendering a radio group.
* It is typically used with a string property constrained with enum options.
*
* @param props - The `WidgetProps` for this component
*/
export default function RadioWidget({ id, options, value, required, disabled, readonly, label, hideLabel, onChange, onBlur, onFocus, }) {
var _a;
const { enumOptions, enumDisabled, emptyValue } = options;
const _onChange = (_, value) => onChange(enumOptionsValueForIndex(value, enumOptions, emptyValue));
const _onBlur = ({ target }) => onBlur(id, enumOptionsValueForIndex(target && target.value, enumOptions, emptyValue));
const _onFocus = ({ target }) => onFocus(id, enumOptionsValueForIndex(target && target.value, enumOptions, emptyValue));
const row = options ? options.inline : false;
const selectedIndex = (_a = enumOptionsIndexForValue(value, enumOptions)) !== null && _a !== void 0 ? _a : null;
return (_jsxs(_Fragment, { children: [labelValue(_jsx(FormLabel, { required: required, htmlFor: id, children: label || undefined }), hideLabel), _jsx(RadioGroup, { id: id, name: id, value: selectedIndex, row: row, onChange: _onChange, onBlur: _onBlur, onFocus: _onFocus, "aria-describedby": ariaDescribedByIds(id), children: Array.isArray(enumOptions) &&
enumOptions.map((option, index) => {
const itemDisabled = Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1;
const radio = (_jsx(FormControlLabel, { control: _jsx(Radio, { name: id, id: optionId(id, index), color: 'primary' }), label: option.label, value: String(index), disabled: disabled || itemDisabled || readonly }, index));
return radio;
}) })] }));
}
//# sourceMappingURL=RadioWidget.js.map