1
- import React , { Fragment , useState } from 'react' ;
2
- import { connect } from 'react-redux' ;
3
- import { getData , JsonFormsState } from '@jsonforms/core' ;
4
- import { JsonForms , JsonFormsDispatch , JsonFormsReduxContext } from '@jsonforms/react' ;
5
- import Grid from "@material-ui/core/Grid" ;
6
- import Typography from "@material-ui/core/Typography" ;
7
- import withStyles , { WithStyles } from "@material-ui/core/styles/withStyles" ;
8
- import createStyles from "@material-ui/core/styles/createStyles" ;
1
+ import React , { Fragment , useState , useEffect , useCallback } from 'react' ;
2
+ import {
3
+ JsonForms ,
4
+ JsonFormsDispatch ,
5
+ JsonFormsReduxContext
6
+ } from '@jsonforms/react' ;
7
+ import { Provider } from 'react-redux' ;
8
+ import Grid from '@material-ui/core/Grid' ;
9
+ import Typography from '@material-ui/core/Typography' ;
10
+ import withStyles , { WithStyles } from '@material-ui/core/styles/withStyles' ;
11
+ import createStyles from '@material-ui/core/styles/createStyles' ;
9
12
import { Tabs , Tab } from '@material-ui/core' ;
10
13
import logo from './logo.svg' ;
11
14
import './App.css' ;
12
15
import schema from './schema.json' ;
13
16
import uischema from './uischema.json' ;
14
- import { materialRenderers } from '@jsonforms/material-renderers' ;
17
+ import {
18
+ materialCells ,
19
+ materialRenderers
20
+ } from '@jsonforms/material-renderers' ;
21
+ import { Store } from 'redux' ;
22
+ import { get } from 'lodash' ;
23
+ import RatingControl from './RatingControl' ;
24
+ import ratingControlTester from './ratingControlTester' ;
15
25
16
26
const styles = createStyles ( {
17
27
container : {
@@ -25,7 +35,7 @@ const styles = createStyles({
25
35
display : 'flex' ,
26
36
justifyContent : 'center' ,
27
37
borderRadius : '0.25em' ,
28
- backgroundColor : '#cecece' ,
38
+ backgroundColor : '#cecece'
29
39
} ,
30
40
demoform : {
31
41
margin : 'auto' ,
@@ -34,93 +44,118 @@ const styles = createStyles({
34
44
} ) ;
35
45
36
46
export interface AppProps extends WithStyles < typeof styles > {
37
- dataAsString : string ;
47
+ store : Store ;
38
48
}
39
49
40
- const App = ( { classes, dataAsString } : AppProps ) => {
50
+ const data = {
51
+ name : 'Send email to Adrian' ,
52
+ description : 'Confirm if you have passed the subject\nHereby ...' ,
53
+ done : true ,
54
+ recurrence : 'Daily' ,
55
+ rating : 3
56
+ } ;
57
+
58
+ const getDataAsStringFromStore = ( store : Store ) =>
59
+ store
60
+ ? JSON . stringify (
61
+ get ( store . getState ( ) , [ 'jsonforms' , 'core' , 'data' ] ) ,
62
+ null ,
63
+ 2
64
+ )
65
+ : '' ;
66
+
67
+ const App = ( { store, classes } : AppProps ) => {
41
68
const [ tabIdx , setTabIdx ] = useState ( 0 ) ;
42
- function handleTabChange ( event : any , newValue : number ) {
43
- setTabIdx ( newValue ) ;
44
- }
69
+ const [ displayDataAsString , setDisplayDataAsString ] = useState ( '' ) ;
70
+ const [ standaloneData , setStandaloneData ] = useState ( data ) ;
71
+ const handleTabChange = useCallback (
72
+ ( event : any , newValue : number ) => {
73
+ setTabIdx ( newValue ) ;
74
+ setDisplayDataAsString (
75
+ newValue === 0
76
+ ? getDataAsStringFromStore ( store )
77
+ : JSON . stringify ( standaloneData , null , 2 )
78
+ ) ;
79
+ } ,
80
+ [ store , standaloneData ]
81
+ ) ;
82
+
83
+ useEffect ( ( ) => {
84
+ const updateStringData = ( ) => {
85
+ const stringData = getDataAsStringFromStore ( store ) ;
86
+ setDisplayDataAsString ( stringData ) ;
87
+ } ;
88
+ store . subscribe ( updateStringData ) ;
89
+ updateStringData ( ) ;
90
+ } , [ store ] ) ;
91
+
92
+ useEffect ( ( ) => {
93
+ setDisplayDataAsString ( JSON . stringify ( standaloneData , null , 2 ) ) ;
94
+ } , [ standaloneData ] ) ;
95
+
45
96
return (
46
97
< Fragment >
47
- < div className = " App" >
48
- < header className = " App-header" >
49
- < img src = { logo } className = " App-logo" alt = " logo" />
50
- < h1 className = " App-title" > Welcome to JSON Forms with React</ h1 >
51
- < p className = " App-intro" > More Forms. Less Code.</ p >
98
+ < div className = ' App' >
99
+ < header className = ' App-header' >
100
+ < img src = { logo } className = ' App-logo' alt = ' logo' />
101
+ < h1 className = ' App-title' > Welcome to JSON Forms with React</ h1 >
102
+ < p className = ' App-intro' > More Forms. Less Code.</ p >
52
103
</ header >
53
104
</ div >
54
105
55
- < Tabs value = { tabIdx } onChange = { handleTabChange } >
56
- < Tab label = "via Redux" />
57
- < Tab label = "Standalone" />
58
- </ Tabs >
59
-
60
- { tabIdx === 0 &&
61
- < Grid container justify = { 'center' } spacing = { 1 } className = { classes . container } >
62
- < Grid item sm = { 6 } >
63
- < Typography
64
- variant = { 'h3' }
65
- className = { classes . title }
66
- >
67
- Bound data
68
- </ Typography >
69
- < div className = { classes . dataContent } >
70
- < pre id = "boundData" > { dataAsString } </ pre >
106
+ < Grid
107
+ container
108
+ justify = { 'center' }
109
+ spacing = { 1 }
110
+ className = { classes . container }
111
+ >
112
+ < Grid item sm = { 6 } >
113
+ < Typography variant = { 'h3' } className = { classes . title } >
114
+ Bound data
115
+ </ Typography >
116
+ < div className = { classes . dataContent } >
117
+ < pre id = 'boundData' > { displayDataAsString } </ pre >
118
+ </ div >
119
+ </ Grid >
120
+ < Grid item sm = { 6 } >
121
+ < Typography variant = { 'h3' } className = { classes . title } >
122
+ Rendered form
123
+ </ Typography >
124
+ < Tabs value = { tabIdx } onChange = { handleTabChange } >
125
+ < Tab label = 'via Redux' />
126
+ < Tab label = 'Standalone' />
127
+ </ Tabs >
128
+ { tabIdx === 0 && (
129
+ < div className = { classes . demoform } id = 'form' >
130
+ { store ? (
131
+ < Provider store = { store } >
132
+ < JsonFormsReduxContext >
133
+ < JsonFormsDispatch />
134
+ </ JsonFormsReduxContext >
135
+ </ Provider >
136
+ ) : null }
71
137
</ div >
72
- </ Grid >
73
- < Grid item sm = { 6 } >
74
- < Typography
75
- variant = { 'h3' }
76
- className = { classes . title }
77
- >
78
- Rendered form
79
- </ Typography >
80
- < div className = { classes . demoform } id = "form" >
81
- < JsonFormsReduxContext >
82
- < JsonFormsDispatch />
83
- </ JsonFormsReduxContext >
138
+ ) }
139
+ { tabIdx === 1 && (
140
+ < div className = { classes . demoform } >
141
+ < JsonForms
142
+ schema = { schema }
143
+ uischema = { uischema }
144
+ data = { standaloneData }
145
+ renderers = { [
146
+ ...materialRenderers ,
147
+ //register custom renderer
148
+ { tester : ratingControlTester , renderer : RatingControl }
149
+ ] }
150
+ cells = { materialCells }
151
+ onChange = { ( { errors, data } ) => setStandaloneData ( data ) }
152
+ />
84
153
</ div >
85
- </ Grid >
154
+ ) }
86
155
</ Grid >
87
- }
88
- { tabIdx === 1 &&
89
- < div className = { classes . demoform } style = { { maxWidth : 1000 } } >
90
- < JsonForms
91
- schema = { schema }
92
- uischema = { uischema }
93
- data = { {
94
- name : 'Send email to Adrian' ,
95
- description : 'Confirm if you have passed the subject\nHereby ...' ,
96
- done : true ,
97
- recurrence : 'Daily' ,
98
- rating : 3 ,
99
- } }
100
- renderers = { materialRenderers }
101
- />
102
- < JsonForms
103
- schema = { schema }
104
- uischema = { uischema }
105
- data = { {
106
- name : undefined ,
107
- due_date : '2019-06-19' ,
108
- description : 'Confirm if you have passed the subject\nHereby ...' ,
109
- done : true ,
110
- recurrence : 'Daily' ,
111
- rating : 3 ,
112
- } }
113
- renderers = { materialRenderers }
114
- />
115
- </ div >
116
- }
156
+ </ Grid >
117
157
</ Fragment >
118
158
) ;
119
159
} ;
120
160
121
- const mapStateToProps = ( state : JsonFormsState ) => {
122
- return { dataAsString : JSON . stringify ( getData ( state ) , null , 2 ) }
123
- } ;
124
-
125
- export default connect ( mapStateToProps ) ( withStyles ( styles ) ( App ) ) ;
126
-
161
+ export default withStyles ( styles ) ( App ) ;
0 commit comments