From 9bc2f6502a60fe1ef0412d0b79129c188ee67c93 Mon Sep 17 00:00:00 2001 From: Florian Gareis Date: Tue, 16 Nov 2021 15:42:54 +0100 Subject: [PATCH] Add uischemas to dependency array Currently the uischema prop of JSONForms was not watched for changes. So when uischemas was changed after JSONForms was rendered, the change did not get applied --- packages/react/src/JsonFormsContext.tsx | 2 +- packages/react/test/JsonFormsContext.test.tsx | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/react/src/JsonFormsContext.tsx b/packages/react/src/JsonFormsContext.tsx index c976607f8d..5bb6ee2cbf 100644 --- a/packages/react/src/JsonFormsContext.tsx +++ b/packages/react/src/JsonFormsContext.tsx @@ -158,7 +158,7 @@ export const JsonFormsStateProvider = ({ children, initState, onChange }: any) = i18n: i18n, // only core dispatch available dispatch: coreDispatch, - }), [core, initState.renderers, initState.cells, config, initState.readonly, i18n]); + }), [core, initState.renderers, initState.cells, config, initState.uischemas, initState.readonly, i18n]); const onChangeRef = useRef(onChange); useEffect(() => { diff --git a/packages/react/test/JsonFormsContext.test.tsx b/packages/react/test/JsonFormsContext.test.tsx index f83b681c77..13baf408e2 100644 --- a/packages/react/test/JsonFormsContext.test.tsx +++ b/packages/react/test/JsonFormsContext.test.tsx @@ -264,3 +264,69 @@ test('withJsonFormsDetailProps - should use uischemas props', () => { expect(mockUISchemasProps.schema).toEqual(schema); expect(mockUISchemasProps.uischemas).toEqual(uischemas); }); + +test('withJsonFormsDetailProps - should update uischemas after change', () => { + const MockUISchemas = (_: StatePropsOfControlWithDetail) => { + return <>; + }; + + const MockBasicRenderer = withJsonFormsDetailProps(MockUISchemas); + + const schema = { + type: 'object', + properties: { + foo: { + type: 'string', + }, + bar: { + type: 'number' + } + } + }; + + const renderers = [ + { + tester: rankWith(1, () => true), + renderer: MockBasicRenderer + } + ]; + + const newUischemas = [ + { + tester: (_jsonSchema: JsonSchema, schemaPath: string) => { + return schemaPath === '#/properties/color' ? 2 : NOT_APPLICABLE; + }, + uischema: { + type: 'HorizontalLayout', + elements: [ + { + type: 'Control', + scope: '#/properties/foo' + }, + { + type: 'Control', + scope: '#/properties/bar' + } + ] + } + } + ]; + + const uischema = { + type: 'Control', + scope: '#' + }; + + const wrapper = mount( + + ); + + wrapper.setProps({ uischemas: newUischemas }); + wrapper.update(); + expect(wrapper.props().uischemas).toEqual(newUischemas); +});