Skip to content

Commit f439974

Browse files
authored
Added TabBased Material Renderer for Categorization (#863)
* Added TabBased Material Renderer for Categorization * Added TabBased Renderer for Categorization * updated categorization example * Fix compile error
1 parent e781407 commit f439974

File tree

3 files changed

+86
-30
lines changed

3 files changed

+86
-30
lines changed

packages/examples/src/categorization.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,6 @@ export const schema = personCoreSchema;
66
export const uischema = {
77
type: 'Categorization',
88
elements: [
9-
{
10-
type: 'Categorization',
11-
label: 'Sub',
12-
elements: [
13-
{
14-
type: 'Category',
15-
label: 'SubPrivate',
16-
elements: [
17-
{
18-
type: 'Control',
19-
scope: '#/properties/name'
20-
}
21-
]
22-
},
23-
{
24-
type: 'Category',
25-
label: 'Additional',
26-
elements: [
27-
{
28-
type: 'Control',
29-
scope: '#/properties/nationality'
30-
},
31-
{
32-
type: 'Control',
33-
scope: '#/properties/vegetarian'
34-
}
35-
]
36-
}
37-
]
38-
},
399
{
4010
type: 'Category',
4111
label: 'Private',
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import * as React from 'react';
2+
import AppBar from 'material-ui/AppBar';
3+
import Tabs, { Tab } from 'material-ui/Tabs';
4+
import {
5+
and,
6+
Categorization,
7+
connectToJsonForms,
8+
mapStateToLayoutProps,
9+
RankedTester,
10+
rankWith,
11+
RendererComponent,
12+
RendererProps,
13+
Tester,
14+
UISchemaElement,
15+
uiTypeIs
16+
} from '@jsonforms/core';
17+
import { MaterialLayoutRenderer, MaterialLayoutRendererProps } from '../util/layout';
18+
19+
const isSingleLevelCategorization: Tester = and(
20+
uiTypeIs('Categorization'),
21+
(uischema: UISchemaElement): boolean => {
22+
const categorization = uischema as Categorization;
23+
24+
return categorization.elements.reduce((acc, e) => acc && e.type === 'Category', true);
25+
}
26+
);
27+
28+
export const materialCategorizationTester: RankedTester = rankWith(1, isSingleLevelCategorization);
29+
export interface CategorizationState {
30+
value: number;
31+
}
32+
33+
export class MaterialCategorizationLayoutRenderer
34+
extends RendererComponent<RendererProps, CategorizationState> {
35+
constructor(props) {
36+
super(props);
37+
38+
this.state = {
39+
value: 0
40+
};
41+
}
42+
43+
render() {
44+
const { uischema, schema, path, visible } = this.props;
45+
const { value } = this.state;
46+
47+
const categorization = uischema as Categorization;
48+
49+
const childProps: MaterialLayoutRendererProps = {
50+
elements: categorization.elements[value].elements,
51+
schema,
52+
path,
53+
direction: 'column',
54+
visible
55+
};
56+
const style: {[x: string]: any} = { marginBottom: '10px' };
57+
if (!visible) {
58+
style.display = 'none';
59+
}
60+
61+
return (
62+
<div style={style}>
63+
<AppBar position='static'>
64+
<Tabs value={value} onChange={this.handleChange}>
65+
{categorization.elements.map((e, idx) => <Tab key={idx} label={e.label} />)}
66+
</Tabs>
67+
</AppBar>
68+
<div style={{ marginTop: '0.5em' }}>
69+
<MaterialLayoutRenderer {...childProps}/>
70+
</div>
71+
</div>
72+
);
73+
}
74+
private handleChange = (_event, value) => {
75+
this.setState({ value });
76+
}
77+
}
78+
79+
export default connectToJsonForms(
80+
mapStateToLayoutProps
81+
)(MaterialCategorizationLayoutRenderer);

packages/material/src/layouts/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import MaterialHorizontalLayout, {
33
materialHorizontalLayoutTester
44
} from './MaterialHorizontalLayout';
55
import MaterialVerticalLayout, { materialVerticalLayoutTester } from './MaterialVerticalLayout';
6+
import MaterialCategorizationLayout, {
7+
materialCategorizationTester
8+
} from './MaterialCategorizationLayout';
69

710
export {
11+
MaterialCategorizationLayout,
12+
materialCategorizationTester,
813
MaterialGroupLayout,
914
materialGroupTester,
1015
MaterialHorizontalLayout,

0 commit comments

Comments
 (0)