Skip to content

Commit cb9fd29

Browse files
committed
[react] Allow renderers ownProp to be passed to JsonForms component
1 parent 250a6fa commit cb9fd29

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

packages/core/src/util/renderer.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,17 +503,20 @@ export const mapStateToLayoutProps = (
503503
};
504504
};
505505

506-
export interface JsonFormsProps extends StatePropsOfRenderer {
507-
renderers?: { tester: RankedTester; renderer: any }[];
506+
export interface OwnPropsOfJsonFormsRenderer extends OwnPropsOfRenderer {
507+
renderers?: JsonFormsRendererRegistryEntry[];
508508
}
509509

510-
export interface StatePropsOfJsonFormsRenderer extends OwnPropsOfRenderer {
510+
export interface JsonFormsProps extends StatePropsOfRenderer {
511511
renderers?: JsonFormsRendererRegistryEntry[];
512512
}
513513

514+
export interface StatePropsOfJsonFormsRenderer
515+
extends OwnPropsOfJsonFormsRenderer {}
516+
514517
export const mapStateToJsonFormsRendererProps = (
515518
state: JsonFormsState,
516-
ownProps: OwnPropsOfRenderer
519+
ownProps: OwnPropsOfJsonFormsRenderer
517520
): StatePropsOfJsonFormsRenderer => {
518521
let uischema = ownProps.uischema;
519522
if (uischema === undefined) {
@@ -525,7 +528,7 @@ export const mapStateToJsonFormsRendererProps = (
525528
}
526529

527530
return {
528-
renderers: get(state.jsonforms, 'renderers') || [],
531+
renderers: ownProps.renderers || get(state.jsonforms, 'renderers') || [],
529532
schema: ownProps.schema || getSchema(state),
530533
uischema
531534
};

packages/react/src/JsonForms.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
JsonFormsProps,
3535
JsonSchema,
3636
mapStateToJsonFormsRendererProps,
37+
OwnPropsOfJsonFormsRenderer,
3738
OwnPropsOfRenderer,
3839
removeId,
3940
StatePropsOfJsonFormsRenderer
@@ -160,7 +161,7 @@ export class JsonFormsDispatchRenderer extends ResolvedJsonFormsDispatchRenderer
160161
}
161162
}
162163

163-
export const JsonForms = connect<StatePropsOfJsonFormsRenderer, {}, OwnPropsOfRenderer>(
164+
export const JsonForms = connect<StatePropsOfJsonFormsRenderer, {}, OwnPropsOfJsonFormsRenderer>(
164165
mapStateToJsonFormsRendererProps,
165166
null
166167
)(JsonFormsDispatchRenderer);

packages/react/test/renderers/JsonForms.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ test('JsonForms renderer should pick most applicable renderer', () => {
138138
uischema: fixture.uischema
139139
});
140140
store.dispatch(registerRenderer(() => 10, CustomRenderer1));
141-
store.dispatch(registerRenderer(() => 5, CustomRenderer1));
141+
store.dispatch(registerRenderer(() => 5, CustomRenderer2));
142142
const wrapper = mount(
143143
<Provider store={store}>
144144
<JsonForms uischema={fixture.uischema} schema={fixture.schema} />
@@ -364,3 +364,24 @@ test.skip('updates schema with ref', () => {
364364
wrapper.unmount();
365365
});
366366
});
367+
368+
test('JsonForms renderer should pick most applicable renderer via ownProps', () => {
369+
const store = initJsonFormsStore({
370+
data: fixture.data,
371+
uischema: fixture.uischema
372+
});
373+
store.dispatch(registerRenderer(() => 10, CustomRenderer1));
374+
store.dispatch(registerRenderer(() => 5, CustomRenderer2));
375+
const wrapper = mount(
376+
<Provider store={store}>
377+
<JsonForms
378+
uischema={fixture.uischema}
379+
schema={fixture.schema}
380+
renderers={[{ tester: () => 3, renderer: CustomRenderer3 }]}
381+
/>
382+
</Provider>
383+
);
384+
385+
expect(wrapper.find('h3').text()).toBe('test');
386+
wrapper.unmount();
387+
});

0 commit comments

Comments
 (0)