|
| 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); |
0 commit comments