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