From 77327d5c28c0a107f27916e62a23299143787e96 Mon Sep 17 00:00:00 2001 From: LukasBoll Date: Fri, 14 Jan 2022 17:56:53 +0100 Subject: [PATCH] Don't modify user-schemas in Material Object Renderer closes #1712 Signed-off-by: Lukas Boll lukas-bool@web.de --- packages/core/src/reducers/reducers.ts | 16 ++++++++++++---- packages/examples/src/examples/object.ts | 13 +++++++++++++ .../src/complex/MaterialObjectRenderer.tsx | 11 +++-------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/core/src/reducers/reducers.ts b/packages/core/src/reducers/reducers.ts index c719b3c95..d90de55c5 100644 --- a/packages/core/src/reducers/reducers.ts +++ b/packages/core/src/reducers/reducers.ts @@ -67,7 +67,7 @@ export const jsonFormsReducerConfig = { * @param schema the JSON schema describing the data to be rendered * @param schemaPath the according schema path * @param path the instance path - * @param fallbackLayoutType the type of the layout to use + * @param fallback the type of the layout to use or a UI-schema-generator function * @param control may be checked for embedded inline uischema options */ export const findUISchema = ( @@ -75,7 +75,7 @@ export const findUISchema = ( schema: JsonSchema, schemaPath: string, path: string, - fallbackLayoutType = 'VerticalLayout', + fallback: string | (() => UISchemaElement) = 'VerticalLayout', control?: ControlElement, rootSchema?: JsonSchema ): UISchemaElement => { @@ -83,8 +83,12 @@ export const findUISchema = ( if (control && control.options && control.options.detail) { if (typeof control.options.detail === 'string') { if (control.options.detail.toUpperCase() === 'GENERATE') { + //use fallback generation function + if(typeof fallback === "function"){ + return fallback(); + } // force generation of uischema - return Generate.uiSchema(schema, fallbackLayoutType); + return Generate.uiSchema(schema, fallback); } } else if (typeof control.options.detail === 'object') { // check if detail is a valid uischema @@ -99,7 +103,11 @@ export const findUISchema = ( // default const uiSchema = findMatchingUISchema(uischemas)(schema, schemaPath, path); if (uiSchema === undefined) { - return Generate.uiSchema(schema, fallbackLayoutType, '#', rootSchema); + //use fallback generation function + if(typeof fallback === 'function'){ + return fallback(); + } + return Generate.uiSchema(schema, fallback, '#', rootSchema); } return uiSchema; }; diff --git a/packages/examples/src/examples/object.ts b/packages/examples/src/examples/object.ts index 44a50104f..46b7278c4 100644 --- a/packages/examples/src/examples/object.ts +++ b/packages/examples/src/examples/object.ts @@ -72,6 +72,19 @@ export const uischemaNonRoot = { scope: '#/properties/address/properties/state', expectedValue: 'DC' } + }, + options: { + detail: { + type: 'Group', + label: 'User Data', + elements: [ + { type: 'Control', scope: '#/properties/name'}, + { + type: 'Control', + scope: '#/properties/mail' + } + ] + } } } ] diff --git a/packages/material/src/complex/MaterialObjectRenderer.tsx b/packages/material/src/complex/MaterialObjectRenderer.tsx index 012487b56..77bcf506d 100644 --- a/packages/material/src/complex/MaterialObjectRenderer.tsx +++ b/packages/material/src/complex/MaterialObjectRenderer.tsx @@ -25,7 +25,7 @@ import isEmpty from 'lodash/isEmpty'; import { findUISchema, - GroupLayout, + Generate, isObjectControl, RankedTester, rankWith, @@ -54,17 +54,12 @@ export const MaterialObjectRenderer = ({ schema, uischema.scope, path, - 'Group', + () => isEmpty(path) ? Generate.uiSchema(schema, 'VerticalLayout') : {...Generate.uiSchema(schema, 'Group'), label}, uischema, rootSchema ), - [uischemas, schema, uischema.scope, path, uischema, rootSchema] + [uischemas, schema, uischema.scope, path, label, uischema, rootSchema] ); - if (isEmpty(path)) { - detailUiSchema.type = 'VerticalLayout'; - } else { - (detailUiSchema as GroupLayout).label = label; - } return (