@rjsf/core 5.1.0 → 5.2.0

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,5 +1,5 @@
1
1
  import React, { Component, useState, useCallback, useEffect, useReducer, useMemo, forwardRef } from 'react';
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, enumOptionsValueForIndex, enumOptionsIsSelected, optionId, enumOptionsSelectValue, enumOptionsDeselectValue, utcToLocal, localToUTC, dataURItoBlob, enumOptionsIndexForValue, createSchemaUtils, shouldRender, isObject as isObject$1, RJSF_ADDITONAL_PROPERTIES_FLAG, NAME_KEY } from '@rjsf/utils';
2
+ import { isFixedItems, allowAdditionalItems, getUiOptions, ITEMS_KEY, getTemplate, TranslatableString, 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, enumOptionsValueForIndex, enumOptionsIsSelected, optionId, enumOptionsSelectValue, enumOptionsDeselectValue, utcToLocal, localToUTC, dataURItoBlob, enumOptionsIndexForValue, englishStringTranslator, createSchemaUtils, shouldRender, isObject as isObject$1, RJSF_ADDITONAL_PROPERTIES_FLAG, NAME_KEY } from '@rjsf/utils';
3
3
  import get from 'lodash-es/get';
4
4
  import isEmpty from 'lodash-es/isEmpty';
5
5
  import _pick from 'lodash-es/pick';
@@ -10,6 +10,7 @@ import { nanoid } from 'nanoid';
10
10
  import omit from 'lodash-es/omit';
11
11
  import has from 'lodash-es/has';
12
12
  import unset from 'lodash-es/unset';
13
+ import Markdown from 'markdown-to-jsx';
13
14
 
14
15
  function _defineProperties(target, props) {
15
16
  for (var i = 0; i < props.length; i++) {
@@ -189,7 +190,7 @@ var ArrayField = /*#__PURE__*/function (_Component) {
189
190
  onChange = _this$props3.onChange,
190
191
  errorSchema = _this$props3.errorSchema;
191
192
  var newErrorSchema;
192
- if (_this.props.errorSchema) {
193
+ if (errorSchema) {
193
194
  newErrorSchema = {};
194
195
  for (var idx in errorSchema) {
195
196
  var i = parseInt(idx);
@@ -366,14 +367,15 @@ var ArrayField = /*#__PURE__*/function (_Component) {
366
367
  uiSchema = _this$props7.uiSchema,
367
368
  idSchema = _this$props7.idSchema,
368
369
  registry = _this$props7.registry;
369
- var schemaUtils = registry.schemaUtils;
370
+ var schemaUtils = registry.schemaUtils,
371
+ translateString = registry.translateString;
370
372
  if (!(ITEMS_KEY in schema)) {
371
373
  var uiOptions = getUiOptions(uiSchema);
372
374
  var UnsupportedFieldTemplate = getTemplate("UnsupportedFieldTemplate", registry, uiOptions);
373
375
  return /*#__PURE__*/React.createElement(UnsupportedFieldTemplate, {
374
376
  schema: schema,
375
377
  idSchema: idSchema,
376
- reason: "Missing items definition",
378
+ reason: translateString(TranslatableString.MissingItems),
377
379
  registry: registry
378
380
  });
379
381
  }
@@ -830,8 +832,11 @@ var ArrayField = /*#__PURE__*/function (_Component) {
830
832
  _createClass(ArrayField, [{
831
833
  key: "itemTitle",
832
834
  get: function get$1() {
833
- var schema = this.props.schema;
834
- return get(schema, [ITEMS_KEY, "title"], get(schema, [ITEMS_KEY, "description"], "Item"));
835
+ var _this$props14 = this.props,
836
+ schema = _this$props14.schema,
837
+ registry = _this$props14.registry;
838
+ var translateString = registry.translateString;
839
+ return get(schema, [ITEMS_KEY, "title"], get(schema, [ITEMS_KEY, "description"], translateString(TranslatableString.ArrayItemTitle)));
835
840
  }
836
841
  }]);
837
842
  return ArrayField;
@@ -860,19 +865,22 @@ function BooleanField(props) {
860
865
  rawErrors = props.rawErrors;
861
866
  var title = schema.title;
862
867
  var widgets = registry.widgets,
863
- formContext = registry.formContext;
868
+ formContext = registry.formContext,
869
+ translateString = registry.translateString;
864
870
  var _getUiOptions = getUiOptions(uiSchema),
865
871
  _getUiOptions$widget = _getUiOptions.widget,
866
872
  widget = _getUiOptions$widget === void 0 ? "checkbox" : _getUiOptions$widget,
867
873
  options = _objectWithoutPropertiesLoose(_getUiOptions, _excluded$8);
868
874
  var Widget = getWidget(schema, widget, widgets);
875
+ var yes = translateString(TranslatableString.YesLabel);
876
+ var no = translateString(TranslatableString.NoLabel);
869
877
  var enumOptions;
870
878
  if (Array.isArray(schema.oneOf)) {
871
879
  enumOptions = optionsList({
872
880
  oneOf: schema.oneOf.map(function (option) {
873
881
  if (isObject(option)) {
874
882
  return _extends({}, option, {
875
- title: option.title || (option["const"] === true ? "Yes" : "No")
883
+ title: option.title || (option["const"] === true ? yes : no)
876
884
  });
877
885
  }
878
886
  return undefined;
@@ -890,10 +898,10 @@ function BooleanField(props) {
890
898
  })) {
891
899
  enumOptions = [{
892
900
  value: enums[0],
893
- label: enums[0] ? "Yes" : "No"
901
+ label: enums[0] ? yes : no
894
902
  }, {
895
903
  value: enums[1],
896
- label: enums[1] ? "Yes" : "No"
904
+ label: enums[1] ? yes : no
897
905
  }];
898
906
  } else {
899
907
  enumOptions = optionsList({
@@ -926,9 +934,6 @@ function BooleanField(props) {
926
934
  }
927
935
 
928
936
  var _excluded$7 = ["widget", "placeholder", "autofocus", "autocomplete", "title"];
929
- /** The prefix used when a oneOf option does not have a title
930
- */
931
- var UNKNOWN_OPTION_PREFIX = "Option";
932
937
  /** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
933
938
  * the currently selected option and cleans up any irrelevant data in `formData`.
934
939
  *
@@ -1067,7 +1072,8 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1067
1072
  schema = _this$props5.schema,
1068
1073
  uiSchema = _this$props5.uiSchema;
1069
1074
  var widgets = registry.widgets,
1070
- fields = registry.fields;
1075
+ fields = registry.fields,
1076
+ translateString = registry.translateString;
1071
1077
  var _SchemaField = fields.SchemaField;
1072
1078
  var _this$state2 = this.state,
1073
1079
  selectedOption = _this$state2.selectedOption,
@@ -1095,10 +1101,11 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1095
1101
  type: baseType
1096
1102
  });
1097
1103
  }
1098
- var optionLabel = title ? title + " " + UNKNOWN_OPTION_PREFIX.toLowerCase() : UNKNOWN_OPTION_PREFIX;
1104
+ var translateEnum = title ? TranslatableString.TitleOptionPrefix : TranslatableString.OptionPrefix;
1105
+ var translateParams = title ? [title] : [];
1099
1106
  var enumOptions = retrievedOptions.map(function (opt, index) {
1100
1107
  return {
1101
- label: opt.title || optionLabel + " " + (index + 1),
1108
+ label: opt.title || translateString(translateEnum, translateParams.concat(String(index + 1))),
1102
1109
  value: index
1103
1110
  };
1104
1111
  });
@@ -1353,9 +1360,8 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1353
1360
  * @param type - The type of the new additional schema property
1354
1361
  */
1355
1362
  _proto.getDefaultValue = function getDefaultValue(type) {
1363
+ var translateString = this.props.registry.translateString;
1356
1364
  switch (type) {
1357
- case "string":
1358
- return "New Value";
1359
1365
  case "array":
1360
1366
  return [];
1361
1367
  case "boolean":
@@ -1366,9 +1372,10 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1366
1372
  return 0;
1367
1373
  case "object":
1368
1374
  return {};
1375
+ case "string":
1369
1376
  default:
1370
1377
  // We don't have a datatype for some reason (perhaps additionalProperties was true)
1371
- return "New Value";
1378
+ return translateString(TranslatableString.NewStringDefault);
1372
1379
  }
1373
1380
  }
1374
1381
  /** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
@@ -1502,7 +1509,8 @@ var COMPONENT_TYPES = {
1502
1509
  */
1503
1510
  function getFieldComponent(schema, uiOptions, idSchema, registry) {
1504
1511
  var field = uiOptions.field;
1505
- var fields = registry.fields;
1512
+ var fields = registry.fields,
1513
+ translateString = registry.translateString;
1506
1514
  if (typeof field === "function") {
1507
1515
  return field;
1508
1516
  }
@@ -1524,7 +1532,7 @@ function getFieldComponent(schema, uiOptions, idSchema, registry) {
1524
1532
  return /*#__PURE__*/React.createElement(UnsupportedFieldTemplate, {
1525
1533
  schema: schema,
1526
1534
  idSchema: idSchema,
1527
- reason: "Unknown field type " + schema.type,
1535
+ reason: translateString(TranslatableString.UnknownFieldType, [String(schema.type)]),
1528
1536
  registry: registry
1529
1537
  });
1530
1538
  };
@@ -2109,24 +2117,27 @@ function IconButton(props) {
2109
2117
  }));
2110
2118
  }
2111
2119
  function MoveDownButton(props) {
2120
+ var translateString = props.registry.translateString;
2112
2121
  return /*#__PURE__*/React.createElement(IconButton, _extends({
2113
- title: "Move down",
2122
+ title: translateString(TranslatableString.MoveDownButton),
2114
2123
  className: "array-item-move-down"
2115
2124
  }, props, {
2116
2125
  icon: "arrow-down"
2117
2126
  }));
2118
2127
  }
2119
2128
  function MoveUpButton(props) {
2129
+ var translateString = props.registry.translateString;
2120
2130
  return /*#__PURE__*/React.createElement(IconButton, _extends({
2121
- title: "Move up",
2131
+ title: translateString(TranslatableString.MoveUpButton),
2122
2132
  className: "array-item-move-up"
2123
2133
  }, props, {
2124
2134
  icon: "arrow-up"
2125
2135
  }));
2126
2136
  }
2127
2137
  function RemoveButton(props) {
2138
+ var translateString = props.registry.translateString;
2128
2139
  return /*#__PURE__*/React.createElement(IconButton, _extends({
2129
- title: "Remove",
2140
+ title: translateString(TranslatableString.RemoveButton),
2130
2141
  className: "array-item-remove"
2131
2142
  }, props, {
2132
2143
  iconType: "danger",
@@ -2141,6 +2152,7 @@ function AddButton(_ref) {
2141
2152
  onClick = _ref.onClick,
2142
2153
  disabled = _ref.disabled,
2143
2154
  registry = _ref.registry;
2155
+ var translateString = registry.translateString;
2144
2156
  return /*#__PURE__*/React.createElement("div", {
2145
2157
  className: "row"
2146
2158
  }, /*#__PURE__*/React.createElement("p", {
@@ -2149,7 +2161,7 @@ function AddButton(_ref) {
2149
2161
  iconType: "info",
2150
2162
  icon: "plus",
2151
2163
  className: "btn-add col-xs-12",
2152
- title: "Add",
2164
+ title: translateString(TranslatableString.AddButton),
2153
2165
  onClick: onClick,
2154
2166
  disabled: disabled,
2155
2167
  registry: registry
@@ -2194,14 +2206,16 @@ function DescriptionField(props) {
2194
2206
  * @param props - The `ErrorListProps` for this component
2195
2207
  */
2196
2208
  function ErrorList(_ref) {
2197
- var errors = _ref.errors;
2209
+ var errors = _ref.errors,
2210
+ registry = _ref.registry;
2211
+ var translateString = registry.translateString;
2198
2212
  return /*#__PURE__*/React.createElement("div", {
2199
2213
  className: "panel panel-danger errors"
2200
2214
  }, /*#__PURE__*/React.createElement("div", {
2201
2215
  className: "panel-heading"
2202
2216
  }, /*#__PURE__*/React.createElement("h3", {
2203
2217
  className: "panel-title"
2204
- }, "Errors")), /*#__PURE__*/React.createElement("ul", {
2218
+ }, translateString(TranslatableString.ErrorsLabel))), /*#__PURE__*/React.createElement("ul", {
2205
2219
  className: "list-group"
2206
2220
  }, errors.map(function (error, i) {
2207
2221
  return /*#__PURE__*/React.createElement("li", {
@@ -2384,10 +2398,22 @@ function TitleField(props) {
2384
2398
  function UnsupportedField(props) {
2385
2399
  var schema = props.schema,
2386
2400
  idSchema = props.idSchema,
2387
- reason = props.reason;
2401
+ reason = props.reason,
2402
+ registry = props.registry;
2403
+ var translateString = registry.translateString;
2404
+ var translateEnum = TranslatableString.UnsupportedField;
2405
+ var translateParams = [];
2406
+ if (idSchema && idSchema.$id) {
2407
+ translateEnum = TranslatableString.UnsupportedFieldWithId;
2408
+ translateParams.push(idSchema.$id);
2409
+ }
2410
+ if (reason) {
2411
+ translateEnum = translateEnum === TranslatableString.UnsupportedField ? TranslatableString.UnsupportedFieldWithReason : TranslatableString.UnsupportedFieldWithIdAndReason;
2412
+ translateParams.push(reason);
2413
+ }
2388
2414
  return /*#__PURE__*/React.createElement("div", {
2389
2415
  className: "unsupported-field"
2390
- }, /*#__PURE__*/React.createElement("p", null, "Unsupported field schema", idSchema && idSchema.$id && /*#__PURE__*/React.createElement("span", null, " for", " field ", /*#__PURE__*/React.createElement("code", null, idSchema.$id)), reason && /*#__PURE__*/React.createElement("em", null, ": ", reason), "."), schema && /*#__PURE__*/React.createElement("pre", null, JSON.stringify(schema, null, 2)));
2416
+ }, /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement(Markdown, null, translateString(translateEnum, translateParams))), schema && /*#__PURE__*/React.createElement("pre", null, JSON.stringify(schema, null, 2)));
2391
2417
  }
2392
2418
 
2393
2419
  /** The `WrapIfAdditional` component is used by the `FieldTemplate` to rename, or remove properties that are
@@ -2409,9 +2435,11 @@ function WrapIfAdditionalTemplate(props) {
2409
2435
  children = props.children,
2410
2436
  uiSchema = props.uiSchema,
2411
2437
  registry = props.registry;
2438
+ var templates = registry.templates,
2439
+ translateString = registry.translateString;
2412
2440
  // Button templates are not overridden in the uiSchema
2413
- var RemoveButton = registry.templates.ButtonTemplates.RemoveButton;
2414
- var keyLabel = label + " Key"; // i18n ?
2441
+ var RemoveButton = templates.ButtonTemplates.RemoveButton;
2442
+ var keyLabel = translateString(TranslatableString.KeyLabel, [label]);
2415
2443
  var additional = (ADDITIONAL_PROPERTY_FLAG in schema);
2416
2444
  if (!additional) {
2417
2445
  return /*#__PURE__*/React.createElement("div", {
@@ -2588,6 +2616,7 @@ function AltDateWidget(_ref2) {
2588
2616
  onFocus = _ref2.onFocus,
2589
2617
  onChange = _ref2.onChange,
2590
2618
  value = _ref2.value;
2619
+ var translateString = registry.translateString;
2591
2620
  var _useReducer = useReducer(function (state, action) {
2592
2621
  return _extends({}, state, action);
2593
2622
  }, parseDateString(value, time)),
@@ -2647,13 +2676,13 @@ function AltDateWidget(_ref2) {
2647
2676
  href: "#",
2648
2677
  className: "btn btn-info btn-now",
2649
2678
  onClick: handleSetNow
2650
- }, "Now")), (options.hideClearButton !== "undefined" ? !options.hideClearButton : true) && /*#__PURE__*/React.createElement("li", {
2679
+ }, translateString(TranslatableString.NowLabel))), (options.hideClearButton !== "undefined" ? !options.hideClearButton : true) && /*#__PURE__*/React.createElement("li", {
2651
2680
  className: "list-inline-item"
2652
2681
  }, /*#__PURE__*/React.createElement("a", {
2653
2682
  href: "#",
2654
2683
  className: "btn btn-warning btn-clear",
2655
2684
  onClick: handleClear
2656
- }, "Clear")));
2685
+ }, translateString(TranslatableString.ClearLabel))));
2657
2686
  }
2658
2687
 
2659
2688
  var _excluded$1 = ["time"];
@@ -2906,10 +2935,12 @@ function processFiles(files) {
2906
2935
  return Promise.all(Array.from(files).map(processFile));
2907
2936
  }
2908
2937
  function FilesInfo(_ref) {
2909
- var filesInfo = _ref.filesInfo;
2938
+ var filesInfo = _ref.filesInfo,
2939
+ registry = _ref.registry;
2910
2940
  if (filesInfo.length === 0) {
2911
2941
  return null;
2912
2942
  }
2943
+ var translateString = registry.translateString;
2913
2944
  return /*#__PURE__*/React.createElement("ul", {
2914
2945
  className: "file-info"
2915
2946
  }, filesInfo.map(function (fileInfo, key) {
@@ -2918,7 +2949,7 @@ function FilesInfo(_ref) {
2918
2949
  type = fileInfo.type;
2919
2950
  return /*#__PURE__*/React.createElement("li", {
2920
2951
  key: key
2921
- }, /*#__PURE__*/React.createElement("strong", null, name), " (", type, ", ", size, " bytes)");
2952
+ }, /*#__PURE__*/React.createElement(Markdown, null, translateString(TranslatableString.FilesInfo, [name, type, String(size)])));
2922
2953
  }));
2923
2954
  }
2924
2955
  function extractFileInfo(dataURLs) {
@@ -2948,7 +2979,8 @@ function FileWidget(_ref2) {
2948
2979
  value = _ref2.value,
2949
2980
  _ref2$autofocus = _ref2.autofocus,
2950
2981
  autofocus = _ref2$autofocus === void 0 ? false : _ref2$autofocus,
2951
- options = _ref2.options;
2982
+ options = _ref2.options,
2983
+ registry = _ref2.registry;
2952
2984
  var extractedFilesInfo = useMemo(function () {
2953
2985
  return Array.isArray(value) ? extractFileInfo(value) : extractFileInfo([value]);
2954
2986
  }, [value]);
@@ -2983,7 +3015,8 @@ function FileWidget(_ref2) {
2983
3015
  accept: options.accept ? String(options.accept) : undefined,
2984
3016
  "aria-describedby": ariaDescribedByIds(id)
2985
3017
  })), /*#__PURE__*/React.createElement(FilesInfo, {
2986
- filesInfo: filesInfo
3018
+ filesInfo: filesInfo,
3019
+ registry: registry
2987
3020
  }));
2988
3021
  }
2989
3022
 
@@ -3295,7 +3328,8 @@ function getDefaultRegistry() {
3295
3328
  templates: templates(),
3296
3329
  widgets: widgets(),
3297
3330
  rootSchema: {},
3298
- formContext: {}
3331
+ formContext: {},
3332
+ translateString: englishStringTranslator
3299
3333
  };
3300
3334
  }
3301
3335
 
@@ -3623,7 +3657,8 @@ var Form = /*#__PURE__*/function (_Component) {
3623
3657
  errorSchema: errorSchema || {},
3624
3658
  schema: schema,
3625
3659
  uiSchema: uiSchema,
3626
- formContext: formContext
3660
+ formContext: formContext,
3661
+ registry: registry
3627
3662
  });
3628
3663
  }
3629
3664
  return null;
@@ -3636,12 +3671,14 @@ var Form = /*#__PURE__*/function (_Component) {
3636
3671
  /** Returns the registry for the form */
3637
3672
  _proto.getRegistry = function getRegistry() {
3638
3673
  var _this$props$templates;
3674
+ var customTranslateString = this.props.translateString;
3639
3675
  var schemaUtils = this.state.schemaUtils;
3640
3676
  var _getDefaultRegistry = getDefaultRegistry(),
3641
3677
  fields = _getDefaultRegistry.fields,
3642
3678
  templates = _getDefaultRegistry.templates,
3643
3679
  widgets = _getDefaultRegistry.widgets,
3644
- formContext = _getDefaultRegistry.formContext;
3680
+ formContext = _getDefaultRegistry.formContext,
3681
+ translateString = _getDefaultRegistry.translateString;
3645
3682
  return {
3646
3683
  fields: _extends({}, fields, this.props.fields),
3647
3684
  templates: _extends({}, templates, this.props.templates, {
@@ -3650,7 +3687,8 @@ var Form = /*#__PURE__*/function (_Component) {
3650
3687
  widgets: _extends({}, widgets, this.props.widgets),
3651
3688
  rootSchema: this.props.schema,
3652
3689
  formContext: this.props.formContext || formContext,
3653
- schemaUtils: schemaUtils
3690
+ schemaUtils: schemaUtils,
3691
+ translateString: customTranslateString || translateString
3654
3692
  };
3655
3693
  }
3656
3694
  /** Provides a function that can be used to programmatically submit the `Form` */;