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