Skip to content

Commit

Permalink
Watch uischemas prop in React bindings
Browse files Browse the repository at this point in the history
The "uischemas" prop of the React state manager was not watched
for changes. Therefore changes to "uischemas" were only applied
when other props also changed. This is now fixed.
  • Loading branch information
TheZoker authored Nov 22, 2021
1 parent 988f282 commit 6bcdbb1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react/src/JsonFormsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
67 changes: 67 additions & 0 deletions packages/react/test/JsonFormsContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,70 @@ 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(
<JsonForms
data={{}}
schema={schema}
uischema={uischema}
renderers={renderers}
/>
);

wrapper.setProps({ uischemas: newUischemas });
wrapper.update();
const mockUISchemasProps = wrapper.find(MockUISchemas).props();
expect(mockUISchemasProps.uischemas).toEqual(newUischemas);
});

0 comments on commit 6bcdbb1

Please sign in to comment.