Skip to content

Commit 67b036e

Browse files
authored
Preserve schemas in React Material Object Renderer
The React Material object renderer supports generating a group layout for its contained properties. The label of the group layout was unconditionally set, overwriting user specified 'detail' and registered UI schemas. This is now fixed. closes #1712 Signed-off-by: Lukas Boll lukas-bool@web.de
1 parent d65d7eb commit 67b036e

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

packages/core/src/reducers/reducers.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,28 @@ export const jsonFormsReducerConfig = {
6767
* @param schema the JSON schema describing the data to be rendered
6868
* @param schemaPath the according schema path
6969
* @param path the instance path
70-
* @param fallbackLayoutType the type of the layout to use
70+
* @param fallback the type of the layout to use or a UI-schema-generator function
7171
* @param control may be checked for embedded inline uischema options
7272
*/
7373
export const findUISchema = (
7474
uischemas: JsonFormsUISchemaRegistryEntry[],
7575
schema: JsonSchema,
7676
schemaPath: string,
7777
path: string,
78-
fallbackLayoutType = 'VerticalLayout',
78+
fallback: string | (() => UISchemaElement) = 'VerticalLayout',
7979
control?: ControlElement,
8080
rootSchema?: JsonSchema
8181
): UISchemaElement => {
8282
// handle options
8383
if (control && control.options && control.options.detail) {
8484
if (typeof control.options.detail === 'string') {
8585
if (control.options.detail.toUpperCase() === 'GENERATE') {
86+
//use fallback generation function
87+
if(typeof fallback === "function"){
88+
return fallback();
89+
}
8690
// force generation of uischema
87-
return Generate.uiSchema(schema, fallbackLayoutType);
91+
return Generate.uiSchema(schema, fallback);
8892
}
8993
} else if (typeof control.options.detail === 'object') {
9094
// check if detail is a valid uischema
@@ -99,7 +103,11 @@ export const findUISchema = (
99103
// default
100104
const uiSchema = findMatchingUISchema(uischemas)(schema, schemaPath, path);
101105
if (uiSchema === undefined) {
102-
return Generate.uiSchema(schema, fallbackLayoutType, '#', rootSchema);
106+
//use fallback generation function
107+
if(typeof fallback === 'function'){
108+
return fallback();
109+
}
110+
return Generate.uiSchema(schema, fallback, '#', rootSchema);
103111
}
104112
return uiSchema;
105113
};

packages/examples/src/examples/object.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ export const uischemaNonRoot = {
7272
scope: '#/properties/address/properties/state',
7373
expectedValue: 'DC'
7474
}
75+
},
76+
options: {
77+
detail: {
78+
type: 'Group',
79+
label: 'User Data',
80+
elements: [
81+
{ type: 'Control', scope: '#/properties/name'},
82+
{
83+
type: 'Control',
84+
scope: '#/properties/mail'
85+
}
86+
]
87+
}
7588
}
7689
}
7790
]

packages/material/src/complex/MaterialObjectRenderer.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import isEmpty from 'lodash/isEmpty';
2626
import {
2727
findUISchema,
28-
GroupLayout,
28+
Generate,
2929
isObjectControl,
3030
RankedTester,
3131
rankWith,
@@ -54,17 +54,12 @@ export const MaterialObjectRenderer = ({
5454
schema,
5555
uischema.scope,
5656
path,
57-
'Group',
57+
() => isEmpty(path) ? Generate.uiSchema(schema, 'VerticalLayout') : {...Generate.uiSchema(schema, 'Group'), label},
5858
uischema,
5959
rootSchema
6060
),
61-
[uischemas, schema, uischema.scope, path, uischema, rootSchema]
61+
[uischemas, schema, uischema.scope, path, label, uischema, rootSchema]
6262
);
63-
if (isEmpty(path)) {
64-
detailUiSchema.type = 'VerticalLayout';
65-
} else {
66-
(detailUiSchema as GroupLayout).label = label;
67-
}
6863
return (
6964
<Hidden xsUp={!visible}>
7065
<JsonFormsDispatch

0 commit comments

Comments
 (0)