@rjsf/core 5.0.0-beta.17 → 5.0.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,26 +5,26 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var React = require('react');
6
6
  var utils = require('@rjsf/utils');
7
7
  var get = require('lodash/get');
8
- var _isEmpty = require('lodash/isEmpty');
8
+ var isEmpty = require('lodash/isEmpty');
9
9
  var _pick = require('lodash/pick');
10
10
  var isObject = require('lodash/isObject');
11
11
  var set = require('lodash/set');
12
12
  var nanoid = require('nanoid');
13
+ var omit = require('lodash/omit');
13
14
  var has = require('lodash/has');
14
15
  var unset = require('lodash/unset');
15
- var omit = require('lodash/omit');
16
16
 
17
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
18
 
19
19
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
20
20
  var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
21
- var _isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(_isEmpty);
21
+ var isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(isEmpty);
22
22
  var _pick__default = /*#__PURE__*/_interopDefaultLegacy(_pick);
23
23
  var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
24
24
  var set__default = /*#__PURE__*/_interopDefaultLegacy(set);
25
+ var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
25
26
  var has__default = /*#__PURE__*/_interopDefaultLegacy(has);
26
27
  var unset__default = /*#__PURE__*/_interopDefaultLegacy(unset);
27
- var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
28
28
 
29
29
  function _defineProperties(target, props) {
30
30
  for (var i = 0; i < props.length; i++) {
@@ -81,37 +81,6 @@ function _objectWithoutPropertiesLoose(source, excluded) {
81
81
  }
82
82
  return target;
83
83
  }
84
- function _unsupportedIterableToArray(o, minLen) {
85
- if (!o) return;
86
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
87
- var n = Object.prototype.toString.call(o).slice(8, -1);
88
- if (n === "Object" && o.constructor) n = o.constructor.name;
89
- if (n === "Map" || n === "Set") return Array.from(o);
90
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
91
- }
92
- function _arrayLikeToArray(arr, len) {
93
- if (len == null || len > arr.length) len = arr.length;
94
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
95
- return arr2;
96
- }
97
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
98
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
99
- if (it) return (it = it.call(o)).next.bind(it);
100
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
101
- if (it) o = it;
102
- var i = 0;
103
- return function () {
104
- if (i >= o.length) return {
105
- done: true
106
- };
107
- return {
108
- done: false,
109
- value: o[i++]
110
- };
111
- };
112
- }
113
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
114
- }
115
84
  function _toPrimitive(input, hint) {
116
85
  if (typeof input !== "object" || input === null) return input;
117
86
  var prim = input[Symbol.toPrimitive];
@@ -971,7 +940,10 @@ function BooleanField(props) {
971
940
  });
972
941
  }
973
942
 
974
- var _excluded$7 = ["widget"];
943
+ var _excluded$7 = ["widget", "placeholder", "autofocus", "autocomplete", "title"];
944
+ /** The prefix used when a oneOf option does not have a title
945
+ */
946
+ var UNKNOWN_OPTION_PREFIX = "Option";
975
947
  /** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
976
948
  * the currently selected option and cleans up any irrelevant data in `formData`.
977
949
  *
@@ -987,49 +959,46 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
987
959
  var _this;
988
960
  _this = _Component.call(this, props) || this;
989
961
  _this.onOptionChange = function (option) {
990
- var selectedOption = parseInt(option, 10);
962
+ var _this$state = _this.state,
963
+ selectedOption = _this$state.selectedOption,
964
+ retrievedOptions = _this$state.retrievedOptions;
991
965
  var _this$props = _this.props,
992
966
  formData = _this$props.formData,
993
967
  onChange = _this$props.onChange,
994
- options = _this$props.options,
995
968
  registry = _this$props.registry;
996
969
  var schemaUtils = registry.schemaUtils;
997
- var newOption = schemaUtils.retrieveSchema(options[selectedOption], formData);
998
- // If the new option is of type object and the current data is an object,
999
- // discard properties added using the old option.
1000
- var newFormData = undefined;
1001
- if (utils.guessType(formData) === "object" && (newOption.type === "object" || newOption.properties)) {
1002
- newFormData = Object.assign({}, formData);
1003
- var optionsToDiscard = options.slice();
1004
- optionsToDiscard.splice(selectedOption, 1);
1005
- // Discard any data added using other options
1006
- for (var _iterator = _createForOfIteratorHelperLoose(optionsToDiscard), _step; !(_step = _iterator()).done;) {
1007
- var _option = _step.value;
1008
- if (_option.properties) {
1009
- for (var key in _option.properties) {
1010
- if (has__default["default"](newFormData, key)) {
1011
- unset__default["default"](newFormData, key);
1012
- }
1013
- }
1014
- }
1015
- }
970
+ var intOption = option !== undefined ? parseInt(option, 10) : -1;
971
+ if (intOption === selectedOption) {
972
+ return;
973
+ }
974
+ var newOption = intOption >= 0 ? retrievedOptions[intOption] : undefined;
975
+ var oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : undefined;
976
+ var newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
977
+ if (newFormData && newOption) {
978
+ // Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
979
+ // so that only the root objects themselves are created without adding undefined children properties
980
+ newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, "excludeObjectChildren");
1016
981
  }
1017
- // Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
1018
- // so that only the root objects themselves are created without adding undefined children properties
1019
- onChange(schemaUtils.getDefaultFormState(options[selectedOption], newFormData, "excludeObjectChildren"), undefined, _this.getFieldId());
982
+ onChange(newFormData, undefined, _this.getFieldId());
1020
983
  _this.setState({
1021
- selectedOption: parseInt(option, 10)
984
+ selectedOption: intOption
1022
985
  });
1023
986
  };
1024
987
  var _this$props2 = _this.props,
1025
988
  _formData = _this$props2.formData,
1026
- _options = _this$props2.options;
989
+ options = _this$props2.options,
990
+ _schemaUtils = _this$props2.registry.schemaUtils;
991
+ // cache the retrieved options in state in case they have $refs to save doing it later
992
+ var _retrievedOptions = options.map(function (opt) {
993
+ return _schemaUtils.retrieveSchema(opt, _formData);
994
+ });
1027
995
  _this.state = {
1028
- selectedOption: _this.getMatchingOption(0, _formData, _options)
996
+ retrievedOptions: _retrievedOptions,
997
+ selectedOption: _this.getMatchingOption(0, _formData, _retrievedOptions)
1029
998
  };
1030
999
  return _this;
1031
1000
  }
1032
- /** React lifecycle methos that is called when the props and/or state for this component is updated. It recomputes the
1001
+ /** React lifecycle method that is called when the props and/or state for this component is updated. It recomputes the
1033
1002
  * currently selected option based on the overall `formData`
1034
1003
  *
1035
1004
  * @param prevProps - The previous `FieldProps` for this template
@@ -1042,14 +1011,31 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1042
1011
  options = _this$props3.options,
1043
1012
  idSchema = _this$props3.idSchema;
1044
1013
  var selectedOption = this.state.selectedOption;
1014
+ var newState = this.state;
1015
+ if (!utils.deepEquals(prevProps.options, options)) {
1016
+ var schemaUtils = this.props.registry.schemaUtils;
1017
+ // re-cache the retrieved options in state in case they have $refs to save doing it later
1018
+ var retrievedOptions = options.map(function (opt) {
1019
+ return schemaUtils.retrieveSchema(opt, formData);
1020
+ });
1021
+ newState = {
1022
+ selectedOption: selectedOption,
1023
+ retrievedOptions: retrievedOptions
1024
+ };
1025
+ }
1045
1026
  if (!utils.deepEquals(formData, prevProps.formData) && idSchema.$id === prevProps.idSchema.$id) {
1046
- var matchingOption = this.getMatchingOption(selectedOption, formData, options);
1047
- if (!prevState || matchingOption === selectedOption) {
1048
- return;
1027
+ var _newState = newState,
1028
+ _retrievedOptions2 = _newState.retrievedOptions;
1029
+ var matchingOption = this.getMatchingOption(selectedOption, formData, _retrievedOptions2);
1030
+ if (prevState && matchingOption !== selectedOption) {
1031
+ newState = {
1032
+ selectedOption: matchingOption,
1033
+ retrievedOptions: _retrievedOptions2
1034
+ };
1049
1035
  }
1050
- this.setState({
1051
- selectedOption: matchingOption
1052
- });
1036
+ }
1037
+ if (newState !== this.state) {
1038
+ this.setState(newState);
1053
1039
  }
1054
1040
  }
1055
1041
  /** Determines the best matching option for the given `formData` and `options`.
@@ -1060,8 +1046,8 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1060
1046
  */;
1061
1047
  _proto.getMatchingOption = function getMatchingOption(selectedOption, formData, options) {
1062
1048
  var schemaUtils = this.props.registry.schemaUtils;
1063
- var option = schemaUtils.getMatchingOption(formData, options);
1064
- if (option !== 0) {
1049
+ var option = schemaUtils.getClosestMatchingOption(formData, options, selectedOption);
1050
+ if (option > 0) {
1065
1051
  return option;
1066
1052
  }
1067
1053
  // If the form data matches none of the options, use the currently selected
@@ -1072,7 +1058,7 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1072
1058
  * to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
1073
1059
  * the `onChange` handler.
1074
1060
  *
1075
- * @param option -
1061
+ * @param option - The new option value being selected
1076
1062
  */;
1077
1063
  _proto.getFieldId = function getFieldId() {
1078
1064
  var _this$props4 = this.props,
@@ -1084,39 +1070,38 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1084
1070
  */;
1085
1071
  _proto.render = function render() {
1086
1072
  var _this$props5 = this.props,
1087
- name = _this$props5.name,
1088
1073
  baseType = _this$props5.baseType,
1089
1074
  _this$props5$disabled = _this$props5.disabled,
1090
1075
  disabled = _this$props5$disabled === void 0 ? false : _this$props5$disabled,
1091
- _this$props5$readonly = _this$props5.readonly,
1092
- readonly = _this$props5$readonly === void 0 ? false : _this$props5$readonly,
1093
- _this$props5$hideErro = _this$props5.hideError,
1094
- hideError = _this$props5$hideErro === void 0 ? false : _this$props5$hideErro,
1095
1076
  _this$props5$errorSch = _this$props5.errorSchema,
1096
1077
  errorSchema = _this$props5$errorSch === void 0 ? {} : _this$props5$errorSch,
1097
- formData = _this$props5.formData,
1098
1078
  formContext = _this$props5.formContext,
1099
- idPrefix = _this$props5.idPrefix,
1100
- idSeparator = _this$props5.idSeparator,
1101
- idSchema = _this$props5.idSchema,
1102
1079
  onBlur = _this$props5.onBlur,
1103
- onChange = _this$props5.onChange,
1104
1080
  onFocus = _this$props5.onFocus,
1105
- options = _this$props5.options,
1106
1081
  registry = _this$props5.registry,
1082
+ schema = _this$props5.schema,
1107
1083
  uiSchema = _this$props5.uiSchema;
1108
1084
  var widgets = registry.widgets,
1109
1085
  fields = registry.fields;
1110
1086
  var _SchemaField = fields.SchemaField;
1111
- var selectedOption = this.state.selectedOption;
1087
+ var _this$state2 = this.state,
1088
+ selectedOption = _this$state2.selectedOption,
1089
+ retrievedOptions = _this$state2.retrievedOptions;
1112
1090
  var _getUiOptions = utils.getUiOptions(uiSchema),
1113
1091
  _getUiOptions$widget = _getUiOptions.widget,
1114
1092
  widget = _getUiOptions$widget === void 0 ? "select" : _getUiOptions$widget,
1093
+ placeholder = _getUiOptions.placeholder,
1094
+ autofocus = _getUiOptions.autofocus,
1095
+ autocomplete = _getUiOptions.autocomplete,
1096
+ _getUiOptions$title = _getUiOptions.title,
1097
+ title = _getUiOptions$title === void 0 ? schema.title : _getUiOptions$title,
1115
1098
  uiOptions = _objectWithoutPropertiesLoose(_getUiOptions, _excluded$7);
1116
1099
  var Widget = utils.getWidget({
1117
1100
  type: "number"
1118
1101
  }, widget, widgets);
1119
- var option = options[selectedOption] || null;
1102
+ var rawErrors = get__default["default"](errorSchema, utils.ERRORS_KEY, []);
1103
+ var fieldErrorSchema = omit__default["default"](errorSchema, [utils.ERRORS_KEY]);
1104
+ var option = retrievedOptions[selectedOption] || null;
1120
1105
  var optionSchema;
1121
1106
  if (option) {
1122
1107
  // If the subschema doesn't declare a type, infer the type from the
@@ -1125,9 +1110,10 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1125
1110
  type: baseType
1126
1111
  });
1127
1112
  }
1128
- var enumOptions = options.map(function (option, index) {
1113
+ var optionLabel = title ? title + " " + UNKNOWN_OPTION_PREFIX.toLowerCase() : UNKNOWN_OPTION_PREFIX;
1114
+ var enumOptions = retrievedOptions.map(function (opt, index) {
1129
1115
  return {
1130
- label: option.title || "Option " + (index + 1),
1116
+ label: opt.title || optionLabel + " " + (index + 1),
1131
1117
  value: index
1132
1118
  };
1133
1119
  });
@@ -1135,7 +1121,7 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1135
1121
  className: "panel panel-default panel-body"
1136
1122
  }, /*#__PURE__*/React__default["default"].createElement("div", {
1137
1123
  className: "form-group"
1138
- }, /*#__PURE__*/React__default["default"].createElement(Widget, _extends({
1124
+ }, /*#__PURE__*/React__default["default"].createElement(Widget, {
1139
1125
  id: this.getFieldId(),
1140
1126
  schema: {
1141
1127
  type: "number",
@@ -1144,32 +1130,23 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1144
1130
  onChange: this.onOptionChange,
1145
1131
  onBlur: onBlur,
1146
1132
  onFocus: onFocus,
1133
+ disabled: disabled || isEmpty__default["default"](enumOptions),
1134
+ multiple: false,
1135
+ rawErrors: rawErrors,
1136
+ errorSchema: fieldErrorSchema,
1147
1137
  value: selectedOption,
1148
- options: {
1138
+ options: _extends({
1149
1139
  enumOptions: enumOptions
1150
- },
1140
+ }, uiOptions),
1151
1141
  registry: registry,
1152
- formContext: formContext
1153
- }, uiOptions, {
1154
- label: ""
1155
- }))), option !== null && /*#__PURE__*/React__default["default"].createElement(_SchemaField, {
1156
- name: name,
1157
- schema: optionSchema,
1158
- uiSchema: uiSchema,
1159
- errorSchema: errorSchema,
1160
- idSchema: idSchema,
1161
- idPrefix: idPrefix,
1162
- idSeparator: idSeparator,
1163
- formData: formData,
1164
1142
  formContext: formContext,
1165
- onChange: onChange,
1166
- onBlur: onBlur,
1167
- onFocus: onFocus,
1168
- registry: registry,
1169
- disabled: disabled,
1170
- readonly: readonly,
1171
- hideError: hideError
1172
- }));
1143
+ placeholder: placeholder,
1144
+ autocomplete: autocomplete,
1145
+ autofocus: autofocus,
1146
+ label: ""
1147
+ })), option !== null && /*#__PURE__*/React__default["default"].createElement(_SchemaField, _extends({}, this.props, {
1148
+ schema: optionSchema
1149
+ })));
1173
1150
  };
1174
1151
  return AnyOfField;
1175
1152
  }(React.Component);
@@ -1348,12 +1325,16 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1348
1325
  var type = undefined;
1349
1326
  if (isObject__default["default"](schema.additionalProperties)) {
1350
1327
  type = schema.additionalProperties.type;
1351
- if (utils.REF_KEY in schema.additionalProperties) {
1328
+ var apSchema = schema.additionalProperties;
1329
+ if (utils.REF_KEY in apSchema) {
1352
1330
  var schemaUtils = registry.schemaUtils;
1353
- var refSchema = schemaUtils.retrieveSchema({
1354
- $ref: schema.additionalProperties[utils.REF_KEY]
1331
+ apSchema = schemaUtils.retrieveSchema({
1332
+ $ref: apSchema[utils.REF_KEY]
1355
1333
  }, formData);
1356
- type = refSchema.type;
1334
+ type = apSchema.type;
1335
+ }
1336
+ if (!type && (utils.ANY_OF_KEY in apSchema || utils.ONE_OF_KEY in apSchema)) {
1337
+ type = "object";
1357
1338
  }
1358
1339
  }
1359
1340
  var newKey = _this.getAvailableKey("newKey", newFormData);
@@ -1708,7 +1689,8 @@ function SchemaFieldRender(props) {
1708
1689
  };
1709
1690
  var _AnyOfField = registry.fields.AnyOfField;
1710
1691
  var _OneOfField = registry.fields.OneOfField;
1711
- return /*#__PURE__*/React__default["default"].createElement(FieldTemplate, _extends({}, fieldProps), /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, field, schema.anyOf && !(uiSchema !== null && uiSchema !== void 0 && uiSchema["ui:field"]) && !schemaUtils.isSelect(schema) && /*#__PURE__*/React__default["default"].createElement(_AnyOfField, {
1692
+ var isReplacingAnyOrOneOf = (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:field"]) && (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:fieldReplacesAnyOrOneOf"]) === true;
1693
+ return /*#__PURE__*/React__default["default"].createElement(FieldTemplate, _extends({}, fieldProps), /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, field, schema.anyOf && !isReplacingAnyOrOneOf && !schemaUtils.isSelect(schema) && /*#__PURE__*/React__default["default"].createElement(_AnyOfField, {
1712
1694
  name: name,
1713
1695
  disabled: disabled,
1714
1696
  readonly: readonly,
@@ -1729,7 +1711,7 @@ function SchemaFieldRender(props) {
1729
1711
  registry: registry,
1730
1712
  schema: schema,
1731
1713
  uiSchema: uiSchema
1732
- }), schema.oneOf && !(uiSchema !== null && uiSchema !== void 0 && uiSchema["ui:field"]) && !schemaUtils.isSelect(schema) && /*#__PURE__*/React__default["default"].createElement(_OneOfField, {
1714
+ }), schema.oneOf && !isReplacingAnyOrOneOf && !schemaUtils.isSelect(schema) && /*#__PURE__*/React__default["default"].createElement(_OneOfField, {
1733
1715
  name: name,
1734
1716
  disabled: disabled,
1735
1717
  readonly: readonly,
@@ -3365,7 +3347,7 @@ var Form = /*#__PURE__*/function (_Component) {
3365
3347
  var formValue = get__default["default"](formData, path);
3366
3348
  // adds path to fieldNames if it points to a value
3367
3349
  // or an empty object/array
3368
- if (typeof formValue !== "object" || _isEmpty__default["default"](formValue)) {
3350
+ if (typeof formValue !== "object" || isEmpty__default["default"](formValue)) {
3369
3351
  acc.push(path);
3370
3352
  }
3371
3353
  });
@@ -3801,9 +3783,9 @@ function withTheme(themeProps) {
3801
3783
  widgets = _ref.widgets,
3802
3784
  templates = _ref.templates,
3803
3785
  directProps = _objectWithoutPropertiesLoose(_ref, _excluded);
3804
- fields = _extends({}, themeProps.fields, fields);
3805
- widgets = _extends({}, themeProps.widgets, widgets);
3806
- templates = _extends({}, themeProps.templates, templates, {
3786
+ fields = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.fields, fields);
3787
+ widgets = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.widgets, widgets);
3788
+ templates = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.templates, templates, {
3807
3789
  ButtonTemplates: _extends({}, themeProps === null || themeProps === void 0 ? void 0 : (_themeProps$templates = themeProps.templates) === null || _themeProps$templates === void 0 ? void 0 : _themeProps$templates.ButtonTemplates, (_templates = templates) === null || _templates === void 0 ? void 0 : _templates.ButtonTemplates)
3808
3790
  });
3809
3791
  return /*#__PURE__*/React__default["default"].createElement(Form, _extends({}, themeProps, directProps, {