@rjsf/core 5.0.0-beta.18 → 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.
@@ -940,7 +940,10 @@ function BooleanField(props) {
940
940
  });
941
941
  }
942
942
 
943
- var _excluded$7 = ["widget", "placeholder", "autofocus", "autocomplete"];
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";
944
947
  /** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
945
948
  * the currently selected option and cleans up any irrelevant data in `formData`.
946
949
  *
@@ -956,19 +959,20 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
956
959
  var _this;
957
960
  _this = _Component.call(this, props) || this;
958
961
  _this.onOptionChange = function (option) {
959
- var selectedOption = _this.state.selectedOption;
962
+ var _this$state = _this.state,
963
+ selectedOption = _this$state.selectedOption,
964
+ retrievedOptions = _this$state.retrievedOptions;
960
965
  var _this$props = _this.props,
961
966
  formData = _this$props.formData,
962
967
  onChange = _this$props.onChange,
963
- options = _this$props.options,
964
968
  registry = _this$props.registry;
965
969
  var schemaUtils = registry.schemaUtils;
966
970
  var intOption = option !== undefined ? parseInt(option, 10) : -1;
967
971
  if (intOption === selectedOption) {
968
972
  return;
969
973
  }
970
- var newOption = intOption >= 0 ? schemaUtils.retrieveSchema(options[intOption], formData) : undefined;
971
- var oldOption = selectedOption >= 0 ? schemaUtils.retrieveSchema(options[selectedOption], formData) : undefined;
974
+ var newOption = intOption >= 0 ? retrievedOptions[intOption] : undefined;
975
+ var oldOption = selectedOption >= 0 ? retrievedOptions[selectedOption] : undefined;
972
976
  var newFormData = schemaUtils.sanitizeDataForNewSchema(newOption, oldOption, formData);
973
977
  if (newFormData && newOption) {
974
978
  // Call getDefaultFormState to make sure defaults are populated on change. Pass "excludeObjectChildren"
@@ -982,13 +986,19 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
982
986
  };
983
987
  var _this$props2 = _this.props,
984
988
  _formData = _this$props2.formData,
985
- _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
+ });
986
995
  _this.state = {
987
- selectedOption: _this.getMatchingOption(0, _formData, _options)
996
+ retrievedOptions: _retrievedOptions,
997
+ selectedOption: _this.getMatchingOption(0, _formData, _retrievedOptions)
988
998
  };
989
999
  return _this;
990
1000
  }
991
- /** 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
992
1002
  * currently selected option based on the overall `formData`
993
1003
  *
994
1004
  * @param prevProps - The previous `FieldProps` for this template
@@ -1001,14 +1011,31 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1001
1011
  options = _this$props3.options,
1002
1012
  idSchema = _this$props3.idSchema;
1003
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
+ }
1004
1026
  if (!utils.deepEquals(formData, prevProps.formData) && idSchema.$id === prevProps.idSchema.$id) {
1005
- var matchingOption = this.getMatchingOption(selectedOption, formData, options);
1006
- if (!prevState || matchingOption === selectedOption) {
1007
- 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
+ };
1008
1035
  }
1009
- this.setState({
1010
- selectedOption: matchingOption
1011
- });
1036
+ }
1037
+ if (newState !== this.state) {
1038
+ this.setState(newState);
1012
1039
  }
1013
1040
  }
1014
1041
  /** Determines the best matching option for the given `formData` and `options`.
@@ -1031,7 +1058,7 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1031
1058
  * to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
1032
1059
  * the `onChange` handler.
1033
1060
  *
1034
- * @param option -
1061
+ * @param option - The new option value being selected
1035
1062
  */;
1036
1063
  _proto.getFieldId = function getFieldId() {
1037
1064
  var _this$props4 = this.props,
@@ -1051,26 +1078,30 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1051
1078
  formContext = _this$props5.formContext,
1052
1079
  onBlur = _this$props5.onBlur,
1053
1080
  onFocus = _this$props5.onFocus,
1054
- options = _this$props5.options,
1055
1081
  registry = _this$props5.registry,
1082
+ schema = _this$props5.schema,
1056
1083
  uiSchema = _this$props5.uiSchema;
1057
1084
  var widgets = registry.widgets,
1058
1085
  fields = registry.fields;
1059
1086
  var _SchemaField = fields.SchemaField;
1060
- var selectedOption = this.state.selectedOption;
1087
+ var _this$state2 = this.state,
1088
+ selectedOption = _this$state2.selectedOption,
1089
+ retrievedOptions = _this$state2.retrievedOptions;
1061
1090
  var _getUiOptions = utils.getUiOptions(uiSchema),
1062
1091
  _getUiOptions$widget = _getUiOptions.widget,
1063
1092
  widget = _getUiOptions$widget === void 0 ? "select" : _getUiOptions$widget,
1064
1093
  placeholder = _getUiOptions.placeholder,
1065
1094
  autofocus = _getUiOptions.autofocus,
1066
1095
  autocomplete = _getUiOptions.autocomplete,
1096
+ _getUiOptions$title = _getUiOptions.title,
1097
+ title = _getUiOptions$title === void 0 ? schema.title : _getUiOptions$title,
1067
1098
  uiOptions = _objectWithoutPropertiesLoose(_getUiOptions, _excluded$7);
1068
1099
  var Widget = utils.getWidget({
1069
1100
  type: "number"
1070
1101
  }, widget, widgets);
1071
1102
  var rawErrors = get__default["default"](errorSchema, utils.ERRORS_KEY, []);
1072
1103
  var fieldErrorSchema = omit__default["default"](errorSchema, [utils.ERRORS_KEY]);
1073
- var option = options[selectedOption] || null;
1104
+ var option = retrievedOptions[selectedOption] || null;
1074
1105
  var optionSchema;
1075
1106
  if (option) {
1076
1107
  // If the subschema doesn't declare a type, infer the type from the
@@ -1079,9 +1110,10 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1079
1110
  type: baseType
1080
1111
  });
1081
1112
  }
1082
- 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) {
1083
1115
  return {
1084
- label: option.title || "Option " + (index + 1),
1116
+ label: opt.title || optionLabel + " " + (index + 1),
1085
1117
  value: index
1086
1118
  };
1087
1119
  });
@@ -1657,7 +1689,8 @@ function SchemaFieldRender(props) {
1657
1689
  };
1658
1690
  var _AnyOfField = registry.fields.AnyOfField;
1659
1691
  var _OneOfField = registry.fields.OneOfField;
1660
- 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, {
1661
1694
  name: name,
1662
1695
  disabled: disabled,
1663
1696
  readonly: readonly,
@@ -1678,7 +1711,7 @@ function SchemaFieldRender(props) {
1678
1711
  registry: registry,
1679
1712
  schema: schema,
1680
1713
  uiSchema: uiSchema
1681
- }), 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, {
1682
1715
  name: name,
1683
1716
  disabled: disabled,
1684
1717
  readonly: readonly,