@rjsf/utils 6.0.0-beta.20 → 6.0.0-beta.22

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 (63) hide show
  1. package/dist/index.cjs +202 -68
  2. package/dist/index.cjs.map +4 -4
  3. package/dist/utils.esm.js +202 -68
  4. package/dist/utils.esm.js.map +4 -4
  5. package/dist/utils.umd.js +175 -57
  6. package/lib/ErrorSchemaBuilder.d.ts +2 -2
  7. package/lib/createSchemaUtils.js +6 -4
  8. package/lib/createSchemaUtils.js.map +1 -1
  9. package/lib/enums.d.ts +6 -0
  10. package/lib/enums.js +6 -0
  11. package/lib/enums.js.map +1 -1
  12. package/lib/guessType.d.ts +1 -1
  13. package/lib/idGenerators.d.ts +8 -1
  14. package/lib/idGenerators.js +10 -1
  15. package/lib/idGenerators.js.map +1 -1
  16. package/lib/index.d.ts +9 -4
  17. package/lib/index.js +9 -4
  18. package/lib/index.js.map +1 -1
  19. package/lib/isFormDataAvailable.d.ts +7 -0
  20. package/lib/isFormDataAvailable.js +13 -0
  21. package/lib/isFormDataAvailable.js.map +1 -0
  22. package/lib/isRootSchema.d.ts +13 -0
  23. package/lib/isRootSchema.js +25 -0
  24. package/lib/isRootSchema.js.map +1 -0
  25. package/lib/nameGenerators.d.ts +13 -0
  26. package/lib/nameGenerators.js +30 -0
  27. package/lib/nameGenerators.js.map +1 -0
  28. package/lib/schema/getDefaultFormState.d.ts +8 -3
  29. package/lib/schema/getDefaultFormState.js +15 -6
  30. package/lib/schema/getDefaultFormState.js.map +1 -1
  31. package/lib/schema/retrieveSchema.d.ts +10 -5
  32. package/lib/schema/retrieveSchema.js +37 -14
  33. package/lib/schema/retrieveSchema.js.map +1 -1
  34. package/lib/shouldRenderOptionalField.d.ts +18 -0
  35. package/lib/shouldRenderOptionalField.js +47 -0
  36. package/lib/shouldRenderOptionalField.js.map +1 -0
  37. package/lib/toFieldPathId.d.ts +4 -2
  38. package/lib/toFieldPathId.js +10 -3
  39. package/lib/toFieldPathId.js.map +1 -1
  40. package/lib/tsconfig.tsbuildinfo +1 -1
  41. package/lib/types.d.ts +87 -43
  42. package/lib/useDeepCompareMemo.d.ts +8 -0
  43. package/lib/useDeepCompareMemo.js +17 -0
  44. package/lib/useDeepCompareMemo.js.map +1 -0
  45. package/lib/validationDataMerge.d.ts +2 -1
  46. package/lib/validationDataMerge.js +3 -2
  47. package/lib/validationDataMerge.js.map +1 -1
  48. package/package.json +1 -1
  49. package/src/ErrorSchemaBuilder.ts +2 -2
  50. package/src/createSchemaUtils.ts +6 -1
  51. package/src/enums.ts +6 -0
  52. package/src/idGenerators.ts +11 -1
  53. package/src/index.ts +15 -2
  54. package/src/isFormDataAvailable.ts +13 -0
  55. package/src/isRootSchema.ts +30 -0
  56. package/src/nameGenerators.ts +43 -0
  57. package/src/schema/getDefaultFormState.ts +20 -2
  58. package/src/schema/retrieveSchema.ts +39 -5
  59. package/src/shouldRenderOptionalField.ts +56 -0
  60. package/src/toFieldPathId.ts +12 -2
  61. package/src/types.ts +100 -47
  62. package/src/useDeepCompareMemo.ts +17 -0
  63. package/src/validationDataMerge.ts +7 -1
package/src/types.ts CHANGED
@@ -2,6 +2,7 @@ import type {
2
2
  ButtonHTMLAttributes,
3
3
  ChangeEvent,
4
4
  ComponentType,
5
+ FocusEvent,
5
6
  HTMLAttributes,
6
7
  ReactElement,
7
8
  ReactNode,
@@ -35,6 +36,9 @@ export type FormContextType = GenericObjectType;
35
36
  */
36
37
  export type TestIdShape = Record<string, string>;
37
38
 
39
+ /** Function to generate HTML name attributes from path segments */
40
+ export type NameGeneratorFunction = (path: FieldPathList, idPrefix: string, isMultiValue?: boolean) => string;
41
+
38
42
  /** Experimental feature that specifies the Array `minItems` default form state behavior
39
43
  */
40
44
  export type Experimental_ArrayMinItems = {
@@ -175,6 +179,8 @@ export type FieldPathId = {
175
179
  $id: string;
176
180
  /** The path for a field */
177
181
  path: FieldPathList;
182
+ /** The optional HTML name attribute for a field, generated by nameGenerator if provided */
183
+ name?: string;
178
184
  };
179
185
 
180
186
  /** Type describing a name used for a field in the `PathSchema` */
@@ -327,9 +333,9 @@ export type TemplatesType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
327
333
  /** The template to use while rendering the description for an array field */
328
334
  ArrayFieldDescriptionTemplate: ComponentType<ArrayFieldDescriptionProps<T, S, F>>;
329
335
  /** The template to use while rendering the buttons for an item in an array field */
330
- ArrayFieldItemButtonsTemplate: ComponentType<ArrayFieldItemButtonsTemplateType<T, S, F>>;
336
+ ArrayFieldItemButtonsTemplate: ComponentType<ArrayFieldItemButtonsTemplateProps<T, S, F>>;
331
337
  /** The template to use while rendering an item in an array field */
332
- ArrayFieldItemTemplate: ComponentType<ArrayFieldItemTemplateType<T, S, F>>;
338
+ ArrayFieldItemTemplate: ComponentType<ArrayFieldItemTemplateProps<T, S, F>>;
333
339
  /** The template to use while rendering the title for an array field */
334
340
  ArrayFieldTitleTemplate: ComponentType<ArrayFieldTitleProps<T, S, F>>;
335
341
  /** The template to use while rendering the standard html input */
@@ -350,6 +356,8 @@ export type TemplatesType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
350
356
  MultiSchemaFieldTemplate: ComponentType<MultiSchemaFieldTemplateProps<T, S, F>>;
351
357
  /** The template to use while rendering an object */
352
358
  ObjectFieldTemplate: ComponentType<ObjectFieldTemplateProps<T, S, F>>;
359
+ /** The template to use while rendering the Optional Data field controls */
360
+ OptionalDataControlsTemplate: ComponentType<OptionalDataControlsTemplateProps<T, S, F>>;
353
361
  /** The template to use for rendering the title of a field */
354
362
  TitleFieldTemplate: ComponentType<TitleFieldProps<T, S, F>>;
355
363
  /** The template to use for rendering information about an unsupported field type in the schema */
@@ -398,6 +406,10 @@ export type GlobalUISchemaOptions = GenericObjectType & {
398
406
  /** Enables the displaying of description text that contains markdown
399
407
  */
400
408
  enableMarkdownInDescription?: boolean;
409
+ /** Enables the rendering of the Optional Data Field UI for specific types of schemas, either `object`, `array` or
410
+ * both. To disable the Optional Data Field UI for a specific field, provide an empty array within the UI schema.
411
+ */
412
+ enableOptionalDataFieldForType?: ('object' | 'array')[];
401
413
  };
402
414
 
403
415
  /** The set of options from the `Form` that will be available on the `Registry` for use in everywhere the `registry` is
@@ -414,6 +426,11 @@ export type GlobalFormOptions = {
414
426
  readonly idSeparator: string;
415
427
  /** The component update strategy used by the Form and its fields for performance optimization */
416
428
  readonly experimental_componentUpdateStrategy?: 'customDeep' | 'shallow' | 'always';
429
+ /** Optional function to generate custom HTML name attributes for form elements. Receives the field path segments
430
+ * and element type (object or array), and returns a custom name string. This allows backends like PHP/Rails
431
+ * (`root[tasks][0][title]`) or Django (`root__tasks-0__title`) to receive form data in their expected format.
432
+ */
433
+ readonly nameGenerator?: NameGeneratorFunction;
417
434
  };
418
435
 
419
436
  /** The object containing the registered core, theme and custom fields and widgets as well as the root schema, form
@@ -541,10 +558,15 @@ export type FieldTemplateProps<
541
558
  formData?: T;
542
559
  /** The value change event handler; Can be called with a new value to change the value for this field */
543
560
  onChange: FieldProps<T, S, F>['onChange'];
544
- /** The key change event handler; Called when the key associated with a field is changed for an additionalProperty */
545
- onKeyChange: (value: string) => () => void;
546
- /** The property drop/removal event handler; Called when a field is removed in an additionalProperty context */
547
- onDropPropertyClick: (value: string) => () => void;
561
+ /** Callback used to handle the changing of an additional property key's name with the new value
562
+ */
563
+ onKeyRename: (newKey: string) => void;
564
+ /** Callback used to handle the changing of an additional property key's name when the input is blurred. The event's
565
+ * target's value will be used as the new value. Its a wrapper callback around `onKeyRename`
566
+ */
567
+ onKeyRenameBlur: (event: FocusEvent<HTMLInputElement>) => void;
568
+ /** Callback used to handle the removal of the additionalProperty */
569
+ onRemoveProperty: () => void;
548
570
  };
549
571
 
550
572
  /** The properties that are passed to the `UnsupportedFieldTemplate` implementation */
@@ -571,6 +593,8 @@ export type TitleFieldProps<
571
593
  title: string;
572
594
  /** A boolean value stating if the field is required */
573
595
  required?: boolean;
596
+ /** Add optional data control */
597
+ optionalDataControl?: ReactNode;
574
598
  };
575
599
 
576
600
  /** The properties that are passed to a `DescriptionFieldTemplate` implementation */
@@ -595,6 +619,8 @@ export type ArrayFieldTitleProps<
595
619
  title?: string;
596
620
  /** The FieldPathId of the field in the hierarchy */
597
621
  fieldPathId: FieldPathId;
622
+ /** Add optional data control */
623
+ optionalDataControl?: ReactNode;
598
624
  };
599
625
 
600
626
  /** The properties that are passed to a `ArrayFieldDescriptionTemplate` implementation */
@@ -610,7 +636,7 @@ export type ArrayFieldDescriptionProps<
610
636
  };
611
637
 
612
638
  /** The properties of the buttons to render for each element in the ArrayFieldTemplateProps.items array */
613
- export type ArrayFieldItemButtonsTemplateType<
639
+ export type ArrayFieldItemButtonsTemplateProps<
614
640
  T = any,
615
641
  S extends StrictRJSFSchema = RJSFSchema,
616
642
  F extends FormContextType = any,
@@ -637,20 +663,22 @@ export type ArrayFieldItemButtonsTemplateType<
637
663
  index: number;
638
664
  /** A number stating the total number `items` in the array */
639
665
  totalItems: number;
640
- /** Returns a function that adds a new item at `index` */
641
- onAddIndexClick: (index: number) => (event?: any) => void;
642
- /** Returns a function that copies the item at `index` into the position at `index + 1` */
643
- onCopyIndexClick: (index: number) => (event?: any) => void;
644
- /** Returns a function that removes the item at `index` */
645
- onDropIndexClick: (index: number) => (event?: any) => void;
646
- /** Returns a function that swaps the items at `index` with `newIndex` */
647
- onReorderClick: (index: number, newIndex: number) => (event?: any) => void;
666
+ /** Callback function that adds a new item below this item */
667
+ onAddItem: (event?: any) => void;
668
+ /** Callback function that copies this item below itself */
669
+ onCopyItem: (event?: any) => void;
670
+ /** Callback function that moves the item up one spot in the list */
671
+ onMoveUpItem: (event?: any) => void;
672
+ /** Callback function that moves the item down one spot in the list */
673
+ onMoveDownItem: (event?: any) => void;
674
+ /** Callback function that removes the item from the list */
675
+ onRemoveItem: (event?: any) => void;
648
676
  /** A boolean value stating if the array item is read-only */
649
677
  readonly?: boolean;
650
678
  };
651
679
 
652
- /** The properties of each element in the ArrayFieldTemplateProps.items array */
653
- export type ArrayFieldItemTemplateType<
680
+ /** The properties used to render the ArrayFieldItemTemplate */
681
+ export type ArrayFieldItemTemplateProps<
654
682
  T = any,
655
683
  S extends StrictRJSFSchema = RJSFSchema,
656
684
  F extends FormContextType = any,
@@ -658,7 +686,7 @@ export type ArrayFieldItemTemplateType<
658
686
  /** The html for the item's content */
659
687
  children: ReactNode;
660
688
  /** The props to pass to the `ArrayFieldItemButtonTemplate` */
661
- buttonsProps: ArrayFieldItemButtonsTemplateType<T, S, F>;
689
+ buttonsProps: ArrayFieldItemButtonsTemplateProps<T, S, F>;
662
690
  /** The className string */
663
691
  className: string;
664
692
  /** A boolean value stating if the array item is disabled */
@@ -672,36 +700,25 @@ export type ArrayFieldItemTemplateType<
672
700
  /** A boolean value stating if the array item is read-only */
673
701
  readonly?: boolean;
674
702
  /** A stable, unique key for the array item */
675
- key: string;
703
+ itemKey: string;
704
+ /** The UI schema of the array item's parent array field used for
705
+ * customization in some themes
706
+ */
707
+ parentUiSchema?: UiSchema<T, S, F>;
676
708
  };
677
709
 
678
- /**
679
- * @deprecated - Use `ArrayFieldItemTemplateType` instead
680
- */
681
- export type ArrayFieldTemplateItemType<
682
- T = any,
683
- S extends StrictRJSFSchema = RJSFSchema,
684
- F extends FormContextType = any,
685
- > = ArrayFieldItemTemplateType<T, S, F>;
686
-
687
- /** The properties that are passed to an `ArrayFieldTemplate` implementation */
688
- export type ArrayFieldTemplateProps<
710
+ /** The common properties of the two container templates: `ArrayFieldTemplateProps` and `ObjectFieldTemplateProps` */
711
+ export type ContainerFieldTemplateProps<
689
712
  T = any,
690
713
  S extends StrictRJSFSchema = RJSFSchema,
691
714
  F extends FormContextType = any,
692
715
  > = RJSFBaseProps<T, S, F> & {
693
- /** A boolean value stating whether new elements can be added to the array */
694
- canAdd?: boolean;
695
716
  /** The className string */
696
717
  className?: string;
697
718
  /** A boolean value stating if the array is disabled */
698
719
  disabled?: boolean;
699
720
  /** The FieldPathId of the field in the hierarchy */
700
721
  fieldPathId: FieldPathId;
701
- /** An array of objects representing the items in the array */
702
- items: ArrayFieldItemTemplateType<T, S, F>[];
703
- /** A function that adds a new item to the array */
704
- onAddClick: (event?: any) => void;
705
722
  /** A boolean value stating if the array is read-only */
706
723
  readonly?: boolean;
707
724
  /** A boolean value stating if the array is required */
@@ -712,8 +729,24 @@ export type ArrayFieldTemplateProps<
712
729
  title: string;
713
730
  /** The formData for this array */
714
731
  formData?: T;
715
- /** The tree of errors for this field and its children */
732
+ /** The optional validation errors in the form of an `ErrorSchema` */
716
733
  errorSchema?: ErrorSchema<T>;
734
+ /** The optional data control node to render within the ObjectFieldTemplate that controls */
735
+ optionalDataControl?: ReactNode;
736
+ };
737
+
738
+ /** The properties that are passed to an `ArrayFieldTemplate` implementation */
739
+ export type ArrayFieldTemplateProps<
740
+ T = any,
741
+ S extends StrictRJSFSchema = RJSFSchema,
742
+ F extends FormContextType = any,
743
+ > = ContainerFieldTemplateProps<T, S, F> & {
744
+ /** A boolean value stating whether new elements can be added to the array */
745
+ canAdd?: boolean;
746
+ /** An array of React elements representing the items in the array */
747
+ items: ReactElement[];
748
+ /** A function that adds a new item to the end of the array */
749
+ onAddClick: (event?: any) => void;
717
750
  /** An array of strings listing all generated error messages from encountered errors for this widget */
718
751
  rawErrors?: string[];
719
752
  };
@@ -737,17 +770,15 @@ export type ObjectFieldTemplateProps<
737
770
  T = any,
738
771
  S extends StrictRJSFSchema = RJSFSchema,
739
772
  F extends FormContextType = any,
740
- > = RJSFBaseProps<T, S, F> & {
741
- /** A string value containing the title for the object */
742
- title: string;
773
+ > = ContainerFieldTemplateProps<T, S, F> & {
743
774
  /** A string value containing the description for the object */
744
775
  description?: string | ReactElement;
745
- /** A boolean value stating if the object is disabled */
746
- disabled?: boolean;
747
776
  /** An array of objects representing the properties in the object */
748
777
  properties: ObjectFieldTemplatePropertyType[];
749
- /** Returns a function that adds a new property to the object (to be used with additionalProperties) */
750
- onAddClick: (schema: S) => () => void;
778
+ /** Callback to use in order to add an new additionalProperty to the object field (to be used with
779
+ * additionalProperties and patternProperties)
780
+ */
781
+ onAddProperty: () => void;
751
782
  /** A boolean value stating if the object is read-only */
752
783
  readonly?: boolean;
753
784
  /** A boolean value stating if the object is required */
@@ -762,6 +793,22 @@ export type ObjectFieldTemplateProps<
762
793
  formData?: T;
763
794
  };
764
795
 
796
+ /** The properties that are passed to a OptionalDataControlsTemplate implementation */
797
+ export type OptionalDataControlsTemplateProps<
798
+ T = any,
799
+ S extends StrictRJSFSchema = RJSFSchema,
800
+ F extends FormContextType = any,
801
+ > = RJSFBaseProps<T, S, F> & {
802
+ /** The generated id for this Optional Data Control instance */
803
+ id: string;
804
+ /** The label to use for the Optional Data Control */
805
+ label: string;
806
+ /** Optional callback to call when clicking on the Optional Data Control to add data */
807
+ onRemoveClick?: () => void;
808
+ /** Optional callback to call when clicking on the Optional Data Control to remove data */
809
+ onAddClick?: () => void;
810
+ };
811
+
765
812
  /** The properties that are passed to a WrapIfAdditionalTemplate implementation */
766
813
  export type WrapIfAdditionalTemplateProps<
767
814
  T = any,
@@ -783,8 +830,9 @@ export type WrapIfAdditionalTemplateProps<
783
830
  | 'disabled'
784
831
  | 'schema'
785
832
  | 'uiSchema'
786
- | 'onKeyChange'
787
- | 'onDropPropertyClick'
833
+ | 'onKeyRename'
834
+ | 'onKeyRenameBlur'
835
+ | 'onRemoveProperty'
788
836
  | 'registry'
789
837
  >;
790
838
 
@@ -850,6 +898,8 @@ export interface WidgetProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F
850
898
  multiple?: boolean;
851
899
  /** An array of strings listing all generated error messages from encountered errors for this widget */
852
900
  rawErrors?: string[];
901
+ /** The optional custom HTML name attribute generated by the nameGenerator function, if provided */
902
+ htmlName?: string;
853
903
  }
854
904
 
855
905
  /** The definition of a React-based Widget component */
@@ -1194,12 +1244,14 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1194
1244
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1195
1245
  * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1196
1246
  * false when computing defaults for any nested object properties.
1247
+ * @param initialDefaultsGenerated - Indicates whether or not initial defaults have been generated
1197
1248
  * @returns - The resulting `formData` with all the defaults provided
1198
1249
  */
1199
1250
  getDefaultFormState(
1200
1251
  schema: S,
1201
1252
  formData?: T,
1202
1253
  includeUndefinedValues?: boolean | 'excludeObjectChildren',
1254
+ initialDefaultsGenerated?: boolean,
1203
1255
  ): T | T[] | undefined;
1204
1256
  /** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
1205
1257
  * should be displayed in a UI.
@@ -1275,9 +1327,10 @@ export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchem
1275
1327
  *
1276
1328
  * @param schema - The schema for which retrieving a schema is desired
1277
1329
  * @param [formData] - The current formData, if any, to assist retrieving a schema
1330
+ * @param [resolveAnyOfOrOneOfRefs] - Optional flag indicating whether to resolved refs in anyOf/oneOf lists
1278
1331
  * @returns - The schema having its conditions, additional properties, references and dependencies resolved
1279
1332
  */
1280
- retrieveSchema(schema: S, formData?: T): S;
1333
+ retrieveSchema(schema: S, formData?: T, resolveAnyOfOrOneOfRefs?: boolean): S;
1281
1334
  /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
1282
1335
  * new schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the
1283
1336
  * nature of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the
@@ -0,0 +1,17 @@
1
+ import { useRef } from 'react';
2
+ import isEqual from 'lodash/isEqual';
3
+
4
+ /** Hook that stores and returns a `T` value. If `newValue` is the same as the stored one, then the stored one is
5
+ * returned to avoid having a component rerender due it being a different object. Otherwise, the `newValue` is stored
6
+ * and returned.
7
+ *
8
+ * @param newValue - The potential new `T` value
9
+ * @returns - The latest stored `T` value
10
+ */
11
+ export default function useDeepCompareMemo<T = unknown>(newValue: T): T {
12
+ const valueRef = useRef<T>(newValue);
13
+ if (!isEqual(newValue, valueRef.current)) {
14
+ valueRef.current = newValue;
15
+ }
16
+ return valueRef.current;
17
+ }
@@ -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 };