Skip to content

Commit 0bef851

Browse files
Lilyedgarmueller
authored andcommitted
Add direction prop to StatePropsOfLayout (#1404)
* Add direction prop to LayoutProps * Update layout default props * [material] Update group renderer
1 parent 0360113 commit 0bef851

File tree

5 files changed

+84
-15
lines changed

5 files changed

+84
-15
lines changed

packages/core/src/util/renderer.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ export interface OwnPropsOfEnum {
202202
options?: any[];
203203
}
204204

205+
export interface OwnPropsOfLayout extends OwnPropsOfRenderer {
206+
direction?: 'row' | 'column';
207+
}
208+
205209
/**
206210
* State-based props of a {@link Renderer}.
207211
*/
@@ -319,6 +323,11 @@ export interface StatePropsOfLayout extends StatePropsOfRenderer {
319323
* All available renderers.
320324
*/
321325
renderers?: any[];
326+
327+
/**
328+
* Direction for the layout to flow
329+
*/
330+
direction: 'row' | 'column';
322331
}
323332

324333
export interface LayoutProps extends StatePropsOfLayout {}
@@ -609,10 +618,16 @@ export interface ArrayControlProps
609618
extends StatePropsOfArrayControl,
610619
DispatchPropsOfArrayControl {}
611620

612-
export const layoutDefaultProps = {
621+
export const layoutDefaultProps: {
622+
visible: boolean;
623+
enabled: boolean;
624+
path: string;
625+
direction: 'row' | 'column';
626+
} = {
613627
visible: true,
614628
enabled: true,
615-
path: ''
629+
path: '',
630+
direction: 'column'
616631
};
617632

618633
/**
@@ -623,8 +638,8 @@ export const layoutDefaultProps = {
623638
*/
624639
export const mapStateToLayoutProps = (
625640
state: JsonFormsState,
626-
ownProps: OwnPropsOfJsonFormsRenderer
627-
): StatePropsOfLayout => {
641+
ownProps: OwnPropsOfLayout
642+
): LayoutProps => {
628643
const rootData = getData(state);
629644
const visible: boolean = has(ownProps, 'visible')
630645
? ownProps.visible
@@ -640,7 +655,8 @@ export const mapStateToLayoutProps = (
640655
enabled,
641656
path: ownProps.path,
642657
uischema: ownProps.uischema,
643-
schema: ownProps.schema
658+
schema: ownProps.schema,
659+
direction: ownProps.direction || layoutDefaultProps.direction
644660
};
645661
};
646662

packages/material/src/layouts/MaterialGroupLayout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ const GroupComponent = React.memo(({ visible, uischema, ...props }: MaterialLayo
5858
);
5959
});
6060

61-
export const MaterializedGroupLayoutRenderer = ({ uischema, schema, path, visible, renderers }: LayoutProps) => {
61+
export const MaterializedGroupLayoutRenderer = ({ uischema, schema, path, visible, renderers, direction }: LayoutProps) => {
6262
const groupLayout = uischema as GroupLayout;
6363

6464
return (
6565
<GroupComponent
6666
elements={groupLayout.elements}
6767
schema={schema}
6868
path={path}
69-
direction={'column'}
69+
direction={direction}
7070
visible={visible}
7171
uischema={uischema}
7272
renderers={renderers}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as React from 'react';
2+
import Enzyme, { mount } from 'enzyme';
3+
import Adapter from 'enzyme-adapter-react-16';
4+
import MaterialGroupLayout from '../../src/layouts/MaterialGroupLayout';
5+
import { MaterialLayoutRenderer } from '../../src/util/layout';
6+
7+
Enzyme.configure({ adapter: new Adapter() });
8+
9+
const schema = {
10+
type: 'object',
11+
properties: {
12+
name: {
13+
type: 'string',
14+
minLength: 3,
15+
description: 'Please enter your name'
16+
},
17+
birthDate: {
18+
type: 'string',
19+
format: 'date',
20+
description: 'Please enter your birth date.'
21+
}
22+
}
23+
};
24+
25+
const uischema = {
26+
type: 'Group',
27+
label: 'My Group',
28+
elements: [
29+
{
30+
type: 'Control',
31+
label: 'Name',
32+
scope: '#/properties/name'
33+
},
34+
{
35+
type: 'Control',
36+
label: 'Birth Date',
37+
scope: '#/properties/birthDate'
38+
}
39+
]
40+
};
41+
42+
describe('Material group layout', () => {
43+
it('should render a GroupComponent with direction column when given no direction LayoutProp', () => {
44+
const wrapper = mount(<MaterialGroupLayout schema={schema} uischema={uischema} />)
45+
expect(wrapper.find(MaterialLayoutRenderer).props().direction).toBe('column')
46+
})
47+
48+
it('should render a GroupComponent with direction row when this is provided as a direction prop', () => {
49+
const wrapper = mount(<MaterialGroupLayout schema={schema} uischema={uischema} direction={'row'} />)
50+
expect(wrapper.find(MaterialLayoutRenderer).props().direction).toBe('row')
51+
})
52+
})

packages/react/src/JsonFormsContext.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
OwnPropsOfEnumCell,
5555
OwnPropsOfJsonFormsRenderer,
5656
OwnPropsOfMasterListItem,
57+
OwnPropsOfLayout,
5758
rendererReducer,
5859
StatePropsOfCombinator,
5960
StatePropsOfControlWithDetail,
@@ -135,7 +136,7 @@ export const ctxToArrayLayoutProps = (ctx: JsonFormsStateContext, props: OwnProp
135136
export const ctxToArrayControlProps = (ctx: JsonFormsStateContext, props: OwnPropsOfControl) =>
136137
mapStateToArrayControlProps({ jsonforms: { ...ctx } }, props);
137138

138-
export const ctxToLayoutProps = (ctx: JsonFormsStateContext, props: OwnPropsOfJsonFormsRenderer): LayoutProps =>
139+
export const ctxToLayoutProps = (ctx: JsonFormsStateContext, props: OwnPropsOfLayout): LayoutProps =>
139140
mapStateToLayoutProps({ jsonforms: { ...ctx } }, props);
140141

141142
export const ctxToControlProps = (ctx: JsonFormsStateContext, props: OwnPropsOfControl) =>
@@ -207,7 +208,7 @@ export const ctxToCellProps = (
207208
return mapStateToCellProps({ jsonforms: { ...ctx } }, ownProps);
208209
}
209210

210-
export const withJsonFormsLayoutProps = (Component: ComponentType<LayoutProps>): ComponentType<OwnPropsOfJsonFormsRenderer> => (props: LayoutProps) => {
211+
export const withJsonFormsLayoutProps = (Component: ComponentType<LayoutProps>): ComponentType<OwnPropsOfLayout> => (props: LayoutProps) => {
211212
const ctx = useJsonForms();
212213
const layoutProps = ctxToLayoutProps(ctx, props);
213214
return (

packages/vanilla/src/complex/categorization/CategorizationRenderer.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/*
22
The MIT License
3-
3+
44
Copyright (c) 2017-2019 EclipseSource Munich
55
https://github.com/eclipsesource/jsonforms
6-
6+
77
Permission is hereby granted, free of charge, to any person obtaining a copy
88
of this software and associated documentation files (the "Software"), to deal
99
in the Software without restriction, including without limitation the rights
1010
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1111
copies of the Software, and to permit persons to whom the Software is
1212
furnished to do so, subject to the following conditions:
13-
13+
1414
The above copyright notice and this permission notice shall be included in
1515
all copies or substantial portions of the Software.
16-
16+
1717
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1818
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1919
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -26,7 +26,7 @@ import React from 'react';
2626
import {
2727
Categorization,
2828
Category,
29-
RendererProps
29+
LayoutProps
3030
} from '@jsonforms/core';
3131
import { RendererComponent, withJsonFormsLayoutProps } from '@jsonforms/react';
3232
import { CategorizationList } from './CategorizationList';
@@ -40,7 +40,7 @@ export interface CategorizationState {
4040
}
4141

4242
class CategorizationRenderer extends RendererComponent<
43-
RendererProps & VanillaRendererProps,
43+
LayoutProps & VanillaRendererProps,
4444
CategorizationState
4545
> {
4646
onCategorySelected = (category: Category) => () => {

0 commit comments

Comments
 (0)