@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.
package/dist/core.esm.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import React, { Component, useState, useCallback, useEffect, useReducer, useMemo, forwardRef } from 'react';
2
- import { isFixedItems, allowAdditionalItems, getUiOptions, ITEMS_KEY, getTemplate, isCustomWidget, getWidget, optionsList, guessType, deepEquals, asNumber, REF_KEY, orderProperties, PROPERTIES_KEY, ADDITIONAL_PROPERTY_FLAG, mergeObjects, UI_OPTIONS_KEY, descriptionId, getSchemaType, ID_KEY, hasWidget, titleId, getInputProps, examplesId, ariaDescribedByIds, getSubmitButtonOptions, errorId, helpId, canExpand, parseDateString, toDateString, pad, schemaRequiresTrueValue, optionId, enumOptionsSelectValue, enumOptionsDeselectValue, utcToLocal, localToUTC, dataURItoBlob, processSelectValue, createSchemaUtils, shouldRender, isObject as isObject$1, RJSF_ADDITONAL_PROPERTIES_FLAG, NAME_KEY } from '@rjsf/utils';
2
+ import { isFixedItems, allowAdditionalItems, getUiOptions, ITEMS_KEY, getTemplate, isCustomWidget, getWidget, optionsList, deepEquals, ERRORS_KEY, asNumber, REF_KEY, orderProperties, PROPERTIES_KEY, ADDITIONAL_PROPERTY_FLAG, ANY_OF_KEY, ONE_OF_KEY, mergeObjects, UI_OPTIONS_KEY, descriptionId, getSchemaType, ID_KEY, hasWidget, titleId, getInputProps, examplesId, ariaDescribedByIds, getSubmitButtonOptions, errorId, helpId, canExpand, parseDateString, toDateString, pad, schemaRequiresTrueValue, optionId, enumOptionsSelectValue, enumOptionsDeselectValue, utcToLocal, localToUTC, dataURItoBlob, processSelectValue, createSchemaUtils, shouldRender, isObject as isObject$1, RJSF_ADDITONAL_PROPERTIES_FLAG, NAME_KEY } from '@rjsf/utils';
3
3
  import get from 'lodash-es/get';
4
- import _isEmpty from 'lodash-es/isEmpty';
4
+ import isEmpty from 'lodash-es/isEmpty';
5
5
  import _pick from 'lodash-es/pick';
6
6
  import isObject from 'lodash-es/isObject';
7
7
  import set from 'lodash-es/set';
8
8
  import { nanoid } from 'nanoid';
9
+ import omit from 'lodash-es/omit';
9
10
  import has from 'lodash-es/has';
10
11
  import unset from 'lodash-es/unset';
11
- import omit from 'lodash-es/omit';
12
12
 
13
13
  function _defineProperties(target, props) {
14
14
  for (var i = 0; i < props.length; i++) {
@@ -65,37 +65,6 @@ function _objectWithoutPropertiesLoose(source, excluded) {
65
65
  }
66
66
  return target;
67
67
  }
68
- function _unsupportedIterableToArray(o, minLen) {
69
- if (!o) return;
70
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
71
- var n = Object.prototype.toString.call(o).slice(8, -1);
72
- if (n === "Object" && o.constructor) n = o.constructor.name;
73
- if (n === "Map" || n === "Set") return Array.from(o);
74
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
75
- }
76
- function _arrayLikeToArray(arr, len) {
77
- if (len == null || len > arr.length) len = arr.length;
78
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
79
- return arr2;
80
- }
81
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
82
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
83
- if (it) return (it = it.call(o)).next.bind(it);
84
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
85
- if (it) o = it;
86
- var i = 0;
87
- return function () {
88
- if (i >= o.length) return {
89
- done: true
90
- };
91
- return {
92
- done: false,
93
- value: o[i++]
94
- };
95
- };
96
- }
97
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
98
- }
99
68
  function _toPrimitive(input, hint) {
100
69
  if (typeof input !== "object" || input === null) return input;
101
70
  var prim = input[Symbol.toPrimitive];
@@ -955,7 +924,7 @@ function BooleanField(props) {
955
924
  });
956
925
  }
957
926
 
958
- var _excluded$7 = ["widget"];
927
+ var _excluded$7 = ["widget", "placeholder", "autofocus", "autocomplete"];
959
928
  /** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
960
929
  * the currently selected option and cleans up any irrelevant data in `formData`.
961
930
  *
@@ -971,38 +940,28 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
971
940
  var _this;
972
941
  _this = _Component.call(this, props) || this;
973
942
  _this.onOptionChange = function (option) {
974
- var selectedOption = parseInt(option, 10);
943
+ var selectedOption = _this.state.selectedOption;
975
944
  var _this$props = _this.props,
976
945
  formData = _this$props.formData,
977
946
  onChange = _this$props.onChange,
978
947
  options = _this$props.options,
979
948
  registry = _this$props.registry;
980
949
  var schemaUtils = registry.schemaUtils;
981
- var newOption = schemaUtils.retrieveSchema(options[selectedOption], formData);
982
- // If the new option is of type object and the current data is an object,
983
- // discard properties added using the old option.
984
- var newFormData = undefined;
985
- if (guessType(formData) === "object" && (newOption.type === "object" || newOption.properties)) {
986
- newFormData = Object.assign({}, formData);
987
- var optionsToDiscard = options.slice();
988
- optionsToDiscard.splice(selectedOption, 1);
989
- // Discard any data added using other options
990
- for (var _iterator = _createForOfIteratorHelperLoose(optionsToDiscard), _step; !(_step = _iterator()).done;) {
991
- var _option = _step.value;
992
- if (_option.properties) {
993
- for (var key in _option.properties) {
994
- if (has(newFormData, key)) {
995
- unset(newFormData, key);
996
- }
997
- }
998
- }
999
- }
950
+ var intOption = option !== undefined ? parseInt(option, 10) : -1;
951
+ if (intOption === selectedOption) {
952
+ return;
1000
953
  }
1001
- // Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
1002
- // so that only the root objects themselves are created without adding undefined children properties
1003
- onChange(schemaUtils.getDefaultFormState(options[selectedOption], newFormData, "excludeObjectChildren"), undefined, _this.getFieldId());
954
+ var newOption = intOption >= 0 ? schemaUtils.retrieveSchema(options[intOption], formData) : undefined;
955
+ var oldOption = selectedOption >= 0 ? schemaUtils.retrieveSchema(options[selectedOption], formData) : undefined;
956
+ var newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
957
+ if (newFormData && newOption) {
958
+ // Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
959
+ // so that only the root objects themselves are created without adding undefined children properties
960
+ newFormData = schemaUtils.getDefaultFormState(newOption, newFormData, "excludeObjectChildren");
961
+ }
962
+ onChange(newFormData, undefined, _this.getFieldId());
1004
963
  _this.setState({
1005
- selectedOption: parseInt(option, 10)
964
+ selectedOption: intOption
1006
965
  });
1007
966
  };
1008
967
  var _this$props2 = _this.props,
@@ -1044,8 +1003,8 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1044
1003
  */;
1045
1004
  _proto.getMatchingOption = function getMatchingOption(selectedOption, formData, options) {
1046
1005
  var schemaUtils = this.props.registry.schemaUtils;
1047
- var option = schemaUtils.getMatchingOption(formData, options);
1048
- if (option !== 0) {
1006
+ var option = schemaUtils.getClosestMatchingOption(formData, options, selectedOption);
1007
+ if (option > 0) {
1049
1008
  return option;
1050
1009
  }
1051
1010
  // If the form data matches none of the options, use the currently selected
@@ -1068,23 +1027,13 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1068
1027
  */;
1069
1028
  _proto.render = function render() {
1070
1029
  var _this$props5 = this.props,
1071
- name = _this$props5.name,
1072
1030
  baseType = _this$props5.baseType,
1073
1031
  _this$props5$disabled = _this$props5.disabled,
1074
1032
  disabled = _this$props5$disabled === void 0 ? false : _this$props5$disabled,
1075
- _this$props5$readonly = _this$props5.readonly,
1076
- readonly = _this$props5$readonly === void 0 ? false : _this$props5$readonly,
1077
- _this$props5$hideErro = _this$props5.hideError,
1078
- hideError = _this$props5$hideErro === void 0 ? false : _this$props5$hideErro,
1079
1033
  _this$props5$errorSch = _this$props5.errorSchema,
1080
1034
  errorSchema = _this$props5$errorSch === void 0 ? {} : _this$props5$errorSch,
1081
- formData = _this$props5.formData,
1082
1035
  formContext = _this$props5.formContext,
1083
- idPrefix = _this$props5.idPrefix,
1084
- idSeparator = _this$props5.idSeparator,
1085
- idSchema = _this$props5.idSchema,
1086
1036
  onBlur = _this$props5.onBlur,
1087
- onChange = _this$props5.onChange,
1088
1037
  onFocus = _this$props5.onFocus,
1089
1038
  options = _this$props5.options,
1090
1039
  registry = _this$props5.registry,
@@ -1096,10 +1045,15 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1096
1045
  var _getUiOptions = getUiOptions(uiSchema),
1097
1046
  _getUiOptions$widget = _getUiOptions.widget,
1098
1047
  widget = _getUiOptions$widget === void 0 ? "select" : _getUiOptions$widget,
1048
+ placeholder = _getUiOptions.placeholder,
1049
+ autofocus = _getUiOptions.autofocus,
1050
+ autocomplete = _getUiOptions.autocomplete,
1099
1051
  uiOptions = _objectWithoutPropertiesLoose(_getUiOptions, _excluded$7);
1100
1052
  var Widget = getWidget({
1101
1053
  type: "number"
1102
1054
  }, widget, widgets);
1055
+ var rawErrors = get(errorSchema, ERRORS_KEY, []);
1056
+ var fieldErrorSchema = omit(errorSchema, [ERRORS_KEY]);
1103
1057
  var option = options[selectedOption] || null;
1104
1058
  var optionSchema;
1105
1059
  if (option) {
@@ -1119,7 +1073,7 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1119
1073
  className: "panel panel-default panel-body"
1120
1074
  }, /*#__PURE__*/React.createElement("div", {
1121
1075
  className: "form-group"
1122
- }, /*#__PURE__*/React.createElement(Widget, _extends({
1076
+ }, /*#__PURE__*/React.createElement(Widget, {
1123
1077
  id: this.getFieldId(),
1124
1078
  schema: {
1125
1079
  type: "number",
@@ -1128,32 +1082,23 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1128
1082
  onChange: this.onOptionChange,
1129
1083
  onBlur: onBlur,
1130
1084
  onFocus: onFocus,
1085
+ disabled: disabled || isEmpty(enumOptions),
1086
+ multiple: false,
1087
+ rawErrors: rawErrors,
1088
+ errorSchema: fieldErrorSchema,
1131
1089
  value: selectedOption,
1132
- options: {
1090
+ options: _extends({
1133
1091
  enumOptions: enumOptions
1134
- },
1092
+ }, uiOptions),
1135
1093
  registry: registry,
1136
- formContext: formContext
1137
- }, uiOptions, {
1138
- label: ""
1139
- }))), option !== null && /*#__PURE__*/React.createElement(_SchemaField, {
1140
- name: name,
1141
- schema: optionSchema,
1142
- uiSchema: uiSchema,
1143
- errorSchema: errorSchema,
1144
- idSchema: idSchema,
1145
- idPrefix: idPrefix,
1146
- idSeparator: idSeparator,
1147
- formData: formData,
1148
1094
  formContext: formContext,
1149
- onChange: onChange,
1150
- onBlur: onBlur,
1151
- onFocus: onFocus,
1152
- registry: registry,
1153
- disabled: disabled,
1154
- readonly: readonly,
1155
- hideError: hideError
1156
- }));
1095
+ placeholder: placeholder,
1096
+ autocomplete: autocomplete,
1097
+ autofocus: autofocus,
1098
+ label: ""
1099
+ })), option !== null && /*#__PURE__*/React.createElement(_SchemaField, _extends({}, this.props, {
1100
+ schema: optionSchema
1101
+ })));
1157
1102
  };
1158
1103
  return AnyOfField;
1159
1104
  }(Component);
@@ -1332,12 +1277,16 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1332
1277
  var type = undefined;
1333
1278
  if (isObject(schema.additionalProperties)) {
1334
1279
  type = schema.additionalProperties.type;
1335
- if (REF_KEY in schema.additionalProperties) {
1280
+ var apSchema = schema.additionalProperties;
1281
+ if (REF_KEY in apSchema) {
1336
1282
  var schemaUtils = registry.schemaUtils;
1337
- var refSchema = schemaUtils.retrieveSchema({
1338
- $ref: schema.additionalProperties[REF_KEY]
1283
+ apSchema = schemaUtils.retrieveSchema({
1284
+ $ref: apSchema[REF_KEY]
1339
1285
  }, formData);
1340
- type = refSchema.type;
1286
+ type = apSchema.type;
1287
+ }
1288
+ if (!type && (ANY_OF_KEY in apSchema || ONE_OF_KEY in apSchema)) {
1289
+ type = "object";
1341
1290
  }
1342
1291
  }
1343
1292
  var newKey = _this.getAvailableKey("newKey", newFormData);
@@ -3349,7 +3298,7 @@ var Form = /*#__PURE__*/function (_Component) {
3349
3298
  var formValue = get(formData, path);
3350
3299
  // adds path to fieldNames if it points to a value
3351
3300
  // or an empty object/array
3352
- if (typeof formValue !== "object" || _isEmpty(formValue)) {
3301
+ if (typeof formValue !== "object" || isEmpty(formValue)) {
3353
3302
  acc.push(path);
3354
3303
  }
3355
3304
  });
@@ -3785,9 +3734,9 @@ function withTheme(themeProps) {
3785
3734
  widgets = _ref.widgets,
3786
3735
  templates = _ref.templates,
3787
3736
  directProps = _objectWithoutPropertiesLoose(_ref, _excluded);
3788
- fields = _extends({}, themeProps.fields, fields);
3789
- widgets = _extends({}, themeProps.widgets, widgets);
3790
- templates = _extends({}, themeProps.templates, templates, {
3737
+ fields = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.fields, fields);
3738
+ widgets = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.widgets, widgets);
3739
+ templates = _extends({}, themeProps === null || themeProps === void 0 ? void 0 : themeProps.templates, templates, {
3791
3740
  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)
3792
3741
  });
3793
3742
  return /*#__PURE__*/React.createElement(Form, _extends({}, themeProps, directProps, {