From 938e1256b223f0182de3457527f1528cdf1ab4eb Mon Sep 17 00:00:00 2001 From: slefebvre Date: Tue, 20 Feb 2024 10:01:11 +0100 Subject: [PATCH] Added oneOf enum case --- .../src/layouts/ExpandPanelRenderer.tsx | 113 ++++++++++++++---- .../renderers/MaterialArrayLayout.test.tsx | 88 ++++++++++++-- 2 files changed, 169 insertions(+), 32 deletions(-) diff --git a/packages/material-renderers/src/layouts/ExpandPanelRenderer.tsx b/packages/material-renderers/src/layouts/ExpandPanelRenderer.tsx index 9168bc06b..1239ba2a7 100644 --- a/packages/material-renderers/src/layouts/ExpandPanelRenderer.tsx +++ b/packages/material-renderers/src/layouts/ExpandPanelRenderer.tsx @@ -21,6 +21,8 @@ import { findUISchema, JsonFormsRendererRegistryEntry, JsonSchema, + JsonSchema4, + JsonSchema7, moveDown, moveUp, Resolve, @@ -33,6 +35,7 @@ import { ArrayTranslations, encode, enumToEnumOptionMapper, + oneOfToEnumOptionMapper, getI18nKeyPrefix, } from '@jsonforms/core'; import { @@ -299,35 +302,36 @@ export const withContextToExpandPanelProps = ( props, }: JsonFormsStateContext & ExpandPanelProps) { const dispatchProps = ctxDispatchToExpandPanelProps(ctx.dispatch); - const { childLabelProp, schema, rootSchema, path, index, uischemas } = - props; + const { + childLabelProp, + schema, + uischema, + rootSchema, + path, + index, + uischemas, + } = props; const childPath = composePaths(path, `${index}`); - const childData = Resolve.data(ctx.core.data, childPath); - let childLabel; - if (childLabelProp) { - childLabel = get(childData, childLabelProp, ''); - - const childSchema = Resolve.schema( + const childLabel = useMemo(() => { + return computeChildLabel( + ctx.core.data, + childPath, + childLabelProp, schema, - `#/properties/${encode(childLabelProp)}`, - rootSchema + rootSchema, + ctx.i18n.translate, + uischema ); - if ( - childSchema && - childSchema.type === 'string' && - (childSchema.enum !== undefined || childSchema.const !== undefined) - ) { - const enumChildLabel = enumToEnumOptionMapper( - childLabel, - ctx.i18n.translate, - getI18nKeyPrefix(props.schema, props.uischema, childPath) - ); - childLabel = enumChildLabel.label; - } - } else { - childLabel = get(childData, getFirstPrimitiveProp(schema), ''); - } + }, [ + ctx.core.data, + childPath, + childLabelProp, + schema, + rootSchema, + ctx.i18n.translate, + uischema, + ]); return ( e.const === currentValue + ); + + if (oneOfSchema === undefined) return currentValue; + + const oneOfChildLabel = oneOfToEnumOptionMapper( + oneOfSchema, + translateFct, + getI18nKeyPrefix(schema, uiSchema, childPath) + ); + + return oneOfChildLabel.label; + } else { + return currentValue; + } + } else { + return get(childData, getFirstPrimitiveProp(schema), ''); + } +} + export const withJsonFormsExpandPanelProps = ( Component: ComponentType ): ComponentType => diff --git a/packages/material-renderers/test/renderers/MaterialArrayLayout.test.tsx b/packages/material-renderers/test/renderers/MaterialArrayLayout.test.tsx index c811ff419..6423d6493 100644 --- a/packages/material-renderers/test/renderers/MaterialArrayLayout.test.tsx +++ b/packages/material-renderers/test/renderers/MaterialArrayLayout.test.tsx @@ -51,7 +51,7 @@ const data = [ }, ]; -const enumData = [ +const enumOrOneOfData = [ { message: 'El Barto was here', messageType: 'MSG_TYPE_1', @@ -94,6 +94,32 @@ const enumSchema: JsonSchema7 = { }, }; +const oneOfSchema = { + type: 'array', + items: { + type: 'object', + properties: { + message: { + type: 'string', + maxLength: 3, + }, + messageType: { + type: 'string', + oneOf: [ + { + const: 'MSG_TYPE_1', + title: 'First message type', + }, + { + const: 'MSG_TYPE_2', + title: 'Second message type', + }, + ], + }, + }, + }, +}; + const nestedSchema: JsonSchema7 = { type: 'array', items: { @@ -166,7 +192,7 @@ const uischemaWithChildLabelProp: ControlElement = { }, }; -const uischemaWithEnumChildLabelProp: ControlElement = { +const uiSchemaWithEnumOrOneOfChildLabelProp: ControlElement = { type: 'Control', scope: '#', options: { @@ -655,15 +681,37 @@ describe('Material array layout', () => { }); it('should render configured enum child label property as translated label', () => { - const core = initCore(enumSchema, uischemaWithEnumChildLabelProp, enumData); + const core = initCore( + enumSchema, + uiSchemaWithEnumOrOneOfChildLabelProp, + enumOrOneOfData + ); const translate = () => 'Translated'; + + // Enum Case - No translation + wrapper = mount( + + + + ); + + expect(wrapper.find(MaterialArrayLayout).length).toBeTruthy(); + + expect(getChildLabel(wrapper, 0)).toBe('MSG_TYPE_1'); + + // Enum Case - Translation wrapper = mount( ); @@ -671,20 +719,46 @@ describe('Material array layout', () => { expect(wrapper.find(MaterialArrayLayout).length).toBeTruthy(); expect(getChildLabel(wrapper, 0)).toBe('Translated'); + }); + it('should render configured oneOf child label property as translated label', () => { + const core = initCore( + oneOfSchema, + uiSchemaWithEnumOrOneOfChildLabelProp, + enumOrOneOfData + ); + const translate = () => 'Translated'; + + // OneOf - No translation wrapper = mount( ); expect(wrapper.find(MaterialArrayLayout).length).toBeTruthy(); - expect(getChildLabel(wrapper, 0)).toBe('MSG_TYPE_1'); + expect(getChildLabel(wrapper, 0)).toBe('First message type'); + + // OneOf Case - Translation + wrapper = mount( + + + + ); + + expect(wrapper.find(MaterialArrayLayout).length).toBeTruthy(); + + expect(getChildLabel(wrapper, 0)).toBe('Translated'); }); });