@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.
@@ -14,6 +14,7 @@ var nanoid = require('nanoid');
14
14
  var omit = require('lodash/omit');
15
15
  var has = require('lodash/has');
16
16
  var unset = require('lodash/unset');
17
+ var Markdown = require('markdown-to-jsx');
17
18
 
18
19
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
20
 
@@ -27,6 +28,7 @@ var set__default = /*#__PURE__*/_interopDefaultLegacy(set);
27
28
  var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
28
29
  var has__default = /*#__PURE__*/_interopDefaultLegacy(has);
29
30
  var unset__default = /*#__PURE__*/_interopDefaultLegacy(unset);
31
+ var Markdown__default = /*#__PURE__*/_interopDefaultLegacy(Markdown);
30
32
 
31
33
  function _defineProperties(target, props) {
32
34
  for (var i = 0; i < props.length; i++) {
@@ -206,7 +208,7 @@ var ArrayField = /*#__PURE__*/function (_Component) {
206
208
  onChange = _this$props3.onChange,
207
209
  errorSchema = _this$props3.errorSchema;
208
210
  var newErrorSchema;
209
- if (_this.props.errorSchema) {
211
+ if (errorSchema) {
210
212
  newErrorSchema = {};
211
213
  for (var idx in errorSchema) {
212
214
  var i = parseInt(idx);
@@ -383,14 +385,15 @@ var ArrayField = /*#__PURE__*/function (_Component) {
383
385
  uiSchema = _this$props7.uiSchema,
384
386
  idSchema = _this$props7.idSchema,
385
387
  registry = _this$props7.registry;
386
- var schemaUtils = registry.schemaUtils;
388
+ var schemaUtils = registry.schemaUtils,
389
+ translateString = registry.translateString;
387
390
  if (!(utils.ITEMS_KEY in schema)) {
388
391
  var uiOptions = utils.getUiOptions(uiSchema);
389
392
  var UnsupportedFieldTemplate = utils.getTemplate("UnsupportedFieldTemplate", registry, uiOptions);
390
393
  return /*#__PURE__*/React__default["default"].createElement(UnsupportedFieldTemplate, {
391
394
  schema: schema,
392
395
  idSchema: idSchema,
393
- reason: "Missing items definition",
396
+ reason: translateString(utils.TranslatableString.MissingItems),
394
397
  registry: registry
395
398
  });
396
399
  }
@@ -847,8 +850,11 @@ var ArrayField = /*#__PURE__*/function (_Component) {
847
850
  _createClass(ArrayField, [{
848
851
  key: "itemTitle",
849
852
  get: function get() {
850
- var schema = this.props.schema;
851
- return get__default["default"](schema, [utils.ITEMS_KEY, "title"], get__default["default"](schema, [utils.ITEMS_KEY, "description"], "Item"));
853
+ var _this$props14 = this.props,
854
+ schema = _this$props14.schema,
855
+ registry = _this$props14.registry;
856
+ var translateString = registry.translateString;
857
+ return get__default["default"](schema, [utils.ITEMS_KEY, "title"], get__default["default"](schema, [utils.ITEMS_KEY, "description"], translateString(utils.TranslatableString.ArrayItemTitle)));
852
858
  }
853
859
  }]);
854
860
  return ArrayField;
@@ -877,19 +883,22 @@ function BooleanField(props) {
877
883
  rawErrors = props.rawErrors;
878
884
  var title = schema.title;
879
885
  var widgets = registry.widgets,
880
- formContext = registry.formContext;
886
+ formContext = registry.formContext,
887
+ translateString = registry.translateString;
881
888
  var _getUiOptions = utils.getUiOptions(uiSchema),
882
889
  _getUiOptions$widget = _getUiOptions.widget,
883
890
  widget = _getUiOptions$widget === void 0 ? "checkbox" : _getUiOptions$widget,
884
891
  options = _objectWithoutPropertiesLoose(_getUiOptions, _excluded$8);
885
892
  var Widget = utils.getWidget(schema, widget, widgets);
893
+ var yes = translateString(utils.TranslatableString.YesLabel);
894
+ var no = translateString(utils.TranslatableString.NoLabel);
886
895
  var enumOptions;
887
896
  if (Array.isArray(schema.oneOf)) {
888
897
  enumOptions = utils.optionsList({
889
898
  oneOf: schema.oneOf.map(function (option) {
890
899
  if (isObject__default["default"](option)) {
891
900
  return _extends({}, option, {
892
- title: option.title || (option["const"] === true ? "Yes" : "No")
901
+ title: option.title || (option["const"] === true ? yes : no)
893
902
  });
894
903
  }
895
904
  return undefined;
@@ -907,10 +916,10 @@ function BooleanField(props) {
907
916
  })) {
908
917
  enumOptions = [{
909
918
  value: enums[0],
910
- label: enums[0] ? "Yes" : "No"
919
+ label: enums[0] ? yes : no
911
920
  }, {
912
921
  value: enums[1],
913
- label: enums[1] ? "Yes" : "No"
922
+ label: enums[1] ? yes : no
914
923
  }];
915
924
  } else {
916
925
  enumOptions = utils.optionsList({
@@ -943,9 +952,6 @@ function BooleanField(props) {
943
952
  }
944
953
 
945
954
  var _excluded$7 = ["widget", "placeholder", "autofocus", "autocomplete", "title"];
946
- /** The prefix used when a oneOf option does not have a title
947
- */
948
- var UNKNOWN_OPTION_PREFIX = "Option";
949
955
  /** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
950
956
  * the currently selected option and cleans up any irrelevant data in `formData`.
951
957
  *
@@ -1084,7 +1090,8 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1084
1090
  schema = _this$props5.schema,
1085
1091
  uiSchema = _this$props5.uiSchema;
1086
1092
  var widgets = registry.widgets,
1087
- fields = registry.fields;
1093
+ fields = registry.fields,
1094
+ translateString = registry.translateString;
1088
1095
  var _SchemaField = fields.SchemaField;
1089
1096
  var _this$state2 = this.state,
1090
1097
  selectedOption = _this$state2.selectedOption,
@@ -1112,10 +1119,11 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1112
1119
  type: baseType
1113
1120
  });
1114
1121
  }
1115
- var optionLabel = title ? title + " " + UNKNOWN_OPTION_PREFIX.toLowerCase() : UNKNOWN_OPTION_PREFIX;
1122
+ var translateEnum = title ? utils.TranslatableString.TitleOptionPrefix : utils.TranslatableString.OptionPrefix;
1123
+ var translateParams = title ? [title] : [];
1116
1124
  var enumOptions = retrievedOptions.map(function (opt, index) {
1117
1125
  return {
1118
- label: opt.title || optionLabel + " " + (index + 1),
1126
+ label: opt.title || translateString(translateEnum, translateParams.concat(String(index + 1))),
1119
1127
  value: index
1120
1128
  };
1121
1129
  });
@@ -1370,9 +1378,8 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1370
1378
  * @param type - The type of the new additional schema property
1371
1379
  */
1372
1380
  _proto.getDefaultValue = function getDefaultValue(type) {
1381
+ var translateString = this.props.registry.translateString;
1373
1382
  switch (type) {
1374
- case "string":
1375
- return "New Value";
1376
1383
  case "array":
1377
1384
  return [];
1378
1385
  case "boolean":
@@ -1383,9 +1390,10 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1383
1390
  return 0;
1384
1391
  case "object":
1385
1392
  return {};
1393
+ case "string":
1386
1394
  default:
1387
1395
  // We don't have a datatype for some reason (perhaps additionalProperties was true)
1388
- return "New Value";
1396
+ return translateString(utils.TranslatableString.NewStringDefault);
1389
1397
  }
1390
1398
  }
1391
1399
  /** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
@@ -1519,7 +1527,8 @@ var COMPONENT_TYPES = {
1519
1527
  */
1520
1528
  function getFieldComponent(schema, uiOptions, idSchema, registry) {
1521
1529
  var field = uiOptions.field;
1522
- var fields = registry.fields;
1530
+ var fields = registry.fields,
1531
+ translateString = registry.translateString;
1523
1532
  if (typeof field === "function") {
1524
1533
  return field;
1525
1534
  }
@@ -1541,7 +1550,7 @@ function getFieldComponent(schema, uiOptions, idSchema, registry) {
1541
1550
  return /*#__PURE__*/React__default["default"].createElement(UnsupportedFieldTemplate, {
1542
1551
  schema: schema,
1543
1552
  idSchema: idSchema,
1544
- reason: "Unknown field type " + schema.type,
1553
+ reason: translateString(utils.TranslatableString.UnknownFieldType, [String(schema.type)]),
1545
1554
  registry: registry
1546
1555
  });
1547
1556
  };
@@ -2126,24 +2135,27 @@ function IconButton(props) {
2126
2135
  }));
2127
2136
  }
2128
2137
  function MoveDownButton(props) {
2138
+ var translateString = props.registry.translateString;
2129
2139
  return /*#__PURE__*/React__default["default"].createElement(IconButton, _extends({
2130
- title: "Move down",
2140
+ title: translateString(utils.TranslatableString.MoveDownButton),
2131
2141
  className: "array-item-move-down"
2132
2142
  }, props, {
2133
2143
  icon: "arrow-down"
2134
2144
  }));
2135
2145
  }
2136
2146
  function MoveUpButton(props) {
2147
+ var translateString = props.registry.translateString;
2137
2148
  return /*#__PURE__*/React__default["default"].createElement(IconButton, _extends({
2138
- title: "Move up",
2149
+ title: translateString(utils.TranslatableString.MoveUpButton),
2139
2150
  className: "array-item-move-up"
2140
2151
  }, props, {
2141
2152
  icon: "arrow-up"
2142
2153
  }));
2143
2154
  }
2144
2155
  function RemoveButton(props) {
2156
+ var translateString = props.registry.translateString;
2145
2157
  return /*#__PURE__*/React__default["default"].createElement(IconButton, _extends({
2146
- title: "Remove",
2158
+ title: translateString(utils.TranslatableString.RemoveButton),
2147
2159
  className: "array-item-remove"
2148
2160
  }, props, {
2149
2161
  iconType: "danger",
@@ -2158,6 +2170,7 @@ function AddButton(_ref) {
2158
2170
  onClick = _ref.onClick,
2159
2171
  disabled = _ref.disabled,
2160
2172
  registry = _ref.registry;
2173
+ var translateString = registry.translateString;
2161
2174
  return /*#__PURE__*/React__default["default"].createElement("div", {
2162
2175
  className: "row"
2163
2176
  }, /*#__PURE__*/React__default["default"].createElement("p", {
@@ -2166,7 +2179,7 @@ function AddButton(_ref) {
2166
2179
  iconType: "info",
2167
2180
  icon: "plus",
2168
2181
  className: "btn-add col-xs-12",
2169
- title: "Add",
2182
+ title: translateString(utils.TranslatableString.AddButton),
2170
2183
  onClick: onClick,
2171
2184
  disabled: disabled,
2172
2185
  registry: registry
@@ -2211,14 +2224,16 @@ function DescriptionField(props) {
2211
2224
  * @param props - The `ErrorListProps` for this component
2212
2225
  */
2213
2226
  function ErrorList(_ref) {
2214
- var errors = _ref.errors;
2227
+ var errors = _ref.errors,
2228
+ registry = _ref.registry;
2229
+ var translateString = registry.translateString;
2215
2230
  return /*#__PURE__*/React__default["default"].createElement("div", {
2216
2231
  className: "panel panel-danger errors"
2217
2232
  }, /*#__PURE__*/React__default["default"].createElement("div", {
2218
2233
  className: "panel-heading"
2219
2234
  }, /*#__PURE__*/React__default["default"].createElement("h3", {
2220
2235
  className: "panel-title"
2221
- }, "Errors")), /*#__PURE__*/React__default["default"].createElement("ul", {
2236
+ }, translateString(utils.TranslatableString.ErrorsLabel))), /*#__PURE__*/React__default["default"].createElement("ul", {
2222
2237
  className: "list-group"
2223
2238
  }, errors.map(function (error, i) {
2224
2239
  return /*#__PURE__*/React__default["default"].createElement("li", {
@@ -2401,10 +2416,22 @@ function TitleField(props) {
2401
2416
  function UnsupportedField(props) {
2402
2417
  var schema = props.schema,
2403
2418
  idSchema = props.idSchema,
2404
- reason = props.reason;
2419
+ reason = props.reason,
2420
+ registry = props.registry;
2421
+ var translateString = registry.translateString;
2422
+ var translateEnum = utils.TranslatableString.UnsupportedField;
2423
+ var translateParams = [];
2424
+ if (idSchema && idSchema.$id) {
2425
+ translateEnum = utils.TranslatableString.UnsupportedFieldWithId;
2426
+ translateParams.push(idSchema.$id);
2427
+ }
2428
+ if (reason) {
2429
+ translateEnum = translateEnum === utils.TranslatableString.UnsupportedField ? utils.TranslatableString.UnsupportedFieldWithReason : utils.TranslatableString.UnsupportedFieldWithIdAndReason;
2430
+ translateParams.push(reason);
2431
+ }
2405
2432
  return /*#__PURE__*/React__default["default"].createElement("div", {
2406
2433
  className: "unsupported-field"
2407
- }, /*#__PURE__*/React__default["default"].createElement("p", null, "Unsupported field schema", idSchema && idSchema.$id && /*#__PURE__*/React__default["default"].createElement("span", null, " for", " field ", /*#__PURE__*/React__default["default"].createElement("code", null, idSchema.$id)), reason && /*#__PURE__*/React__default["default"].createElement("em", null, ": ", reason), "."), schema && /*#__PURE__*/React__default["default"].createElement("pre", null, JSON.stringify(schema, null, 2)));
2434
+ }, /*#__PURE__*/React__default["default"].createElement("p", null, /*#__PURE__*/React__default["default"].createElement(Markdown__default["default"], null, translateString(translateEnum, translateParams))), schema && /*#__PURE__*/React__default["default"].createElement("pre", null, JSON.stringify(schema, null, 2)));
2408
2435
  }
2409
2436
 
2410
2437
  /** The `WrapIfAdditional` component is used by the `FieldTemplate` to rename, or remove properties that are
@@ -2426,9 +2453,11 @@ function WrapIfAdditionalTemplate(props) {
2426
2453
  children = props.children,
2427
2454
  uiSchema = props.uiSchema,
2428
2455
  registry = props.registry;
2456
+ var templates = registry.templates,
2457
+ translateString = registry.translateString;
2429
2458
  // Button templates are not overridden in the uiSchema
2430
- var RemoveButton = registry.templates.ButtonTemplates.RemoveButton;
2431
- var keyLabel = label + " Key"; // i18n ?
2459
+ var RemoveButton = templates.ButtonTemplates.RemoveButton;
2460
+ var keyLabel = translateString(utils.TranslatableString.KeyLabel, [label]);
2432
2461
  var additional = (utils.ADDITIONAL_PROPERTY_FLAG in schema);
2433
2462
  if (!additional) {
2434
2463
  return /*#__PURE__*/React__default["default"].createElement("div", {
@@ -2605,6 +2634,7 @@ function AltDateWidget(_ref2) {
2605
2634
  onFocus = _ref2.onFocus,
2606
2635
  onChange = _ref2.onChange,
2607
2636
  value = _ref2.value;
2637
+ var translateString = registry.translateString;
2608
2638
  var _useReducer = React.useReducer(function (state, action) {
2609
2639
  return _extends({}, state, action);
2610
2640
  }, utils.parseDateString(value, time)),
@@ -2664,13 +2694,13 @@ function AltDateWidget(_ref2) {
2664
2694
  href: "#",
2665
2695
  className: "btn btn-info btn-now",
2666
2696
  onClick: handleSetNow
2667
- }, "Now")), (options.hideClearButton !== "undefined" ? !options.hideClearButton : true) && /*#__PURE__*/React__default["default"].createElement("li", {
2697
+ }, translateString(utils.TranslatableString.NowLabel))), (options.hideClearButton !== "undefined" ? !options.hideClearButton : true) && /*#__PURE__*/React__default["default"].createElement("li", {
2668
2698
  className: "list-inline-item"
2669
2699
  }, /*#__PURE__*/React__default["default"].createElement("a", {
2670
2700
  href: "#",
2671
2701
  className: "btn btn-warning btn-clear",
2672
2702
  onClick: handleClear
2673
- }, "Clear")));
2703
+ }, translateString(utils.TranslatableString.ClearLabel))));
2674
2704
  }
2675
2705
 
2676
2706
  var _excluded$1 = ["time"];
@@ -2923,10 +2953,12 @@ function processFiles(files) {
2923
2953
  return Promise.all(Array.from(files).map(processFile));
2924
2954
  }
2925
2955
  function FilesInfo(_ref) {
2926
- var filesInfo = _ref.filesInfo;
2956
+ var filesInfo = _ref.filesInfo,
2957
+ registry = _ref.registry;
2927
2958
  if (filesInfo.length === 0) {
2928
2959
  return null;
2929
2960
  }
2961
+ var translateString = registry.translateString;
2930
2962
  return /*#__PURE__*/React__default["default"].createElement("ul", {
2931
2963
  className: "file-info"
2932
2964
  }, filesInfo.map(function (fileInfo, key) {
@@ -2935,7 +2967,7 @@ function FilesInfo(_ref) {
2935
2967
  type = fileInfo.type;
2936
2968
  return /*#__PURE__*/React__default["default"].createElement("li", {
2937
2969
  key: key
2938
- }, /*#__PURE__*/React__default["default"].createElement("strong", null, name), " (", type, ", ", size, " bytes)");
2970
+ }, /*#__PURE__*/React__default["default"].createElement(Markdown__default["default"], null, translateString(utils.TranslatableString.FilesInfo, [name, type, String(size)])));
2939
2971
  }));
2940
2972
  }
2941
2973
  function extractFileInfo(dataURLs) {
@@ -2965,7 +2997,8 @@ function FileWidget(_ref2) {
2965
2997
  value = _ref2.value,
2966
2998
  _ref2$autofocus = _ref2.autofocus,
2967
2999
  autofocus = _ref2$autofocus === void 0 ? false : _ref2$autofocus,
2968
- options = _ref2.options;
3000
+ options = _ref2.options,
3001
+ registry = _ref2.registry;
2969
3002
  var extractedFilesInfo = React.useMemo(function () {
2970
3003
  return Array.isArray(value) ? extractFileInfo(value) : extractFileInfo([value]);
2971
3004
  }, [value]);
@@ -3000,7 +3033,8 @@ function FileWidget(_ref2) {
3000
3033
  accept: options.accept ? String(options.accept) : undefined,
3001
3034
  "aria-describedby": utils.ariaDescribedByIds(id)
3002
3035
  })), /*#__PURE__*/React__default["default"].createElement(FilesInfo, {
3003
- filesInfo: filesInfo
3036
+ filesInfo: filesInfo,
3037
+ registry: registry
3004
3038
  }));
3005
3039
  }
3006
3040
 
@@ -3312,7 +3346,8 @@ function getDefaultRegistry() {
3312
3346
  templates: templates(),
3313
3347
  widgets: widgets(),
3314
3348
  rootSchema: {},
3315
- formContext: {}
3349
+ formContext: {},
3350
+ translateString: utils.englishStringTranslator
3316
3351
  };
3317
3352
  }
3318
3353
 
@@ -3640,7 +3675,8 @@ var Form = /*#__PURE__*/function (_Component) {
3640
3675
  errorSchema: errorSchema || {},
3641
3676
  schema: schema,
3642
3677
  uiSchema: uiSchema,
3643
- formContext: formContext
3678
+ formContext: formContext,
3679
+ registry: registry
3644
3680
  });
3645
3681
  }
3646
3682
  return null;
@@ -3653,12 +3689,14 @@ var Form = /*#__PURE__*/function (_Component) {
3653
3689
  /** Returns the registry for the form */
3654
3690
  _proto.getRegistry = function getRegistry() {
3655
3691
  var _this$props$templates;
3692
+ var customTranslateString = this.props.translateString;
3656
3693
  var schemaUtils = this.state.schemaUtils;
3657
3694
  var _getDefaultRegistry = getDefaultRegistry(),
3658
3695
  fields = _getDefaultRegistry.fields,
3659
3696
  templates = _getDefaultRegistry.templates,
3660
3697
  widgets = _getDefaultRegistry.widgets,
3661
- formContext = _getDefaultRegistry.formContext;
3698
+ formContext = _getDefaultRegistry.formContext,
3699
+ translateString = _getDefaultRegistry.translateString;
3662
3700
  return {
3663
3701
  fields: _extends({}, fields, this.props.fields),
3664
3702
  templates: _extends({}, templates, this.props.templates, {
@@ -3667,7 +3705,8 @@ var Form = /*#__PURE__*/function (_Component) {
3667
3705
  widgets: _extends({}, widgets, this.props.widgets),
3668
3706
  rootSchema: this.props.schema,
3669
3707
  formContext: this.props.formContext || formContext,
3670
- schemaUtils: schemaUtils
3708
+ schemaUtils: schemaUtils,
3709
+ translateString: customTranslateString || translateString
3671
3710
  };
3672
3711
  }
3673
3712
  /** Provides a function that can be used to programmatically submit the `Form` */;