@rjsf/core 6.0.0-beta.9 → 6.0.1
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,8 +1,8 @@
|
|
|
1
|
-
import { ComponentType,
|
|
1
|
+
import { ComponentType, ReactNode } from 'react';
|
|
2
2
|
import {
|
|
3
3
|
ANY_OF_KEY,
|
|
4
|
-
ErrorSchema,
|
|
5
4
|
FieldProps,
|
|
5
|
+
FieldPathId,
|
|
6
6
|
FormContextType,
|
|
7
7
|
GenericObjectType,
|
|
8
8
|
getDiscriminatorFieldFromSchema,
|
|
@@ -11,21 +11,20 @@ import {
|
|
|
11
11
|
getUiOptions,
|
|
12
12
|
hashObject,
|
|
13
13
|
ID_KEY,
|
|
14
|
-
IdSchema,
|
|
15
14
|
lookupFromFormContext,
|
|
16
|
-
mergeObjects,
|
|
17
15
|
ONE_OF_KEY,
|
|
18
16
|
PROPERTIES_KEY,
|
|
19
17
|
READONLY_KEY,
|
|
20
18
|
RJSFSchema,
|
|
21
19
|
Registry,
|
|
22
|
-
SchemaUtilsType,
|
|
23
20
|
StrictRJSFSchema,
|
|
21
|
+
toFieldPathId,
|
|
24
22
|
UI_OPTIONS_KEY,
|
|
25
23
|
UI_GLOBAL_OPTIONS_KEY,
|
|
26
24
|
UiSchema,
|
|
25
|
+
ITEMS_KEY,
|
|
26
|
+
useDeepCompareMemo,
|
|
27
27
|
} from '@rjsf/utils';
|
|
28
|
-
import cloneDeep from 'lodash/cloneDeep';
|
|
29
28
|
import each from 'lodash/each';
|
|
30
29
|
import flatten from 'lodash/flatten';
|
|
31
30
|
import get from 'lodash/get';
|
|
@@ -39,6 +38,7 @@ import isObject from 'lodash/isObject';
|
|
|
39
38
|
import isPlainObject from 'lodash/isPlainObject';
|
|
40
39
|
import isString from 'lodash/isString';
|
|
41
40
|
import isUndefined from 'lodash/isUndefined';
|
|
41
|
+
import last from 'lodash/last';
|
|
42
42
|
import set from 'lodash/set';
|
|
43
43
|
|
|
44
44
|
/** The enumeration of the three different Layout GridTemplate type values
|
|
@@ -130,6 +130,603 @@ function getNonNullishValue<T = unknown>(value?: T, fallback?: T): T | undefined
|
|
|
130
130
|
return value ?? fallback;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
/** Detects if a `str` is made up entirely of numeric characters
|
|
134
|
+
*
|
|
135
|
+
* @param str - The string to check to see if it is a numeric index
|
|
136
|
+
* @return - True if the string consists entirely of numeric characters
|
|
137
|
+
*/
|
|
138
|
+
function isNumericIndex(str: string) {
|
|
139
|
+
return /^\d+?$/.test(str); // Matches positive integers
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const LAYOUT_GRID_FIELD_TEST_IDS = getTestIds();
|
|
143
|
+
|
|
144
|
+
/** Computes the uiSchema for the field with `name` from the `uiProps` and `uiSchema` provided. The field UI Schema
|
|
145
|
+
* will always contain a copy of the global options from the `uiSchema` (so they can be passed down) as well as
|
|
146
|
+
* copying them into the local ui options. When the `forceReadonly` flag is true, then the field UI Schema is
|
|
147
|
+
* updated to make "readonly" be true. When the `schemaReadonly` flag is true AND the field UI Schema does NOT have
|
|
148
|
+
* the flag already provided, then we also make "readonly" true. We always make sure to return the final value of the
|
|
149
|
+
* field UI Schema's "readonly" flag as `uiReadonly` along with the `fieldUiSchema` in the return value.
|
|
150
|
+
*
|
|
151
|
+
* @param field - The name of the field to pull the existing UI Schema for
|
|
152
|
+
* @param uiProps - Any props that should be put into the field's uiSchema
|
|
153
|
+
* @param [uiSchema] - The optional UI Schema from which to get the UI schema for the field
|
|
154
|
+
* @param [schemaReadonly] - Optional flag indicating whether the schema indicates the field is readonly
|
|
155
|
+
* @param [forceReadonly] - Optional flag indicating whether the Form itself is in readonly mode
|
|
156
|
+
*/
|
|
157
|
+
export function computeFieldUiSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
158
|
+
field: string,
|
|
159
|
+
uiProps: ConfigObject,
|
|
160
|
+
uiSchema?: UiSchema<T, S, F>,
|
|
161
|
+
schemaReadonly?: boolean,
|
|
162
|
+
forceReadonly?: boolean,
|
|
163
|
+
) {
|
|
164
|
+
const globalUiOptions = get(uiSchema, [UI_GLOBAL_OPTIONS_KEY], {});
|
|
165
|
+
const localUiSchema = get(uiSchema, field);
|
|
166
|
+
const localUiOptions = { ...get(localUiSchema, [UI_OPTIONS_KEY], {}), ...uiProps, ...globalUiOptions };
|
|
167
|
+
const fieldUiSchema = { ...localUiSchema };
|
|
168
|
+
if (!isEmpty(localUiOptions)) {
|
|
169
|
+
set(fieldUiSchema, [UI_OPTIONS_KEY], localUiOptions);
|
|
170
|
+
}
|
|
171
|
+
if (!isEmpty(globalUiOptions)) {
|
|
172
|
+
// pass the global uiOptions down to the field uiSchema so that they can be applied to all nested fields
|
|
173
|
+
set(fieldUiSchema, [UI_GLOBAL_OPTIONS_KEY], globalUiOptions);
|
|
174
|
+
}
|
|
175
|
+
let { readonly: uiReadonly } = getUiOptions<T, S, F>(fieldUiSchema);
|
|
176
|
+
if (forceReadonly === true || (isUndefined(uiReadonly) && schemaReadonly === true)) {
|
|
177
|
+
// If we are forcing all widgets to be readonly, OR the schema indicates it is readonly AND the uiSchema does not
|
|
178
|
+
// have an overriding value, then update the uiSchema to set readonly to true. Doing this will
|
|
179
|
+
uiReadonly = true;
|
|
180
|
+
if (has(localUiOptions, READONLY_KEY)) {
|
|
181
|
+
// If the local options has the key value provided in it, then set that one to true
|
|
182
|
+
set(fieldUiSchema, [UI_OPTIONS_KEY, READONLY_KEY], true);
|
|
183
|
+
} else {
|
|
184
|
+
// otherwise set the `ui:` version
|
|
185
|
+
set(fieldUiSchema, `ui:${READONLY_KEY}`, true);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return { fieldUiSchema, uiReadonly };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Given an `operator`, `datum` and `value` determines whether this condition is considered matching. Matching
|
|
192
|
+
* depends on the `operator`. The `datum` and `value` are converted into arrays if they aren't already and then the
|
|
193
|
+
* contents of the two arrays are compared using the `operator`. When `operator` is All, then the two arrays must be
|
|
194
|
+
* equal to match. When `operator` is SOME then the intersection of the two arrays must have at least one value in
|
|
195
|
+
* common to match. When `operator` is NONE then the intersection of the two arrays must not have any values in common
|
|
196
|
+
* to match.
|
|
197
|
+
*
|
|
198
|
+
* @param [operator] - The optional operator for the condition
|
|
199
|
+
* @param [datum] - The optional datum for the condition, this can be an item or a list of items of type unknown
|
|
200
|
+
* @param [value='$0m3tH1nG Un3xP3cT3d'] The optional value for the condition, defaulting to a highly unlikely value
|
|
201
|
+
* to avoid comparing two undefined elements when `value` was forgotten in the condition definition.
|
|
202
|
+
* This can be an item or a list of items of type unknown
|
|
203
|
+
* @returns - True if the condition matches, false otherwise
|
|
204
|
+
*/
|
|
205
|
+
export function conditionMatches(
|
|
206
|
+
operator?: Operators,
|
|
207
|
+
datum?: unknown,
|
|
208
|
+
value: unknown = '$0m3tH1nG Un3xP3cT3d',
|
|
209
|
+
): boolean {
|
|
210
|
+
const data = flatten([datum]).sort();
|
|
211
|
+
const values = flatten([value]).sort();
|
|
212
|
+
switch (operator) {
|
|
213
|
+
case Operators.ALL:
|
|
214
|
+
return isEqual(data, values);
|
|
215
|
+
case Operators.SOME:
|
|
216
|
+
return intersection(data, values).length > 0;
|
|
217
|
+
case Operators.NONE:
|
|
218
|
+
return intersection(data, values).length === 0;
|
|
219
|
+
default:
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** From within the `layoutGridSchema` finds the `children` and any extra `gridProps` from the object keyed by
|
|
225
|
+
* `schemaKey`. If the `children` contains extra `gridProps` and those props contain a `className` string, try to
|
|
226
|
+
* lookup whether that `className` has a replacement value in the `registry` using the `FORM_CONTEXT_LOOKUP_BASE`.
|
|
227
|
+
* When the `className` value contains multiple classNames separated by a space, the lookup will look for a
|
|
228
|
+
* replacement value for each `className` and combine them into one.
|
|
229
|
+
*
|
|
230
|
+
* @param layoutGridSchema - The GridSchemaType instance from which to obtain the `schemaKey` children and extra props
|
|
231
|
+
* @param schemaKey - A `GridType` value, used to get the children and extra props from within the `layoutGridSchema`
|
|
232
|
+
* @param registry - The `@rjsf` Registry from which to look up `classNames` if they are present in the extra props
|
|
233
|
+
* @returns - An object containing the list of `LayoutGridSchemaType` `children` and any extra `gridProps`
|
|
234
|
+
* @throws - A `TypeError` when the `children` is not an array
|
|
235
|
+
*/
|
|
236
|
+
export function findChildrenAndProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
237
|
+
layoutGridSchema: GridSchemaType,
|
|
238
|
+
schemaKey: GridType,
|
|
239
|
+
registry: Registry<T, S, F>,
|
|
240
|
+
) {
|
|
241
|
+
let gridProps: GridProps = {};
|
|
242
|
+
let children = layoutGridSchema[schemaKey];
|
|
243
|
+
if (isPlainObject(children)) {
|
|
244
|
+
const { children: elements, className: toMapClassNames, ...otherProps } = children as ConfigObject;
|
|
245
|
+
children = elements;
|
|
246
|
+
if (toMapClassNames) {
|
|
247
|
+
const classes = toMapClassNames.split(' ');
|
|
248
|
+
const className = classes.map((ele: string) => lookupFromFormContext<T, S, F>(registry, ele, ele)).join(' ');
|
|
249
|
+
gridProps = { ...otherProps, className };
|
|
250
|
+
} else {
|
|
251
|
+
gridProps = otherProps;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (!Array.isArray(children)) {
|
|
255
|
+
throw new TypeError(`Expected array for "${schemaKey}" in ${JSON.stringify(layoutGridSchema)}`);
|
|
256
|
+
}
|
|
257
|
+
return { children: children as LayoutGridSchemaType[], gridProps };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Computes the `rawSchema` and `fieldPathId` for a `schema` and a `potentialIndex`. If the `schema` is of type array,
|
|
261
|
+
* has an `ITEMS_KEY` element and `potentialIndex` represents a numeric value, the element at `ITEMS_KEY` is checked
|
|
262
|
+
* to see if it is an array. If it is AND the `potentialIndex`th element is available, it is used as the `rawSchema`,
|
|
263
|
+
* otherwise the last value of the element is used. If it is not, then the element is used as the `rawSchema`. In
|
|
264
|
+
* either case, an `fieldPathId` is computed for the array index. If the `schema` does not represent an array or the
|
|
265
|
+
* `potentialIndex` is not a numeric value, then `rawSchema` is returned as undefined and given `fieldPathId` is returned
|
|
266
|
+
* as is.
|
|
267
|
+
*
|
|
268
|
+
* @param schema - The schema to generate the fieldPathId for
|
|
269
|
+
* @param fieldPathId - The FieldPathId for the schema
|
|
270
|
+
* @param potentialIndex - A string containing a potential index
|
|
271
|
+
* @returns - An object containing the `rawSchema` and `fieldPathId` of an array item, otherwise an undefined `rawSchema`
|
|
272
|
+
*/
|
|
273
|
+
export function computeArraySchemasIfPresent<S extends StrictRJSFSchema = RJSFSchema>(
|
|
274
|
+
schema: S | undefined,
|
|
275
|
+
fieldPathId: FieldPathId,
|
|
276
|
+
potentialIndex: string,
|
|
277
|
+
): {
|
|
278
|
+
rawSchema?: S;
|
|
279
|
+
fieldPathId: FieldPathId;
|
|
280
|
+
} {
|
|
281
|
+
let rawSchema: S | undefined;
|
|
282
|
+
if (isNumericIndex(potentialIndex) && schema && schema?.type === 'array' && has(schema, ITEMS_KEY)) {
|
|
283
|
+
const index = Number(potentialIndex);
|
|
284
|
+
const items = schema[ITEMS_KEY];
|
|
285
|
+
if (Array.isArray(items)) {
|
|
286
|
+
if (index > items.length) {
|
|
287
|
+
rawSchema = last(items) as S;
|
|
288
|
+
} else {
|
|
289
|
+
rawSchema = items[index] as S;
|
|
290
|
+
}
|
|
291
|
+
} else {
|
|
292
|
+
rawSchema = items as S;
|
|
293
|
+
}
|
|
294
|
+
fieldPathId = {
|
|
295
|
+
[ID_KEY]: fieldPathId[ID_KEY],
|
|
296
|
+
path: [...fieldPathId.path.slice(0, fieldPathId.path.length - 1), index],
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
return { rawSchema, fieldPathId };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Given a `dottedPath` to a field in the `initialSchema`, iterate through each individual path in the schema until
|
|
303
|
+
* the leaf path is found and returned (along with whether that leaf path `isRequired`) OR no schema exists for an
|
|
304
|
+
* element in the path. If the leaf schema element happens to be a oneOf/anyOf then also return the oneOf/anyOf as
|
|
305
|
+
* `options`.
|
|
306
|
+
*
|
|
307
|
+
* @param registry - The registry
|
|
308
|
+
* @param dottedPath - The dotted-path to the field for which to get the schema
|
|
309
|
+
* @param initialSchema - The initial schema to start the search from
|
|
310
|
+
* @param formData - The formData, useful for resolving a oneOf/anyOf selection in the path hierarchy
|
|
311
|
+
* @param initialFieldIdPath - The initial fieldPathId to start the search from
|
|
312
|
+
* @returns - An object containing the destination schema, isRequired and isReadonly flags for the field and options
|
|
313
|
+
* info if a oneOf/anyOf
|
|
314
|
+
*/
|
|
315
|
+
export function getSchemaDetailsForField<
|
|
316
|
+
T = any,
|
|
317
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
318
|
+
F extends FormContextType = any,
|
|
319
|
+
>(
|
|
320
|
+
registry: Registry<T, S, F>,
|
|
321
|
+
dottedPath: string,
|
|
322
|
+
initialSchema: S,
|
|
323
|
+
formData: FieldProps<T, S, F>['formData'],
|
|
324
|
+
initialFieldIdPath: FieldPathId,
|
|
325
|
+
): {
|
|
326
|
+
schema?: S;
|
|
327
|
+
isRequired: boolean;
|
|
328
|
+
isReadonly?: boolean;
|
|
329
|
+
optionsInfo?: OneOfOptionsInfoType<S>;
|
|
330
|
+
fieldPathId: FieldPathId;
|
|
331
|
+
} {
|
|
332
|
+
const { schemaUtils, globalFormOptions } = registry;
|
|
333
|
+
let rawSchema: S = initialSchema;
|
|
334
|
+
let fieldPathId = initialFieldIdPath;
|
|
335
|
+
const parts: string[] = dottedPath.split('.');
|
|
336
|
+
const leafPath: string | undefined = parts.pop(); // pop off the last element in the list as the leaf
|
|
337
|
+
let schema: S | undefined = schemaUtils.retrieveSchema(rawSchema, formData); // always returns an object
|
|
338
|
+
let innerData = formData;
|
|
339
|
+
let isReadonly: boolean | undefined = schema.readOnly;
|
|
340
|
+
|
|
341
|
+
// For all the remaining path parts
|
|
342
|
+
parts.forEach((part) => {
|
|
343
|
+
// dive into the properties of the current schema (when it exists) and get the schema for the next part
|
|
344
|
+
fieldPathId = toFieldPathId(part, globalFormOptions, fieldPathId);
|
|
345
|
+
if (has(schema, PROPERTIES_KEY)) {
|
|
346
|
+
rawSchema = get(schema, [PROPERTIES_KEY, part], {}) as S;
|
|
347
|
+
} else if (schema && (has(schema, ONE_OF_KEY) || has(schema, ANY_OF_KEY))) {
|
|
348
|
+
const xxx = has(schema, ONE_OF_KEY) ? ONE_OF_KEY : ANY_OF_KEY;
|
|
349
|
+
// When the schema represents a oneOf/anyOf, find the selected schema for it and grab the inner part
|
|
350
|
+
const selectedSchema = schemaUtils.findSelectedOptionInXxxOf(schema, part, xxx, innerData);
|
|
351
|
+
rawSchema = get(selectedSchema, [PROPERTIES_KEY, part], {}) as S;
|
|
352
|
+
} else {
|
|
353
|
+
const result = computeArraySchemasIfPresent<S>(schema, fieldPathId, part);
|
|
354
|
+
rawSchema = result.rawSchema ?? ({} as S);
|
|
355
|
+
fieldPathId = result.fieldPathId;
|
|
356
|
+
}
|
|
357
|
+
// Now drill into the innerData for the part, returning an empty object by default if it doesn't exist
|
|
358
|
+
innerData = get(innerData, part, {}) as T;
|
|
359
|
+
// Resolve any `$ref`s for the current rawSchema
|
|
360
|
+
schema = schemaUtils.retrieveSchema(rawSchema, innerData);
|
|
361
|
+
isReadonly = getNonNullishValue(schema.readOnly, isReadonly);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
let optionsInfo: OneOfOptionsInfoType<S> | undefined;
|
|
365
|
+
let isRequired = false;
|
|
366
|
+
// retrieveSchema will return an empty schema in the worst case scenario, convert it to undefined
|
|
367
|
+
if (isEmpty(schema)) {
|
|
368
|
+
schema = undefined;
|
|
369
|
+
}
|
|
370
|
+
if (schema && leafPath) {
|
|
371
|
+
// When we have both a schema and a leafPath...
|
|
372
|
+
if (schema && (has(schema, ONE_OF_KEY) || has(schema, ANY_OF_KEY))) {
|
|
373
|
+
const xxx = has(schema, ONE_OF_KEY) ? ONE_OF_KEY : ANY_OF_KEY;
|
|
374
|
+
// Grab the selected schema for the oneOf/anyOf value for the leafPath using the innerData
|
|
375
|
+
schema = schemaUtils.findSelectedOptionInXxxOf(schema, leafPath, xxx, innerData);
|
|
376
|
+
}
|
|
377
|
+
fieldPathId = toFieldPathId(leafPath, globalFormOptions, fieldPathId);
|
|
378
|
+
isRequired = schema !== undefined && Array.isArray(schema.required) && includes(schema.required, leafPath);
|
|
379
|
+
const result = computeArraySchemasIfPresent<S>(schema, fieldPathId, leafPath);
|
|
380
|
+
if (result.rawSchema) {
|
|
381
|
+
schema = result.rawSchema;
|
|
382
|
+
fieldPathId = result.fieldPathId;
|
|
383
|
+
} else {
|
|
384
|
+
// Now grab the schema from the leafPath of the current schema properties
|
|
385
|
+
schema = get(schema, [PROPERTIES_KEY, leafPath]) as S | undefined;
|
|
386
|
+
// Resolve any `$ref`s for the current schema
|
|
387
|
+
schema = schema ? schemaUtils.retrieveSchema(schema) : schema;
|
|
388
|
+
}
|
|
389
|
+
isReadonly = getNonNullishValue(schema?.readOnly, isReadonly);
|
|
390
|
+
if (schema && (has(schema, ONE_OF_KEY) || has(schema, ANY_OF_KEY))) {
|
|
391
|
+
const xxx = has(schema, ONE_OF_KEY) ? ONE_OF_KEY : ANY_OF_KEY;
|
|
392
|
+
// Set the options if we have a schema with a oneOf/anyOf
|
|
393
|
+
const discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
394
|
+
optionsInfo = { options: schema[xxx] as S[], hasDiscriminator: !!discriminator };
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return { schema, isRequired, isReadonly, optionsInfo, fieldPathId };
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Gets the custom render component from the `render`, by either determining that it is either already a function or
|
|
402
|
+
* it is a non-function value that can be used to look up the function in the registry. If no function can be found,
|
|
403
|
+
* null is returned.
|
|
404
|
+
*
|
|
405
|
+
* @param render - The potential render function or lookup name to one
|
|
406
|
+
* @param registry - The `@rjsf` Registry from which to look up `classNames` if they are present in the extra props
|
|
407
|
+
* @returns - Either a render function if available, or null if not
|
|
408
|
+
*/
|
|
409
|
+
export function getCustomRenderComponent<
|
|
410
|
+
T = any,
|
|
411
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
412
|
+
F extends FormContextType = any,
|
|
413
|
+
>(render: string | RenderComponent, registry: Registry<T, S, F>): RenderComponent | null {
|
|
414
|
+
let customRenderer = render;
|
|
415
|
+
if (isString(customRenderer)) {
|
|
416
|
+
customRenderer = lookupFromFormContext<T, S, F>(registry, customRenderer);
|
|
417
|
+
}
|
|
418
|
+
if (isFunction(customRenderer)) {
|
|
419
|
+
return customRenderer;
|
|
420
|
+
}
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Extract the `name`, and optional `render` and all other props from the `gridSchema`. We look up the `render` to
|
|
425
|
+
* see if can be resolved to a UIComponent. If `name` does not exist and there is an optional `render` UIComponent, we
|
|
426
|
+
* set the `rendered` component with only specified props for that component in the object.
|
|
427
|
+
*
|
|
428
|
+
* @param registry - The `@rjsf` Registry from which to look up `classNames` if they are present in the extra props
|
|
429
|
+
* @param gridSchema - The string or object that represents the configuration for the grid field
|
|
430
|
+
* @returns - The UIComponentPropsType computed from the gridSchema
|
|
431
|
+
*/
|
|
432
|
+
export function computeUIComponentPropsFromGridSchema<
|
|
433
|
+
T = any,
|
|
434
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
435
|
+
F extends FormContextType = any,
|
|
436
|
+
>(registry: Registry<T, S, F>, gridSchema?: string | ConfigObject): UIComponentPropsType {
|
|
437
|
+
let name: string;
|
|
438
|
+
let UIComponent: RenderComponent | null = null;
|
|
439
|
+
let uiProps: ConfigObject = {};
|
|
440
|
+
let rendered: ReactNode | undefined;
|
|
441
|
+
if (isString(gridSchema) || isUndefined(gridSchema)) {
|
|
442
|
+
name = gridSchema ?? '';
|
|
443
|
+
} else {
|
|
444
|
+
const { name: innerName = '', render, ...innerProps } = gridSchema;
|
|
445
|
+
name = innerName;
|
|
446
|
+
uiProps = innerProps;
|
|
447
|
+
if (!isEmpty(uiProps)) {
|
|
448
|
+
// Transform any `$lookup=` in the uiProps props with the appropriate value
|
|
449
|
+
each(uiProps, (prop: ConfigObject, key: string) => {
|
|
450
|
+
if (isString(prop)) {
|
|
451
|
+
const match: string[] | null = LOOKUP_REGEX.exec(prop);
|
|
452
|
+
if (Array.isArray(match) && match.length > 1) {
|
|
453
|
+
const name = match[1];
|
|
454
|
+
uiProps[key] = lookupFromFormContext(registry, name, name);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
UIComponent = getCustomRenderComponent<T, S, F>(render, registry);
|
|
460
|
+
if (!innerName && UIComponent) {
|
|
461
|
+
rendered = <UIComponent {...innerProps} data-testid={LAYOUT_GRID_FIELD_TEST_IDS.uiComponent} />;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return { name, UIComponent, uiProps, rendered };
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* The props for the LayoutGridFieldChildren component.
|
|
469
|
+
*/
|
|
470
|
+
type LayoutGridFieldChildrenProps<
|
|
471
|
+
T = any,
|
|
472
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
473
|
+
F extends FormContextType = any,
|
|
474
|
+
> = LayoutGridFieldProps<T, S, F> & {
|
|
475
|
+
/** The list of strings or objects that represents the configurations for the children fields */
|
|
476
|
+
childrenLayoutGridSchemaId: LayoutGridSchemaType[];
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
/** Iterates through all the `childrenLayoutGridSchemaId`, rendering a nested `LayoutGridField` for each item in the
|
|
480
|
+
* list, passing all the props for the current `LayoutGridField` along, updating the `schema` by calling
|
|
481
|
+
* `retrieveSchema()` on it to resolve any `$ref`s. In addition to the updated `schema`, each item in
|
|
482
|
+
* `childrenLayoutGridSchemaId` is passed as `layoutGridSchema`.
|
|
483
|
+
*
|
|
484
|
+
* @returns - The nested `LayoutGridField`s
|
|
485
|
+
*/
|
|
486
|
+
function LayoutGridFieldChildren<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
487
|
+
props: LayoutGridFieldChildrenProps<T, S, F>,
|
|
488
|
+
) {
|
|
489
|
+
const { childrenLayoutGridSchemaId, ...layoutGridFieldProps } = props;
|
|
490
|
+
const { registry, schema: rawSchema, formData } = layoutGridFieldProps;
|
|
491
|
+
const { schemaUtils } = registry;
|
|
492
|
+
const schema = schemaUtils.retrieveSchema(rawSchema, formData);
|
|
493
|
+
return childrenLayoutGridSchemaId.map((layoutGridSchema) => (
|
|
494
|
+
<LayoutGridField<T, S, F>
|
|
495
|
+
{...layoutGridFieldProps}
|
|
496
|
+
key={`layoutGrid-${hashObject(layoutGridSchema)}`}
|
|
497
|
+
schema={schema}
|
|
498
|
+
layoutGridSchema={layoutGridSchema}
|
|
499
|
+
/>
|
|
500
|
+
));
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Describes the props for LayoutGridCondition, LayoutGridCol, LayoutGridColumns, and LayoutGridRow. This is typically
|
|
505
|
+
* the original props passed through from the nearest ancestor LayoutGridField, plus the layoutGridSchema for the
|
|
506
|
+
* current node.
|
|
507
|
+
*/
|
|
508
|
+
type LayoutFieldProps<
|
|
509
|
+
T = any,
|
|
510
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
511
|
+
F extends FormContextType = any,
|
|
512
|
+
> = LayoutGridFieldProps<T, S, F> & {
|
|
513
|
+
/** The string or object that represents the configuration for the grid field */
|
|
514
|
+
layoutGridSchema: GridSchemaType;
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
/** Renders the `children` of the `GridType.CONDITION` if it passes. The `layoutGridSchema` for the
|
|
518
|
+
* `GridType.CONDITION` is separated into the `children` and other `gridProps`. The `gridProps` are used to extract
|
|
519
|
+
* the `operator`, `field` and `value` of the condition. If the condition matches, then all of the `children` are
|
|
520
|
+
* rendered, otherwise null is returned.
|
|
521
|
+
*
|
|
522
|
+
* @returns - The rendered the children for the `GridType.CONDITION` or null
|
|
523
|
+
*/
|
|
524
|
+
function LayoutGridCondition<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
525
|
+
props: LayoutFieldProps<T, S, F>,
|
|
526
|
+
) {
|
|
527
|
+
const { layoutGridSchema, ...layoutGridFieldProps } = props;
|
|
528
|
+
const { formData, registry } = layoutGridFieldProps;
|
|
529
|
+
const { children, gridProps } = findChildrenAndProps<T, S, F>(layoutGridSchema, GridType.CONDITION, registry);
|
|
530
|
+
const { operator, field = '', value } = gridProps;
|
|
531
|
+
const fieldData = get(formData, field, null);
|
|
532
|
+
if (conditionMatches(operator, fieldData, value)) {
|
|
533
|
+
return <LayoutGridFieldChildren {...layoutGridFieldProps} childrenLayoutGridSchemaId={children} />;
|
|
534
|
+
}
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** Renders a `GridTemplate` as an item. The `layoutGridSchema` for the `GridType.COLUMN` is separated into the
|
|
539
|
+
* `children` and other `gridProps`. The `gridProps` will be spread onto the outer `GridTemplate`. Inside the
|
|
540
|
+
* `GridTemplate` all the `children` are rendered.
|
|
541
|
+
*
|
|
542
|
+
* @returns - The rendered `GridTemplate` containing the children for the `GridType.COLUMN`
|
|
543
|
+
*/
|
|
544
|
+
function LayoutGridCol<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
545
|
+
props: LayoutFieldProps<T, S, F>,
|
|
546
|
+
) {
|
|
547
|
+
const { layoutGridSchema, ...layoutGridFieldProps } = props;
|
|
548
|
+
const { registry, uiSchema } = layoutGridFieldProps;
|
|
549
|
+
const { children, gridProps } = findChildrenAndProps<T, S, F>(layoutGridSchema, GridType.COLUMN, registry);
|
|
550
|
+
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
551
|
+
const GridTemplate = getTemplate<'GridTemplate', T, S, F>('GridTemplate', registry, uiOptions);
|
|
552
|
+
|
|
553
|
+
return (
|
|
554
|
+
<GridTemplate column data-testid={LAYOUT_GRID_FIELD_TEST_IDS.col} {...gridProps}>
|
|
555
|
+
<LayoutGridFieldChildren {...layoutGridFieldProps} childrenLayoutGridSchemaId={children} />
|
|
556
|
+
</GridTemplate>
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/** Renders a `GridTemplate` as an item. The `layoutGridSchema` for the `GridType.COLUMNS` is separated into the
|
|
561
|
+
* `children` and other `gridProps`. The `children` is iterated on and `gridProps` will be spread onto the outer
|
|
562
|
+
* `GridTemplate`. Each child will have their own rendered `GridTemplate`.
|
|
563
|
+
*
|
|
564
|
+
* @returns - The rendered `GridTemplate` containing the children for the `GridType.COLUMNS`
|
|
565
|
+
*/
|
|
566
|
+
function LayoutGridColumns<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
567
|
+
props: LayoutFieldProps<T, S, F>,
|
|
568
|
+
) {
|
|
569
|
+
const { layoutGridSchema, ...layoutGridFieldProps } = props;
|
|
570
|
+
|
|
571
|
+
const { registry, uiSchema } = layoutGridFieldProps;
|
|
572
|
+
const { children, gridProps } = findChildrenAndProps<T, S, F>(layoutGridSchema, GridType.COLUMNS, registry);
|
|
573
|
+
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
574
|
+
const GridTemplate = getTemplate<'GridTemplate', T, S, F>('GridTemplate', registry, uiOptions);
|
|
575
|
+
|
|
576
|
+
return children.map((child) => (
|
|
577
|
+
<GridTemplate
|
|
578
|
+
column
|
|
579
|
+
key={`column-${hashObject(child)}`}
|
|
580
|
+
data-testid={LAYOUT_GRID_FIELD_TEST_IDS.col}
|
|
581
|
+
{...gridProps}
|
|
582
|
+
>
|
|
583
|
+
<LayoutGridFieldChildren {...layoutGridFieldProps} childrenLayoutGridSchemaId={[child]} />
|
|
584
|
+
</GridTemplate>
|
|
585
|
+
));
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** Renders a `GridTemplate` as a container. The `layoutGridSchema` for the `GridType.ROW` is separated into the
|
|
589
|
+
* `children` and other `gridProps`. The `gridProps` will be spread onto the outer `GridTemplate`. Inside of the
|
|
590
|
+
* `GridTemplate` all of the `children` are rendered.
|
|
591
|
+
*
|
|
592
|
+
* @returns - The rendered `GridTemplate` containing the children for the `GridType.ROW`
|
|
593
|
+
*/
|
|
594
|
+
function LayoutGridRow<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
595
|
+
props: LayoutFieldProps<T, S, F>,
|
|
596
|
+
) {
|
|
597
|
+
const { layoutGridSchema, ...layoutGridFieldProps } = props;
|
|
598
|
+
|
|
599
|
+
const { registry, uiSchema } = layoutGridFieldProps;
|
|
600
|
+
const { children, gridProps } = findChildrenAndProps<T, S, F>(layoutGridSchema, GridType.ROW, registry);
|
|
601
|
+
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
602
|
+
const GridTemplate = getTemplate<'GridTemplate', T, S, F>('GridTemplate', registry, uiOptions);
|
|
603
|
+
|
|
604
|
+
return (
|
|
605
|
+
<GridTemplate {...gridProps} data-testid={LAYOUT_GRID_FIELD_TEST_IDS.row}>
|
|
606
|
+
<LayoutGridFieldChildren {...layoutGridFieldProps} childrenLayoutGridSchemaId={children} />
|
|
607
|
+
</GridTemplate>
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* The props for the LayoutGridFieldComponent.
|
|
613
|
+
*/
|
|
614
|
+
type LayoutGridFieldComponentProps<
|
|
615
|
+
T = any,
|
|
616
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
617
|
+
F extends FormContextType = any,
|
|
618
|
+
> = LayoutGridFieldProps<T, S, F> & {
|
|
619
|
+
/** The string or object that represents the configuration for the grid field */
|
|
620
|
+
gridSchema?: ConfigObject | string;
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
/** Renders the field described by `gridSchema`. If `gridSchema` is not an object, then is will be assumed
|
|
624
|
+
* to be the dotted-path to the field in the schema. Otherwise, we extract the `name`, and optional `render` and all
|
|
625
|
+
* other props. If `name` does not exist and there is an optional `render`, we return the `render` component with only
|
|
626
|
+
* specified props for that component. If `name` exists, we take the name, the initial & root schemas and the formData
|
|
627
|
+
* and get the destination schema, is required state and optional oneOf/anyOf options for it. If the destination
|
|
628
|
+
* schema was located along with oneOf/anyOf options then a `LayoutMultiSchemaField` will be rendered with the
|
|
629
|
+
* `uiSchema`, `errorSchema`, `fieldPathId` and `formData` drilled down to the dotted-path field, spreading any other
|
|
630
|
+
* props from `gridSchema` into the `ui:options`. If the destination schema located without any oneOf/anyOf options,
|
|
631
|
+
* then a `SchemaField` will be rendered with the same props as mentioned in the previous sentence. If no destination
|
|
632
|
+
* schema was located, but a custom render component was found, then it will be rendered with many of the non-event
|
|
633
|
+
* handling props. If none of the previous render paths are valid, then a null is returned.
|
|
634
|
+
*
|
|
635
|
+
* @returns - One of `LayoutMultiSchemaField`, `SchemaField`, a custom render component or null, depending
|
|
636
|
+
*/
|
|
637
|
+
function LayoutGridFieldComponent<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
638
|
+
props: LayoutGridFieldComponentProps<T, S, F>,
|
|
639
|
+
) {
|
|
640
|
+
const {
|
|
641
|
+
gridSchema,
|
|
642
|
+
schema: initialSchema,
|
|
643
|
+
uiSchema,
|
|
644
|
+
errorSchema,
|
|
645
|
+
fieldPathId,
|
|
646
|
+
onBlur,
|
|
647
|
+
onFocus,
|
|
648
|
+
formData,
|
|
649
|
+
readonly,
|
|
650
|
+
registry,
|
|
651
|
+
layoutGridSchema, // Used to pull this out of otherProps since we don't want to pass it through
|
|
652
|
+
...otherProps
|
|
653
|
+
} = props;
|
|
654
|
+
const { onChange } = otherProps;
|
|
655
|
+
const { fields } = registry;
|
|
656
|
+
const { SchemaField, LayoutMultiSchemaField } = fields;
|
|
657
|
+
|
|
658
|
+
const uiComponentProps = computeUIComponentPropsFromGridSchema(registry, gridSchema);
|
|
659
|
+
const { name, UIComponent, uiProps } = uiComponentProps;
|
|
660
|
+
const {
|
|
661
|
+
schema,
|
|
662
|
+
isRequired,
|
|
663
|
+
isReadonly,
|
|
664
|
+
optionsInfo,
|
|
665
|
+
fieldPathId: fieldIdSchema,
|
|
666
|
+
} = getSchemaDetailsForField<T, S, F>(registry, name, initialSchema, formData, fieldPathId);
|
|
667
|
+
const memoFieldPathId = useDeepCompareMemo<FieldPathId>(fieldIdSchema);
|
|
668
|
+
|
|
669
|
+
if (uiComponentProps.rendered) {
|
|
670
|
+
return uiComponentProps.rendered;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
if (schema) {
|
|
674
|
+
const Field = optionsInfo?.hasDiscriminator ? LayoutMultiSchemaField : SchemaField;
|
|
675
|
+
// Call this function to get the appropriate UISchema, which will always have its `readonly` state matching the
|
|
676
|
+
// `uiReadonly` flag that it returns. This is done since the `SchemaField` will always defer to the `readonly`
|
|
677
|
+
// state in the uiSchema over anything in the props or schema. Because we are implementing the "readonly" state of
|
|
678
|
+
// the `Form` via the prop passed to `LayoutGridField` we need to make sure the uiSchema always has a true value
|
|
679
|
+
// when it is needed
|
|
680
|
+
const { fieldUiSchema, uiReadonly } = computeFieldUiSchema<T, S, F>(name, uiProps, uiSchema, isReadonly, readonly);
|
|
681
|
+
|
|
682
|
+
return (
|
|
683
|
+
<Field
|
|
684
|
+
data-testid={
|
|
685
|
+
optionsInfo?.hasDiscriminator
|
|
686
|
+
? LAYOUT_GRID_FIELD_TEST_IDS.layoutMultiSchemaField
|
|
687
|
+
: LAYOUT_GRID_FIELD_TEST_IDS.field
|
|
688
|
+
}
|
|
689
|
+
{...otherProps}
|
|
690
|
+
name={name}
|
|
691
|
+
required={isRequired}
|
|
692
|
+
readonly={uiReadonly}
|
|
693
|
+
schema={schema}
|
|
694
|
+
uiSchema={fieldUiSchema}
|
|
695
|
+
errorSchema={get(errorSchema, name)}
|
|
696
|
+
fieldPathId={memoFieldPathId}
|
|
697
|
+
formData={get(formData, name)}
|
|
698
|
+
onChange={onChange}
|
|
699
|
+
onBlur={onBlur}
|
|
700
|
+
onFocus={onFocus}
|
|
701
|
+
options={optionsInfo?.options}
|
|
702
|
+
registry={registry}
|
|
703
|
+
/>
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (UIComponent) {
|
|
708
|
+
return (
|
|
709
|
+
<UIComponent
|
|
710
|
+
data-testid={LAYOUT_GRID_FIELD_TEST_IDS.uiComponent}
|
|
711
|
+
{...otherProps}
|
|
712
|
+
name={name}
|
|
713
|
+
required={isRequired}
|
|
714
|
+
formData={formData}
|
|
715
|
+
readOnly={!!isReadonly || readonly}
|
|
716
|
+
errorSchema={errorSchema}
|
|
717
|
+
uiSchema={uiSchema}
|
|
718
|
+
schema={initialSchema}
|
|
719
|
+
fieldPathId={fieldPathId}
|
|
720
|
+
onBlur={onBlur}
|
|
721
|
+
onFocus={onFocus}
|
|
722
|
+
registry={registry}
|
|
723
|
+
{...uiProps}
|
|
724
|
+
/>
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
return null;
|
|
728
|
+
}
|
|
729
|
+
|
|
133
730
|
/** The `LayoutGridField` will render a schema, uiSchema and formData combination out into a GridTemplate in the shape
|
|
134
731
|
* described in the uiSchema. To define the grid to use to render the elements within a field in the schema, provide in
|
|
135
732
|
* the uiSchema for that field the object contained under a `ui:layoutGrid` element. E.g. (as a JSON object):
|
|
@@ -353,612 +950,38 @@ function getNonNullishValue<T = unknown>(value?: T, fallback?: T): T | undefined
|
|
|
353
950
|
* ]
|
|
354
951
|
* ```
|
|
355
952
|
*/
|
|
356
|
-
export default
|
|
953
|
+
export default function LayoutGridField<
|
|
357
954
|
T = any,
|
|
358
955
|
S extends StrictRJSFSchema = RJSFSchema,
|
|
359
956
|
F extends FormContextType = any,
|
|
360
|
-
>
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
static TEST_IDS = getTestIds();
|
|
366
|
-
/** Computes the uiSchema for the field with `name` from the `uiProps` and `uiSchema` provided. The field UI Schema
|
|
367
|
-
* will always contain a copy of the global options from the `uiSchema` (so they can be passed down) as well as
|
|
368
|
-
* copying them into the local ui options. When the `forceReadonly` flag is true, then the field UI Schema is
|
|
369
|
-
* updated to make "readonly" be true. When the `schemaReadonly` flag is true AND the field UI Schema does NOT have
|
|
370
|
-
* the flag already provided, then we also make "readonly" true. We always make sure to return the final value of the
|
|
371
|
-
* field UI Schema's "readonly" flag as `uiReadonly` along with the `fieldUiSchema` in the return value.
|
|
372
|
-
*
|
|
373
|
-
* @param field - The name of the field to pull the existing UI Schema for
|
|
374
|
-
* @param uiProps - Any props that should be put into the field's uiSchema
|
|
375
|
-
* @param [uiSchema] - The optional UI Schema from which to get the UI schema for the field
|
|
376
|
-
* @param [schemaReadonly] - Optional flag indicating whether the schema indicates the field is readonly
|
|
377
|
-
* @param [forceReadonly] - Optional flag indicating whether the Form itself is in readonly mode
|
|
378
|
-
*/
|
|
379
|
-
static computeFieldUiSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
380
|
-
field: string,
|
|
381
|
-
uiProps: ConfigObject,
|
|
382
|
-
uiSchema?: UiSchema<T, S, F>,
|
|
383
|
-
schemaReadonly?: boolean,
|
|
384
|
-
forceReadonly?: boolean,
|
|
385
|
-
) {
|
|
386
|
-
const globalUiOptions = get(uiSchema, [UI_GLOBAL_OPTIONS_KEY], {});
|
|
387
|
-
const localUiSchema = get(uiSchema, field);
|
|
388
|
-
const localUiOptions = { ...get(localUiSchema, [UI_OPTIONS_KEY], {}), ...uiProps, ...globalUiOptions };
|
|
389
|
-
const fieldUiSchema = { ...localUiSchema };
|
|
390
|
-
if (!isEmpty(localUiOptions)) {
|
|
391
|
-
set(fieldUiSchema, [UI_OPTIONS_KEY], localUiOptions);
|
|
392
|
-
}
|
|
393
|
-
if (!isEmpty(globalUiOptions)) {
|
|
394
|
-
// pass the global uiOptions down to the field uiSchema so that they can be applied to all nested fields
|
|
395
|
-
set(fieldUiSchema, [UI_GLOBAL_OPTIONS_KEY], globalUiOptions);
|
|
396
|
-
}
|
|
397
|
-
let { readonly: uiReadonly } = getUiOptions<T, S, F>(fieldUiSchema);
|
|
398
|
-
if (forceReadonly === true || (isUndefined(uiReadonly) && schemaReadonly === true)) {
|
|
399
|
-
// If we are forcing all widgets to be readonly, OR the schema indicates it is readonly AND the uiSchema does not
|
|
400
|
-
// have an overriding value, then update the uiSchema to set readonly to true. Doing this will
|
|
401
|
-
uiReadonly = true;
|
|
402
|
-
if (has(localUiOptions, READONLY_KEY)) {
|
|
403
|
-
// If the local options has the key value provided in it, then set that one to true
|
|
404
|
-
set(fieldUiSchema, [UI_OPTIONS_KEY, READONLY_KEY], true);
|
|
405
|
-
} else {
|
|
406
|
-
// otherwise set the `ui:` version
|
|
407
|
-
set(fieldUiSchema, `ui:${READONLY_KEY}`, true);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return { fieldUiSchema, uiReadonly };
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/** Given an `operator`, `datum` and `value` determines whether this condition is considered matching. Matching
|
|
414
|
-
* depends on the `operator`. The `datum` and `value` are converted into arrays if they aren't already and then the
|
|
415
|
-
* contents of the two arrays are compared using the `operator`. When `operator` is All, then the two arrays must be
|
|
416
|
-
* equal to match. When `operator` is SOME then the intersection of the two arrays must have at least one value in
|
|
417
|
-
* common to match. When `operator` is NONE then the intersection of the two arrays must not have any values in common
|
|
418
|
-
* to match.
|
|
419
|
-
*
|
|
420
|
-
* @param [operator] - The optional operator for the condition
|
|
421
|
-
* @param [datum] - The optional datum for the condition, this can be an item or a list of items of type unknown
|
|
422
|
-
* @param [value='$0m3tH1nG Un3xP3cT3d'] The optional value for the condition, defaulting to a highly unlikely value
|
|
423
|
-
* to avoid comparing two undefined elements when `value` was forgotten in the condition definition.
|
|
424
|
-
* This can be an item or a list of items of type unknown
|
|
425
|
-
* @returns - True if the condition matches, false otherwise
|
|
426
|
-
*/
|
|
427
|
-
static conditionMatches(operator?: Operators, datum?: unknown, value: unknown = '$0m3tH1nG Un3xP3cT3d'): boolean {
|
|
428
|
-
const data = flatten([datum]).sort();
|
|
429
|
-
const values = flatten([value]).sort();
|
|
430
|
-
switch (operator) {
|
|
431
|
-
case Operators.ALL:
|
|
432
|
-
return isEqual(data, values);
|
|
433
|
-
case Operators.SOME:
|
|
434
|
-
return intersection(data, values).length > 0;
|
|
435
|
-
case Operators.NONE:
|
|
436
|
-
return intersection(data, values).length === 0;
|
|
437
|
-
default:
|
|
438
|
-
return false;
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/** From within the `layoutGridSchema` finds the `children` and any extra `gridProps` from the object keyed by
|
|
443
|
-
* `schemaKey`. If the `children` contains extra `gridProps` and those props contain a `className` string, try to
|
|
444
|
-
* lookup whether that `className` has a replacement value in the `registry` using the `FORM_CONTEXT_LOOKUP_BASE`.
|
|
445
|
-
* When the `className` value contains multiple classNames separated by a space, the lookup will look for a
|
|
446
|
-
* replacement value for each `className` and combine them into one.
|
|
447
|
-
*
|
|
448
|
-
* @param layoutGridSchema - The GridSchemaType instance from which to obtain the `schemaKey` children and extra props
|
|
449
|
-
* @param schemaKey - A `GridType` value, used to get the children and extra props from within the `layoutGridSchema`
|
|
450
|
-
* @param registry - The `@rjsf` Registry from which to look up `classNames` if they are present in the extra props
|
|
451
|
-
* @returns - An object containing the list of `LayoutGridSchemaType` `children` and any extra `gridProps`
|
|
452
|
-
* @throws - A `TypeError` when the `children` is not an array
|
|
453
|
-
*/
|
|
454
|
-
static findChildrenAndProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
455
|
-
layoutGridSchema: GridSchemaType,
|
|
456
|
-
schemaKey: GridType,
|
|
457
|
-
registry: Registry<T, S, F>,
|
|
458
|
-
) {
|
|
459
|
-
let gridProps: GridProps = {};
|
|
460
|
-
let children = layoutGridSchema[schemaKey];
|
|
461
|
-
if (isPlainObject(children)) {
|
|
462
|
-
const { children: elements, className: toMapClassNames, ...otherProps } = children as ConfigObject;
|
|
463
|
-
children = elements;
|
|
464
|
-
if (toMapClassNames) {
|
|
465
|
-
const classes = toMapClassNames.split(' ');
|
|
466
|
-
const className = classes.map((ele: string) => lookupFromFormContext<T, S, F>(registry, ele, ele)).join(' ');
|
|
467
|
-
gridProps = { ...otherProps, className };
|
|
468
|
-
} else {
|
|
469
|
-
gridProps = otherProps;
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
if (!Array.isArray(children)) {
|
|
473
|
-
throw new TypeError(`Expected array for "${schemaKey}" in ${JSON.stringify(layoutGridSchema)}`);
|
|
474
|
-
}
|
|
475
|
-
return { children: children as LayoutGridSchemaType[], gridProps };
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
/** Generates an idSchema for the `schema` using `@rjsf`'s `toIdSchema` util, passing the `baseIdSchema`'s `$id` value
|
|
479
|
-
* as the id prefix.
|
|
480
|
-
*
|
|
481
|
-
* @param schemaUtils - The `SchemaUtilsType` used to call `toIdSchema`
|
|
482
|
-
* @param schema - The schema to generate the idSchema for
|
|
483
|
-
* @param baseIdSchema - The IdSchema for the base
|
|
484
|
-
* @param formData - The formData to pass the `toIdSchema`
|
|
485
|
-
* @param [idSeparator] - The param to pass into the `toIdSchema` util which will use it to join the `idSchema` paths
|
|
486
|
-
* @returns - The generated `idSchema` for the `schema`
|
|
487
|
-
*/
|
|
488
|
-
static getIdSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
489
|
-
schemaUtils: SchemaUtilsType<T, S, F>,
|
|
490
|
-
baseIdSchema: IdSchema<T>,
|
|
491
|
-
formData: FieldProps['formData'],
|
|
492
|
-
schema: S = {} as S,
|
|
493
|
-
idSeparator?: string,
|
|
494
|
-
): FieldProps<T, S, F>['idSchema'] {
|
|
495
|
-
const baseId = get(baseIdSchema, ID_KEY);
|
|
496
|
-
return schemaUtils.toIdSchema(schema, baseId, formData, baseId, idSeparator);
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
/** Given a `dottedPath` to a field in the `initialSchema`, iterate through each individual path in the schema until
|
|
500
|
-
* the leaf path is found and returned (along with whether that leaf path `isRequired`) OR no schema exists for an
|
|
501
|
-
* element in the path. If the leaf schema element happens to be a oneOf/anyOf then also return the oneOf/anyOf as
|
|
502
|
-
* `options`.
|
|
503
|
-
*
|
|
504
|
-
* @param schemaUtils - The `SchemaUtilsType` used to call `retrieveSchema`
|
|
505
|
-
* @param dottedPath - The dotted-path to the field for which to get the schema
|
|
506
|
-
* @param initialSchema - The initial schema to start the search from
|
|
507
|
-
* @param formData - The formData, useful for resolving a oneOf/anyOf selection in the path hierarchy
|
|
508
|
-
* @param initialIdSchema - The initial idSchema to start the search from
|
|
509
|
-
* @param [idSeparator] - The param to pass into the `toIdSchema` util which will use it to join the `idSchema` paths
|
|
510
|
-
* @returns - An object containing the destination schema, isRequired and isReadonly flags for the field and options
|
|
511
|
-
* info if a oneOf/anyOf
|
|
512
|
-
*/
|
|
513
|
-
static getSchemaDetailsForField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
514
|
-
schemaUtils: SchemaUtilsType<T, S, F>,
|
|
515
|
-
dottedPath: string,
|
|
516
|
-
initialSchema: S,
|
|
517
|
-
formData: FieldProps<T, S, F>['formData'],
|
|
518
|
-
initialIdSchema: IdSchema<T>,
|
|
519
|
-
idSeparator?: string,
|
|
520
|
-
): {
|
|
521
|
-
schema?: S;
|
|
522
|
-
isRequired: boolean;
|
|
523
|
-
isReadonly?: boolean;
|
|
524
|
-
optionsInfo?: OneOfOptionsInfoType<S>;
|
|
525
|
-
idSchema: IdSchema<T>;
|
|
526
|
-
} {
|
|
527
|
-
let rawSchema: S = initialSchema;
|
|
528
|
-
let idSchema = initialIdSchema;
|
|
529
|
-
const parts: string[] = dottedPath.split('.');
|
|
530
|
-
const leafPath: string | undefined = parts.pop(); // pop off the last element in the list as the leaf
|
|
531
|
-
let schema: S | undefined = schemaUtils.retrieveSchema(rawSchema, formData); // always returns an object
|
|
532
|
-
let innerData = formData;
|
|
533
|
-
let isReadonly: boolean | undefined = schema.readOnly;
|
|
534
|
-
|
|
535
|
-
// For all the remaining path parts
|
|
536
|
-
parts.forEach((part) => {
|
|
537
|
-
// dive into the properties of the current schema (when it exists) and get the schema for the next part
|
|
538
|
-
if (has(schema, PROPERTIES_KEY)) {
|
|
539
|
-
rawSchema = get(schema, [PROPERTIES_KEY, part], {}) as S;
|
|
540
|
-
idSchema = get(idSchema, part, {}) as IdSchema<T>;
|
|
541
|
-
} else if (schema && (has(schema, ONE_OF_KEY) || has(schema, ANY_OF_KEY))) {
|
|
542
|
-
const xxx = has(schema, ONE_OF_KEY) ? ONE_OF_KEY : ANY_OF_KEY;
|
|
543
|
-
// When the schema represents a oneOf/anyOf, find the selected schema for it and grab the inner part
|
|
544
|
-
const selectedSchema = schemaUtils.findSelectedOptionInXxxOf(schema, part, xxx, innerData);
|
|
545
|
-
const selectedIdSchema = LayoutGridField.getIdSchema<T, S, F>(
|
|
546
|
-
schemaUtils,
|
|
547
|
-
idSchema,
|
|
548
|
-
formData,
|
|
549
|
-
selectedSchema,
|
|
550
|
-
idSeparator,
|
|
551
|
-
);
|
|
552
|
-
rawSchema = get(selectedSchema, [PROPERTIES_KEY, part], {}) as S;
|
|
553
|
-
idSchema = get(selectedIdSchema, part, {}) as IdSchema<T>;
|
|
554
|
-
} else {
|
|
555
|
-
rawSchema = {} as S;
|
|
556
|
-
}
|
|
557
|
-
// Now drill into the innerData for the part, returning an empty object by default if it doesn't exist
|
|
558
|
-
innerData = get(innerData, part, {}) as T;
|
|
559
|
-
// Resolve any `$ref`s for the current rawSchema
|
|
560
|
-
schema = schemaUtils.retrieveSchema(rawSchema, innerData);
|
|
561
|
-
isReadonly = getNonNullishValue(schema.readOnly, isReadonly);
|
|
562
|
-
});
|
|
563
|
-
|
|
564
|
-
let optionsInfo: OneOfOptionsInfoType<S> | undefined;
|
|
565
|
-
let isRequired = false;
|
|
566
|
-
// retrieveSchema will return an empty schema in the worst case scenario, convert it to undefined
|
|
567
|
-
if (isEmpty(schema)) {
|
|
568
|
-
schema = undefined;
|
|
569
|
-
}
|
|
570
|
-
if (schema && leafPath) {
|
|
571
|
-
// When we have both a schema and a leafPath...
|
|
572
|
-
if (schema && (has(schema, ONE_OF_KEY) || has(schema, ANY_OF_KEY))) {
|
|
573
|
-
const xxx = has(schema, ONE_OF_KEY) ? ONE_OF_KEY : ANY_OF_KEY;
|
|
574
|
-
// Grab the selected schema for the oneOf/anyOf value for the leafPath using the innerData
|
|
575
|
-
schema = schemaUtils.findSelectedOptionInXxxOf(schema, leafPath, xxx, innerData);
|
|
576
|
-
// Generate the idSchema for the oneOf/anyOf value then merge with the existing `idSchema`
|
|
577
|
-
const rawIdSchema = LayoutGridField.getIdSchema<T, S, F>(schemaUtils, idSchema, formData, schema, idSeparator);
|
|
578
|
-
idSchema = mergeObjects(rawIdSchema, idSchema) as IdSchema<T>;
|
|
579
|
-
}
|
|
580
|
-
isRequired = schema !== undefined && Array.isArray(schema.required) && includes(schema.required, leafPath);
|
|
581
|
-
// Now grab the schema from the leafPath of the current schema properties
|
|
582
|
-
schema = get(schema, [PROPERTIES_KEY, leafPath]) as S | undefined;
|
|
583
|
-
// Resolve any `$ref`s for the current schema
|
|
584
|
-
schema = schema ? schemaUtils.retrieveSchema(schema) : schema;
|
|
585
|
-
idSchema = get(idSchema, leafPath, {}) as IdSchema<T>;
|
|
586
|
-
isReadonly = getNonNullishValue(schema?.readOnly, isReadonly);
|
|
587
|
-
if (schema && (has(schema, ONE_OF_KEY) || has(schema, ANY_OF_KEY))) {
|
|
588
|
-
const xxx = has(schema, ONE_OF_KEY) ? ONE_OF_KEY : ANY_OF_KEY;
|
|
589
|
-
// Set the options if we have a schema with a oneOf/anyOf
|
|
590
|
-
const discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
591
|
-
optionsInfo = { options: schema[xxx] as S[], hasDiscriminator: !!discriminator };
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
return { schema, isRequired, isReadonly, optionsInfo, idSchema };
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
/** Gets the custom render component from the `render`, by either determining that it is either already a function or
|
|
599
|
-
* it is a non-function value that can be used to look up the function in the registry. If no function can be found,
|
|
600
|
-
* null is returned.
|
|
601
|
-
*
|
|
602
|
-
* @param render - The potential render function or lookup name to one
|
|
603
|
-
* @param registry - The `@rjsf` Registry from which to look up `classNames` if they are present in the extra props
|
|
604
|
-
* @returns - Either a render function if available, or null if not
|
|
605
|
-
*/
|
|
606
|
-
static getCustomRenderComponent<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
607
|
-
render: string | RenderComponent,
|
|
608
|
-
registry: Registry<T, S, F>,
|
|
609
|
-
): RenderComponent | null {
|
|
610
|
-
let customRenderer = render;
|
|
611
|
-
if (isString(customRenderer)) {
|
|
612
|
-
customRenderer = lookupFromFormContext<T, S, F>(registry, customRenderer);
|
|
613
|
-
}
|
|
614
|
-
if (isFunction(customRenderer)) {
|
|
615
|
-
return customRenderer;
|
|
616
|
-
}
|
|
617
|
-
return null;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
/** Extract the `name`, and optional `render` and all other props from the `gridSchema`. We look up the `render` to
|
|
621
|
-
* see if can be resolved to a UIComponent. If `name` does not exist and there is an optional `render` UIComponent, we
|
|
622
|
-
* set the `rendered` component with only specified props for that component in the object.
|
|
623
|
-
*
|
|
624
|
-
* @param registry - The `@rjsf` Registry from which to look up `classNames` if they are present in the extra props
|
|
625
|
-
* @param gridSchema - The string or object that represents the configuration for the grid field
|
|
626
|
-
* @returns - The UIComponentPropsType computed from the gridSchema
|
|
627
|
-
*/
|
|
628
|
-
static computeUIComponentPropsFromGridSchema<
|
|
629
|
-
T = any,
|
|
630
|
-
S extends StrictRJSFSchema = RJSFSchema,
|
|
631
|
-
F extends FormContextType = any,
|
|
632
|
-
>(registry: Registry<T, S, F>, gridSchema?: string | ConfigObject): UIComponentPropsType {
|
|
633
|
-
let name: string;
|
|
634
|
-
let UIComponent: RenderComponent | null = null;
|
|
635
|
-
let uiProps: ConfigObject = {};
|
|
636
|
-
let rendered: ReactNode | undefined;
|
|
637
|
-
if (isString(gridSchema) || isUndefined(gridSchema)) {
|
|
638
|
-
name = gridSchema ?? '';
|
|
639
|
-
} else {
|
|
640
|
-
const { name: innerName = '', render, ...innerProps } = gridSchema;
|
|
641
|
-
name = innerName;
|
|
642
|
-
uiProps = innerProps;
|
|
643
|
-
if (!isEmpty(uiProps)) {
|
|
644
|
-
// Transform any `$lookup=` in the uiProps props with the appropriate value
|
|
645
|
-
each(uiProps, (prop: ConfigObject, key: string) => {
|
|
646
|
-
if (isString(prop)) {
|
|
647
|
-
const match: string[] | null = LOOKUP_REGEX.exec(prop);
|
|
648
|
-
if (Array.isArray(match) && match.length > 1) {
|
|
649
|
-
const name = match[1];
|
|
650
|
-
uiProps[key] = lookupFromFormContext(registry, name, name);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
});
|
|
654
|
-
}
|
|
655
|
-
UIComponent = LayoutGridField.getCustomRenderComponent<T, S, F>(render, registry);
|
|
656
|
-
if (!innerName && UIComponent) {
|
|
657
|
-
rendered = <UIComponent {...innerProps} data-testid={LayoutGridField.TEST_IDS.uiComponent} />;
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
return { name, UIComponent, uiProps, rendered };
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
/** Constructs an `LayoutGridField` with the given `props`
|
|
664
|
-
*
|
|
665
|
-
* @param props - The `LayoutGridField` for this template
|
|
666
|
-
*/
|
|
667
|
-
constructor(props: LayoutGridFieldProps<T, S, F>) {
|
|
668
|
-
super(props);
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
/** Generates an `onChange` handler for the field associated with the `dottedPath`. This handler will clone and update
|
|
672
|
-
* the `formData` with the new `value` and the `errorSchema` if an `errSchema` is provided. After updating those two
|
|
673
|
-
* elements, they will then be passed on to the `onChange` handler of the `LayoutFieldGrid`.
|
|
674
|
-
*
|
|
675
|
-
* @param dottedPath - The dotted-path to the field for which to generate the onChange handler
|
|
676
|
-
* @returns - The `onChange` handling function for the `dottedPath` field
|
|
677
|
-
*/
|
|
678
|
-
onFieldChange = (dottedPath: string) => {
|
|
679
|
-
return (value: unknown, errSchema?: ErrorSchema<T>, id?: string) => {
|
|
680
|
-
const { onChange, errorSchema, formData } = this.props;
|
|
681
|
-
const newFormData = cloneDeep(formData || ({} as T));
|
|
682
|
-
let newErrorSchema = errorSchema;
|
|
683
|
-
if (errSchema && errorSchema) {
|
|
684
|
-
newErrorSchema = cloneDeep(errorSchema);
|
|
685
|
-
set(newErrorSchema, dottedPath, errSchema);
|
|
686
|
-
}
|
|
687
|
-
set(newFormData as object, dottedPath, value);
|
|
688
|
-
onChange(newFormData, newErrorSchema, id);
|
|
689
|
-
};
|
|
690
|
-
};
|
|
691
|
-
|
|
692
|
-
/** Renders the `children` of the `GridType.CONDITION` if it passes. The `layoutGridSchema` for the
|
|
693
|
-
* `GridType.CONDITION` is separated into the `children` and other `gridProps`. The `gridProps` are used to extract
|
|
694
|
-
* the `operator`, `field` and `value` of the condition. If the condition matches, then all of the `children` are
|
|
695
|
-
* rendered, otherwise null is returned.
|
|
696
|
-
*
|
|
697
|
-
* @param layoutGridSchema - The string or object that represents the configuration for the grid field
|
|
698
|
-
* @returns - The rendered the children for the `GridType.CONDITION` or null
|
|
699
|
-
*/
|
|
700
|
-
renderCondition(layoutGridSchema: GridSchemaType) {
|
|
701
|
-
const { formData, registry } = this.props;
|
|
702
|
-
const { children, gridProps } = LayoutGridField.findChildrenAndProps<T, S, F>(
|
|
703
|
-
layoutGridSchema,
|
|
704
|
-
GridType.CONDITION,
|
|
705
|
-
registry,
|
|
706
|
-
);
|
|
707
|
-
const { operator, field = '', value } = gridProps;
|
|
708
|
-
const fieldData = get(formData, field, null);
|
|
709
|
-
if (LayoutGridField.conditionMatches(operator, fieldData, value)) {
|
|
710
|
-
return this.renderChildren(children);
|
|
711
|
-
}
|
|
712
|
-
return null;
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
/** Renders a material-ui `GridTemplate` as an item. The `layoutGridSchema` for the `GridType.COLUMN` is separated
|
|
716
|
-
* into the `children` and other `gridProps`. The `gridProps` will be spread onto the outer `GridTemplate`. Inside
|
|
717
|
-
* the `GridTemplate` all the `children` are rendered.
|
|
718
|
-
*
|
|
719
|
-
* @param layoutGridSchema - The string or object that represents the configuration for the grid field
|
|
720
|
-
* @returns - The rendered `GridTemplate` containing the children for the `GridType.COLUMN`
|
|
721
|
-
*/
|
|
722
|
-
renderCol(layoutGridSchema: GridSchemaType) {
|
|
723
|
-
const { registry, uiSchema } = this.props;
|
|
724
|
-
const { children, gridProps } = LayoutGridField.findChildrenAndProps<T, S, F>(
|
|
725
|
-
layoutGridSchema,
|
|
726
|
-
GridType.COLUMN,
|
|
727
|
-
registry,
|
|
728
|
-
);
|
|
729
|
-
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
730
|
-
const GridTemplate = getTemplate<'GridTemplate', T, S, F>('GridTemplate', registry, uiOptions);
|
|
731
|
-
|
|
732
|
-
return (
|
|
733
|
-
<GridTemplate column data-testid={LayoutGridField.TEST_IDS.col} {...gridProps}>
|
|
734
|
-
{this.renderChildren(children)}
|
|
735
|
-
</GridTemplate>
|
|
736
|
-
);
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
/** Renders a material-ui `GridTemplate` as an item. The `layoutGridSchema` for the `GridType.COLUMNS` is separated
|
|
740
|
-
* into the `children` and other `gridProps`. The `children` is iterated on and `gridProps` will be spread onto the
|
|
741
|
-
* outer `GridTemplate`. Each child will have their own rendered `GridTemplate`.
|
|
742
|
-
*
|
|
743
|
-
* @param layoutGridSchema - The string or object that represents the configuration for the grid field
|
|
744
|
-
* @returns - The rendered `GridTemplate` containing the children for the `GridType.COLUMNS`
|
|
745
|
-
*/
|
|
746
|
-
renderColumns(layoutGridSchema: GridSchemaType) {
|
|
747
|
-
const { registry, uiSchema } = this.props;
|
|
748
|
-
const { children, gridProps } = LayoutGridField.findChildrenAndProps<T, S, F>(
|
|
749
|
-
layoutGridSchema,
|
|
750
|
-
GridType.COLUMNS,
|
|
751
|
-
registry,
|
|
752
|
-
);
|
|
753
|
-
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
754
|
-
const GridTemplate = getTemplate<'GridTemplate', T, S, F>('GridTemplate', registry, uiOptions);
|
|
755
|
-
|
|
756
|
-
return children.map((child) => (
|
|
757
|
-
<GridTemplate
|
|
758
|
-
column
|
|
759
|
-
key={`column-${hashObject(child)}`}
|
|
760
|
-
data-testid={LayoutGridField.TEST_IDS.col}
|
|
761
|
-
{...gridProps}
|
|
762
|
-
>
|
|
763
|
-
{this.renderChildren([child])}
|
|
764
|
-
</GridTemplate>
|
|
765
|
-
));
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
/** Renders a material-ui `GridTemplate` as a container. The
|
|
769
|
-
* `layoutGridSchema` for the `GridType.ROW` is separated into the `children` and other `gridProps`. The `gridProps`
|
|
770
|
-
* will be spread onto the outer `GridTemplate`. Inside of the `GridTemplate` all of the `children` are rendered.
|
|
771
|
-
*
|
|
772
|
-
* @param layoutGridSchema - The string or object that represents the configuration for the grid field
|
|
773
|
-
* @returns - The rendered `GridTemplate` containing the children for the `GridType.ROW`
|
|
774
|
-
*/
|
|
775
|
-
renderRow(layoutGridSchema: GridSchemaType) {
|
|
776
|
-
const { registry, uiSchema } = this.props;
|
|
777
|
-
const { children, gridProps } = LayoutGridField.findChildrenAndProps<T, S, F>(
|
|
778
|
-
layoutGridSchema,
|
|
779
|
-
GridType.ROW,
|
|
780
|
-
registry,
|
|
781
|
-
);
|
|
782
|
-
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
783
|
-
const GridTemplate = getTemplate<'GridTemplate', T, S, F>('GridTemplate', registry, uiOptions);
|
|
784
|
-
|
|
785
|
-
return (
|
|
786
|
-
<GridTemplate {...gridProps} data-testid={LayoutGridField.TEST_IDS.row}>
|
|
787
|
-
{this.renderChildren(children)}
|
|
788
|
-
</GridTemplate>
|
|
789
|
-
);
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
/** Iterates through all the `childrenLayoutGridSchema`, rendering a nested `LayoutGridField` for each item in the
|
|
793
|
-
* list, passing all the props for the current `LayoutGridField` along, updating the `schema` by calling
|
|
794
|
-
* `retrieveSchema()` on it to resolve any `$ref`s. In addition to the updated `schema`, each item in
|
|
795
|
-
* `childrenLayoutGridSchema` is passed as `layoutGridSchema`.
|
|
796
|
-
*
|
|
797
|
-
* @param childrenLayoutGridSchema - The list of strings or objects that represents the configurations for the
|
|
798
|
-
* children fields
|
|
799
|
-
* @returns - The nested `LayoutGridField`s
|
|
957
|
+
>(props: LayoutGridFieldProps<T, S, F>) {
|
|
958
|
+
/** Render the `LayoutGridField`. If there isn't a `layoutGridSchema` prop defined, then try pulling it out of the
|
|
959
|
+
* `uiSchema` via `ui:LayoutGridField`. If `layoutGridSchema` is an object, then check to see if any of the properties
|
|
960
|
+
* match one of the `GridType`s. If so, call the appropriate render function for the type. Otherwise, just call the
|
|
961
|
+
* generic `renderField()` function with the `layoutGridSchema`.
|
|
800
962
|
*/
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
return childrenLayoutGridSchema.map((layoutGridSchema) => (
|
|
807
|
-
<LayoutGridField<T, S, F>
|
|
808
|
-
{...this.props}
|
|
809
|
-
key={`layoutGrid-${hashObject(layoutGridSchema)}`}
|
|
810
|
-
schema={schema}
|
|
811
|
-
layoutGridSchema={layoutGridSchema}
|
|
812
|
-
/>
|
|
813
|
-
));
|
|
963
|
+
const { uiSchema } = props;
|
|
964
|
+
let { layoutGridSchema } = props;
|
|
965
|
+
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
966
|
+
if (!layoutGridSchema && LAYOUT_GRID_UI_OPTION in uiOptions && isObject(uiOptions[LAYOUT_GRID_UI_OPTION])) {
|
|
967
|
+
layoutGridSchema = uiOptions[LAYOUT_GRID_UI_OPTION];
|
|
814
968
|
}
|
|
815
969
|
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
* specified props for that component. If `name` exists, we take the name, the initial & root schemas and the formData
|
|
820
|
-
* and get the destination schema, is required state and optional oneOf/anyOf options for it. If the destination
|
|
821
|
-
* schema was located along with oneOf/anyOf options then a `LayoutMultiSchemaField` will be rendered with the
|
|
822
|
-
* `uiSchema`, `errorSchema`, `idSchema` and `formData` drilled down to the dotted-path field, spreading any other
|
|
823
|
-
* props from `gridSchema` into the `ui:options`. If the destination schema located without any oneOf/anyOf options,
|
|
824
|
-
* then a `SchemaField` will be rendered with the same props as mentioned in the previous sentence. If no destination
|
|
825
|
-
* schema was located, but a custom render component was found, then it will be rendered with many of the non-event
|
|
826
|
-
* handling props. If none of the previous render paths are valid, then a null is returned.
|
|
827
|
-
*
|
|
828
|
-
* @param gridSchema - The string or object that represents the configuration for the grid field
|
|
829
|
-
* @returns - One of `LayoutMultiSchemaField`, `SchemaField`, a custom render component or null, depending
|
|
830
|
-
*/
|
|
831
|
-
renderField(gridSchema?: ConfigObject | string) {
|
|
832
|
-
const {
|
|
833
|
-
schema: initialSchema,
|
|
834
|
-
uiSchema,
|
|
835
|
-
errorSchema,
|
|
836
|
-
idSchema,
|
|
837
|
-
onBlur,
|
|
838
|
-
onFocus,
|
|
839
|
-
formData,
|
|
840
|
-
readonly,
|
|
841
|
-
registry,
|
|
842
|
-
idSeparator,
|
|
843
|
-
layoutGridSchema, // Used to pull this out of otherProps since we don't want to pass it through
|
|
844
|
-
...otherProps
|
|
845
|
-
} = this.props;
|
|
846
|
-
const { fields, schemaUtils } = registry;
|
|
847
|
-
const { SchemaField, LayoutMultiSchemaField } = fields;
|
|
848
|
-
const uiComponentProps = LayoutGridField.computeUIComponentPropsFromGridSchema(registry, gridSchema);
|
|
849
|
-
if (uiComponentProps.rendered) {
|
|
850
|
-
return uiComponentProps.rendered;
|
|
970
|
+
if (isObject(layoutGridSchema)) {
|
|
971
|
+
if (GridType.ROW in layoutGridSchema) {
|
|
972
|
+
return <LayoutGridRow {...props} layoutGridSchema={layoutGridSchema} />;
|
|
851
973
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
schema,
|
|
855
|
-
isRequired,
|
|
856
|
-
isReadonly,
|
|
857
|
-
optionsInfo,
|
|
858
|
-
idSchema: fieldIdSchema,
|
|
859
|
-
} = LayoutGridField.getSchemaDetailsForField<T, S, F>(
|
|
860
|
-
schemaUtils,
|
|
861
|
-
name,
|
|
862
|
-
initialSchema,
|
|
863
|
-
formData,
|
|
864
|
-
idSchema,
|
|
865
|
-
idSeparator,
|
|
866
|
-
);
|
|
867
|
-
|
|
868
|
-
if (schema) {
|
|
869
|
-
const Field = optionsInfo?.hasDiscriminator ? LayoutMultiSchemaField : SchemaField;
|
|
870
|
-
// Call this function to get the appropriate UISchema, which will always have its `readonly` state matching the
|
|
871
|
-
// `uiReadonly` flag that it returns. This is done since the `SchemaField` will always defer to the `readonly`
|
|
872
|
-
// state in the uiSchema over anything in the props or schema. Because we are implementing the "readonly" state of
|
|
873
|
-
// the `Form` via the prop passed to `LayoutGridField` we need to make sure the uiSchema always has a true value
|
|
874
|
-
// when it is needed
|
|
875
|
-
const { fieldUiSchema, uiReadonly } = LayoutGridField.computeFieldUiSchema<T, S, F>(
|
|
876
|
-
name,
|
|
877
|
-
uiProps,
|
|
878
|
-
uiSchema,
|
|
879
|
-
isReadonly,
|
|
880
|
-
readonly,
|
|
881
|
-
);
|
|
882
|
-
|
|
883
|
-
return (
|
|
884
|
-
<Field
|
|
885
|
-
data-testid={
|
|
886
|
-
optionsInfo?.hasDiscriminator
|
|
887
|
-
? LayoutGridField.TEST_IDS.layoutMultiSchemaField
|
|
888
|
-
: LayoutGridField.TEST_IDS.field
|
|
889
|
-
}
|
|
890
|
-
{...otherProps}
|
|
891
|
-
name={name}
|
|
892
|
-
required={isRequired}
|
|
893
|
-
readonly={uiReadonly}
|
|
894
|
-
schema={schema}
|
|
895
|
-
uiSchema={fieldUiSchema}
|
|
896
|
-
errorSchema={get(errorSchema, name)}
|
|
897
|
-
idSchema={fieldIdSchema}
|
|
898
|
-
idSeparator={idSeparator}
|
|
899
|
-
formData={get(formData, name)}
|
|
900
|
-
onChange={this.onFieldChange(name)}
|
|
901
|
-
onBlur={onBlur}
|
|
902
|
-
onFocus={onFocus}
|
|
903
|
-
options={optionsInfo?.options}
|
|
904
|
-
registry={registry}
|
|
905
|
-
/>
|
|
906
|
-
);
|
|
974
|
+
if (GridType.COLUMN in layoutGridSchema) {
|
|
975
|
+
return <LayoutGridCol {...props} layoutGridSchema={layoutGridSchema} />;
|
|
907
976
|
}
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
return (
|
|
911
|
-
<UIComponent
|
|
912
|
-
data-testid={LayoutGridField.TEST_IDS.uiComponent}
|
|
913
|
-
{...otherProps}
|
|
914
|
-
name={name}
|
|
915
|
-
required={isRequired}
|
|
916
|
-
formData={formData}
|
|
917
|
-
readOnly={!!isReadonly || readonly}
|
|
918
|
-
errorSchema={errorSchema}
|
|
919
|
-
uiSchema={uiSchema}
|
|
920
|
-
schema={initialSchema}
|
|
921
|
-
idSchema={idSchema}
|
|
922
|
-
idSeparator={idSeparator}
|
|
923
|
-
onBlur={onBlur}
|
|
924
|
-
onFocus={onFocus}
|
|
925
|
-
registry={registry}
|
|
926
|
-
{...uiProps}
|
|
927
|
-
/>
|
|
928
|
-
);
|
|
929
|
-
}
|
|
930
|
-
return null;
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
/** Renders the `LayoutGridField`. If there isn't a `layoutGridSchema` prop defined, then try pulling it out of the
|
|
934
|
-
* `uiSchema` via `ui:LayoutGridField`. If `layoutGridSchema` is an object, then check to see if any of the properties
|
|
935
|
-
* match one of the `GridType`s. If so, call the appropriate render function for the type. Otherwise, just call the
|
|
936
|
-
* generic `renderField()` function with the `layoutGridSchema`.
|
|
937
|
-
*
|
|
938
|
-
* @returns - the rendered `LayoutGridField`
|
|
939
|
-
*/
|
|
940
|
-
render() {
|
|
941
|
-
const { uiSchema } = this.props;
|
|
942
|
-
let { layoutGridSchema } = this.props;
|
|
943
|
-
const uiOptions = getUiOptions<T, S, F>(uiSchema);
|
|
944
|
-
if (!layoutGridSchema && LAYOUT_GRID_UI_OPTION in uiOptions && isObject(uiOptions[LAYOUT_GRID_UI_OPTION])) {
|
|
945
|
-
layoutGridSchema = uiOptions[LAYOUT_GRID_UI_OPTION];
|
|
977
|
+
if (GridType.COLUMNS in layoutGridSchema) {
|
|
978
|
+
return <LayoutGridColumns {...props} layoutGridSchema={layoutGridSchema} />;
|
|
946
979
|
}
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
if (GridType.ROW in layoutGridSchema) {
|
|
950
|
-
return this.renderRow(layoutGridSchema as GridSchemaType);
|
|
951
|
-
}
|
|
952
|
-
if (GridType.COLUMN in layoutGridSchema) {
|
|
953
|
-
return this.renderCol(layoutGridSchema as GridSchemaType);
|
|
954
|
-
}
|
|
955
|
-
if (GridType.COLUMNS in layoutGridSchema) {
|
|
956
|
-
return this.renderColumns(layoutGridSchema as GridSchemaType);
|
|
957
|
-
}
|
|
958
|
-
if (GridType.CONDITION in layoutGridSchema) {
|
|
959
|
-
return this.renderCondition(layoutGridSchema as GridSchemaType);
|
|
960
|
-
}
|
|
980
|
+
if (GridType.CONDITION in layoutGridSchema) {
|
|
981
|
+
return <LayoutGridCondition {...props} layoutGridSchema={layoutGridSchema} />;
|
|
961
982
|
}
|
|
962
|
-
return this.renderField(layoutGridSchema);
|
|
963
983
|
}
|
|
984
|
+
return <LayoutGridFieldComponent {...props} gridSchema={layoutGridSchema} />;
|
|
964
985
|
}
|
|
986
|
+
|
|
987
|
+
LayoutGridField.TEST_IDS = LAYOUT_GRID_FIELD_TEST_IDS;
|