@rjsf/core 6.0.0-beta.8 → 6.0.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.
Files changed (160) hide show
  1. package/README.md +2 -0
  2. package/dist/core.umd.js +2042 -1987
  3. package/dist/index.cjs +4909 -0
  4. package/dist/index.cjs.map +7 -0
  5. package/dist/index.esm.js +2509 -2389
  6. package/dist/index.esm.js.map +4 -4
  7. package/lib/components/Form.d.ts +137 -34
  8. package/lib/components/Form.d.ts.map +1 -1
  9. package/lib/components/Form.js +318 -173
  10. package/lib/components/fields/ArrayField.d.ts +2 -187
  11. package/lib/components/fields/ArrayField.d.ts.map +1 -1
  12. package/lib/components/fields/ArrayField.js +526 -492
  13. package/lib/components/fields/BooleanField.d.ts.map +1 -1
  14. package/lib/components/fields/BooleanField.js +8 -3
  15. package/lib/components/fields/FallbackField.d.ts +7 -0
  16. package/lib/components/fields/FallbackField.d.ts.map +1 -0
  17. package/lib/components/fields/FallbackField.js +72 -0
  18. package/lib/components/fields/LayoutGridField.d.ts +109 -186
  19. package/lib/components/fields/LayoutGridField.d.ts.map +1 -1
  20. package/lib/components/fields/LayoutGridField.js +426 -426
  21. package/lib/components/fields/LayoutHeaderField.d.ts +1 -1
  22. package/lib/components/fields/LayoutHeaderField.js +3 -3
  23. package/lib/components/fields/LayoutMultiSchemaField.d.ts.map +1 -1
  24. package/lib/components/fields/LayoutMultiSchemaField.js +6 -6
  25. package/lib/components/fields/MultiSchemaField.d.ts.map +1 -1
  26. package/lib/components/fields/MultiSchemaField.js +16 -10
  27. package/lib/components/fields/NullField.js +3 -3
  28. package/lib/components/fields/NumberField.d.ts.map +1 -1
  29. package/lib/components/fields/NumberField.js +3 -3
  30. package/lib/components/fields/ObjectField.d.ts +2 -68
  31. package/lib/components/fields/ObjectField.d.ts.map +1 -1
  32. package/lib/components/fields/ObjectField.js +163 -163
  33. package/lib/components/fields/OptionalDataControlsField.d.ts +8 -0
  34. package/lib/components/fields/OptionalDataControlsField.d.ts.map +1 -0
  35. package/lib/components/fields/OptionalDataControlsField.js +43 -0
  36. package/lib/components/fields/SchemaField.d.ts.map +1 -1
  37. package/lib/components/fields/SchemaField.js +52 -30
  38. package/lib/components/fields/StringField.d.ts.map +1 -1
  39. package/lib/components/fields/StringField.js +8 -3
  40. package/lib/components/fields/index.d.ts.map +1 -1
  41. package/lib/components/fields/index.js +4 -0
  42. package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts +1 -1
  43. package/lib/components/templates/ArrayFieldDescriptionTemplate.js +3 -3
  44. package/lib/components/templates/ArrayFieldItemButtonsTemplate.d.ts +3 -3
  45. package/lib/components/templates/ArrayFieldItemButtonsTemplate.d.ts.map +1 -1
  46. package/lib/components/templates/ArrayFieldItemButtonsTemplate.js +3 -8
  47. package/lib/components/templates/ArrayFieldItemTemplate.d.ts +3 -3
  48. package/lib/components/templates/ArrayFieldItemTemplate.d.ts.map +1 -1
  49. package/lib/components/templates/ArrayFieldItemTemplate.js +1 -1
  50. package/lib/components/templates/ArrayFieldTemplate.d.ts +1 -1
  51. package/lib/components/templates/ArrayFieldTemplate.d.ts.map +1 -1
  52. package/lib/components/templates/ArrayFieldTemplate.js +4 -5
  53. package/lib/components/templates/ArrayFieldTitleTemplate.d.ts +1 -1
  54. package/lib/components/templates/ArrayFieldTitleTemplate.d.ts.map +1 -1
  55. package/lib/components/templates/ArrayFieldTitleTemplate.js +3 -3
  56. package/lib/components/templates/BaseInputTemplate.js +2 -2
  57. package/lib/components/templates/ButtonTemplates/AddButton.d.ts +1 -1
  58. package/lib/components/templates/ButtonTemplates/AddButton.d.ts.map +1 -1
  59. package/lib/components/templates/ButtonTemplates/AddButton.js +2 -2
  60. package/lib/components/templates/FallbackFieldTemplate.d.ts +7 -0
  61. package/lib/components/templates/FallbackFieldTemplate.d.ts.map +1 -0
  62. package/lib/components/templates/FallbackFieldTemplate.js +12 -0
  63. package/lib/components/templates/FieldErrorTemplate.js +2 -2
  64. package/lib/components/templates/FieldHelpTemplate.js +2 -2
  65. package/lib/components/templates/MultiSchemaFieldTemplate.d.ts +8 -0
  66. package/lib/components/templates/MultiSchemaFieldTemplate.d.ts.map +1 -0
  67. package/lib/components/templates/MultiSchemaFieldTemplate.js +10 -0
  68. package/lib/components/templates/ObjectFieldTemplate.d.ts.map +1 -1
  69. package/lib/components/templates/ObjectFieldTemplate.js +3 -2
  70. package/lib/components/templates/OptionalDataControlsTemplate.d.ts +11 -0
  71. package/lib/components/templates/OptionalDataControlsTemplate.d.ts.map +1 -0
  72. package/lib/components/templates/OptionalDataControlsTemplate.js +20 -0
  73. package/lib/components/templates/TitleField.d.ts.map +1 -1
  74. package/lib/components/templates/TitleField.js +2 -2
  75. package/lib/components/templates/UnsupportedField.js +3 -3
  76. package/lib/components/templates/WrapIfAdditionalTemplate.js +2 -2
  77. package/lib/components/templates/index.d.ts.map +1 -1
  78. package/lib/components/templates/index.js +6 -0
  79. package/lib/components/widgets/AltDateWidget.d.ts +1 -1
  80. package/lib/components/widgets/AltDateWidget.d.ts.map +1 -1
  81. package/lib/components/widgets/AltDateWidget.js +5 -46
  82. package/lib/components/widgets/CheckboxWidget.d.ts +1 -1
  83. package/lib/components/widgets/CheckboxWidget.d.ts.map +1 -1
  84. package/lib/components/widgets/CheckboxWidget.js +2 -2
  85. package/lib/components/widgets/CheckboxesWidget.d.ts +1 -1
  86. package/lib/components/widgets/CheckboxesWidget.d.ts.map +1 -1
  87. package/lib/components/widgets/CheckboxesWidget.js +4 -4
  88. package/lib/components/widgets/FileWidget.d.ts.map +1 -1
  89. package/lib/components/widgets/FileWidget.js +7 -87
  90. package/lib/components/widgets/HiddenWidget.d.ts +1 -1
  91. package/lib/components/widgets/HiddenWidget.d.ts.map +1 -1
  92. package/lib/components/widgets/HiddenWidget.js +2 -2
  93. package/lib/components/widgets/RadioWidget.d.ts +1 -1
  94. package/lib/components/widgets/RadioWidget.d.ts.map +1 -1
  95. package/lib/components/widgets/RadioWidget.js +2 -2
  96. package/lib/components/widgets/RatingWidget.d.ts +1 -1
  97. package/lib/components/widgets/RatingWidget.d.ts.map +1 -1
  98. package/lib/components/widgets/RatingWidget.js +2 -2
  99. package/lib/components/widgets/SelectWidget.d.ts +1 -1
  100. package/lib/components/widgets/SelectWidget.d.ts.map +1 -1
  101. package/lib/components/widgets/SelectWidget.js +2 -2
  102. package/lib/components/widgets/TextareaWidget.d.ts +1 -1
  103. package/lib/components/widgets/TextareaWidget.d.ts.map +1 -1
  104. package/lib/components/widgets/TextareaWidget.js +2 -2
  105. package/lib/getDefaultRegistry.d.ts.map +1 -1
  106. package/lib/getDefaultRegistry.js +6 -1
  107. package/lib/getTestRegistry.d.ts +5 -0
  108. package/lib/getTestRegistry.d.ts.map +1 -0
  109. package/lib/getTestRegistry.js +23 -0
  110. package/lib/index.d.ts +2 -1
  111. package/lib/index.d.ts.map +1 -1
  112. package/lib/index.js +2 -1
  113. package/lib/tsconfig.tsbuildinfo +1 -1
  114. package/package.json +35 -20
  115. package/src/components/Form.tsx +468 -206
  116. package/src/components/fields/ArrayField.tsx +871 -723
  117. package/src/components/fields/BooleanField.tsx +14 -5
  118. package/src/components/fields/FallbackField.tsx +157 -0
  119. package/src/components/fields/LayoutGridField.tsx +626 -603
  120. package/src/components/fields/LayoutHeaderField.tsx +3 -3
  121. package/src/components/fields/LayoutMultiSchemaField.tsx +9 -10
  122. package/src/components/fields/MultiSchemaField.tsx +57 -36
  123. package/src/components/fields/NullField.tsx +3 -3
  124. package/src/components/fields/NumberField.tsx +11 -3
  125. package/src/components/fields/ObjectField.tsx +308 -239
  126. package/src/components/fields/OptionalDataControlsField.tsx +84 -0
  127. package/src/components/fields/SchemaField.tsx +75 -94
  128. package/src/components/fields/StringField.tsx +14 -5
  129. package/src/components/fields/index.ts +4 -0
  130. package/src/components/templates/ArrayFieldDescriptionTemplate.tsx +3 -3
  131. package/src/components/templates/ArrayFieldItemButtonsTemplate.tsx +16 -21
  132. package/src/components/templates/ArrayFieldItemTemplate.tsx +3 -3
  133. package/src/components/templates/ArrayFieldTemplate.tsx +11 -18
  134. package/src/components/templates/ArrayFieldTitleTemplate.tsx +4 -3
  135. package/src/components/templates/BaseInputTemplate.tsx +5 -5
  136. package/src/components/templates/ButtonTemplates/AddButton.tsx +2 -0
  137. package/src/components/templates/FallbackFieldTemplate.tsx +28 -0
  138. package/src/components/templates/FieldErrorTemplate.tsx +2 -2
  139. package/src/components/templates/FieldHelpTemplate.tsx +2 -2
  140. package/src/components/templates/MultiSchemaFieldTemplate.tsx +20 -0
  141. package/src/components/templates/ObjectFieldTemplate.tsx +12 -7
  142. package/src/components/templates/OptionalDataControlsTemplate.tsx +43 -0
  143. package/src/components/templates/TitleField.tsx +6 -1
  144. package/src/components/templates/UnsupportedField.tsx +3 -3
  145. package/src/components/templates/WrapIfAdditionalTemplate.tsx +5 -5
  146. package/src/components/templates/index.ts +6 -0
  147. package/src/components/widgets/AltDateWidget.tsx +8 -126
  148. package/src/components/widgets/CheckboxWidget.tsx +4 -3
  149. package/src/components/widgets/CheckboxesWidget.tsx +5 -4
  150. package/src/components/widgets/FileWidget.tsx +11 -102
  151. package/src/components/widgets/HiddenWidget.tsx +2 -1
  152. package/src/components/widgets/RadioWidget.tsx +3 -2
  153. package/src/components/widgets/RatingWidget.tsx +2 -1
  154. package/src/components/widgets/SelectWidget.tsx +3 -2
  155. package/src/components/widgets/TextareaWidget.tsx +3 -2
  156. package/src/getDefaultRegistry.ts +14 -1
  157. package/src/getTestRegistry.tsx +38 -0
  158. package/src/index.ts +2 -1
  159. package/dist/index.js +0 -4834
  160. package/dist/index.js.map +0 -7
@@ -1,21 +1,28 @@
1
- import { Component } from 'react';
1
+ import { FocusEvent, useCallback, useState } from 'react';
2
2
  import {
3
+ ADDITIONAL_PROPERTY_FLAG,
4
+ ANY_OF_KEY,
3
5
  getTemplate,
4
6
  getUiOptions,
7
+ hashObject,
8
+ isFormDataAvailable,
5
9
  orderProperties,
10
+ shouldRenderOptionalField,
11
+ toFieldPathId,
12
+ useDeepCompareMemo,
6
13
  ErrorSchema,
14
+ FieldPathId,
15
+ FieldPathList,
7
16
  FieldProps,
8
17
  FormContextType,
9
18
  GenericObjectType,
10
- IdSchema,
19
+ ONE_OF_KEY,
20
+ PROPERTIES_KEY,
21
+ REF_KEY,
22
+ Registry,
11
23
  RJSFSchema,
12
24
  StrictRJSFSchema,
13
25
  TranslatableString,
14
- ADDITIONAL_PROPERTY_FLAG,
15
- PROPERTIES_KEY,
16
- REF_KEY,
17
- ANY_OF_KEY,
18
- ONE_OF_KEY,
19
26
  } from '@rjsf/utils';
20
27
  import Markdown from 'markdown-to-jsx';
21
28
  import get from 'lodash/get';
@@ -24,38 +31,86 @@ import isObject from 'lodash/isObject';
24
31
  import set from 'lodash/set';
25
32
  import unset from 'lodash/unset';
26
33
 
27
- /** Type used for the state of the `ObjectField` component */
28
- type ObjectFieldState = {
29
- /** Flag indicating whether an additional property key was modified */
30
- wasPropertyKeyModified: boolean;
31
- /** The set of additional properties */
32
- additionalProperties: object;
33
- };
34
-
35
- /** The `ObjectField` component is used to render a field in the schema that is of type `object`. It tracks whether an
36
- * additional property key was modified and what it was modified to
34
+ /** Returns a flag indicating whether the `name` field is required in the object schema
37
35
  *
38
- * @param props - The `FieldProps` for this template
36
+ * @param schema - The schema to check
37
+ * @param name - The name of the field to check for required-ness
38
+ * @returns - True if the field `name` is required, false otherwise
39
39
  */
40
- class ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> extends Component<
41
- FieldProps<T, S, F>,
42
- ObjectFieldState
43
- > {
44
- /** Set up the initial state */
45
- state = {
46
- wasPropertyKeyModified: false,
47
- additionalProperties: {},
48
- };
40
+ function isRequired<S extends StrictRJSFSchema = RJSFSchema>(schema: S, name: string) {
41
+ return Array.isArray(schema.required) && schema.required.indexOf(name) !== -1;
42
+ }
49
43
 
50
- /** Returns a flag indicating whether the `name` field is required in the object schema
51
- *
52
- * @param name - The name of the field to check for required-ness
53
- * @returns - True if the field `name` is required, false otherwise
54
- */
55
- isRequired(name: string) {
56
- const { schema } = this.props;
57
- return Array.isArray(schema.required) && schema.required.indexOf(name) !== -1;
44
+ /** Returns a default value to be used for a new additional schema property of the given `type`
45
+ *
46
+ * @param translateString - The string translation function from the registry
47
+ * @param type - The type of the new additional schema property
48
+ */
49
+ function getDefaultValue<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
50
+ translateString: Registry<T, S, F>['translateString'],
51
+ type?: RJSFSchema['type'],
52
+ ) {
53
+ switch (type) {
54
+ case 'array':
55
+ return [];
56
+ case 'boolean':
57
+ return false;
58
+ case 'null':
59
+ return null;
60
+ case 'number':
61
+ return 0;
62
+ case 'object':
63
+ return {};
64
+ case 'string':
65
+ default:
66
+ // We don't have a datatype for some reason (perhaps additionalProperties was true)
67
+ return translateString(TranslatableString.NewStringDefault);
58
68
  }
69
+ }
70
+
71
+ /** Props for the `ObjectFieldProperty` component */
72
+ interface ObjectFieldPropertyProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>
73
+ extends Omit<FieldProps<T, S, F>, 'name'> {
74
+ /** The name of the property within the parent object */
75
+ propertyName: string;
76
+ /** Flag indicating whether this property was added by the additionalProperties UI */
77
+ addedByAdditionalProperties: boolean;
78
+ /** Callback that handles the rename of an additionalProperties-based property key */
79
+ handleKeyRename: (oldKey: string, newKey: string) => void;
80
+ /** Callback that handles the removal of an additionalProperties-based property with key */
81
+ handleRemoveProperty: (keyName: string) => void;
82
+ }
83
+
84
+ /** The `ObjectFieldProperty` component is used to render the `SchemaField` for a child property of an object
85
+ */
86
+ function ObjectFieldProperty<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
87
+ props: ObjectFieldPropertyProps<T, S, F>,
88
+ ) {
89
+ const {
90
+ fieldPathId,
91
+ schema,
92
+ registry,
93
+ uiSchema,
94
+ errorSchema,
95
+ formData,
96
+ onChange,
97
+ onBlur,
98
+ onFocus,
99
+ disabled,
100
+ readonly,
101
+ required,
102
+ hideError,
103
+ propertyName,
104
+ handleKeyRename,
105
+ handleRemoveProperty,
106
+ addedByAdditionalProperties,
107
+ } = props;
108
+ const [wasPropertyKeyModified, setWasPropertyKeyModified] = useState(false);
109
+ const { globalFormOptions, fields } = registry;
110
+ const { SchemaField } = fields;
111
+ const innerFieldIdPathId = useDeepCompareMemo<FieldPathId>(
112
+ toFieldPathId(propertyName, globalFormOptions, fieldPathId.path),
113
+ );
59
114
 
60
115
  /** Returns the `onPropertyChange` handler for the `name` field. Handles the special case where a user is attempting
61
116
  * to clear the data for a field added as an additional property. Calls the `onChange()` handler with the updated
@@ -65,142 +120,147 @@ class ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
65
120
  * @param addedByAdditionalProperties - Flag indicating whether this property is an additional property
66
121
  * @returns - The onPropertyChange callback for the `name` property
67
122
  */
68
- onPropertyChange = (name: string, addedByAdditionalProperties = false) => {
69
- return (value: T | undefined, newErrorSchema?: ErrorSchema<T>, id?: string) => {
70
- const { formData, onChange, errorSchema } = this.props;
123
+ const onPropertyChange = useCallback(
124
+ (value: T | undefined, path: FieldPathList, newErrorSchema?: ErrorSchema<T>, id?: string) => {
71
125
  if (value === undefined && addedByAdditionalProperties) {
72
- // Don't set value = undefined for fields added by
73
- // additionalProperties. Doing so removes them from the
74
- // formData, which causes them to completely disappear
75
- // (including the input field for the property name). Unlike
76
- // fields which are "mandated" by the schema, these fields can
77
- // be set to undefined by clicking a "delete field" button, so
78
- // set empty values to the empty string.
126
+ // Don't set value = undefined for fields added by additionalProperties. Doing so removes them from the
127
+ // formData, which causes them to completely disappear (including the input field for the property name). Unlike
128
+ // fields which are "mandated" by the schema, these fields can be set to undefined by clicking a "delete field"
129
+ // button, so set empty values to the empty string.
79
130
  value = '' as unknown as T;
80
131
  }
81
- const newFormData = { ...formData, [name]: value } as unknown as T;
82
- onChange(
83
- newFormData,
84
- errorSchema &&
85
- errorSchema && {
86
- ...errorSchema,
87
- [name]: newErrorSchema,
88
- },
89
- id,
90
- );
91
- };
92
- };
132
+ onChange(value, path, newErrorSchema, id);
133
+ },
134
+ [onChange, addedByAdditionalProperties],
135
+ );
93
136
 
94
- /** Returns a callback to handle the onDropPropertyClick event for the given `key` which removes the old `key` data
95
- * and calls the `onChange` callback with it
96
- *
97
- * @param key - The key for which the drop callback is desired
98
- * @returns - The drop property click callback
137
+ /** The key change event handler; Called when the key associated with a field is changed for an additionalProperty.
138
+ * simply returns a function that call the `handleKeyChange()` event with the value
99
139
  */
100
- onDropPropertyClick = (key: string) => {
101
- return (event: DragEvent) => {
102
- event.preventDefault();
103
- const { onChange, formData } = this.props;
104
- const copiedFormData = { ...formData } as T;
105
- unset(copiedFormData, key);
106
- onChange(copiedFormData);
107
- };
108
- };
140
+ const onKeyRename = useCallback(
141
+ (value: string) => {
142
+ if (propertyName !== value) {
143
+ setWasPropertyKeyModified(true);
144
+ }
145
+ handleKeyRename(propertyName, value);
146
+ },
147
+ [propertyName, handleKeyRename],
148
+ );
109
149
 
110
- /** Computes the next available key name from the `preferredKey`, indexing through the already existing keys until one
111
- * that is already not assigned is found.
112
- *
113
- * @param preferredKey - The preferred name of a new key
114
- * @param [formData] - The form data in which to check if the desired key already exists
115
- * @returns - The name of the next available key from `preferredKey`
150
+ /** Returns a callback the handle the blur event, getting the value from the target and passing that along to the
151
+ * `handleKeyChange` function
116
152
  */
117
- getAvailableKey = (preferredKey: string, formData?: T) => {
118
- const { uiSchema, registry } = this.props;
119
- const { duplicateKeySuffixSeparator = '-' } = getUiOptions<T, S, F>(uiSchema, registry.globalUiOptions);
153
+ const onKeyRenameBlur = useCallback(
154
+ (event: FocusEvent<HTMLInputElement>) => {
155
+ const {
156
+ target: { value },
157
+ } = event;
158
+ onKeyRename(value);
159
+ },
160
+ [onKeyRename],
161
+ );
120
162
 
121
- let index = 0;
122
- let newKey = preferredKey;
123
- while (has(formData, newKey)) {
124
- newKey = `${preferredKey}${duplicateKeySuffixSeparator}${++index}`;
125
- }
126
- return newKey;
127
- };
128
-
129
- /** Returns a callback function that deals with the rename of a key for an additional property for a schema. That
130
- * callback will attempt to rename the key and move the existing data to that key, calling `onChange` when it does.
131
- *
132
- * @param oldValue - The old value of a field
133
- * @returns - The key change callback function
163
+ /** The property drop/removal event handler; Called when a field is removed in an additionalProperty context
134
164
  */
135
- onKeyChange = (oldValue: any) => {
136
- return (value: any, newErrorSchema: ErrorSchema<T>) => {
137
- if (oldValue === value) {
138
- return;
139
- }
140
- const { formData, onChange, errorSchema } = this.props;
165
+ const onRemoveProperty = useCallback(() => {
166
+ handleRemoveProperty(propertyName);
167
+ }, [propertyName, handleRemoveProperty]);
141
168
 
142
- value = this.getAvailableKey(value, formData);
143
- const newFormData: GenericObjectType = {
144
- ...(formData as GenericObjectType),
145
- };
146
- const newKeys: GenericObjectType = { [oldValue]: value };
147
- const keyValues = Object.keys(newFormData).map((key) => {
148
- const newKey = newKeys[key] || key;
149
- return { [newKey]: newFormData[key] };
150
- });
151
- const renamedObj = Object.assign({}, ...keyValues);
169
+ return (
170
+ <SchemaField
171
+ name={propertyName}
172
+ required={required}
173
+ schema={schema}
174
+ uiSchema={uiSchema}
175
+ errorSchema={errorSchema}
176
+ fieldPathId={innerFieldIdPathId}
177
+ formData={formData}
178
+ wasPropertyKeyModified={wasPropertyKeyModified}
179
+ onKeyRename={onKeyRename}
180
+ onKeyRenameBlur={onKeyRenameBlur}
181
+ onRemoveProperty={onRemoveProperty}
182
+ onChange={onPropertyChange}
183
+ onBlur={onBlur}
184
+ onFocus={onFocus}
185
+ registry={registry}
186
+ disabled={disabled}
187
+ readonly={readonly}
188
+ hideError={hideError}
189
+ />
190
+ );
191
+ }
152
192
 
153
- this.setState({ wasPropertyKeyModified: true });
193
+ /** The `ObjectField` component is used to render a field in the schema that is of type `object`. It tracks whether an
194
+ * additional property key was modified and what it was modified to
195
+ *
196
+ * @param props - The `FieldProps` for this template
197
+ */
198
+ export default function ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
199
+ props: FieldProps<T, S, F>,
200
+ ) {
201
+ const {
202
+ schema: rawSchema,
203
+ uiSchema = {},
204
+ formData,
205
+ errorSchema,
206
+ fieldPathId,
207
+ name,
208
+ required = false,
209
+ disabled,
210
+ readonly,
211
+ hideError,
212
+ onBlur,
213
+ onFocus,
214
+ onChange,
215
+ registry,
216
+ title,
217
+ } = props;
218
+ const { fields, schemaUtils, translateString, globalUiOptions } = registry;
219
+ const { OptionalDataControlsField } = fields;
220
+ const schema: S = schemaUtils.retrieveSchema(rawSchema, formData, true);
221
+ const uiOptions = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
222
+ const { properties: schemaProperties = {} } = schema;
223
+ const formDataHash = hashObject(formData || {});
224
+ // All the children will use childFieldPathId if present in the props, falling back to the fieldPathId
225
+ const childFieldPathId = props.childFieldPathId ?? fieldPathId;
154
226
 
155
- onChange(
156
- renamedObj,
157
- errorSchema &&
158
- errorSchema && {
159
- ...errorSchema,
160
- [value]: newErrorSchema,
161
- },
162
- );
163
- };
164
- };
227
+ const templateTitle = uiOptions.title ?? schema.title ?? title ?? name;
228
+ const description = uiOptions.description ?? schema.description;
229
+ const renderOptionalField = shouldRenderOptionalField<T, S, F>(registry, schema, required, uiSchema);
230
+ const hasFormData = isFormDataAvailable<T>(formData);
231
+ let orderedProperties: string[] = [];
165
232
 
166
- /** Returns a default value to be used for a new additional schema property of the given `type`
233
+ /** Computes the next available key name from the `preferredKey`, indexing through the already existing keys until one
234
+ * that is already not assigned is found.
167
235
  *
168
- * @param type - The type of the new additional schema property
236
+ * @param preferredKey - The preferred name of a new key
237
+ * @param [formData] - The form data in which to check if the desired key already exists
238
+ * @returns - The name of the next available key from `preferredKey`
169
239
  */
170
- getDefaultValue(type?: RJSFSchema['type']) {
171
- const {
172
- registry: { translateString },
173
- } = this.props;
174
- switch (type) {
175
- case 'array':
176
- return [];
177
- case 'boolean':
178
- return false;
179
- case 'null':
180
- return null;
181
- case 'number':
182
- return 0;
183
- case 'object':
184
- return {};
185
- case 'string':
186
- default:
187
- // We don't have a datatype for some reason (perhaps additionalProperties was true)
188
- return translateString(TranslatableString.NewStringDefault);
189
- }
190
- }
240
+ const getAvailableKey = useCallback(
241
+ (preferredKey: string, formData?: T) => {
242
+ const { duplicateKeySuffixSeparator = '-' } = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
243
+
244
+ let index = 0;
245
+ let newKey = preferredKey;
246
+ while (has(formData, newKey)) {
247
+ newKey = `${preferredKey}${duplicateKeySuffixSeparator}${++index}`;
248
+ }
249
+ return newKey;
250
+ },
251
+ [uiSchema, globalUiOptions],
252
+ );
191
253
 
192
254
  /** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
193
255
  * default data for that field has been added to the formData.
194
- *
195
- * @param schema - The schema element to which the new property is being added
196
256
  */
197
- handleAddClick = (schema: S) => () => {
257
+ const onAddProperty = useCallback(() => {
198
258
  if (!(schema.additionalProperties || schema.patternProperties)) {
199
259
  return;
200
260
  }
201
- const { formData, onChange, registry } = this.props;
261
+ const { translateString } = registry;
202
262
  const newFormData = { ...formData } as T;
203
- const newKey = this.getAvailableKey('newKey', newFormData);
263
+ const newKey = getAvailableKey('newKey', newFormData);
204
264
  if (schema.patternProperties) {
205
265
  // Cast this to make the `set` work properly
206
266
  set(newFormData as GenericObjectType, newKey, null);
@@ -215,7 +275,7 @@ class ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
215
275
  let apSchema = schema.additionalProperties;
216
276
  if (REF_KEY in apSchema) {
217
277
  const { schemaUtils } = registry;
218
- apSchema = schemaUtils.retrieveSchema({ $ref: apSchema[REF_KEY] } as S, formData);
278
+ apSchema = schemaUtils.retrieveSchema({ [REF_KEY]: apSchema[REF_KEY] } as S, formData);
219
279
  type = apSchema.type;
220
280
  constValue = apSchema.const;
221
281
  defaultValue = apSchema.default;
@@ -225,45 +285,53 @@ class ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
225
285
  }
226
286
  }
227
287
 
228
- const newValue = constValue ?? defaultValue ?? this.getDefaultValue(type);
288
+ const newValue = constValue ?? defaultValue ?? getDefaultValue<T, S, F>(translateString, type);
229
289
  // Cast this to make the `set` work properly
230
290
  set(newFormData as GenericObjectType, newKey, newValue);
231
291
  }
232
292
 
233
- onChange(newFormData);
234
- };
293
+ onChange(newFormData, childFieldPathId.path);
294
+ }, [formData, onChange, registry, childFieldPathId, getAvailableKey, schema]);
235
295
 
236
- /** Renders the `ObjectField` from the given props
296
+ /** Returns a callback function that deals with the rename of a key for an additional property for a schema. That
297
+ * callback will attempt to rename the key and move the existing data to that key, calling `onChange` when it does.
298
+ *
299
+ * @param oldKey - The old key for the field
300
+ * @param newKey - The new key for the field
301
+ * @returns - The key change callback function
237
302
  */
238
- render() {
239
- const {
240
- schema: rawSchema,
241
- uiSchema = {},
242
- formData,
243
- errorSchema,
244
- idSchema,
245
- name,
246
- required = false,
247
- disabled,
248
- readonly,
249
- hideError,
250
- idPrefix,
251
- idSeparator,
252
- onBlur,
253
- onFocus,
254
- registry,
255
- title,
256
- } = this.props;
303
+ const handleKeyRename = useCallback(
304
+ (oldKey: string, newKey: string) => {
305
+ if (oldKey !== newKey) {
306
+ const actualNewKey = getAvailableKey(newKey, formData);
307
+ const newFormData: GenericObjectType = {
308
+ ...(formData as GenericObjectType),
309
+ };
310
+ const newKeys: GenericObjectType = { [oldKey]: actualNewKey };
311
+ const keyValues = Object.keys(newFormData).map((key) => {
312
+ const newKey = newKeys[key] || key;
313
+ return { [newKey]: newFormData[key] };
314
+ });
315
+ const renamedObj = Object.assign({}, ...keyValues);
257
316
 
258
- const { fields, formContext, schemaUtils, translateString, globalUiOptions } = registry;
259
- const { SchemaField } = fields;
260
- const schema: S = schemaUtils.retrieveSchema(rawSchema, formData);
261
- const uiOptions = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
262
- const { properties: schemaProperties = {} } = schema;
317
+ onChange(renamedObj, childFieldPathId.path);
318
+ }
319
+ },
320
+ [formData, onChange, childFieldPathId, getAvailableKey],
321
+ );
263
322
 
264
- const templateTitle = uiOptions.title ?? schema.title ?? title ?? name;
265
- const description = uiOptions.description ?? schema.description;
266
- let orderedProperties: string[];
323
+ /** Handles the remove click which removes the old `key` data and calls the `onChange` callback with it
324
+ */
325
+ const handleRemoveProperty = useCallback(
326
+ (key: string) => {
327
+ const copiedFormData = { ...formData } as T;
328
+ unset(copiedFormData, key);
329
+ onChange(copiedFormData, childFieldPathId.path);
330
+ },
331
+ [onChange, childFieldPathId, formData],
332
+ );
333
+
334
+ if (!renderOptionalField || hasFormData) {
267
335
  try {
268
336
  const properties = Object.keys(schemaProperties);
269
337
  orderedProperties = orderProperties(properties, uiOptions.order);
@@ -279,65 +347,66 @@ class ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
279
347
  </div>
280
348
  );
281
349
  }
350
+ }
282
351
 
283
- const Template = getTemplate<'ObjectFieldTemplate', T, S, F>('ObjectFieldTemplate', registry, uiOptions);
284
-
285
- const templateProps = {
286
- // getDisplayLabel() always returns false for object types, so just check the `uiOptions.label`
287
- title: uiOptions.label === false ? '' : templateTitle,
288
- description: uiOptions.label === false ? undefined : description,
289
- properties: orderedProperties.map((name) => {
290
- const addedByAdditionalProperties = has(schema, [PROPERTIES_KEY, name, ADDITIONAL_PROPERTY_FLAG]);
291
- const fieldUiSchema = addedByAdditionalProperties ? uiSchema.additionalProperties : uiSchema[name];
292
- const hidden = getUiOptions<T, S, F>(fieldUiSchema).widget === 'hidden';
293
- const fieldIdSchema: IdSchema<T> = get(idSchema, [name], {});
352
+ const Template = getTemplate<'ObjectFieldTemplate', T, S, F>('ObjectFieldTemplate', registry, uiOptions);
353
+ const optionalDataControl = renderOptionalField ? (
354
+ <OptionalDataControlsField {...props} fieldPathId={childFieldPathId} schema={schema} />
355
+ ) : undefined;
294
356
 
295
- return {
296
- content: (
297
- <SchemaField
298
- key={name}
299
- name={name}
300
- required={this.isRequired(name)}
301
- schema={get(schema, [PROPERTIES_KEY, name], {}) as S}
302
- uiSchema={fieldUiSchema}
303
- errorSchema={get(errorSchema, name)}
304
- idSchema={fieldIdSchema}
305
- idPrefix={idPrefix}
306
- idSeparator={idSeparator}
307
- formData={get(formData, name)}
308
- formContext={formContext}
309
- wasPropertyKeyModified={this.state.wasPropertyKeyModified}
310
- onKeyChange={this.onKeyChange(name)}
311
- onChange={this.onPropertyChange(name, addedByAdditionalProperties)}
312
- onBlur={onBlur}
313
- onFocus={onFocus}
314
- registry={registry}
315
- disabled={disabled}
316
- readonly={readonly}
317
- hideError={hideError}
318
- onDropPropertyClick={this.onDropPropertyClick}
319
- />
320
- ),
321
- name,
322
- readonly,
323
- disabled,
324
- required,
325
- hidden,
326
- };
327
- }),
328
- readonly,
329
- disabled,
330
- required,
331
- idSchema,
332
- uiSchema,
333
- errorSchema,
334
- schema,
335
- formData,
336
- formContext,
337
- registry,
338
- };
339
- return <Template {...templateProps} onAddClick={this.handleAddClick} />;
340
- }
357
+ const templateProps = {
358
+ // getDisplayLabel() always returns false for object types, so just check the `uiOptions.label`
359
+ title: uiOptions.label === false ? '' : templateTitle,
360
+ description: uiOptions.label === false ? undefined : description,
361
+ properties: orderedProperties.map((name) => {
362
+ const addedByAdditionalProperties = has(schema, [PROPERTIES_KEY, name, ADDITIONAL_PROPERTY_FLAG]);
363
+ const fieldUiSchema = addedByAdditionalProperties ? uiSchema.additionalProperties : uiSchema[name];
364
+ const hidden = getUiOptions<T, S, F>(fieldUiSchema).widget === 'hidden';
365
+ const content = (
366
+ <ObjectFieldProperty<T, S, F>
367
+ // For regular properties, the key is just the name. For additionalProperties, the key is a combination of the
368
+ // name and the hash of the formData so that react rerenders the components with the updated additional
369
+ // property related callback which will change due to formData changes
370
+ key={addedByAdditionalProperties ? `${name}-${formDataHash}` : name}
371
+ propertyName={name}
372
+ required={isRequired<S>(schema, name)}
373
+ schema={get(schema, [PROPERTIES_KEY, name], {}) as S}
374
+ uiSchema={fieldUiSchema}
375
+ errorSchema={get(errorSchema, name)}
376
+ fieldPathId={childFieldPathId}
377
+ formData={get(formData, name)}
378
+ handleKeyRename={handleKeyRename}
379
+ handleRemoveProperty={handleRemoveProperty}
380
+ addedByAdditionalProperties={addedByAdditionalProperties}
381
+ onChange={onChange}
382
+ onBlur={onBlur}
383
+ onFocus={onFocus}
384
+ registry={registry}
385
+ disabled={disabled}
386
+ readonly={readonly}
387
+ hideError={hideError}
388
+ />
389
+ );
390
+ return {
391
+ content,
392
+ name,
393
+ readonly,
394
+ disabled,
395
+ required,
396
+ hidden,
397
+ };
398
+ }),
399
+ readonly,
400
+ disabled,
401
+ required,
402
+ fieldPathId,
403
+ uiSchema,
404
+ errorSchema,
405
+ schema,
406
+ formData,
407
+ registry,
408
+ optionalDataControl,
409
+ className: renderOptionalField ? 'rjsf-optional-object-field' : undefined,
410
+ };
411
+ return <Template {...templateProps} onAddProperty={onAddProperty} />;
341
412
  }
342
-
343
- export default ObjectField;