@rjsf/core 5.11.2 → 5.12.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 (232) hide show
  1. package/dist/core.umd.js +3464 -0
  2. package/dist/index.esm.js +3814 -0
  3. package/dist/index.esm.js.map +7 -0
  4. package/dist/index.js +3714 -5
  5. package/dist/index.js.map +7 -0
  6. package/{dist/index.d.ts → lib/components/Form.d.ts} +321 -337
  7. package/lib/components/Form.js +474 -0
  8. package/lib/components/Form.js.map +1 -0
  9. package/lib/components/fields/ArrayField.d.ts +179 -0
  10. package/lib/components/fields/ArrayField.js +568 -0
  11. package/lib/components/fields/ArrayField.js.map +1 -0
  12. package/lib/components/fields/BooleanField.d.ts +9 -0
  13. package/lib/components/fields/BooleanField.js +62 -0
  14. package/lib/components/fields/BooleanField.js.map +1 -0
  15. package/lib/components/fields/MultiSchemaField.d.ts +47 -0
  16. package/lib/components/fields/MultiSchemaField.js +129 -0
  17. package/lib/components/fields/MultiSchemaField.js.map +1 -0
  18. package/lib/components/fields/NullField.d.ts +8 -0
  19. package/lib/components/fields/NullField.js +17 -0
  20. package/lib/components/fields/NullField.js.map +1 -0
  21. package/lib/components/fields/NumberField.d.ts +21 -0
  22. package/lib/components/fields/NumberField.js +70 -0
  23. package/lib/components/fields/NumberField.js.map +1 -0
  24. package/lib/components/fields/ObjectField.d.ts +73 -0
  25. package/lib/components/fields/ObjectField.js +222 -0
  26. package/lib/components/fields/ObjectField.js.map +1 -0
  27. package/lib/components/fields/SchemaField.d.ts +10 -0
  28. package/lib/components/fields/SchemaField.js +172 -0
  29. package/lib/components/fields/SchemaField.js.map +1 -0
  30. package/lib/components/fields/StringField.d.ts +8 -0
  31. package/lib/components/fields/StringField.js +25 -0
  32. package/lib/components/fields/StringField.js.map +1 -0
  33. package/lib/components/fields/index.d.ts +3 -0
  34. package/lib/components/fields/index.js +24 -0
  35. package/lib/components/fields/index.js.map +1 -0
  36. package/lib/components/templates/ArrayFieldDescriptionTemplate.d.ts +8 -0
  37. package/lib/components/templates/ArrayFieldDescriptionTemplate.js +18 -0
  38. package/lib/components/templates/ArrayFieldDescriptionTemplate.js.map +1 -0
  39. package/lib/components/templates/ArrayFieldItemTemplate.d.ts +7 -0
  40. package/lib/components/templates/ArrayFieldItemTemplate.js +20 -0
  41. package/lib/components/templates/ArrayFieldItemTemplate.js.map +1 -0
  42. package/lib/components/templates/ArrayFieldTemplate.d.ts +7 -0
  43. package/lib/components/templates/ArrayFieldTemplate.js +22 -0
  44. package/lib/components/templates/ArrayFieldTemplate.js.map +1 -0
  45. package/lib/components/templates/ArrayFieldTitleTemplate.d.ts +8 -0
  46. package/lib/components/templates/ArrayFieldTitleTemplate.js +18 -0
  47. package/lib/components/templates/ArrayFieldTitleTemplate.js.map +1 -0
  48. package/lib/components/templates/BaseInputTemplate.d.ts +9 -0
  49. package/lib/components/templates/BaseInputTemplate.js +39 -0
  50. package/lib/components/templates/BaseInputTemplate.js.map +1 -0
  51. package/lib/components/templates/ButtonTemplates/AddButton.d.ts +5 -0
  52. package/lib/components/templates/ButtonTemplates/AddButton.js +10 -0
  53. package/lib/components/templates/ButtonTemplates/AddButton.js.map +1 -0
  54. package/lib/components/templates/ButtonTemplates/IconButton.d.ts +7 -0
  55. package/lib/components/templates/ButtonTemplates/IconButton.js +24 -0
  56. package/lib/components/templates/ButtonTemplates/IconButton.js.map +1 -0
  57. package/lib/components/templates/ButtonTemplates/SubmitButton.d.ts +5 -0
  58. package/lib/components/templates/ButtonTemplates/SubmitButton.js +12 -0
  59. package/lib/components/templates/ButtonTemplates/SubmitButton.js.map +1 -0
  60. package/lib/components/templates/ButtonTemplates/index.d.ts +3 -0
  61. package/lib/components/templates/ButtonTemplates/index.js +15 -0
  62. package/lib/components/templates/ButtonTemplates/index.js.map +1 -0
  63. package/lib/components/templates/DescriptionField.d.ts +7 -0
  64. package/lib/components/templates/DescriptionField.js +18 -0
  65. package/lib/components/templates/DescriptionField.js.map +1 -0
  66. package/lib/components/templates/ErrorList.d.ts +7 -0
  67. package/lib/components/templates/ErrorList.js +13 -0
  68. package/lib/components/templates/ErrorList.js.map +1 -0
  69. package/lib/components/templates/FieldErrorTemplate.d.ts +7 -0
  70. package/lib/components/templates/FieldErrorTemplate.js +19 -0
  71. package/lib/components/templates/FieldErrorTemplate.js.map +1 -0
  72. package/lib/components/templates/FieldHelpTemplate.d.ts +7 -0
  73. package/lib/components/templates/FieldHelpTemplate.js +18 -0
  74. package/lib/components/templates/FieldHelpTemplate.js.map +1 -0
  75. package/lib/components/templates/FieldTemplate/FieldTemplate.d.ts +8 -0
  76. package/lib/components/templates/FieldTemplate/FieldTemplate.js +18 -0
  77. package/lib/components/templates/FieldTemplate/FieldTemplate.js.map +1 -0
  78. package/lib/components/templates/FieldTemplate/Label.d.ts +14 -0
  79. package/lib/components/templates/FieldTemplate/Label.js +14 -0
  80. package/lib/components/templates/FieldTemplate/Label.js.map +1 -0
  81. package/lib/components/templates/FieldTemplate/index.d.ts +2 -0
  82. package/lib/components/templates/FieldTemplate/index.js +3 -0
  83. package/lib/components/templates/FieldTemplate/index.js.map +1 -0
  84. package/lib/components/templates/ObjectFieldTemplate.d.ts +9 -0
  85. package/lib/components/templates/ObjectFieldTemplate.js +18 -0
  86. package/lib/components/templates/ObjectFieldTemplate.js.map +1 -0
  87. package/lib/components/templates/TitleField.d.ts +7 -0
  88. package/lib/components/templates/TitleField.js +11 -0
  89. package/lib/components/templates/TitleField.js.map +1 -0
  90. package/lib/components/templates/UnsupportedField.d.ts +9 -0
  91. package/lib/components/templates/UnsupportedField.js +28 -0
  92. package/lib/components/templates/UnsupportedField.js.map +1 -0
  93. package/lib/components/templates/WrapIfAdditionalTemplate.d.ts +8 -0
  94. package/lib/components/templates/WrapIfAdditionalTemplate.js +21 -0
  95. package/lib/components/templates/WrapIfAdditionalTemplate.js.map +1 -0
  96. package/lib/components/templates/index.d.ts +3 -0
  97. package/lib/components/templates/index.js +36 -0
  98. package/lib/components/templates/index.js.map +1 -0
  99. package/lib/components/widgets/AltDateTimeWidget.d.ts +9 -0
  100. package/lib/components/widgets/AltDateTimeWidget.js +14 -0
  101. package/lib/components/widgets/AltDateTimeWidget.js.map +1 -0
  102. package/lib/components/widgets/AltDateWidget.d.ts +7 -0
  103. package/lib/components/widgets/AltDateWidget.js +77 -0
  104. package/lib/components/widgets/AltDateWidget.js.map +1 -0
  105. package/lib/components/widgets/CheckboxWidget.d.ts +9 -0
  106. package/lib/components/widgets/CheckboxWidget.js +23 -0
  107. package/lib/components/widgets/CheckboxWidget.js.map +1 -0
  108. package/lib/components/widgets/CheckboxesWidget.d.ts +9 -0
  109. package/lib/components/widgets/CheckboxesWidget.js +31 -0
  110. package/lib/components/widgets/CheckboxesWidget.js.map +1 -0
  111. package/lib/components/widgets/ColorWidget.d.ts +8 -0
  112. package/lib/components/widgets/ColorWidget.js +13 -0
  113. package/lib/components/widgets/ColorWidget.js.map +1 -0
  114. package/lib/components/widgets/DateTimeWidget.d.ts +8 -0
  115. package/lib/components/widgets/DateTimeWidget.js +13 -0
  116. package/lib/components/widgets/DateTimeWidget.js.map +1 -0
  117. package/lib/components/widgets/DateWidget.d.ts +8 -0
  118. package/lib/components/widgets/DateWidget.js +15 -0
  119. package/lib/components/widgets/DateWidget.js.map +1 -0
  120. package/lib/components/widgets/EmailWidget.d.ts +7 -0
  121. package/lib/components/widgets/EmailWidget.js +12 -0
  122. package/lib/components/widgets/EmailWidget.js.map +1 -0
  123. package/lib/components/widgets/FileWidget.d.ts +8 -0
  124. package/lib/components/widgets/FileWidget.js +105 -0
  125. package/lib/components/widgets/FileWidget.js.map +1 -0
  126. package/lib/components/widgets/HiddenWidget.d.ts +9 -0
  127. package/lib/components/widgets/HiddenWidget.js +11 -0
  128. package/lib/components/widgets/HiddenWidget.js.map +1 -0
  129. package/lib/components/widgets/PasswordWidget.d.ts +7 -0
  130. package/lib/components/widgets/PasswordWidget.js +12 -0
  131. package/lib/components/widgets/PasswordWidget.js.map +1 -0
  132. package/lib/components/widgets/RadioWidget.d.ts +9 -0
  133. package/lib/components/widgets/RadioWidget.js +24 -0
  134. package/lib/components/widgets/RadioWidget.js.map +1 -0
  135. package/lib/components/widgets/RangeWidget.d.ts +8 -0
  136. package/lib/components/widgets/RangeWidget.js +11 -0
  137. package/lib/components/widgets/RangeWidget.js.map +1 -0
  138. package/lib/components/widgets/SelectWidget.d.ts +9 -0
  139. package/lib/components/widgets/SelectWidget.js +41 -0
  140. package/lib/components/widgets/SelectWidget.js.map +1 -0
  141. package/lib/components/widgets/TextWidget.d.ts +7 -0
  142. package/lib/components/widgets/TextWidget.js +12 -0
  143. package/lib/components/widgets/TextWidget.js.map +1 -0
  144. package/lib/components/widgets/TextareaWidget.d.ts +14 -0
  145. package/lib/components/widgets/TextareaWidget.js +19 -0
  146. package/lib/components/widgets/TextareaWidget.js.map +1 -0
  147. package/lib/components/widgets/TimeWidget.d.ts +8 -0
  148. package/lib/components/widgets/TimeWidget.js +15 -0
  149. package/lib/components/widgets/TimeWidget.js.map +1 -0
  150. package/lib/components/widgets/URLWidget.d.ts +7 -0
  151. package/lib/components/widgets/URLWidget.js +12 -0
  152. package/lib/components/widgets/URLWidget.js.map +1 -0
  153. package/lib/components/widgets/UpDownWidget.d.ts +7 -0
  154. package/lib/components/widgets/UpDownWidget.js +12 -0
  155. package/lib/components/widgets/UpDownWidget.js.map +1 -0
  156. package/lib/components/widgets/index.d.ts +3 -0
  157. package/lib/components/widgets/index.js +44 -0
  158. package/lib/components/widgets/index.js.map +1 -0
  159. package/lib/getDefaultRegistry.d.ts +6 -0
  160. package/lib/getDefaultRegistry.js +19 -0
  161. package/lib/getDefaultRegistry.js.map +1 -0
  162. package/lib/index.d.ts +6 -0
  163. package/lib/index.js +6 -0
  164. package/lib/index.js.map +1 -0
  165. package/lib/withTheme.d.ts +9 -0
  166. package/lib/withTheme.js +16 -0
  167. package/lib/withTheme.js.map +1 -0
  168. package/package.json +23 -16
  169. package/src/components/Form.tsx +853 -0
  170. package/src/components/fields/ArrayField.tsx +875 -0
  171. package/src/components/fields/BooleanField.tsx +114 -0
  172. package/src/components/fields/MultiSchemaField.tsx +221 -0
  173. package/src/components/fields/NullField.tsx +22 -0
  174. package/src/components/fields/NumberField.tsx +86 -0
  175. package/src/components/fields/ObjectField.tsx +331 -0
  176. package/src/components/fields/SchemaField.tsx +360 -0
  177. package/src/components/fields/StringField.tsx +71 -0
  178. package/src/components/fields/index.ts +31 -0
  179. package/src/components/templates/ArrayFieldDescriptionTemplate.tsx +41 -0
  180. package/src/components/templates/ArrayFieldItemTemplate.tsx +90 -0
  181. package/src/components/templates/ArrayFieldTemplate.tsx +88 -0
  182. package/src/components/templates/ArrayFieldTitleTemplate.tsx +43 -0
  183. package/src/components/templates/BaseInputTemplate.tsx +102 -0
  184. package/src/components/templates/ButtonTemplates/AddButton.tsx +29 -0
  185. package/src/components/templates/ButtonTemplates/IconButton.tsx +77 -0
  186. package/src/components/templates/ButtonTemplates/SubmitButton.tsx +21 -0
  187. package/src/components/templates/ButtonTemplates/index.ts +22 -0
  188. package/src/components/templates/DescriptionField.tsx +29 -0
  189. package/src/components/templates/ErrorList.tsx +35 -0
  190. package/src/components/templates/FieldErrorTemplate.tsx +33 -0
  191. package/src/components/templates/FieldHelpTemplate.tsx +29 -0
  192. package/src/components/templates/FieldTemplate/FieldTemplate.tsx +41 -0
  193. package/src/components/templates/FieldTemplate/Label.tsx +27 -0
  194. package/src/components/templates/FieldTemplate/index.ts +3 -0
  195. package/src/components/templates/ObjectFieldTemplate.tsx +83 -0
  196. package/src/components/templates/TitleField.tsx +19 -0
  197. package/src/components/templates/UnsupportedField.tsx +37 -0
  198. package/src/components/templates/WrapIfAdditionalTemplate.tsx +80 -0
  199. package/src/components/templates/index.ts +43 -0
  200. package/src/components/widgets/AltDateTimeWidget.tsx +16 -0
  201. package/src/components/widgets/AltDateWidget.tsx +198 -0
  202. package/src/components/widgets/CheckboxWidget.tsx +92 -0
  203. package/src/components/widgets/CheckboxesWidget.tsx +92 -0
  204. package/src/components/widgets/ColorWidget.tsx +14 -0
  205. package/src/components/widgets/DateTimeWidget.tsx +31 -0
  206. package/src/components/widgets/DateWidget.tsx +17 -0
  207. package/src/components/widgets/EmailWidget.tsx +13 -0
  208. package/src/components/widgets/FileWidget.tsx +178 -0
  209. package/src/components/widgets/HiddenWidget.tsx +15 -0
  210. package/src/components/widgets/PasswordWidget.tsx +15 -0
  211. package/src/components/widgets/RadioWidget.tsx +88 -0
  212. package/src/components/widgets/RangeWidget.tsx +23 -0
  213. package/src/components/widgets/SelectWidget.tsx +100 -0
  214. package/src/components/widgets/TextWidget.tsx +13 -0
  215. package/src/components/widgets/TextareaWidget.tsx +61 -0
  216. package/src/components/widgets/TimeWidget.tsx +17 -0
  217. package/src/components/widgets/URLWidget.tsx +13 -0
  218. package/src/components/widgets/UpDownWidget.tsx +13 -0
  219. package/src/components/widgets/index.ts +51 -0
  220. package/src/getDefaultRegistry.ts +24 -0
  221. package/src/index.ts +8 -0
  222. package/src/withTheme.tsx +42 -0
  223. package/dist/core.cjs.development.js +0 -4403
  224. package/dist/core.cjs.development.js.map +0 -1
  225. package/dist/core.cjs.production.min.js +0 -2
  226. package/dist/core.cjs.production.min.js.map +0 -1
  227. package/dist/core.esm.js +0 -4383
  228. package/dist/core.esm.js.map +0 -1
  229. package/dist/core.umd.development.js +0 -4393
  230. package/dist/core.umd.development.js.map +0 -1
  231. package/dist/core.umd.production.min.js +0 -2
  232. package/dist/core.umd.production.min.js.map +0 -1
@@ -0,0 +1,331 @@
1
+ import { Component } from 'react';
2
+ import {
3
+ getTemplate,
4
+ getUiOptions,
5
+ orderProperties,
6
+ ErrorSchema,
7
+ FieldProps,
8
+ FormContextType,
9
+ GenericObjectType,
10
+ IdSchema,
11
+ RJSFSchema,
12
+ StrictRJSFSchema,
13
+ TranslatableString,
14
+ ADDITIONAL_PROPERTY_FLAG,
15
+ PROPERTIES_KEY,
16
+ REF_KEY,
17
+ ANY_OF_KEY,
18
+ ONE_OF_KEY,
19
+ } from '@rjsf/utils';
20
+ import Markdown from 'markdown-to-jsx';
21
+ import get from 'lodash/get';
22
+ import has from 'lodash/has';
23
+ import isObject from 'lodash/isObject';
24
+ import set from 'lodash/set';
25
+ import unset from 'lodash/unset';
26
+
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
37
+ *
38
+ * @param props - The `FieldProps` for this template
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
+ };
49
+
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;
58
+ }
59
+
60
+ /** Returns the `onPropertyChange` handler for the `name` field. Handles the special case where a user is attempting
61
+ * to clear the data for a field added as an additional property. Calls the `onChange()` handler with the updated
62
+ * formData.
63
+ *
64
+ * @param name - The name of the property
65
+ * @param addedByAdditionalProperties - Flag indicating whether this property is an additional property
66
+ * @returns - The onPropertyChange callback for the `name` property
67
+ */
68
+ onPropertyChange = (name: string, addedByAdditionalProperties = false) => {
69
+ return (value: T | undefined, newErrorSchema?: ErrorSchema<T>, id?: string) => {
70
+ const { formData, onChange, errorSchema } = this.props;
71
+ 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.
79
+ value = '' as unknown as T;
80
+ }
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
+ };
93
+
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
99
+ */
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
+ };
109
+
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`
116
+ */
117
+ getAvailableKey = (preferredKey: string, formData?: T) => {
118
+ const { uiSchema, registry } = this.props;
119
+ const { duplicateKeySuffixSeparator = '-' } = getUiOptions<T, S, F>(uiSchema, registry.globalUiOptions);
120
+
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
134
+ */
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;
141
+
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);
152
+
153
+ this.setState({ wasPropertyKeyModified: true });
154
+
155
+ onChange(
156
+ renamedObj,
157
+ errorSchema &&
158
+ errorSchema && {
159
+ ...errorSchema,
160
+ [value]: newErrorSchema,
161
+ }
162
+ );
163
+ };
164
+ };
165
+
166
+ /** Returns a default value to be used for a new additional schema property of the given `type`
167
+ *
168
+ * @param type - The type of the new additional schema property
169
+ */
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
+ }
191
+
192
+ /** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
193
+ * 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
+ */
197
+ handleAddClick = (schema: S) => () => {
198
+ if (!schema.additionalProperties) {
199
+ return;
200
+ }
201
+ const { formData, onChange, registry } = this.props;
202
+ const newFormData = { ...formData } as T;
203
+
204
+ let type: RJSFSchema['type'] = undefined;
205
+ if (isObject(schema.additionalProperties)) {
206
+ type = schema.additionalProperties.type;
207
+ let apSchema = schema.additionalProperties;
208
+ if (REF_KEY in apSchema) {
209
+ const { schemaUtils } = registry;
210
+ apSchema = schemaUtils.retrieveSchema({ $ref: apSchema[REF_KEY] } as S, formData);
211
+ type = apSchema.type;
212
+ }
213
+ if (!type && (ANY_OF_KEY in apSchema || ONE_OF_KEY in apSchema)) {
214
+ type = 'object';
215
+ }
216
+ }
217
+
218
+ const newKey = this.getAvailableKey('newKey', newFormData);
219
+ // Cast this to make the `set` work properly
220
+ set(newFormData as GenericObjectType, newKey, this.getDefaultValue(type));
221
+
222
+ onChange(newFormData);
223
+ };
224
+
225
+ /** Renders the `ObjectField` from the given props
226
+ */
227
+ render() {
228
+ const {
229
+ schema: rawSchema,
230
+ uiSchema = {},
231
+ formData,
232
+ errorSchema,
233
+ idSchema,
234
+ name,
235
+ required = false,
236
+ disabled = false,
237
+ readonly = false,
238
+ hideError,
239
+ idPrefix,
240
+ idSeparator,
241
+ onBlur,
242
+ onFocus,
243
+ registry,
244
+ } = this.props;
245
+
246
+ const { fields, formContext, schemaUtils, translateString, globalUiOptions } = registry;
247
+ const { SchemaField } = fields;
248
+ const schema: S = schemaUtils.retrieveSchema(rawSchema, formData);
249
+ const uiOptions = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
250
+ const { properties: schemaProperties = {} } = schema;
251
+
252
+ const title = uiOptions.title ?? schema.title ?? name;
253
+ const description = uiOptions.description ?? schema.description;
254
+ let orderedProperties: string[];
255
+ try {
256
+ const properties = Object.keys(schemaProperties);
257
+ orderedProperties = orderProperties(properties, uiOptions.order);
258
+ } catch (err) {
259
+ return (
260
+ <div>
261
+ <p className='config-error' style={{ color: 'red' }}>
262
+ <Markdown>
263
+ {translateString(TranslatableString.InvalidObjectField, [name || 'root', (err as Error).message])}
264
+ </Markdown>
265
+ </p>
266
+ <pre>{JSON.stringify(schema)}</pre>
267
+ </div>
268
+ );
269
+ }
270
+
271
+ const Template = getTemplate<'ObjectFieldTemplate', T, S, F>('ObjectFieldTemplate', registry, uiOptions);
272
+
273
+ const templateProps = {
274
+ // getDisplayLabel() always returns false for object types, so just check the `uiOptions.label`
275
+ title: uiOptions.label === false ? '' : title,
276
+ description: uiOptions.label === false ? undefined : description,
277
+ properties: orderedProperties.map((name) => {
278
+ const addedByAdditionalProperties = has(schema, [PROPERTIES_KEY, name, ADDITIONAL_PROPERTY_FLAG]);
279
+ const fieldUiSchema = addedByAdditionalProperties ? uiSchema.additionalProperties : uiSchema[name];
280
+ const hidden = getUiOptions<T, S, F>(fieldUiSchema).widget === 'hidden';
281
+ const fieldIdSchema: IdSchema<T> = get(idSchema, [name], {});
282
+
283
+ return {
284
+ content: (
285
+ <SchemaField
286
+ key={name}
287
+ name={name}
288
+ required={this.isRequired(name)}
289
+ schema={get(schema, [PROPERTIES_KEY, name], {})}
290
+ uiSchema={fieldUiSchema}
291
+ errorSchema={get(errorSchema, name)}
292
+ idSchema={fieldIdSchema}
293
+ idPrefix={idPrefix}
294
+ idSeparator={idSeparator}
295
+ formData={get(formData, name)}
296
+ formContext={formContext}
297
+ wasPropertyKeyModified={this.state.wasPropertyKeyModified}
298
+ onKeyChange={this.onKeyChange(name)}
299
+ onChange={this.onPropertyChange(name, addedByAdditionalProperties)}
300
+ onBlur={onBlur}
301
+ onFocus={onFocus}
302
+ registry={registry}
303
+ disabled={disabled}
304
+ readonly={readonly}
305
+ hideError={hideError}
306
+ onDropPropertyClick={this.onDropPropertyClick}
307
+ />
308
+ ),
309
+ name,
310
+ readonly,
311
+ disabled,
312
+ required,
313
+ hidden,
314
+ };
315
+ }),
316
+ readonly,
317
+ disabled,
318
+ required,
319
+ idSchema,
320
+ uiSchema,
321
+ errorSchema,
322
+ schema,
323
+ formData,
324
+ formContext,
325
+ registry,
326
+ };
327
+ return <Template {...templateProps} onAddClick={this.handleAddClick} />;
328
+ }
329
+ }
330
+
331
+ export default ObjectField;
@@ -0,0 +1,360 @@
1
+ import { useCallback, Component } from 'react';
2
+ import {
3
+ ADDITIONAL_PROPERTY_FLAG,
4
+ deepEquals,
5
+ descriptionId,
6
+ ErrorSchema,
7
+ FieldProps,
8
+ FieldTemplateProps,
9
+ FormContextType,
10
+ getSchemaType,
11
+ getTemplate,
12
+ getUiOptions,
13
+ ID_KEY,
14
+ IdSchema,
15
+ mergeObjects,
16
+ Registry,
17
+ RJSFSchema,
18
+ StrictRJSFSchema,
19
+ TranslatableString,
20
+ UI_OPTIONS_KEY,
21
+ UIOptionsType,
22
+ } from '@rjsf/utils';
23
+ import isObject from 'lodash/isObject';
24
+ import omit from 'lodash/omit';
25
+ import Markdown from 'markdown-to-jsx';
26
+
27
+ /** The map of component type to FieldName */
28
+ const COMPONENT_TYPES: { [key: string]: string } = {
29
+ array: 'ArrayField',
30
+ boolean: 'BooleanField',
31
+ integer: 'NumberField',
32
+ number: 'NumberField',
33
+ object: 'ObjectField',
34
+ string: 'StringField',
35
+ null: 'NullField',
36
+ };
37
+
38
+ /** Computes and returns which `Field` implementation to return in order to render the field represented by the
39
+ * `schema`. The `uiOptions` are used to alter what potential `Field` implementation is actually returned. If no
40
+ * appropriate `Field` implementation can be found then a wrapper around `UnsupportedFieldTemplate` is used.
41
+ *
42
+ * @param schema - The schema from which to obtain the type
43
+ * @param uiOptions - The UI Options that may affect the component decision
44
+ * @param idSchema - The id that is passed to the `UnsupportedFieldTemplate`
45
+ * @param registry - The registry from which fields and templates are obtained
46
+ * @returns - The `Field` component that is used to render the actual field data
47
+ */
48
+ function getFieldComponent<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
49
+ schema: S,
50
+ uiOptions: UIOptionsType<T, S, F>,
51
+ idSchema: IdSchema<T>,
52
+ registry: Registry<T, S, F>
53
+ ) {
54
+ const field = uiOptions.field;
55
+ const { fields, translateString } = registry;
56
+ if (typeof field === 'function') {
57
+ return field;
58
+ }
59
+ if (typeof field === 'string' && field in fields) {
60
+ return fields[field];
61
+ }
62
+
63
+ const schemaType = getSchemaType(schema);
64
+ const type: string = Array.isArray(schemaType) ? schemaType[0] : schemaType || '';
65
+
66
+ const schemaId = schema.$id;
67
+
68
+ let componentName = COMPONENT_TYPES[type];
69
+ if (schemaId && schemaId in fields) {
70
+ componentName = schemaId;
71
+ }
72
+
73
+ // If the type is not defined and the schema uses 'anyOf' or 'oneOf', don't
74
+ // render a field and let the MultiSchemaField component handle the form display
75
+ if (!componentName && (schema.anyOf || schema.oneOf)) {
76
+ return () => null;
77
+ }
78
+
79
+ return componentName in fields
80
+ ? fields[componentName]
81
+ : () => {
82
+ const UnsupportedFieldTemplate = getTemplate<'UnsupportedFieldTemplate', T, S, F>(
83
+ 'UnsupportedFieldTemplate',
84
+ registry,
85
+ uiOptions
86
+ );
87
+
88
+ return (
89
+ <UnsupportedFieldTemplate
90
+ schema={schema}
91
+ idSchema={idSchema}
92
+ reason={translateString(TranslatableString.UnknownFieldType, [String(schema.type)])}
93
+ registry={registry}
94
+ />
95
+ );
96
+ };
97
+ }
98
+
99
+ /** The `SchemaFieldRender` component is the work-horse of react-jsonschema-form, determining what kind of real field to
100
+ * render based on the `schema`, `uiSchema` and all the other props. It also deals with rendering the `anyOf` and
101
+ * `oneOf` fields.
102
+ *
103
+ * @param props - The `FieldProps` for this component
104
+ */
105
+ function SchemaFieldRender<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
106
+ props: FieldProps<T, S, F>
107
+ ) {
108
+ const {
109
+ schema: _schema,
110
+ idSchema: _idSchema,
111
+ uiSchema,
112
+ formData,
113
+ errorSchema,
114
+ idPrefix,
115
+ idSeparator,
116
+ name,
117
+ onChange,
118
+ onKeyChange,
119
+ onDropPropertyClick,
120
+ required,
121
+ registry,
122
+ wasPropertyKeyModified = false,
123
+ } = props;
124
+ const { formContext, schemaUtils, globalUiOptions } = registry;
125
+ const uiOptions = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
126
+ const FieldTemplate = getTemplate<'FieldTemplate', T, S, F>('FieldTemplate', registry, uiOptions);
127
+ const DescriptionFieldTemplate = getTemplate<'DescriptionFieldTemplate', T, S, F>(
128
+ 'DescriptionFieldTemplate',
129
+ registry,
130
+ uiOptions
131
+ );
132
+ const FieldHelpTemplate = getTemplate<'FieldHelpTemplate', T, S, F>('FieldHelpTemplate', registry, uiOptions);
133
+ const FieldErrorTemplate = getTemplate<'FieldErrorTemplate', T, S, F>('FieldErrorTemplate', registry, uiOptions);
134
+ const schema = schemaUtils.retrieveSchema(_schema, formData);
135
+ const fieldId = _idSchema[ID_KEY];
136
+ const idSchema = mergeObjects(
137
+ schemaUtils.toIdSchema(schema, fieldId, formData, idPrefix, idSeparator),
138
+ _idSchema
139
+ ) as IdSchema<T>;
140
+
141
+ /** Intermediary `onChange` handler for field components that will inject the `id` of the current field into the
142
+ * `onChange` chain if it is not already being provided from a deeper level in the hierarchy
143
+ */
144
+ const handleFieldComponentChange = useCallback(
145
+ (formData: T | undefined, newErrorSchema?: ErrorSchema<T>, id?: string) => {
146
+ const theId = id || fieldId;
147
+ return onChange(formData, newErrorSchema, theId);
148
+ },
149
+ [fieldId, onChange]
150
+ );
151
+
152
+ const FieldComponent = getFieldComponent<T, S, F>(schema, uiOptions, idSchema, registry);
153
+ const disabled = Boolean(props.disabled || uiOptions.disabled);
154
+ const readonly = Boolean(props.readonly || uiOptions.readonly || props.schema.readOnly || schema.readOnly);
155
+ const uiSchemaHideError = uiOptions.hideError;
156
+ // Set hideError to the value provided in the uiSchema, otherwise stick with the prop to propagate to children
157
+ const hideError = uiSchemaHideError === undefined ? props.hideError : Boolean(uiSchemaHideError);
158
+ const autofocus = Boolean(props.autofocus || uiOptions.autofocus);
159
+ if (Object.keys(schema).length === 0) {
160
+ return null;
161
+ }
162
+
163
+ const displayLabel = schemaUtils.getDisplayLabel(schema, uiSchema, globalUiOptions);
164
+
165
+ const { __errors, ...fieldErrorSchema } = errorSchema || {};
166
+ // See #439: uiSchema: Don't pass consumed class names or style to child components
167
+ const fieldUiSchema = omit(uiSchema, ['ui:classNames', 'classNames', 'ui:style']);
168
+ if (UI_OPTIONS_KEY in fieldUiSchema) {
169
+ fieldUiSchema[UI_OPTIONS_KEY] = omit(fieldUiSchema[UI_OPTIONS_KEY], ['classNames', 'style']);
170
+ }
171
+
172
+ const field = (
173
+ <FieldComponent
174
+ {...props}
175
+ onChange={handleFieldComponentChange}
176
+ idSchema={idSchema}
177
+ schema={schema}
178
+ uiSchema={fieldUiSchema}
179
+ disabled={disabled}
180
+ readonly={readonly}
181
+ hideError={hideError}
182
+ autofocus={autofocus}
183
+ errorSchema={fieldErrorSchema}
184
+ formContext={formContext}
185
+ rawErrors={__errors}
186
+ />
187
+ );
188
+
189
+ const id = idSchema[ID_KEY];
190
+
191
+ // If this schema has a title defined, but the user has set a new key/label, retain their input.
192
+ let label;
193
+ if (wasPropertyKeyModified) {
194
+ label = name;
195
+ } else {
196
+ label = ADDITIONAL_PROPERTY_FLAG in schema ? name : uiOptions.title || props.schema.title || schema.title || name;
197
+ }
198
+
199
+ const description = uiOptions.description || props.schema.description || schema.description || '';
200
+
201
+ const richDescription = uiOptions.enableMarkdownInDescription ? <Markdown>{description}</Markdown> : description;
202
+
203
+ const help = uiOptions.help;
204
+ const hidden = uiOptions.widget === 'hidden';
205
+
206
+ const classNames = ['form-group', 'field', `field-${getSchemaType(schema)}`];
207
+ if (!hideError && __errors && __errors.length > 0) {
208
+ classNames.push('field-error has-error has-danger');
209
+ }
210
+ if (uiSchema?.classNames) {
211
+ if (process.env.NODE_ENV !== 'production') {
212
+ console.warn(
213
+ "'uiSchema.classNames' is deprecated and may be removed in a major release; Use 'ui:classNames' instead."
214
+ );
215
+ }
216
+ classNames.push(uiSchema.classNames);
217
+ }
218
+ if (uiOptions.classNames) {
219
+ classNames.push(uiOptions.classNames);
220
+ }
221
+
222
+ const helpComponent = (
223
+ <FieldHelpTemplate
224
+ help={help}
225
+ idSchema={idSchema}
226
+ schema={schema}
227
+ uiSchema={uiSchema}
228
+ hasErrors={!hideError && __errors && __errors.length > 0}
229
+ registry={registry}
230
+ />
231
+ );
232
+ /*
233
+ * AnyOf/OneOf errors handled by child schema
234
+ */
235
+ const errorsComponent =
236
+ hideError || schema.anyOf || schema.oneOf ? undefined : (
237
+ <FieldErrorTemplate
238
+ errors={__errors}
239
+ errorSchema={errorSchema}
240
+ idSchema={idSchema}
241
+ schema={schema}
242
+ uiSchema={uiSchema}
243
+ registry={registry}
244
+ />
245
+ );
246
+ const fieldProps: Omit<FieldTemplateProps<T, S, F>, 'children'> = {
247
+ description: (
248
+ <DescriptionFieldTemplate
249
+ id={descriptionId<T>(id)}
250
+ description={richDescription}
251
+ schema={schema}
252
+ uiSchema={uiSchema}
253
+ registry={registry}
254
+ />
255
+ ),
256
+ rawDescription: description,
257
+ help: helpComponent,
258
+ rawHelp: typeof help === 'string' ? help : undefined,
259
+ errors: errorsComponent,
260
+ rawErrors: hideError ? undefined : __errors,
261
+ id,
262
+ label,
263
+ hidden,
264
+ onChange,
265
+ onKeyChange,
266
+ onDropPropertyClick,
267
+ required,
268
+ disabled,
269
+ readonly,
270
+ hideError,
271
+ displayLabel,
272
+ classNames: classNames.join(' ').trim(),
273
+ style: uiOptions.style,
274
+ formContext,
275
+ formData,
276
+ schema,
277
+ uiSchema,
278
+ registry,
279
+ };
280
+
281
+ const _AnyOfField = registry.fields.AnyOfField;
282
+ const _OneOfField = registry.fields.OneOfField;
283
+ const isReplacingAnyOrOneOf = uiSchema?.['ui:field'] && uiSchema?.['ui:fieldReplacesAnyOrOneOf'] === true;
284
+
285
+ return (
286
+ <FieldTemplate {...fieldProps}>
287
+ <>
288
+ {field}
289
+ {/*
290
+ If the schema `anyOf` or 'oneOf' can be rendered as a select control, don't
291
+ render the selection and let `StringField` component handle
292
+ rendering
293
+ */}
294
+ {schema.anyOf && !isReplacingAnyOrOneOf && !schemaUtils.isSelect(schema) && (
295
+ <_AnyOfField
296
+ name={name}
297
+ disabled={disabled}
298
+ readonly={readonly}
299
+ hideError={hideError}
300
+ errorSchema={errorSchema}
301
+ formData={formData}
302
+ formContext={formContext}
303
+ idPrefix={idPrefix}
304
+ idSchema={idSchema}
305
+ idSeparator={idSeparator}
306
+ onBlur={props.onBlur}
307
+ onChange={props.onChange}
308
+ onFocus={props.onFocus}
309
+ options={schema.anyOf.map((_schema) =>
310
+ schemaUtils.retrieveSchema(isObject(_schema) ? (_schema as S) : ({} as S), formData)
311
+ )}
312
+ registry={registry}
313
+ schema={schema}
314
+ uiSchema={uiSchema}
315
+ />
316
+ )}
317
+ {schema.oneOf && !isReplacingAnyOrOneOf && !schemaUtils.isSelect(schema) && (
318
+ <_OneOfField
319
+ name={name}
320
+ disabled={disabled}
321
+ readonly={readonly}
322
+ hideError={hideError}
323
+ errorSchema={errorSchema}
324
+ formData={formData}
325
+ formContext={formContext}
326
+ idPrefix={idPrefix}
327
+ idSchema={idSchema}
328
+ idSeparator={idSeparator}
329
+ onBlur={props.onBlur}
330
+ onChange={props.onChange}
331
+ onFocus={props.onFocus}
332
+ options={schema.oneOf.map((_schema) =>
333
+ schemaUtils.retrieveSchema(isObject(_schema) ? (_schema as S) : ({} as S), formData)
334
+ )}
335
+ registry={registry}
336
+ schema={schema}
337
+ uiSchema={uiSchema}
338
+ />
339
+ )}
340
+ </>
341
+ </FieldTemplate>
342
+ );
343
+ }
344
+
345
+ /** The `SchemaField` component determines whether it is necessary to rerender the component based on any props changes
346
+ * and if so, calls the `SchemaFieldRender` component with the props.
347
+ */
348
+ class SchemaField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> extends Component<
349
+ FieldProps<T, S, F>
350
+ > {
351
+ shouldComponentUpdate(nextProps: Readonly<FieldProps<T, S, F>>) {
352
+ return !deepEquals(this.props, nextProps);
353
+ }
354
+
355
+ render() {
356
+ return <SchemaFieldRender<T, S, F> {...this.props} />;
357
+ }
358
+ }
359
+
360
+ export default SchemaField;