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

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,7 @@ function BooleanField(props) {
971
940
  });
972
941
  }
973
942
 
974
- var _excluded$7 = ["widget"];
943
+ var _excluded$7 = ["widget", "placeholder", "autofocus", "autocomplete"];
975
944
  /** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
976
945
  * the currently selected option and cleans up any irrelevant data in `formData`.
977
946
  *
@@ -987,38 +956,28 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
987
956
  var _this;
988
957
  _this = _Component.call(this, props) || this;
989
958
  _this.onOptionChange = function (option) {
990
- var selectedOption = parseInt(option, 10);
959
+ var selectedOption = _this.state.selectedOption;
991
960
  var _this$props = _this.props,
992
961
  formData = _this$props.formData,
993
962
  onChange = _this$props.onChange,
994
963
  options = _this$props.options,
995
964
  registry = _this$props.registry;
996
965
  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
- }
966
+ var intOption = option !== undefined ? parseInt(option, 10) : -1;
967
+ if (intOption === selectedOption) {
968
+ return;
969
+ }
970
+ var newOption = intOption >= 0 ? schemaUtils.retrieveSchema(options[intOption], formData) : undefined;
971
+ var oldOption = selectedOption >= 0 ? schemaUtils.retrieveSchema(options[selectedOption], formData) : undefined;
972
+ var newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
973
+ if (newFormData && newOption) {
974
+ // Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
975
+ // so that only the root objects themselves are created without adding undefined children properties
976
+ newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, "excludeObjectChildren");
1016
977
  }
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());
978
+ onChange(newFormData, undefined, _this.getFieldId());
1020
979
  _this.setState({
1021
- selectedOption: parseInt(option, 10)
980
+ selectedOption: intOption
1022
981
  });
1023
982
  };
1024
983
  var _this$props2 = _this.props,
@@ -1060,8 +1019,8 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1060
1019
  */;
1061
1020
  _proto.getMatchingOption = function getMatchingOption(selectedOption, formData, options) {
1062
1021
  var schemaUtils = this.props.registry.schemaUtils;
1063
- var option = schemaUtils.getMatchingOption(formData, options);
1064
- if (option !== 0) {
1022
+ var option = schemaUtils.getClosestMatchingOption(formData, options, selectedOption);
1023
+ if (option > 0) {
1065
1024
  return option;
1066
1025
  }
1067
1026
  // If the form data matches none of the options, use the currently selected
@@ -1084,23 +1043,13 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1084
1043
  */;
1085
1044
  _proto.render = function render() {
1086
1045
  var _this$props5 = this.props,
1087
- name = _this$props5.name,
1088
1046
  baseType = _this$props5.baseType,
1089
1047
  _this$props5$disabled = _this$props5.disabled,
1090
1048
  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
1049
  _this$props5$errorSch = _this$props5.errorSchema,
1096
1050
  errorSchema = _this$props5$errorSch === void 0 ? {} : _this$props5$errorSch,
1097
- formData = _this$props5.formData,
1098
1051
  formContext = _this$props5.formContext,
1099
- idPrefix = _this$props5.idPrefix,
1100
- idSeparator = _this$props5.idSeparator,
1101
- idSchema = _this$props5.idSchema,
1102
1052
  onBlur = _this$props5.onBlur,
1103
- onChange = _this$props5.onChange,
1104
1053
  onFocus = _this$props5.onFocus,
1105
1054
  options = _this$props5.options,
1106
1055
  registry = _this$props5.registry,
@@ -1112,10 +1061,15 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1112
1061
  var _getUiOptions = utils.getUiOptions(uiSchema),
1113
1062
  _getUiOptions$widget = _getUiOptions.widget,
1114
1063
  widget = _getUiOptions$widget === void 0 ? "select" : _getUiOptions$widget,
1064
+ placeholder = _getUiOptions.placeholder,
1065
+ autofocus = _getUiOptions.autofocus,
1066
+ autocomplete = _getUiOptions.autocomplete,
1115
1067
  uiOptions = _objectWithoutPropertiesLoose(_getUiOptions, _excluded$7);
1116
1068
  var Widget = utils.getWidget({
1117
1069
  type: "number"
1118
1070
  }, widget, widgets);
1071
+ var rawErrors = get__default["default"](errorSchema, utils.ERRORS_KEY, []);
1072
+ var fieldErrorSchema = omit__default["default"](errorSchema, [utils.ERRORS_KEY]);
1119
1073
  var option = options[selectedOption] || null;
1120
1074
  var optionSchema;
1121
1075
  if (option) {
@@ -1135,7 +1089,7 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1135
1089
  className: "panel panel-default panel-body"
1136
1090
  }, /*#__PURE__*/React__default["default"].createElement("div", {
1137
1091
  className: "form-group"
1138
- }, /*#__PURE__*/React__default["default"].createElement(Widget, _extends({
1092
+ }, /*#__PURE__*/React__default["default"].createElement(Widget, {
1139
1093
  id: this.getFieldId(),
1140
1094
  schema: {
1141
1095
  type: "number",
@@ -1144,32 +1098,23 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1144
1098
  onChange: this.onOptionChange,
1145
1099
  onBlur: onBlur,
1146
1100
  onFocus: onFocus,
1101
+ disabled: disabled || isEmpty__default["default"](enumOptions),
1102
+ multiple: false,
1103
+ rawErrors: rawErrors,
1104
+ errorSchema: fieldErrorSchema,
1147
1105
  value: selectedOption,
1148
- options: {
1106
+ options: _extends({
1149
1107
  enumOptions: enumOptions
1150
- },
1108
+ }, uiOptions),
1151
1109
  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
1110
  formContext: formContext,
1165
- onChange: onChange,
1166
- onBlur: onBlur,
1167
- onFocus: onFocus,
1168
- registry: registry,
1169
- disabled: disabled,
1170
- readonly: readonly,
1171
- hideError: hideError
1172
- }));
1111
+ placeholder: placeholder,
1112
+ autocomplete: autocomplete,
1113
+ autofocus: autofocus,
1114
+ label: ""
1115
+ })), option !== null && /*#__PURE__*/React__default["default"].createElement(_SchemaField, _extends({}, this.props, {
1116
+ schema: optionSchema
1117
+ })));
1173
1118
  };
1174
1119
  return AnyOfField;
1175
1120
  }(React.Component);
@@ -1348,12 +1293,16 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1348
1293
  var type = undefined;
1349
1294
  if (isObject__default["default"](schema.additionalProperties)) {
1350
1295
  type = schema.additionalProperties.type;
1351
- if (utils.REF_KEY in schema.additionalProperties) {
1296
+ var apSchema = schema.additionalProperties;
1297
+ if (utils.REF_KEY in apSchema) {
1352
1298
  var schemaUtils = registry.schemaUtils;
1353
- var refSchema = schemaUtils.retrieveSchema({
1354
- $ref: schema.additionalProperties[utils.REF_KEY]
1299
+ apSchema = schemaUtils.retrieveSchema({
1300
+ $ref: apSchema[utils.REF_KEY]
1355
1301
  }, formData);
1356
- type = refSchema.type;
1302
+ type = apSchema.type;
1303
+ }
1304
+ if (!type && (utils.ANY_OF_KEY in apSchema || utils.ONE_OF_KEY in apSchema)) {
1305
+ type = "object";
1357
1306
  }
1358
1307
  }
1359
1308
  var newKey = _this.getAvailableKey("newKey", newFormData);
@@ -3365,7 +3314,7 @@ var Form = /*#__PURE__*/function (_Component) {
3365
3314
  var formValue = get__default["default"](formData, path);
3366
3315
  // adds path to fieldNames if it points to a value
3367
3316
  // or an empty object/array
3368
- if (typeof formValue !== "object" || _isEmpty__default["default"](formValue)) {
3317
+ if (typeof formValue !== "object" || isEmpty__default["default"](formValue)) {
3369
3318
  acc.push(path);
3370
3319
  }
3371
3320
  });
@@ -3801,9 +3750,9 @@ function withTheme(themeProps) {
3801
3750
  widgets = _ref.widgets,
3802
3751
  templates = _ref.templates,
3803
3752
  directProps = _objectWithoutPropertiesLoose(_ref, _excluded);
3804
- fields = _extends({}, themeProps.fields, fields);
3805
- widgets = _extends({}, themeProps.widgets, widgets);
3806
- templates = _extends({}, themeProps.templates, templates, {
3753
+ fields = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.fields, fields);
3754
+ widgets = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.widgets, widgets);
3755
+ templates = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.templates, templates, {
3807
3756
  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
3757
  });
3809
3758
  return /*#__PURE__*/React__default["default"].createElement(Form, _extends({}, themeProps, directProps, {