@rjsf/utils 6.0.0-beta.2 → 6.0.0-beta.21

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.
Files changed (99) hide show
  1. package/dist/{index.js → index.cjs} +396 -264
  2. package/dist/index.cjs.map +7 -0
  3. package/dist/utils.esm.js +395 -263
  4. package/dist/utils.esm.js.map +4 -4
  5. package/dist/utils.umd.js +368 -249
  6. package/lib/ErrorSchemaBuilder.d.ts +2 -2
  7. package/lib/canExpand.d.ts +1 -1
  8. package/lib/constants.d.ts +3 -0
  9. package/lib/constants.js +3 -0
  10. package/lib/constants.js.map +1 -1
  11. package/lib/createSchemaUtils.js +25 -18
  12. package/lib/createSchemaUtils.js.map +1 -1
  13. package/lib/enums.d.ts +9 -3
  14. package/lib/enums.js +9 -3
  15. package/lib/enums.js.map +1 -1
  16. package/lib/findSchemaDefinition.d.ts +7 -1
  17. package/lib/findSchemaDefinition.js +48 -6
  18. package/lib/findSchemaDefinition.js.map +1 -1
  19. package/lib/getTestIds.js +2 -2
  20. package/lib/getTestIds.js.map +1 -1
  21. package/lib/getUiOptions.js +4 -0
  22. package/lib/getUiOptions.js.map +1 -1
  23. package/lib/getWidget.js +3 -3
  24. package/lib/getWidget.js.map +1 -1
  25. package/lib/guessType.d.ts +1 -1
  26. package/lib/idGenerators.d.ts +22 -15
  27. package/lib/idGenerators.js +17 -8
  28. package/lib/idGenerators.js.map +1 -1
  29. package/lib/index.d.ts +10 -4
  30. package/lib/index.js +9 -4
  31. package/lib/index.js.map +1 -1
  32. package/lib/isFormDataAvailable.d.ts +7 -0
  33. package/lib/isFormDataAvailable.js +13 -0
  34. package/lib/isFormDataAvailable.js.map +1 -0
  35. package/lib/isRootSchema.d.ts +13 -0
  36. package/lib/isRootSchema.js +25 -0
  37. package/lib/isRootSchema.js.map +1 -0
  38. package/lib/mergeDefaultsWithFormData.js +14 -2
  39. package/lib/mergeDefaultsWithFormData.js.map +1 -1
  40. package/lib/schema/findFieldInSchema.d.ts +1 -1
  41. package/lib/schema/findFieldInSchema.js +1 -1
  42. package/lib/schema/getDefaultFormState.d.ts +17 -3
  43. package/lib/schema/getDefaultFormState.js +73 -27
  44. package/lib/schema/getDefaultFormState.js.map +1 -1
  45. package/lib/schema/getDisplayLabel.js +2 -2
  46. package/lib/schema/getDisplayLabel.js.map +1 -1
  47. package/lib/schema/index.d.ts +1 -2
  48. package/lib/schema/index.js +1 -2
  49. package/lib/schema/index.js.map +1 -1
  50. package/lib/schema/retrieveSchema.d.ts +11 -6
  51. package/lib/schema/retrieveSchema.js +42 -19
  52. package/lib/schema/retrieveSchema.js.map +1 -1
  53. package/lib/shallowEquals.d.ts +8 -0
  54. package/lib/shallowEquals.js +36 -0
  55. package/lib/shallowEquals.js.map +1 -0
  56. package/lib/shouldRender.d.ts +8 -2
  57. package/lib/shouldRender.js +17 -2
  58. package/lib/shouldRender.js.map +1 -1
  59. package/lib/shouldRenderOptionalField.d.ts +18 -0
  60. package/lib/shouldRenderOptionalField.js +47 -0
  61. package/lib/shouldRenderOptionalField.js.map +1 -0
  62. package/lib/toFieldPathId.d.ts +12 -0
  63. package/lib/toFieldPathId.js +19 -0
  64. package/lib/toFieldPathId.js.map +1 -0
  65. package/lib/tsconfig.tsbuildinfo +1 -1
  66. package/lib/types.d.ts +136 -81
  67. package/lib/validationDataMerge.d.ts +2 -1
  68. package/lib/validationDataMerge.js +3 -2
  69. package/lib/validationDataMerge.js.map +1 -1
  70. package/package.json +13 -14
  71. package/src/ErrorSchemaBuilder.ts +2 -2
  72. package/src/constants.ts +3 -0
  73. package/src/createSchemaUtils.ts +25 -26
  74. package/src/enums.ts +9 -3
  75. package/src/findSchemaDefinition.ts +55 -6
  76. package/src/getTestIds.ts +2 -2
  77. package/src/getUiOptions.ts +4 -0
  78. package/src/getWidget.tsx +3 -3
  79. package/src/idGenerators.ts +35 -25
  80. package/src/index.ts +16 -2
  81. package/src/isFormDataAvailable.ts +13 -0
  82. package/src/isRootSchema.ts +30 -0
  83. package/src/mergeDefaultsWithFormData.ts +16 -2
  84. package/src/schema/findFieldInSchema.ts +1 -1
  85. package/src/schema/getDefaultFormState.ts +95 -33
  86. package/src/schema/getDisplayLabel.ts +2 -2
  87. package/src/schema/index.ts +0 -2
  88. package/src/schema/retrieveSchema.ts +46 -10
  89. package/src/shallowEquals.ts +41 -0
  90. package/src/shouldRender.ts +27 -2
  91. package/src/shouldRenderOptionalField.ts +56 -0
  92. package/src/toFieldPathId.ts +24 -0
  93. package/src/types.ts +156 -84
  94. package/src/validationDataMerge.ts +7 -1
  95. package/dist/index.js.map +0 -7
  96. package/lib/schema/toIdSchema.d.ts +0 -14
  97. package/lib/schema/toIdSchema.js +0 -62
  98. package/lib/schema/toIdSchema.js.map +0 -1
  99. package/src/schema/toIdSchema.ts +0 -131
@@ -0,0 +1,56 @@
1
+ import isObject from 'lodash/isObject';
2
+ import uniq from 'lodash/uniq';
3
+
4
+ import { FormContextType, Registry, RJSFSchema, StrictRJSFSchema, UiSchema } from './types';
5
+ import getSchemaType from './getSchemaType';
6
+ import getUiOptions from './getUiOptions';
7
+ import isRootSchema from './isRootSchema';
8
+ import { ANY_OF_KEY, ONE_OF_KEY } from './constants';
9
+
10
+ /** Returns the unique list of schema types for all of the options in a anyOf/oneOf
11
+ *
12
+ * @param schemas - The list of schemas representing the XxxOf options
13
+ * @returns - All of the unique types contained within the oneOf list
14
+ */
15
+ export function getSchemaTypesForXxxOf<S extends StrictRJSFSchema = RJSFSchema>(schemas: S[]): string | string[] {
16
+ const allTypes: string[] = uniq(
17
+ schemas
18
+ .map((s) => (isObject(s) ? getSchemaType(s) : undefined))
19
+ .flat()
20
+ .filter((t) => t !== undefined),
21
+ );
22
+ return allTypes.length === 1 ? allTypes[0] : allTypes;
23
+ }
24
+
25
+ /** Determines whether the field information from the combination of `schema` and `required` along with the
26
+ * `enableOptionalDataFieldForType` settings from the global UI options in the `registry` all indicate that this field
27
+ * should be rendered with the Optional Data Controls UI.
28
+ *
29
+ * @param registry - The `registry` object
30
+ * @param schema - The schema for the field
31
+ * @param required - Flag indicating whether the field is required
32
+ * @param [uiSchema] - The uiSchema for the field
33
+ * @return - True if the field should be rendered with the optional field UI, otherwise false
34
+ */
35
+ export default function shouldRenderOptionalField<
36
+ T = any,
37
+ S extends StrictRJSFSchema = RJSFSchema,
38
+ F extends FormContextType = any,
39
+ >(registry: Registry<T, S, F>, schema: S, required: boolean, uiSchema?: UiSchema<T, S, F>): boolean {
40
+ const { enableOptionalDataFieldForType = [] } = getUiOptions<T, S, F>(uiSchema, registry.globalUiOptions);
41
+ let schemaType: ReturnType<typeof getSchemaType<S>>;
42
+ if (ANY_OF_KEY in schema && Array.isArray(schema[ANY_OF_KEY])) {
43
+ schemaType = getSchemaTypesForXxxOf(schema[ANY_OF_KEY] as S[]);
44
+ } else if (ONE_OF_KEY in schema && Array.isArray(schema[ONE_OF_KEY])) {
45
+ schemaType = getSchemaTypesForXxxOf(schema[ONE_OF_KEY] as S[]);
46
+ } else {
47
+ schemaType = getSchemaType<S>(schema);
48
+ }
49
+ return (
50
+ !isRootSchema(registry, schema) &&
51
+ !required &&
52
+ !!schemaType &&
53
+ !Array.isArray(schemaType) &&
54
+ !!enableOptionalDataFieldForType.find((val) => val === schemaType)
55
+ );
56
+ }
@@ -0,0 +1,24 @@
1
+ import { ID_KEY } from './constants';
2
+ import { FieldPathId, FieldPathList, GlobalFormOptions } from './types';
3
+
4
+ /** Constructs the `FieldPathId` for `fieldPath`. If `parentPathId` is provided, the `fieldPath` is appended to the end
5
+ * of the parent path. Then the `ID_KEY` of the resulting `FieldPathId` is constructed from the `idPrefix` and
6
+ * `idSeparator` contained within the `globalFormOptions`. If `fieldPath` is passed as an empty string, it will simply
7
+ * generate the path from the `parentPath` (if provided) and the `idPrefix` and `idSeparator`
8
+ *
9
+ * @param fieldPath - The property name or array index of the current field element
10
+ * @param globalFormOptions - The `GlobalFormOptions` used to get the `idPrefix` and `idSeparator`
11
+ * @param [parentPath] - The optional `FieldPathId` or `FieldPathList` of the parent element for this field element
12
+ * @returns - The `FieldPathId` for the given `fieldPath` and the optional `parentPathId`
13
+ */
14
+ export default function toFieldPathId(
15
+ fieldPath: string | number,
16
+ globalFormOptions: GlobalFormOptions,
17
+ parentPath?: FieldPathId | FieldPathList,
18
+ ): FieldPathId {
19
+ const basePath = Array.isArray(parentPath) ? parentPath : parentPath?.path;
20
+ const childPath = fieldPath === '' ? [] : [fieldPath];
21
+ const path = basePath ? basePath.concat(...childPath) : childPath;
22
+ const id = [globalFormOptions.idPrefix, ...path].join(globalFormOptions.idSeparator);
23
+ return { path, [ID_KEY]: id };
24
+ }
package/src/types.ts CHANGED
@@ -158,20 +158,25 @@ export type InputPropsType = Omit<RangeSpecType, 'step'> & {
158
158
  accept?: HTMLInputElement['accept'];
159
159
  };
160
160
 
161
- /** Type describing an id used for a field in the `IdSchema` */
162
- export type FieldId = {
161
+ /** The list of path elements that represents where in the schema a field is located. When the item in the field list is
162
+ * a string, then it represents the name of the property within an object. When it is a number, then it represents the
163
+ * index within an array.
164
+ *
165
+ * For example:
166
+ * `[]` represents the root object of the schema
167
+ * `['foo', 'bar']` represents the `bar` element contained within the `foo` element of the schema
168
+ * `['baz', 1]` represents the second element in the list `baz` of the schema
169
+ */
170
+ export type FieldPathList = (string | number)[];
171
+
172
+ /** Type describing an id and path used for a field */
173
+ export type FieldPathId = {
163
174
  /** The id for a field */
164
175
  $id: string;
176
+ /** The path for a field */
177
+ path: FieldPathList;
165
178
  };
166
179
 
167
- /** Type describing a recursive structure of `FieldId`s for an object with a non-empty set of keys */
168
- export type IdSchema<T = any> = T extends GenericObjectType
169
- ? FieldId & {
170
- /** The set of ids for fields in the recursive object structure */
171
- [key in keyof T]?: IdSchema<T[key]>;
172
- }
173
- : FieldId;
174
-
175
180
  /** Type describing a name used for a field in the `PathSchema` */
176
181
  export type FieldPath = {
177
182
  /** The name of a field */
@@ -212,6 +217,8 @@ export type RJSFValidationError = {
212
217
  schemaPath?: string;
213
218
  /** Full error name, for example ".name is a required property" */
214
219
  stack: string;
220
+ /** The title property for the failing field*/
221
+ title?: string;
215
222
  };
216
223
 
217
224
  /** The type that describes an error in a field */
@@ -261,8 +268,6 @@ export type ErrorListProps<
261
268
  errorSchema: ErrorSchema<T>;
262
269
  /** An array of the errors */
263
270
  errors: RJSFValidationError[];
264
- /** The `formContext` object that was passed to `Form` */
265
- formContext?: F;
266
271
  };
267
272
 
268
273
  /** The properties that are passed to an `FieldErrorTemplate` implementation */
@@ -275,8 +280,8 @@ export type FieldErrorProps<
275
280
  errorSchema?: ErrorSchema<T>;
276
281
  /** An array of the errors */
277
282
  errors?: Array<string | ReactElement>;
278
- /** The tree of unique ids for every child field */
279
- idSchema: IdSchema<T>;
283
+ /** The FieldPathId of the field in the hierarchy */
284
+ fieldPathId: FieldPathId;
280
285
  };
281
286
 
282
287
  /** The properties that are passed to an `FieldHelpTemplate` implementation */
@@ -287,8 +292,8 @@ export type FieldHelpProps<
287
292
  > = RJSFBaseProps<T, S, F> & {
288
293
  /** The help information to be rendered */
289
294
  help?: string | ReactElement;
290
- /** The tree of unique ids for every child field */
291
- idSchema: IdSchema<T>;
295
+ /** The FieldPathId of the field in the hierarchy */
296
+ fieldPathId: FieldPathId;
292
297
  /** Flag indicating whether there are errors associated with this field */
293
298
  hasErrors?: boolean;
294
299
  };
@@ -341,8 +346,12 @@ export type TemplatesType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
341
346
  FieldTemplate: ComponentType<FieldTemplateProps<T, S, F>>;
342
347
  /** The template to use to render a Grid element */
343
348
  GridTemplate: ComponentType<GridTemplateProps>;
349
+ /** The template to use while rendering a multi-schema field (i.e. anyOf, oneOf) */
350
+ MultiSchemaFieldTemplate: ComponentType<MultiSchemaFieldTemplateProps<T, S, F>>;
344
351
  /** The template to use while rendering an object */
345
352
  ObjectFieldTemplate: ComponentType<ObjectFieldTemplateProps<T, S, F>>;
353
+ /** The template to use while rendering the Optional Data field controls */
354
+ OptionalDataControlsTemplate: ComponentType<OptionalDataControlsTemplateProps<T, S, F>>;
346
355
  /** The template to use for rendering the title of a field */
347
356
  TitleFieldTemplate: ComponentType<TitleFieldProps<T, S, F>>;
348
357
  /** The template to use for rendering information about an unsupported field type in the schema */
@@ -370,9 +379,10 @@ export type TemplatesType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
370
379
  };
371
380
 
372
381
  /** The set of UiSchema options that can be set globally and used as fallbacks at an individual template, field or
373
- * widget level when no field-level value of the option is provided.
382
+ * widget level when no field-level value of the option is provided. Extends GenericObjectType to support allowing users
383
+ * to provide any value they need for their customizations.
374
384
  */
375
- export type GlobalUISchemaOptions = {
385
+ export type GlobalUISchemaOptions = GenericObjectType & {
376
386
  /** Flag, if set to `false`, new items cannot be added to array fields, unless overridden (defaults to true) */
377
387
  addable?: boolean;
378
388
  /** Flag, if set to `true`, array items can be copied (defaults to false) */
@@ -390,6 +400,26 @@ export type GlobalUISchemaOptions = {
390
400
  /** Enables the displaying of description text that contains markdown
391
401
  */
392
402
  enableMarkdownInDescription?: boolean;
403
+ /** Enables the rendering of the Optional Data Field UI for specific types of schemas, either `object`, `array` or
404
+ * both. To disable the Optional Data Field UI for a specific field, provide an empty array within the UI schema.
405
+ */
406
+ enableOptionalDataFieldForType?: ('object' | 'array')[];
407
+ };
408
+
409
+ /** The set of options from the `Form` that will be available on the `Registry` for use in everywhere the `registry` is
410
+ * available.
411
+ */
412
+ export type GlobalFormOptions = {
413
+ /** To avoid collisions with existing ids in the DOM, it is possible to change the prefix used for ids;
414
+ * Default is `root`. This prop is passed to the `toFilePathId()` function within the RJSF field implementations.
415
+ */
416
+ readonly idPrefix: string;
417
+ /** To avoid using a path separator that is present in field names, it is possible to change the separator used for
418
+ * ids; Default is `_`. This prop is passed to the `toFilePathId()` function within the RJSF field implementations.
419
+ */
420
+ readonly idSeparator: string;
421
+ /** The component update strategy used by the Form and its fields for performance optimization */
422
+ readonly experimental_componentUpdateStrategy?: 'customDeep' | 'shallow' | 'always';
393
423
  };
394
424
 
395
425
  /** The object containing the registered core, theme and custom fields and widgets as well as the root schema, form
@@ -400,7 +430,7 @@ export interface Registry<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
400
430
  * registered fields
401
431
  */
402
432
  fields: RegistryFieldsType<T, S, F>;
403
- /** The set of templates used by the `Form`. Includes templates from `core`, theme-specific fields and any custom
433
+ /** The set of templates used by the `Form`. Includes templates from `core`, theme-specific templates and any custom
404
434
  * registered templates
405
435
  */
406
436
  templates: TemplatesType<T, S, F>;
@@ -418,29 +448,31 @@ export interface Registry<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
418
448
  schemaUtils: SchemaUtilsType<T, S>;
419
449
  /** The string translation function to use when displaying any of the RJSF strings in templates, fields or widgets */
420
450
  translateString: (stringKey: TranslatableString, params?: string[]) => string;
451
+ /** The global Form Options that are available for all templates, fields and widgets to access */
452
+ readonly globalFormOptions: GlobalFormOptions;
421
453
  /** The optional global UI Options that are available for all templates, fields and widgets to access */
422
454
  globalUiOptions?: GlobalUISchemaOptions;
423
455
  }
424
456
 
425
- /** The properties that are passed to a Field implementation */
457
+ /** The properties that are passed to a `Field` implementation */
426
458
  export interface FieldProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>
427
459
  extends GenericObjectType,
428
460
  RJSFBaseProps<T, S, F>,
429
461
  Pick<HTMLAttributes<HTMLElement>, Exclude<keyof HTMLAttributes<HTMLElement>, 'onBlur' | 'onFocus' | 'onChange'>> {
430
- /** The tree of unique ids for every child field */
431
- idSchema: IdSchema<T>;
462
+ /** The FieldPathId of the field in the hierarchy */
463
+ fieldPathId: FieldPathId;
432
464
  /** The data for this field */
433
465
  formData?: T;
434
466
  /** The tree of errors for this field and its children */
435
467
  errorSchema?: ErrorSchema<T>;
436
- /** The field change event handler; called with the updated form data and an optional `ErrorSchema` */
437
- onChange: (newFormData: T | undefined, es?: ErrorSchema<T>, id?: string) => any;
468
+ /** The field change event handler; called with the updated field value, the change path for the value
469
+ * (defaults to an empty array), an optional ErrorSchema and the optional id of the field being changed
470
+ */
471
+ onChange: (newValue: T | undefined, path: FieldPathList, es?: ErrorSchema<T>, id?: string) => void;
438
472
  /** The input blur event handler; call it with the field id and value */
439
473
  onBlur: (id: string, value: any) => void;
440
474
  /** The input focus event handler; call it with the field id and value */
441
475
  onFocus: (id: string, value: any) => void;
442
- /** The `formContext` object that you passed to `Form` */
443
- formContext?: F;
444
476
  /** A boolean value stating if the field should autofocus */
445
477
  autofocus?: boolean;
446
478
  /** A boolean value stating if the field is disabled */
@@ -453,14 +485,6 @@ export interface FieldProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F
453
485
  required?: boolean;
454
486
  /** The unique name of the field, usually derived from the name of the property in the JSONSchema */
455
487
  name: string;
456
- /** To avoid collisions with existing ids in the DOM, it is possible to change the prefix used for ids;
457
- * Default is `root`
458
- */
459
- idPrefix?: string;
460
- /** To avoid using a path separator that is present in field names, it is possible to change the separator used for
461
- * ids (Default is `_`)
462
- */
463
- idSeparator?: string;
464
488
  /** An array of strings listing all generated error messages from encountered errors for this field */
465
489
  rawErrors?: string[];
466
490
  }
@@ -468,9 +492,12 @@ export interface FieldProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F
468
492
  /** The definition of a React-based Field component */
469
493
  export type Field<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> = ComponentType<
470
494
  FieldProps<T, S, F>
471
- >;
495
+ > & {
496
+ /** The optional TEST_IDS block that some fields contain, exported for testing purposes */
497
+ TEST_IDS?: TestIdShape;
498
+ };
472
499
 
473
- /** The properties that are passed to a FieldTemplate implementation */
500
+ /** The properties that are passed to a `FieldTemplate` implementation */
474
501
  export type FieldTemplateProps<
475
502
  T = any,
476
503
  S extends StrictRJSFSchema = RJSFSchema,
@@ -516,8 +543,6 @@ export type FieldTemplateProps<
516
543
  * you don't want to clutter the UI
517
544
  */
518
545
  displayLabel?: boolean;
519
- /** The `formContext` object that was passed to `Form` */
520
- formContext?: F;
521
546
  /** The formData for this field */
522
547
  formData?: T;
523
548
  /** The value change event handler; Can be called with a new value to change the value for this field */
@@ -534,8 +559,8 @@ export type UnsupportedFieldProps<
534
559
  S extends StrictRJSFSchema = RJSFSchema,
535
560
  F extends FormContextType = any,
536
561
  > = RJSFBaseProps<T, S, F> & {
537
- /** The tree of unique ids for every child field */
538
- idSchema?: IdSchema<T>;
562
+ /** The FieldPathId of the field in the hierarchy */
563
+ fieldPathId: FieldPathId;
539
564
  /** The reason why the schema field has an unsupported type */
540
565
  reason: string;
541
566
  };
@@ -552,6 +577,8 @@ export type TitleFieldProps<
552
577
  title: string;
553
578
  /** A boolean value stating if the field is required */
554
579
  required?: boolean;
580
+ /** Add optional data control */
581
+ optionalDataControl?: ReactNode;
555
582
  };
556
583
 
557
584
  /** The properties that are passed to a `DescriptionFieldTemplate` implementation */
@@ -574,8 +601,10 @@ export type ArrayFieldTitleProps<
574
601
  > = Omit<TitleFieldProps<T, S, F>, 'id' | 'title'> & {
575
602
  /** The title for the field being rendered */
576
603
  title?: string;
577
- /** The idSchema of the field in the hierarchy */
578
- idSchema: IdSchema<T>;
604
+ /** The FieldPathId of the field in the hierarchy */
605
+ fieldPathId: FieldPathId;
606
+ /** Add optional data control */
607
+ optionalDataControl?: ReactNode;
579
608
  };
580
609
 
581
610
  /** The properties that are passed to a `ArrayFieldDescriptionTemplate` implementation */
@@ -586,8 +615,8 @@ export type ArrayFieldDescriptionProps<
586
615
  > = Omit<DescriptionFieldProps<T, S, F>, 'id' | 'description'> & {
587
616
  /** The description of the field being rendered */
588
617
  description?: string | ReactElement;
589
- /** The idSchema of the field in the hierarchy */
590
- idSchema: IdSchema<T>;
618
+ /** An object containing the id and path for this field */
619
+ fieldPathId: FieldPathId;
591
620
  };
592
621
 
593
622
  /** The properties of the buttons to render for each element in the ArrayFieldTemplateProps.items array */
@@ -596,8 +625,8 @@ export type ArrayFieldItemButtonsTemplateType<
596
625
  S extends StrictRJSFSchema = RJSFSchema,
597
626
  F extends FormContextType = any,
598
627
  > = RJSFBaseProps<T, S, F> & {
599
- /** The idSchema of the item for which buttons are being rendered */
600
- idSchema: IdSchema<T>;
628
+ /** The FieldPathId of the item for which buttons are being rendered */
629
+ fieldPathId: FieldPathId;
601
630
  /** The className string */
602
631
  className?: string;
603
632
  /** Any optional style attributes */
@@ -665,24 +694,18 @@ export type ArrayFieldTemplateItemType<
665
694
  F extends FormContextType = any,
666
695
  > = ArrayFieldItemTemplateType<T, S, F>;
667
696
 
668
- /** The properties that are passed to an ArrayFieldTemplate implementation */
669
- export type ArrayFieldTemplateProps<
697
+ /** The common properties of the two container templates: `ArrayFieldTemplateProps` and `ObjectFieldTemplateProps` */
698
+ export type ContainerFieldTemplateProps<
670
699
  T = any,
671
700
  S extends StrictRJSFSchema = RJSFSchema,
672
701
  F extends FormContextType = any,
673
702
  > = RJSFBaseProps<T, S, F> & {
674
- /** A boolean value stating whether new elements can be added to the array */
675
- canAdd?: boolean;
676
703
  /** The className string */
677
704
  className?: string;
678
705
  /** A boolean value stating if the array is disabled */
679
706
  disabled?: boolean;
680
- /** An object containing the id for this object & ids for its properties */
681
- idSchema: IdSchema<T>;
682
- /** An array of objects representing the items in the array */
683
- items: ArrayFieldItemTemplateType<T, S, F>[];
684
- /** A function that adds a new item to the array */
685
- onAddClick: (event?: any) => void;
707
+ /** The FieldPathId of the field in the hierarchy */
708
+ fieldPathId: FieldPathId;
686
709
  /** A boolean value stating if the array is read-only */
687
710
  readonly?: boolean;
688
711
  /** A boolean value stating if the array is required */
@@ -691,12 +714,26 @@ export type ArrayFieldTemplateProps<
691
714
  hideError?: boolean;
692
715
  /** A string value containing the title for the array */
693
716
  title: string;
694
- /** The `formContext` object that was passed to Form */
695
- formContext?: F;
696
717
  /** The formData for this array */
697
718
  formData?: T;
698
- /** The tree of errors for this field and its children */
719
+ /** The optional validation errors in the form of an `ErrorSchema` */
699
720
  errorSchema?: ErrorSchema<T>;
721
+ /** The optional data control node to render within the ObjectFieldTemplate that controls */
722
+ optionalDataControl?: ReactNode;
723
+ };
724
+
725
+ /** The properties that are passed to an `ArrayFieldTemplate` implementation */
726
+ export type ArrayFieldTemplateProps<
727
+ T = any,
728
+ S extends StrictRJSFSchema = RJSFSchema,
729
+ F extends FormContextType = any,
730
+ > = ContainerFieldTemplateProps<T, S, F> & {
731
+ /** A boolean value stating whether new elements can be added to the array */
732
+ canAdd?: boolean;
733
+ /** An array of objects representing the items in the array */
734
+ items: ArrayFieldItemTemplateType<T, S, F>[];
735
+ /** A function that adds a new item to the array */
736
+ onAddClick: (event?: any) => void;
700
737
  /** An array of strings listing all generated error messages from encountered errors for this widget */
701
738
  rawErrors?: string[];
702
739
  };
@@ -720,13 +757,9 @@ export type ObjectFieldTemplateProps<
720
757
  T = any,
721
758
  S extends StrictRJSFSchema = RJSFSchema,
722
759
  F extends FormContextType = any,
723
- > = RJSFBaseProps<T, S, F> & {
724
- /** A string value containing the title for the object */
725
- title: string;
760
+ > = ContainerFieldTemplateProps<T, S, F> & {
726
761
  /** A string value containing the description for the object */
727
762
  description?: string | ReactElement;
728
- /** A boolean value stating if the object is disabled */
729
- disabled?: boolean;
730
763
  /** An array of objects representing the properties in the object */
731
764
  properties: ObjectFieldTemplatePropertyType[];
732
765
  /** Returns a function that adds a new property to the object (to be used with additionalProperties) */
@@ -737,14 +770,28 @@ export type ObjectFieldTemplateProps<
737
770
  required?: boolean;
738
771
  /** A boolean value stating if the field is hiding its errors */
739
772
  hideError?: boolean;
740
- /** An object containing the id for this object & ids for its properties */
741
- idSchema: IdSchema<T>;
773
+ /** The FieldPathId of the field in the hierarchy */
774
+ fieldPathId: FieldPathId;
742
775
  /** The optional validation errors in the form of an `ErrorSchema` */
743
776
  errorSchema?: ErrorSchema<T>;
744
777
  /** The form data for the object */
745
778
  formData?: T;
746
- /** The `formContext` object that was passed to Form */
747
- formContext?: F;
779
+ };
780
+
781
+ /** The properties that are passed to a OptionalDataControlsTemplate implementation */
782
+ export type OptionalDataControlsTemplateProps<
783
+ T = any,
784
+ S extends StrictRJSFSchema = RJSFSchema,
785
+ F extends FormContextType = any,
786
+ > = RJSFBaseProps<T, S, F> & {
787
+ /** The generated id for this Optional Data Control instance */
788
+ id: string;
789
+ /** The label to use for the Optional Data Control */
790
+ label: string;
791
+ /** Optional callback to call when clicking on the Optional Data Control to add data */
792
+ onRemoveClick?: () => void;
793
+ /** Optional callback to call when clicking on the Optional Data Control to remove data */
794
+ onAddClick?: () => void;
748
795
  };
749
796
 
750
797
  /** The properties that are passed to a WrapIfAdditionalTemplate implementation */
@@ -773,11 +820,23 @@ export type WrapIfAdditionalTemplateProps<
773
820
  | 'registry'
774
821
  >;
775
822
 
776
- /** The properties that are passed to a Widget implementation */
823
+ /** The properties that are passed to a MultiSchemaFieldTemplate implementation */
824
+ export interface MultiSchemaFieldTemplateProps<
825
+ T = any,
826
+ S extends StrictRJSFSchema = RJSFSchema,
827
+ F extends FormContextType = any,
828
+ > extends RJSFBaseProps<T, S, F> {
829
+ /** The rendered widget used to select a schema option */
830
+ selector: ReactNode;
831
+ /** The rendered SchemaField for the selected schema option */
832
+ optionSchemaField: ReactNode;
833
+ }
834
+
835
+ /** The properties that are passed to a `Widget` implementation */
777
836
  export interface WidgetProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>
778
837
  extends GenericObjectType,
779
838
  RJSFBaseProps<T, S, F>,
780
- Pick<HTMLAttributes<HTMLElement>, Exclude<keyof HTMLAttributes<HTMLElement>, 'onBlur' | 'onFocus'>> {
839
+ Pick<HTMLAttributes<HTMLElement>, Exclude<keyof HTMLAttributes<HTMLElement>, 'onBlur' | 'onFocus' | 'onChange'>> {
781
840
  /** The generated id for this widget, used to provide unique `name`s and `id`s for the HTML field elements rendered by
782
841
  * widgets
783
842
  */
@@ -807,8 +866,6 @@ export interface WidgetProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F
807
866
  /** The enum options list for a type that supports them */
808
867
  enumOptions?: EnumOptionsType<S>[];
809
868
  };
810
- /** The `formContext` object that you passed to `Form` */
811
- formContext?: F;
812
869
  /** The input blur event handler; call it with the widget id and value */
813
870
  onBlur: (id: string, value: any) => void;
814
871
  /** The value change event handler; call it with the new value every time it changes */
@@ -985,6 +1042,13 @@ export type UIOptionsType<
985
1042
  [key: string]: boolean | number | string | object | any[] | null | undefined;
986
1043
  };
987
1044
 
1045
+ /**
1046
+ * A utility type that extracts the element type from an array type.
1047
+ * If the type is not an array, it returns the type itself as a safe fallback.
1048
+ * Handles both standard arrays and readonly arrays.
1049
+ */
1050
+ export type ArrayElement<A> = A extends readonly (infer E)[] ? E : A;
1051
+
988
1052
  /** Type describing the well-known properties of the `UiSchema` while also supporting all user defined properties,
989
1053
  * starting with `ui:`.
990
1054
  */
@@ -998,7 +1062,10 @@ export type UiSchema<
998
1062
  * Registry for use everywhere.
999
1063
  */
1000
1064
  'ui:globalOptions'?: GlobalUISchemaOptions;
1001
- /** Allows the form to generate a unique prefix for the `Form`'s root prefix */
1065
+ /** Allows the form to generate a unique prefix for the `Form`'s root prefix
1066
+ *
1067
+ * @deprecated - use `Form.idPrefix` instead, will be removed in a future major version
1068
+ */
1002
1069
  'ui:rootFieldId'?: string;
1003
1070
  /** By default, any field that is rendered for an `anyOf`/`oneOf` schema will be wrapped inside the `AnyOfField` or
1004
1071
  * `OneOfField` component. This default behavior may be undesirable if your custom field already handles behavior
@@ -1010,6 +1077,13 @@ export type UiSchema<
1010
1077
  'ui:fieldReplacesAnyOrOneOf'?: boolean;
1011
1078
  /** An object that contains all the potential UI options in a single object */
1012
1079
  'ui:options'?: UIOptionsType<T, S, F>;
1080
+ /** The uiSchema for items in an array. Can be an object for a uniform uiSchema across all items (current behavior),
1081
+ * or a function that returns a dynamic uiSchema based on the item's data and index.
1082
+ * When using a function, it receives the item data, index, and optionally the form context as parameters.
1083
+ */
1084
+ items?:
1085
+ | UiSchema<ArrayElement<T>, S, F>
1086
+ | ((itemData: ArrayElement<T>, index: number, formContext?: F) => UiSchema<ArrayElement<T>, S, F>);
1013
1087
  };
1014
1088
 
1015
1089
  /** A `CustomValidator` function takes in a `formData`, `errors` and `uiSchema` objects and returns the given `errors`
@@ -1096,6 +1170,11 @@ export interface FoundFieldType<S extends StrictRJSFSchema = RJSFSchema> {
1096
1170
  * set of APIs to the `@rjsf/core` components and the various themes as well.
1097
1171
  */
1098
1172
  export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> {
1173
+ /** Returns the `rootSchema` in the `SchemaUtilsType`
1174
+ *
1175
+ * @returns - The rootSchema
1176
+ */
1177
+ getRootSchema(): S;
1099
1178
  /** Returns the `ValidatorType` in the `SchemaUtilsType`
1100
1179
  *
1101
1180
  * @returns - The `ValidatorType`
@@ -1147,12 +1226,14 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1147
1226
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1148
1227
  * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1149
1228
  * false when computing defaults for any nested object properties.
1229
+ * @param initialDefaultsGenerated - Indicates whether or not initial defaults have been generated
1150
1230
  * @returns - The resulting `formData` with all the defaults provided
1151
1231
  */
1152
1232
  getDefaultFormState(
1153
1233
  schema: S,
1154
1234
  formData?: T,
1155
1235
  includeUndefinedValues?: boolean | 'excludeObjectChildren',
1236
+ initialDefaultsGenerated?: boolean,
1156
1237
  ): T | T[] | undefined;
1157
1238
  /** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
1158
1239
  * should be displayed in a UI.
@@ -1228,9 +1309,10 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1228
1309
  *
1229
1310
  * @param schema - The schema for which retrieving a schema is desired
1230
1311
  * @param [formData] - The current formData, if any, to assist retrieving a schema
1312
+ * @param [resolveAnyOfOrOneOfRefs] - Optional flag indicating whether to resolved refs in anyOf/oneOf lists
1231
1313
  * @returns - The schema having its conditions, additional properties, references and dependencies resolved
1232
1314
  */
1233
- retrieveSchema(schema: S, formData?: T): S;
1315
+ retrieveSchema(schema: S, formData?: T, resolveAnyOfOrOneOfRefs?: boolean): S;
1234
1316
  /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
1235
1317
  * new schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the
1236
1318
  * nature of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the
@@ -1243,16 +1325,6 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1243
1325
  * to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
1244
1326
  */
1245
1327
  sanitizeDataForNewSchema(newSchema?: S, oldSchema?: S, data?: any): T;
1246
- /** Generates an `IdSchema` object for the `schema`, recursively
1247
- *
1248
- * @param schema - The schema for which the display label flag is desired
1249
- * @param [id] - The base id for the schema
1250
- * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1251
- * @param [idPrefix='root'] - The prefix to use for the id
1252
- * @param [idSeparator='_'] - The separator to use for the path segments in the id
1253
- * @returns - The `IdSchema` object for the `schema`
1254
- */
1255
- toIdSchema(schema: S, id?: string, formData?: T, idPrefix?: string, idSeparator?: string): IdSchema<T>;
1256
1328
  /** Generates an `PathSchema` object for the `schema`, recursively
1257
1329
  *
1258
1330
  * @param schema - The schema for which the display label flag is desired
@@ -11,11 +11,13 @@ import { ErrorSchema, ValidationData } from './types';
11
11
  *
12
12
  * @param validationData - The current `ValidationData` into which to merge the additional errors
13
13
  * @param [additionalErrorSchema] - The optional additional set of errors in an `ErrorSchema`
14
+ * @param [preventDuplicates=false] - Optional flag, if true, will call `mergeObjects()` with `preventDuplicates`
14
15
  * @returns - The `validationData` with the additional errors from `additionalErrorSchema` merged into it, if provided.
15
16
  */
16
17
  export default function validationDataMerge<T = any>(
17
18
  validationData: ValidationData<T>,
18
19
  additionalErrorSchema?: ErrorSchema<T>,
20
+ preventDuplicates = false,
19
21
  ): ValidationData<T> {
20
22
  if (!additionalErrorSchema) {
21
23
  return validationData;
@@ -24,7 +26,11 @@ export default function validationDataMerge<T = any>(
24
26
  let errors = toErrorList(additionalErrorSchema);
25
27
  let errorSchema = additionalErrorSchema;
26
28
  if (!isEmpty(oldErrorSchema)) {
27
- errorSchema = mergeObjects(oldErrorSchema, additionalErrorSchema, true) as ErrorSchema<T>;
29
+ errorSchema = mergeObjects(
30
+ oldErrorSchema,
31
+ additionalErrorSchema,
32
+ preventDuplicates ? 'preventDuplicates' : true,
33
+ ) as ErrorSchema<T>;
28
34
  errors = [...oldErrors].concat(errors);
29
35
  }
30
36
  return { errorSchema, errors };