@springmicro/forms 0.6.4 → 0.7.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.
- package/.eslintrc.cjs +22 -22
- package/README.md +11 -11
- package/dist/index.d.ts +0 -0
- package/dist/index.js +0 -0
- package/dist/index.umd.cjs +0 -0
- package/package.json +3 -3
- package/src/builder/bottom-drawer.tsx +429 -429
- package/src/builder/form-builder.tsx +256 -256
- package/src/builder/modal.tsx +39 -39
- package/src/builder/nodes/node-base.tsx +94 -94
- package/src/builder/nodes/node-child-helpers.tsx +273 -273
- package/src/builder/nodes/node-parent.tsx +187 -187
- package/src/builder/nodes/node-types/array-node.tsx +134 -134
- package/src/builder/nodes/node-types/date-node.tsx +60 -60
- package/src/builder/nodes/node-types/file-node.tsx +67 -67
- package/src/builder/nodes/node-types/integer-node.tsx +60 -60
- package/src/builder/nodes/node-types/object-node.tsx +67 -67
- package/src/builder/nodes/node-types/text-node.tsx +66 -66
- package/src/fields/ArrayField.tsx +875 -875
- package/src/fields/BooleanField.tsx +110 -110
- package/src/fields/MultiSchemaField.tsx +236 -236
- package/src/fields/NullField.tsx +22 -22
- package/src/fields/NumberField.tsx +87 -87
- package/src/fields/ObjectField.tsx +338 -338
- package/src/fields/SchemaField.tsx +402 -402
- package/src/fields/StringField.tsx +67 -67
- package/src/fields/index.ts +24 -24
- package/src/index.tsx +26 -26
- package/src/interfaces/MessagesProps.interface.ts +5 -5
- package/src/interfaces/Option.interface.ts +4 -4
- package/src/styles/select.styles.ts +28 -28
- package/src/templates/ArrayFieldDescriptionTemplate.tsx +42 -42
- package/src/templates/ArrayFieldItemTemplate.tsx +78 -78
- package/src/templates/ArrayFieldTemplate.tsx +90 -90
- package/src/templates/ArrayFieldTitleTemplate.tsx +44 -44
- package/src/templates/BaseInputTemplate.tsx +94 -94
- package/src/templates/ButtonTemplates/AddButton.tsx +29 -29
- package/src/templates/ButtonTemplates/IconButton.tsx +49 -49
- package/src/templates/ButtonTemplates/SubmitButton.tsx +29 -29
- package/src/templates/ButtonTemplates/index.ts +16 -16
- package/src/templates/DescriptionField.tsx +29 -29
- package/src/templates/ErrorList.tsx +25 -25
- package/src/templates/FieldTemplate/FieldTemplate.tsx +39 -39
- package/src/templates/FieldTemplate/Label.tsx +29 -29
- package/src/templates/FieldTemplate/WrapIfAdditional.tsx +85 -85
- package/src/templates/FieldTemplate/index.ts +3 -3
- package/src/templates/ObjectFieldTemplate.tsx +79 -79
- package/src/templates/TitleField.tsx +20 -20
- package/src/templates/UnsupportedField.tsx +29 -29
- package/src/templates/index.ts +32 -32
- package/src/types/Message.type.ts +6 -6
- package/src/types/RawMessage.type.ts +15 -15
- package/src/types/form-builder.ts +135 -135
- package/src/types/utils.type.ts +1 -1
- package/src/utils/form-builder.ts +424 -424
- package/src/utils/processSelectValue.ts +50 -50
- package/src/widgets/AltDateTimeWidget.tsx +17 -17
- package/src/widgets/AltDateWidget.tsx +216 -216
- package/src/widgets/CheckboxWidget.tsx +80 -80
- package/src/widgets/CheckboxesWidget.tsx +74 -74
- package/src/widgets/ColorWidget.tsx +26 -26
- package/src/widgets/DateTimeWidget.tsx +28 -28
- package/src/widgets/DateWidget.tsx +36 -36
- package/src/widgets/EmailWidget.tsx +19 -19
- package/src/widgets/FileWidget.tsx +144 -144
- package/src/widgets/HiddenWidget.tsx +22 -22
- package/src/widgets/PasswordWidget.tsx +20 -20
- package/src/widgets/RadioWidget.tsx +87 -87
- package/src/widgets/RangeWidget.tsx +24 -24
- package/src/widgets/SelectWidget.tsx +99 -99
- package/src/widgets/TextWidget.tsx +19 -19
- package/src/widgets/TextareaWidget.tsx +64 -64
- package/src/widgets/URLWidget.tsx +19 -19
- package/src/widgets/UpDownWidget.tsx +20 -20
- package/src/widgets/index.ts +43 -43
- package/tsconfig.json +24 -24
- package/tsconfig.node.json +10 -10
- package/vite.config.ts +25 -25
|
@@ -1,338 +1,338 @@
|
|
|
1
|
-
import React, { Component } from "react";
|
|
2
|
-
import type {
|
|
3
|
-
ErrorSchema,
|
|
4
|
-
FieldProps,
|
|
5
|
-
GenericObjectType,
|
|
6
|
-
IdSchema,
|
|
7
|
-
RJSFSchema,
|
|
8
|
-
} from "@rjsf/utils";
|
|
9
|
-
import {
|
|
10
|
-
getTemplate,
|
|
11
|
-
getUiOptions,
|
|
12
|
-
orderProperties,
|
|
13
|
-
ADDITIONAL_PROPERTY_FLAG,
|
|
14
|
-
PROPERTIES_KEY,
|
|
15
|
-
REF_KEY,
|
|
16
|
-
} from "@rjsf/utils";
|
|
17
|
-
import get from "lodash/get";
|
|
18
|
-
import has from "lodash/has";
|
|
19
|
-
import isObject from "lodash/isObject";
|
|
20
|
-
import set from "lodash/set";
|
|
21
|
-
import unset from "lodash/unset";
|
|
22
|
-
|
|
23
|
-
/** Type used for the state of the `ObjectField` component */
|
|
24
|
-
type ObjectFieldState = {
|
|
25
|
-
/** Flag indicating whether an additional property key was modified */
|
|
26
|
-
wasPropertyKeyModified: boolean;
|
|
27
|
-
/** The set of additional properties */
|
|
28
|
-
additionalProperties: object;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/** The `ObjectField` component is used to render a field in the schema that is of type `object`. It tracks whether an
|
|
32
|
-
* additional property key was modified and what it was modified to
|
|
33
|
-
*
|
|
34
|
-
* @param props - The `FieldProps` for this template
|
|
35
|
-
*/
|
|
36
|
-
class ObjectField<T = any, F extends GenericObjectType = any> extends Component<
|
|
37
|
-
FieldProps<T, F>,
|
|
38
|
-
ObjectFieldState
|
|
39
|
-
> {
|
|
40
|
-
/** Set up the initial state */
|
|
41
|
-
state = {
|
|
42
|
-
wasPropertyKeyModified: false,
|
|
43
|
-
additionalProperties: {},
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/** Returns a flag indicating whether the `name` field is required in the object schema
|
|
47
|
-
*
|
|
48
|
-
* @param name - The name of the field to check for required-ness
|
|
49
|
-
* @returns - True if the field `name` is required, false otherwise
|
|
50
|
-
*/
|
|
51
|
-
isRequired(name: string) {
|
|
52
|
-
const { schema } = this.props;
|
|
53
|
-
return (
|
|
54
|
-
Array.isArray(schema.required) && schema.required.indexOf(name) !== -1
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Returns the `onPropertyChange` handler for the `name` field. Handles the special case where a user is attempting
|
|
59
|
-
* to clear the data for a field added as an additional property. Calls the `onChange()` handler with the updated
|
|
60
|
-
* formData.
|
|
61
|
-
*
|
|
62
|
-
* @param name - The name of the property
|
|
63
|
-
* @param addedByAdditionalProperties - Flag indicating whether this property is an additional property
|
|
64
|
-
* @returns - The onPropertyChange callback for the `name` property
|
|
65
|
-
*/
|
|
66
|
-
onPropertyChange = (name: string, addedByAdditionalProperties = false) => {
|
|
67
|
-
return (value: T, newErrorSchema?: ErrorSchema<T>) => {
|
|
68
|
-
const { formData, onChange, errorSchema } = this.props;
|
|
69
|
-
if (value === undefined && addedByAdditionalProperties) {
|
|
70
|
-
// Don't set value = undefined for fields added by
|
|
71
|
-
// additionalProperties. Doing so removes them from the
|
|
72
|
-
// formData, which causes them to completely disappear
|
|
73
|
-
// (including the input field for the property name). Unlike
|
|
74
|
-
// fields which are "mandated" by the schema, these fields can
|
|
75
|
-
// be set to undefined by clicking a "delete field" button, so
|
|
76
|
-
// set empty values to the empty string.
|
|
77
|
-
value = "" as unknown as T;
|
|
78
|
-
}
|
|
79
|
-
const newFormData = { ...formData, [name]: value };
|
|
80
|
-
onChange(
|
|
81
|
-
// @ts-ignore
|
|
82
|
-
newFormData,
|
|
83
|
-
errorSchema &&
|
|
84
|
-
errorSchema && {
|
|
85
|
-
...errorSchema,
|
|
86
|
-
[name]: newErrorSchema,
|
|
87
|
-
}
|
|
88
|
-
);
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/** Returns a callback to handle the onDropPropertyClick event for the given `key` which removes the old `key` data
|
|
93
|
-
* and calls the `onChange` callback with it
|
|
94
|
-
*
|
|
95
|
-
* @param key - The key for which the drop callback is desired
|
|
96
|
-
* @returns - The drop property click callback
|
|
97
|
-
*/
|
|
98
|
-
onDropPropertyClick = (key: string) => {
|
|
99
|
-
return (event: DragEvent) => {
|
|
100
|
-
event.preventDefault();
|
|
101
|
-
const { onChange, formData } = this.props;
|
|
102
|
-
const copiedFormData = { ...formData };
|
|
103
|
-
unset(copiedFormData, key);
|
|
104
|
-
// @ts-ignore
|
|
105
|
-
onChange(copiedFormData);
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/** Computes the next available key name from the `preferredKey`, indexing through the already existing keys until one
|
|
110
|
-
* that is already not assigned is found.
|
|
111
|
-
*
|
|
112
|
-
* @param preferredKey - The preferred name of a new key
|
|
113
|
-
* @param formData - The form data in which to check if the desired key already exists
|
|
114
|
-
* @returns - The name of the next available key from `preferredKey`
|
|
115
|
-
*/
|
|
116
|
-
getAvailableKey = (preferredKey: string, formData: T) => {
|
|
117
|
-
const { uiSchema } = this.props;
|
|
118
|
-
const { duplicateKeySuffixSeparator = "-" } = getUiOptions<T, F>(uiSchema);
|
|
119
|
-
|
|
120
|
-
let index = 0;
|
|
121
|
-
let newKey = preferredKey;
|
|
122
|
-
// @ts-ignore
|
|
123
|
-
while (newKey in formData) {
|
|
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
|
-
// @ts-ignore
|
|
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
|
-
switch (type) {
|
|
172
|
-
case "string":
|
|
173
|
-
return "New Value";
|
|
174
|
-
case "array":
|
|
175
|
-
return [];
|
|
176
|
-
case "boolean":
|
|
177
|
-
return false;
|
|
178
|
-
case "null":
|
|
179
|
-
return null;
|
|
180
|
-
case "number":
|
|
181
|
-
return 0;
|
|
182
|
-
case "object":
|
|
183
|
-
return {};
|
|
184
|
-
default:
|
|
185
|
-
// We don't have a datatype for some reason (perhaps additionalProperties was true)
|
|
186
|
-
return "New Value";
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
|
|
191
|
-
* default data for that field has been added to the formData.
|
|
192
|
-
*
|
|
193
|
-
* @param schema - The schema element to which the new property is being added
|
|
194
|
-
*/
|
|
195
|
-
handleAddClick = (schema: RJSFSchema) => () => {
|
|
196
|
-
if (!isObject(schema.additionalProperties)) {
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
const { formData, onChange, registry } = this.props;
|
|
200
|
-
let type = schema.additionalProperties.type;
|
|
201
|
-
const newFormData = { ...formData };
|
|
202
|
-
|
|
203
|
-
if (REF_KEY in schema.additionalProperties) {
|
|
204
|
-
const { schemaUtils } = registry;
|
|
205
|
-
const refSchema = schemaUtils.retrieveSchema(
|
|
206
|
-
// @ts-ignore
|
|
207
|
-
{ $ref: schema.additionalProperties[REF_KEY] },
|
|
208
|
-
formData
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
type = refSchema.type;
|
|
212
|
-
}
|
|
213
|
-
// @ts-ignore
|
|
214
|
-
const newKey = this.getAvailableKey("newKey", newFormData);
|
|
215
|
-
// Cast this to make the `set` work properly
|
|
216
|
-
set(newFormData as GenericObjectType, newKey, this.getDefaultValue(type));
|
|
217
|
-
// @ts-ignore
|
|
218
|
-
onChange(newFormData);
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
/** Renders the `ObjectField` from the given props
|
|
222
|
-
*/
|
|
223
|
-
render() {
|
|
224
|
-
const {
|
|
225
|
-
schema: rawSchema,
|
|
226
|
-
uiSchema = {},
|
|
227
|
-
formData,
|
|
228
|
-
errorSchema,
|
|
229
|
-
idSchema,
|
|
230
|
-
name,
|
|
231
|
-
required = false,
|
|
232
|
-
disabled = false,
|
|
233
|
-
readonly = false,
|
|
234
|
-
hideError,
|
|
235
|
-
idPrefix,
|
|
236
|
-
idSeparator,
|
|
237
|
-
onBlur,
|
|
238
|
-
onFocus,
|
|
239
|
-
registry,
|
|
240
|
-
} = this.props;
|
|
241
|
-
|
|
242
|
-
const { fields, formContext, schemaUtils } = registry;
|
|
243
|
-
const { SchemaField } = fields;
|
|
244
|
-
const schema = schemaUtils.retrieveSchema(rawSchema, formData);
|
|
245
|
-
const uiOptions = getUiOptions<T, F>(uiSchema);
|
|
246
|
-
const { properties: schemaProperties = {} } = schema;
|
|
247
|
-
|
|
248
|
-
const title = schema.title === undefined ? name : schema.title;
|
|
249
|
-
const description = uiOptions.description || schema.description;
|
|
250
|
-
let orderedProperties: string[];
|
|
251
|
-
try {
|
|
252
|
-
const properties = Object.keys(schemaProperties);
|
|
253
|
-
orderedProperties = orderProperties(properties, uiOptions.order);
|
|
254
|
-
} catch (err) {
|
|
255
|
-
return (
|
|
256
|
-
<div>
|
|
257
|
-
<p className="config-error" style={{ color: "red" }}>
|
|
258
|
-
Invalid {name || "root"} object field configuration:
|
|
259
|
-
<em>{(err as Error).message}</em>.
|
|
260
|
-
</p>
|
|
261
|
-
<pre>{JSON.stringify(schema)}</pre>
|
|
262
|
-
</div>
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const Template = getTemplate<"ObjectFieldTemplate", T, F>(
|
|
267
|
-
"ObjectFieldTemplate",
|
|
268
|
-
registry,
|
|
269
|
-
uiOptions
|
|
270
|
-
);
|
|
271
|
-
|
|
272
|
-
const templateProps = {
|
|
273
|
-
title: uiOptions.title || title,
|
|
274
|
-
description,
|
|
275
|
-
properties: orderedProperties.map((name) => {
|
|
276
|
-
const addedByAdditionalProperties = has(schema, [
|
|
277
|
-
PROPERTIES_KEY,
|
|
278
|
-
name,
|
|
279
|
-
ADDITIONAL_PROPERTY_FLAG,
|
|
280
|
-
]);
|
|
281
|
-
const fieldUiSchema = addedByAdditionalProperties
|
|
282
|
-
? uiSchema.additionalProperties
|
|
283
|
-
: uiSchema[name];
|
|
284
|
-
const hidden = getUiOptions<T, F>(fieldUiSchema).widget === "hidden";
|
|
285
|
-
const fieldIdSchema: IdSchema<T> = get(idSchema, [name], {});
|
|
286
|
-
|
|
287
|
-
return {
|
|
288
|
-
content: (
|
|
289
|
-
<SchemaField
|
|
290
|
-
key={name}
|
|
291
|
-
name={name}
|
|
292
|
-
required={this.isRequired(name)}
|
|
293
|
-
schema={get(schema, [PROPERTIES_KEY, name], {}) as unknown as F}
|
|
294
|
-
uiSchema={fieldUiSchema}
|
|
295
|
-
errorSchema={get(errorSchema, name)}
|
|
296
|
-
idSchema={fieldIdSchema}
|
|
297
|
-
idPrefix={idPrefix}
|
|
298
|
-
idSeparator={idSeparator}
|
|
299
|
-
formData={get(formData, name)}
|
|
300
|
-
formContext={formContext}
|
|
301
|
-
wasPropertyKeyModified={this.state.wasPropertyKeyModified}
|
|
302
|
-
onKeyChange={this.onKeyChange(name)}
|
|
303
|
-
// @ts-ignore
|
|
304
|
-
onChange={this.onPropertyChange(
|
|
305
|
-
name,
|
|
306
|
-
addedByAdditionalProperties
|
|
307
|
-
)}
|
|
308
|
-
onBlur={onBlur}
|
|
309
|
-
onFocus={onFocus}
|
|
310
|
-
registry={registry}
|
|
311
|
-
disabled={disabled}
|
|
312
|
-
readonly={readonly}
|
|
313
|
-
hideError={hideError}
|
|
314
|
-
onDropPropertyClick={this.onDropPropertyClick}
|
|
315
|
-
/>
|
|
316
|
-
),
|
|
317
|
-
name,
|
|
318
|
-
readonly,
|
|
319
|
-
disabled,
|
|
320
|
-
required,
|
|
321
|
-
hidden,
|
|
322
|
-
};
|
|
323
|
-
}),
|
|
324
|
-
readonly,
|
|
325
|
-
disabled,
|
|
326
|
-
required,
|
|
327
|
-
idSchema,
|
|
328
|
-
uiSchema,
|
|
329
|
-
schema,
|
|
330
|
-
formData,
|
|
331
|
-
formContext,
|
|
332
|
-
registry,
|
|
333
|
-
};
|
|
334
|
-
return <Template {...templateProps} onAddClick={this.handleAddClick} />;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
export default ObjectField;
|
|
1
|
+
import React, { Component } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
ErrorSchema,
|
|
4
|
+
FieldProps,
|
|
5
|
+
GenericObjectType,
|
|
6
|
+
IdSchema,
|
|
7
|
+
RJSFSchema,
|
|
8
|
+
} from "@rjsf/utils";
|
|
9
|
+
import {
|
|
10
|
+
getTemplate,
|
|
11
|
+
getUiOptions,
|
|
12
|
+
orderProperties,
|
|
13
|
+
ADDITIONAL_PROPERTY_FLAG,
|
|
14
|
+
PROPERTIES_KEY,
|
|
15
|
+
REF_KEY,
|
|
16
|
+
} from "@rjsf/utils";
|
|
17
|
+
import get from "lodash/get";
|
|
18
|
+
import has from "lodash/has";
|
|
19
|
+
import isObject from "lodash/isObject";
|
|
20
|
+
import set from "lodash/set";
|
|
21
|
+
import unset from "lodash/unset";
|
|
22
|
+
|
|
23
|
+
/** Type used for the state of the `ObjectField` component */
|
|
24
|
+
type ObjectFieldState = {
|
|
25
|
+
/** Flag indicating whether an additional property key was modified */
|
|
26
|
+
wasPropertyKeyModified: boolean;
|
|
27
|
+
/** The set of additional properties */
|
|
28
|
+
additionalProperties: object;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** The `ObjectField` component is used to render a field in the schema that is of type `object`. It tracks whether an
|
|
32
|
+
* additional property key was modified and what it was modified to
|
|
33
|
+
*
|
|
34
|
+
* @param props - The `FieldProps` for this template
|
|
35
|
+
*/
|
|
36
|
+
class ObjectField<T = any, F extends GenericObjectType = any> extends Component<
|
|
37
|
+
FieldProps<T, F>,
|
|
38
|
+
ObjectFieldState
|
|
39
|
+
> {
|
|
40
|
+
/** Set up the initial state */
|
|
41
|
+
state = {
|
|
42
|
+
wasPropertyKeyModified: false,
|
|
43
|
+
additionalProperties: {},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Returns a flag indicating whether the `name` field is required in the object schema
|
|
47
|
+
*
|
|
48
|
+
* @param name - The name of the field to check for required-ness
|
|
49
|
+
* @returns - True if the field `name` is required, false otherwise
|
|
50
|
+
*/
|
|
51
|
+
isRequired(name: string) {
|
|
52
|
+
const { schema } = this.props;
|
|
53
|
+
return (
|
|
54
|
+
Array.isArray(schema.required) && schema.required.indexOf(name) !== -1
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Returns the `onPropertyChange` handler for the `name` field. Handles the special case where a user is attempting
|
|
59
|
+
* to clear the data for a field added as an additional property. Calls the `onChange()` handler with the updated
|
|
60
|
+
* formData.
|
|
61
|
+
*
|
|
62
|
+
* @param name - The name of the property
|
|
63
|
+
* @param addedByAdditionalProperties - Flag indicating whether this property is an additional property
|
|
64
|
+
* @returns - The onPropertyChange callback for the `name` property
|
|
65
|
+
*/
|
|
66
|
+
onPropertyChange = (name: string, addedByAdditionalProperties = false) => {
|
|
67
|
+
return (value: T, newErrorSchema?: ErrorSchema<T>) => {
|
|
68
|
+
const { formData, onChange, errorSchema } = this.props;
|
|
69
|
+
if (value === undefined && addedByAdditionalProperties) {
|
|
70
|
+
// Don't set value = undefined for fields added by
|
|
71
|
+
// additionalProperties. Doing so removes them from the
|
|
72
|
+
// formData, which causes them to completely disappear
|
|
73
|
+
// (including the input field for the property name). Unlike
|
|
74
|
+
// fields which are "mandated" by the schema, these fields can
|
|
75
|
+
// be set to undefined by clicking a "delete field" button, so
|
|
76
|
+
// set empty values to the empty string.
|
|
77
|
+
value = "" as unknown as T;
|
|
78
|
+
}
|
|
79
|
+
const newFormData = { ...formData, [name]: value };
|
|
80
|
+
onChange(
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
newFormData,
|
|
83
|
+
errorSchema &&
|
|
84
|
+
errorSchema && {
|
|
85
|
+
...errorSchema,
|
|
86
|
+
[name]: newErrorSchema,
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** Returns a callback to handle the onDropPropertyClick event for the given `key` which removes the old `key` data
|
|
93
|
+
* and calls the `onChange` callback with it
|
|
94
|
+
*
|
|
95
|
+
* @param key - The key for which the drop callback is desired
|
|
96
|
+
* @returns - The drop property click callback
|
|
97
|
+
*/
|
|
98
|
+
onDropPropertyClick = (key: string) => {
|
|
99
|
+
return (event: DragEvent) => {
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
const { onChange, formData } = this.props;
|
|
102
|
+
const copiedFormData = { ...formData };
|
|
103
|
+
unset(copiedFormData, key);
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
onChange(copiedFormData);
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** Computes the next available key name from the `preferredKey`, indexing through the already existing keys until one
|
|
110
|
+
* that is already not assigned is found.
|
|
111
|
+
*
|
|
112
|
+
* @param preferredKey - The preferred name of a new key
|
|
113
|
+
* @param formData - The form data in which to check if the desired key already exists
|
|
114
|
+
* @returns - The name of the next available key from `preferredKey`
|
|
115
|
+
*/
|
|
116
|
+
getAvailableKey = (preferredKey: string, formData: T) => {
|
|
117
|
+
const { uiSchema } = this.props;
|
|
118
|
+
const { duplicateKeySuffixSeparator = "-" } = getUiOptions<T, F>(uiSchema);
|
|
119
|
+
|
|
120
|
+
let index = 0;
|
|
121
|
+
let newKey = preferredKey;
|
|
122
|
+
// @ts-ignore
|
|
123
|
+
while (newKey in formData) {
|
|
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
|
+
// @ts-ignore
|
|
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
|
+
switch (type) {
|
|
172
|
+
case "string":
|
|
173
|
+
return "New Value";
|
|
174
|
+
case "array":
|
|
175
|
+
return [];
|
|
176
|
+
case "boolean":
|
|
177
|
+
return false;
|
|
178
|
+
case "null":
|
|
179
|
+
return null;
|
|
180
|
+
case "number":
|
|
181
|
+
return 0;
|
|
182
|
+
case "object":
|
|
183
|
+
return {};
|
|
184
|
+
default:
|
|
185
|
+
// We don't have a datatype for some reason (perhaps additionalProperties was true)
|
|
186
|
+
return "New Value";
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Handles the adding of a new additional property on the given `schema`. Calls the `onChange` callback once the new
|
|
191
|
+
* default data for that field has been added to the formData.
|
|
192
|
+
*
|
|
193
|
+
* @param schema - The schema element to which the new property is being added
|
|
194
|
+
*/
|
|
195
|
+
handleAddClick = (schema: RJSFSchema) => () => {
|
|
196
|
+
if (!isObject(schema.additionalProperties)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const { formData, onChange, registry } = this.props;
|
|
200
|
+
let type = schema.additionalProperties.type;
|
|
201
|
+
const newFormData = { ...formData };
|
|
202
|
+
|
|
203
|
+
if (REF_KEY in schema.additionalProperties) {
|
|
204
|
+
const { schemaUtils } = registry;
|
|
205
|
+
const refSchema = schemaUtils.retrieveSchema(
|
|
206
|
+
// @ts-ignore
|
|
207
|
+
{ $ref: schema.additionalProperties[REF_KEY] },
|
|
208
|
+
formData
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
type = refSchema.type;
|
|
212
|
+
}
|
|
213
|
+
// @ts-ignore
|
|
214
|
+
const newKey = this.getAvailableKey("newKey", newFormData);
|
|
215
|
+
// Cast this to make the `set` work properly
|
|
216
|
+
set(newFormData as GenericObjectType, newKey, this.getDefaultValue(type));
|
|
217
|
+
// @ts-ignore
|
|
218
|
+
onChange(newFormData);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/** Renders the `ObjectField` from the given props
|
|
222
|
+
*/
|
|
223
|
+
render() {
|
|
224
|
+
const {
|
|
225
|
+
schema: rawSchema,
|
|
226
|
+
uiSchema = {},
|
|
227
|
+
formData,
|
|
228
|
+
errorSchema,
|
|
229
|
+
idSchema,
|
|
230
|
+
name,
|
|
231
|
+
required = false,
|
|
232
|
+
disabled = false,
|
|
233
|
+
readonly = false,
|
|
234
|
+
hideError,
|
|
235
|
+
idPrefix,
|
|
236
|
+
idSeparator,
|
|
237
|
+
onBlur,
|
|
238
|
+
onFocus,
|
|
239
|
+
registry,
|
|
240
|
+
} = this.props;
|
|
241
|
+
|
|
242
|
+
const { fields, formContext, schemaUtils } = registry;
|
|
243
|
+
const { SchemaField } = fields;
|
|
244
|
+
const schema = schemaUtils.retrieveSchema(rawSchema, formData);
|
|
245
|
+
const uiOptions = getUiOptions<T, F>(uiSchema);
|
|
246
|
+
const { properties: schemaProperties = {} } = schema;
|
|
247
|
+
|
|
248
|
+
const title = schema.title === undefined ? name : schema.title;
|
|
249
|
+
const description = uiOptions.description || schema.description;
|
|
250
|
+
let orderedProperties: string[];
|
|
251
|
+
try {
|
|
252
|
+
const properties = Object.keys(schemaProperties);
|
|
253
|
+
orderedProperties = orderProperties(properties, uiOptions.order);
|
|
254
|
+
} catch (err) {
|
|
255
|
+
return (
|
|
256
|
+
<div>
|
|
257
|
+
<p className="config-error" style={{ color: "red" }}>
|
|
258
|
+
Invalid {name || "root"} object field configuration:
|
|
259
|
+
<em>{(err as Error).message}</em>.
|
|
260
|
+
</p>
|
|
261
|
+
<pre>{JSON.stringify(schema)}</pre>
|
|
262
|
+
</div>
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const Template = getTemplate<"ObjectFieldTemplate", T, F>(
|
|
267
|
+
"ObjectFieldTemplate",
|
|
268
|
+
registry,
|
|
269
|
+
uiOptions
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
const templateProps = {
|
|
273
|
+
title: uiOptions.title || title,
|
|
274
|
+
description,
|
|
275
|
+
properties: orderedProperties.map((name) => {
|
|
276
|
+
const addedByAdditionalProperties = has(schema, [
|
|
277
|
+
PROPERTIES_KEY,
|
|
278
|
+
name,
|
|
279
|
+
ADDITIONAL_PROPERTY_FLAG,
|
|
280
|
+
]);
|
|
281
|
+
const fieldUiSchema = addedByAdditionalProperties
|
|
282
|
+
? uiSchema.additionalProperties
|
|
283
|
+
: uiSchema[name];
|
|
284
|
+
const hidden = getUiOptions<T, F>(fieldUiSchema).widget === "hidden";
|
|
285
|
+
const fieldIdSchema: IdSchema<T> = get(idSchema, [name], {});
|
|
286
|
+
|
|
287
|
+
return {
|
|
288
|
+
content: (
|
|
289
|
+
<SchemaField
|
|
290
|
+
key={name}
|
|
291
|
+
name={name}
|
|
292
|
+
required={this.isRequired(name)}
|
|
293
|
+
schema={get(schema, [PROPERTIES_KEY, name], {}) as unknown as F}
|
|
294
|
+
uiSchema={fieldUiSchema}
|
|
295
|
+
errorSchema={get(errorSchema, name)}
|
|
296
|
+
idSchema={fieldIdSchema}
|
|
297
|
+
idPrefix={idPrefix}
|
|
298
|
+
idSeparator={idSeparator}
|
|
299
|
+
formData={get(formData, name)}
|
|
300
|
+
formContext={formContext}
|
|
301
|
+
wasPropertyKeyModified={this.state.wasPropertyKeyModified}
|
|
302
|
+
onKeyChange={this.onKeyChange(name)}
|
|
303
|
+
// @ts-ignore
|
|
304
|
+
onChange={this.onPropertyChange(
|
|
305
|
+
name,
|
|
306
|
+
addedByAdditionalProperties
|
|
307
|
+
)}
|
|
308
|
+
onBlur={onBlur}
|
|
309
|
+
onFocus={onFocus}
|
|
310
|
+
registry={registry}
|
|
311
|
+
disabled={disabled}
|
|
312
|
+
readonly={readonly}
|
|
313
|
+
hideError={hideError}
|
|
314
|
+
onDropPropertyClick={this.onDropPropertyClick}
|
|
315
|
+
/>
|
|
316
|
+
),
|
|
317
|
+
name,
|
|
318
|
+
readonly,
|
|
319
|
+
disabled,
|
|
320
|
+
required,
|
|
321
|
+
hidden,
|
|
322
|
+
};
|
|
323
|
+
}),
|
|
324
|
+
readonly,
|
|
325
|
+
disabled,
|
|
326
|
+
required,
|
|
327
|
+
idSchema,
|
|
328
|
+
uiSchema,
|
|
329
|
+
schema,
|
|
330
|
+
formData,
|
|
331
|
+
formContext,
|
|
332
|
+
registry,
|
|
333
|
+
};
|
|
334
|
+
return <Template {...templateProps} onAddClick={this.handleAddClick} />;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export default ObjectField;
|