@rjsf/core 5.5.0 → 5.5.2

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,6 +1,6 @@
1
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
2
  import { Component, useState, useCallback, useEffect, useReducer, useMemo, createRef, forwardRef } from 'react';
3
- 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, labelValue, enumOptionsValueForIndex, enumOptionsIsSelected, optionId, enumOptionsSelectValue, enumOptionsDeselectValue, utcToLocal, localToUTC, dataURItoBlob, enumOptionsIndexForValue, englishStringTranslator, createSchemaUtils, shouldRender, UI_GLOBAL_OPTIONS_KEY, isObject as isObject$1, RJSF_ADDITONAL_PROPERTIES_FLAG, NAME_KEY } from '@rjsf/utils';
3
+ 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, labelValue, enumOptionsValueForIndex, enumOptionsIsSelected, optionId, enumOptionsSelectValue, enumOptionsDeselectValue, utcToLocal, localToUTC, dataURItoBlob, enumOptionsIndexForValue, englishStringTranslator, createSchemaUtils, shouldRender, UI_GLOBAL_OPTIONS_KEY, SUBMIT_BTN_OPTIONS_KEY, isObject as isObject$1, RJSF_ADDITONAL_PROPERTIES_FLAG, NAME_KEY } from '@rjsf/utils';
4
4
  import get from 'lodash-es/get';
5
5
  import isEmpty from 'lodash-es/isEmpty';
6
6
  import _pick from 'lodash-es/pick';
@@ -129,6 +129,9 @@ var ArrayField = /*#__PURE__*/function (_Component) {
129
129
  function ArrayField(props) {
130
130
  var _this;
131
131
  _this = _Component.call(this, props) || this;
132
+ /** Returns the default form information for an item based on the schema for that item. Deals with the possibility
133
+ * that the schema is fixed and allows additional items.
134
+ */
132
135
  _this._getNewFormDataRow = function () {
133
136
  var _this$props = _this.props,
134
137
  schema = _this$props.schema,
@@ -141,14 +144,32 @@ var ArrayField = /*#__PURE__*/function (_Component) {
141
144
  // Cast this as a T to work around schema utils being for T[] caused by the FieldProps<T[], S, F> call on the class
142
145
  return schemaUtils.getDefaultFormState(itemSchema);
143
146
  };
147
+ /** Callback handler for when the user clicks on the add button. Creates a new row of keyed form data at the end of
148
+ * the list, adding it into the state, and then returning `onChange()` with the plain form data converted from the
149
+ * keyed data
150
+ *
151
+ * @param event - The event for the click
152
+ */
144
153
  _this.onAddClick = function (event) {
145
154
  _this._handleAddClick(event);
146
155
  };
156
+ /** Callback handler for when the user clicks on the add button on an existing array element. Creates a new row of
157
+ * keyed form data inserted at the `index`, adding it into the state, and then returning `onChange()` with the plain
158
+ * form data converted from the keyed data
159
+ *
160
+ * @param index - The index at which the add button is clicked
161
+ */
147
162
  _this.onAddIndexClick = function (index) {
148
163
  return function (event) {
149
164
  _this._handleAddClick(event, index);
150
165
  };
151
166
  };
167
+ /** Callback handler for when the user clicks on the copy button on an existing array element. Clones the row of
168
+ * keyed form data at the `index` into the next position in the state, and then returning `onChange()` with the plain
169
+ * form data converted from the keyed data
170
+ *
171
+ * @param index - The index at which the copy button is clicked
172
+ */
152
173
  _this.onCopyIndexClick = function (index) {
153
174
  return function (event) {
154
175
  if (event) {
@@ -174,6 +195,12 @@ var ArrayField = /*#__PURE__*/function (_Component) {
174
195
  });
175
196
  };
176
197
  };
198
+ /** Callback handler for when the user clicks on the remove button on an existing array element. Removes the row of
199
+ * keyed form data at the `index` in the state, and then returning `onChange()` with the plain form data converted
200
+ * from the keyed data
201
+ *
202
+ * @param index - The index at which the remove button is clicked
203
+ */
177
204
  _this.onDropIndexClick = function (index) {
178
205
  return function (event) {
179
206
  if (event) {
@@ -207,6 +234,13 @@ var ArrayField = /*#__PURE__*/function (_Component) {
207
234
  });
208
235
  };
209
236
  };
237
+ /** Callback handler for when the user clicks on one of the move item buttons on an existing array element. Moves the
238
+ * row of keyed form data at the `index` to the `newIndex` in the state, and then returning `onChange()` with the
239
+ * plain form data converted from the keyed data
240
+ *
241
+ * @param index - The index of the item to move
242
+ * @param newIndex - The index to where the item is to be moved
243
+ */
210
244
  _this.onReorderClick = function (index, newIndex) {
211
245
  return function (event) {
212
246
  if (event) {
@@ -247,6 +281,11 @@ var ArrayField = /*#__PURE__*/function (_Component) {
247
281
  });
248
282
  };
249
283
  };
284
+ /** Callback handler used to deal with changing the value of the data in the array at the `index`. Calls the
285
+ * `onChange` callback with the updated form data
286
+ *
287
+ * @param index - The index of the item being changed
288
+ */
250
289
  _this.onChangeForIndex = function (index) {
251
290
  return function (value, newErrorSchema, id) {
252
291
  var _extends2;
@@ -264,6 +303,7 @@ var ArrayField = /*#__PURE__*/function (_Component) {
264
303
  onChange(newFormData, errorSchema && errorSchema && _extends({}, errorSchema, (_extends2 = {}, _extends2[index] = newErrorSchema, _extends2)), id);
265
304
  };
266
305
  };
306
+ /** Callback handler used to change the value for a checkbox */
267
307
  _this.onSelectChange = function (value) {
268
308
  var _this$props5 = _this.props,
269
309
  onChange = _this$props5.onChange,
@@ -347,10 +387,7 @@ var ArrayField = /*#__PURE__*/function (_Component) {
347
387
  }
348
388
  }
349
389
  return addable;
350
- }
351
- /** Returns the default form information for an item based on the schema for that item. Deals with the possibility
352
- * that the schema is fixed and allows additional items.
353
- */;
390
+ };
354
391
  /** Callback handler for when the user clicks on the add or add at index buttons. Creates a new row of keyed form data
355
392
  * either at the end of the list (when index is not specified) or inserted at the `index` when it is, adding it into
356
393
  * the state, and then returning `onChange()` with the plain form data converted from the keyed data
@@ -380,13 +417,7 @@ var ArrayField = /*#__PURE__*/function (_Component) {
380
417
  }, function () {
381
418
  return onChange(keyedToPlainFormData(newKeyedFormData));
382
419
  });
383
- }
384
- /** Callback handler for when the user clicks on the add button. Creates a new row of keyed form data at the end of
385
- * the list, adding it into the state, and then returning `onChange()` with the plain form data converted from the
386
- * keyed data
387
- *
388
- * @param event - The event for the click
389
- */;
420
+ };
390
421
  /** Renders the `ArrayField` depending on the specific needs of the schema and uischema elements
391
422
  */
392
423
  _proto.render = function render() {
@@ -1007,6 +1038,12 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1007
1038
  function AnyOfField(props) {
1008
1039
  var _this;
1009
1040
  _this = _Component.call(this, props) || this;
1041
+ /** Callback handler to remember what the currently selected option is. In addition to that the `formData` is updated
1042
+ * to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
1043
+ * the `onChange` handler.
1044
+ *
1045
+ * @param option - The new option value being selected
1046
+ */
1010
1047
  _this.onOptionChange = function (option) {
1011
1048
  var _this$state = _this.state,
1012
1049
  selectedOption = _this$state.selectedOption,
@@ -1102,13 +1139,7 @@ var AnyOfField = /*#__PURE__*/function (_Component) {
1102
1139
  // If the form data matches none of the options, use the currently selected
1103
1140
  // option, assuming it's available; otherwise use the first option
1104
1141
  return selectedOption || 0;
1105
- }
1106
- /** Callback handler to remember what the currently selected option is. In addition to that the `formData` is updated
1107
- * to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
1108
- * the `onChange` handler.
1109
- *
1110
- * @param option - The new option value being selected
1111
- */;
1142
+ };
1112
1143
  _proto.getFieldId = function getFieldId() {
1113
1144
  var _this$props4 = this.props,
1114
1145
  idSchema = _this$props4.idSchema,
@@ -1295,10 +1326,19 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1295
1326
  args[_key] = arguments[_key];
1296
1327
  }
1297
1328
  _this = _Component.call.apply(_Component, [this].concat(args)) || this;
1329
+ /** Set up the initial state */
1298
1330
  _this.state = {
1299
1331
  wasPropertyKeyModified: false,
1300
1332
  additionalProperties: {}
1301
1333
  };
1334
+ /** Returns the `onPropertyChange` handler for the `name` field. Handles the special case where a user is attempting
1335
+ * to clear the data for a field added as an additional property. Calls the `onChange()` handler with the updated
1336
+ * formData.
1337
+ *
1338
+ * @param name - The name of the property
1339
+ * @param addedByAdditionalProperties - Flag indicating whether this property is an additional property
1340
+ * @returns - The onPropertyChange callback for the `name` property
1341
+ */
1302
1342
  _this.onPropertyChange = function (name, addedByAdditionalProperties) {
1303
1343
  if (addedByAdditionalProperties === void 0) {
1304
1344
  addedByAdditionalProperties = false;
@@ -1323,6 +1363,12 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1323
1363
  onChange(newFormData, errorSchema && errorSchema && _extends({}, errorSchema, (_extends3 = {}, _extends3[name] = newErrorSchema, _extends3)), id);
1324
1364
  };
1325
1365
  };
1366
+ /** Returns a callback to handle the onDropPropertyClick event for the given `key` which removes the old `key` data
1367
+ * and calls the `onChange` callback with it
1368
+ *
1369
+ * @param key - The key for which the drop callback is desired
1370
+ * @returns - The drop property click callback
1371
+ */
1326
1372
  _this.onDropPropertyClick = function (key) {
1327
1373
  return function (event) {
1328
1374
  event.preventDefault();
@@ -1334,6 +1380,13 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1334
1380
  onChange(copiedFormData);
1335
1381
  };
1336
1382
  };
1383
+ /** Computes the next available key name from the `preferredKey`, indexing through the already existing keys until one
1384
+ * that is already not assigned is found.
1385
+ *
1386
+ * @param preferredKey - The preferred name of a new key
1387
+ * @param [formData] - The form data in which to check if the desired key already exists
1388
+ * @returns - The name of the next available key from `preferredKey`
1389
+ */
1337
1390
  _this.getAvailableKey = function (preferredKey, formData) {
1338
1391
  var _this$props3 = _this.props,
1339
1392
  uiSchema = _this$props3.uiSchema,
@@ -1348,6 +1401,12 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1348
1401
  }
1349
1402
  return newKey;
1350
1403
  };
1404
+ /** Returns a callback function that deals with the rename of a key for an additional property for a schema. That
1405
+ * callback will attempt to rename the key and move the existing data to that key, calling `onChange` when it does.
1406
+ *
1407
+ * @param oldValue - The old value of a field
1408
+ * @returns - The key change callback function
1409
+ */
1351
1410
  _this.onKeyChange = function (oldValue) {
1352
1411
  return function (value, newErrorSchema) {
1353
1412
  var _newKeys, _extends4;
@@ -1373,6 +1432,11 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1373
1432
  onChange(renamedObj, errorSchema && errorSchema && _extends({}, errorSchema, (_extends4 = {}, _extends4[value] = newErrorSchema, _extends4)));
1374
1433
  };
1375
1434
  };
1435
+ /** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
1436
+ * default data for that field has been added to the formData.
1437
+ *
1438
+ * @param schema - The schema element to which the new property is being added
1439
+ */
1376
1440
  _this.handleAddClick = function (schema) {
1377
1441
  return function () {
1378
1442
  if (!schema.additionalProperties) {
@@ -1415,15 +1479,7 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1415
1479
  _proto.isRequired = function isRequired(name) {
1416
1480
  var schema = this.props.schema;
1417
1481
  return Array.isArray(schema.required) && schema.required.indexOf(name) !== -1;
1418
- }
1419
- /** Returns the `onPropertyChange` handler for the `name` field. Handles the special case where a user is attempting
1420
- * to clear the data for a field added as an additional property. Calls the `onChange()` handler with the updated
1421
- * formData.
1422
- *
1423
- * @param name - The name of the property
1424
- * @param addedByAdditionalProperties - Flag indicating whether this property is an additional property
1425
- * @returns - The onPropertyChange callback for the `name` property
1426
- */;
1482
+ };
1427
1483
  /** Returns a default value to be used for a new additional schema property of the given `type`
1428
1484
  *
1429
1485
  * @param type - The type of the new additional schema property
@@ -1446,12 +1502,7 @@ var ObjectField = /*#__PURE__*/function (_Component) {
1446
1502
  // We don't have a datatype for some reason (perhaps additionalProperties was true)
1447
1503
  return translateString(TranslatableString.NewStringDefault);
1448
1504
  }
1449
- }
1450
- /** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
1451
- * default data for that field has been added to the formData.
1452
- *
1453
- * @param schema - The schema element to which the new property is being added
1454
- */;
1505
+ };
1455
1506
  /** Renders the `ObjectField` from the given props
1456
1507
  */
1457
1508
  _proto.render = function render() {
@@ -3585,10 +3636,6 @@ function getDefaultRegistry() {
3585
3636
  /** The `Form` component renders the outer form and all the fields defined in the `schema` */
3586
3637
  var Form = /*#__PURE__*/function (_Component) {
3587
3638
  _inheritsLoose(Form, _Component);
3588
- /** The ref used to hold the `form` element, this needs to be `any` because `tagName` or `_internalFormWrapper` can
3589
- * provide any possible type here
3590
- */
3591
-
3592
3639
  /** Constructs the `Form` from the `props`. Will setup the initial state from the props. It will also call the
3593
3640
  * `onChange` handler if the initially provided `formData` is modified to add missing default values as part of the
3594
3641
  * state construction.
@@ -3598,7 +3645,15 @@ var Form = /*#__PURE__*/function (_Component) {
3598
3645
  function Form(props) {
3599
3646
  var _this;
3600
3647
  _this = _Component.call(this, props) || this;
3648
+ /** The ref used to hold the `form` element, this needs to be `any` because `tagName` or `_internalFormWrapper` can
3649
+ * provide any possible type here
3650
+ */
3601
3651
  _this.formElement = void 0;
3652
+ /** Returns the `formData` with only the elements specified in the `fields` list
3653
+ *
3654
+ * @param formData - The data for the `Form`
3655
+ * @param fields - The fields to keep while filtering
3656
+ */
3602
3657
  _this.getUsedFormData = function (formData, fields) {
3603
3658
  // For the case of a single input form
3604
3659
  if (fields.length === 0 && typeof formData !== 'object') {
@@ -3613,6 +3668,11 @@ var Form = /*#__PURE__*/function (_Component) {
3613
3668
  }
3614
3669
  return data;
3615
3670
  };
3671
+ /** Returns the list of field names from inspecting the `pathSchema` as well as using the `formData`
3672
+ *
3673
+ * @param pathSchema - The `PathSchema` object for the form
3674
+ * @param [formData] - The form data to use while checking for empty objects/arrays
3675
+ */
3616
3676
  _this.getFieldNames = function (pathSchema, formData) {
3617
3677
  var getAllPaths = function getAllPaths(_obj, acc, paths) {
3618
3678
  if (acc === void 0) {
@@ -3647,6 +3707,17 @@ var Form = /*#__PURE__*/function (_Component) {
3647
3707
  };
3648
3708
  return getAllPaths(pathSchema);
3649
3709
  };
3710
+ /** Function to handle changes made to a field in the `Form`. This handler receives an entirely new copy of the
3711
+ * `formData` along with a new `ErrorSchema`. It will first update the `formData` with any missing default fields and
3712
+ * then, if `omitExtraData` and `liveOmit` are turned on, the `formData` will be filterer to remove any extra data not
3713
+ * in a form field. Then, the resulting formData will be validated if required. The state will be updated with the new
3714
+ * updated (potentially filtered) `formData`, any errors that resulted from validation. Finally the `onChange`
3715
+ * callback will be called if specified with the updated state.
3716
+ *
3717
+ * @param formData - The new form data from a change to a field
3718
+ * @param newErrorSchema - The new `ErrorSchema` based on the field change
3719
+ * @param id - The id of the field that caused the change
3720
+ */
3650
3721
  _this.onChange = function (formData, newErrorSchema, id) {
3651
3722
  var _this$props = _this.props,
3652
3723
  extraErrors = _this$props.extraErrors,
@@ -3707,6 +3778,12 @@ var Form = /*#__PURE__*/function (_Component) {
3707
3778
  return onChange && onChange(_extends({}, _this.state, state), id);
3708
3779
  });
3709
3780
  };
3781
+ /**
3782
+ * Callback function to handle reset form data.
3783
+ * - Reset all fields with default values.
3784
+ * - Reset validations and errors
3785
+ *
3786
+ */
3710
3787
  _this.reset = function () {
3711
3788
  var onChange = _this.props.onChange;
3712
3789
  var newState = _this.getStateFromProps(_this.props, undefined);
@@ -3722,18 +3799,38 @@ var Form = /*#__PURE__*/function (_Component) {
3722
3799
  return onChange && onChange(_extends({}, _this.state, state));
3723
3800
  });
3724
3801
  };
3802
+ /** Callback function to handle when a field on the form is blurred. Calls the `onBlur` callback for the `Form` if it
3803
+ * was provided.
3804
+ *
3805
+ * @param id - The unique `id` of the field that was blurred
3806
+ * @param data - The data associated with the field that was blurred
3807
+ */
3725
3808
  _this.onBlur = function (id, data) {
3726
3809
  var onBlur = _this.props.onBlur;
3727
3810
  if (onBlur) {
3728
3811
  onBlur(id, data);
3729
3812
  }
3730
3813
  };
3814
+ /** Callback function to handle when a field on the form is focused. Calls the `onFocus` callback for the `Form` if it
3815
+ * was provided.
3816
+ *
3817
+ * @param id - The unique `id` of the field that was focused
3818
+ * @param data - The data associated with the field that was focused
3819
+ */
3731
3820
  _this.onFocus = function (id, data) {
3732
3821
  var onFocus = _this.props.onFocus;
3733
3822
  if (onFocus) {
3734
3823
  onFocus(id, data);
3735
3824
  }
3736
3825
  };
3826
+ /** Callback function to handle when the form is submitted. First, it prevents the default event behavior. Nothing
3827
+ * happens if the target and currentTarget of the event are not the same. It will omit any extra data in the
3828
+ * `formData` in the state if `omitExtraData` is true. It will validate the resulting `formData`, reporting errors
3829
+ * via the `onError()` callback unless validation is disabled. Finally it will add in any `extraErrors` and then call
3830
+ * back the `onSubmit` callback if it was provided.
3831
+ *
3832
+ * @param event - The submit HTML form event
3833
+ */
3737
3834
  _this.onSubmit = function (event) {
3738
3835
  event.preventDefault();
3739
3836
  if (event.target !== event.currentTarget) {
@@ -3926,12 +4023,7 @@ var Form = /*#__PURE__*/function (_Component) {
3926
4023
  });
3927
4024
  }
3928
4025
  return null;
3929
- }
3930
- /** Returns the `formData` with only the elements specified in the `fields` list
3931
- *
3932
- * @param formData - The data for the `Form`
3933
- * @param fields - The fields to keep while filtering
3934
- */;
4026
+ };
3935
4027
  /** Returns the registry for the form */
3936
4028
  _proto.getRegistry = function getRegistry() {
3937
4029
  var _this$props$templates;
@@ -4049,6 +4141,7 @@ var Form = /*#__PURE__*/function (_Component) {
4049
4141
  * needed along with the submit button or any children of the form.
4050
4142
  */;
4051
4143
  _proto.render = function render() {
4144
+ var _UI_OPTIONS_KEY, _submitUiSchema;
4052
4145
  var _this$props7 = this.props,
4053
4146
  children = _this$props7.children,
4054
4147
  id = _this$props7.id,
@@ -4088,6 +4181,17 @@ var Form = /*#__PURE__*/function (_Component) {
4088
4181
  // NOTE, the `as` prop is native to `semantic-ui` and is emulated in the `material-ui` theme
4089
4182
  var as = _internalFormWrapper ? tagName : undefined;
4090
4183
  var FormTag = _internalFormWrapper || tagName || 'form';
4184
+ var _getUiOptions = getUiOptions(uiSchema),
4185
+ _getUiOptions$SUBMIT_ = _getUiOptions[SUBMIT_BTN_OPTIONS_KEY],
4186
+ submitOptions = _getUiOptions$SUBMIT_ === void 0 ? {} : _getUiOptions$SUBMIT_;
4187
+ if (disabled) {
4188
+ submitOptions = _extends({}, submitOptions, {
4189
+ props: _extends({}, submitOptions.props, {
4190
+ disabled: true
4191
+ })
4192
+ });
4193
+ }
4194
+ var submitUiSchema = (_submitUiSchema = {}, _submitUiSchema[UI_OPTIONS_KEY] = (_UI_OPTIONS_KEY = {}, _UI_OPTIONS_KEY[SUBMIT_BTN_OPTIONS_KEY] = submitOptions, _UI_OPTIONS_KEY), _submitUiSchema);
4091
4195
  return jsxs(FormTag, {
4092
4196
  className: className ? className : 'rjsf',
4093
4197
  id: id,
@@ -4119,7 +4223,7 @@ var Form = /*#__PURE__*/function (_Component) {
4119
4223
  disabled: disabled,
4120
4224
  readonly: readonly
4121
4225
  }), children ? children : jsx(SubmitButton, {
4122
- uiSchema: uiSchema,
4226
+ uiSchema: submitUiSchema,
4123
4227
  registry: registry
4124
4228
  }), showErrorList === 'bottom' && this.renderErrors(registry)]
4125
4229
  });