23
23
THE SOFTWARE.
24
24
*/
25
25
26
- import React , { useEffect , useState } from 'react' ;
26
+ import React , { useEffect , useMemo , useState } from 'react' ;
27
27
import { JsonForms , JsonFormsInitStateProps , JsonFormsReactProps } from '@jsonforms/react' ;
28
28
import { ExampleDescription } from '@jsonforms/examples' ;
29
29
import {
@@ -91,10 +91,10 @@ const App = ({ examples, cells, renderers}: AppProps) => {
91
91
const [ currentExample , setExample ] = useState < ExampleDescription > ( examples [ 0 ] ) ;
92
92
const [ currentIndex , setIndex ] = useState < number > ( 0 ) ;
93
93
const [ dataAsString , setDataAsString ] = useState < any > ( '' ) ;
94
- const [ schemaAsString , setSchemaAsString ] = useState < any > ( '' ) ;
95
- const [ uiSchemaAsString , setUiSchemaAsString ] = useState < any > ( '' ) ;
96
94
const [ props , setProps ] = useState < any > ( getProps ( currentExample , cells , renderers ) ) ;
97
95
const [ showPanel , setShowPanel ] = useState < boolean > ( true ) ;
96
+ const schemaAsString = useMemo ( ( ) => JSON . stringify ( props . schema , null , 2 ) , [ props . schema ] ) ;
97
+ const uiSchemaAsString = useMemo ( ( ) => JSON . stringify ( props . uischema , null , 2 ) , [ props . uischema ] ) ;
98
98
99
99
const actions : Action [ ] = currentExample . actions ;
100
100
@@ -119,41 +119,10 @@ const App = ({ examples, cells, renderers}: AppProps) => {
119
119
}
120
120
} ;
121
121
122
- useEffect ( ( ) => {
123
- if ( props . schema != undefined ) {
124
- setSchemaAsString ( JSON . stringify ( props . schema , null , 2 ) ) ;
125
- } else {
126
- setSchemaAsString ( 'No schema set' ) ;
127
- }
128
- if ( props . uischema != undefined ) {
129
- setUiSchemaAsString ( JSON . stringify ( props . uischema , null , 2 ) ) ;
130
- } else {
131
- setUiSchemaAsString ( 'No ui schema set' ) ;
132
- }
133
- } , [ props . schema , props . uischema ] ) ;
134
-
135
- useEffect ( ( ) => {
136
- var panel = document . getElementsByClassName ( 'props-panel' ) [ 0 ] ;
137
- if ( showPanel ) {
138
- panel . classList . remove ( 'hide' ) ;
139
- } else {
140
- panel . classList . add ( 'hide' ) ;
141
- }
142
- } , [ showPanel ] ) ;
143
-
144
122
const changeData = ( data : any ) => {
145
123
setDataAsString ( JSON . stringify ( data , null , 2 ) ) ;
146
124
} ;
147
125
148
- const togglePanelHeight = ( ) => {
149
- var panel = document . getElementsByClassName ( 'panel-wrapper' ) [ 0 ] ;
150
- panel . classList . toggle ( 'full-height' ) ;
151
- }
152
-
153
- const togglePanel = ( checked : boolean ) => {
154
- setShowPanel ( checked ) ;
155
- }
156
-
157
126
return (
158
127
< div >
159
128
< div className = 'App' >
@@ -182,47 +151,47 @@ const App = ({ examples, cells, renderers}: AppProps) => {
182
151
</ select >
183
152
</ div >
184
153
< div className = 'toggle-panel' >
185
- < input type = 'checkbox' id = 'panel' name = 'panel' checked = { showPanel } onChange = { e => togglePanel ( e . target . checked ) } />
154
+ < input type = 'checkbox' id = 'panel' name = 'panel' checked = { showPanel } onChange = { ( ) => setShowPanel ( ! showPanel ) } />
186
155
< label htmlFor = 'panel' > Show sidepanel</ label >
187
156
</ div >
188
157
</ div >
189
158
190
159
< div className = 'demo-wrapper' >
191
- < div className = 'props-panel' >
192
- < Tabs >
193
- < TabList >
194
- < Tab > Data</ Tab >
195
- < Tab > Schema</ Tab >
196
- < Tab > UISchema</ Tab >
197
- </ TabList >
198
- < p className = 'expand-hint' onClick = { ( ) => togglePanelHeight ( ) } > Click here to expand</ p >
199
- < div className = 'panel-wrapper' >
200
- < TabPanel >
201
- < Highlight className = 'json' >
202
- { dataAsString }
203
- </ Highlight >
204
- </ TabPanel >
205
- < TabPanel >
206
- < Highlight className = 'json' >
207
- { schemaAsString }
208
- </ Highlight >
209
- </ TabPanel >
210
- < TabPanel >
211
- < Highlight className = 'json' >
212
- { uiSchemaAsString }
213
- </ Highlight >
214
- </ TabPanel >
215
- </ div >
216
- </ Tabs >
217
- </ div >
160
+ { showPanel &&
161
+ < div className = 'props-panel' >
162
+ < Tabs >
163
+ < TabList >
164
+ < Tab > Data</ Tab >
165
+ < Tab > Schema</ Tab >
166
+ < Tab > UISchema</ Tab >
167
+ </ TabList >
168
+ < div className = "panel-wrapper" >
169
+ < TabPanel >
170
+ < Highlight className = 'json' >
171
+ { dataAsString }
172
+ </ Highlight >
173
+ </ TabPanel >
174
+ < TabPanel >
175
+ < Highlight className = 'json' >
176
+ { schemaAsString }
177
+ </ Highlight >
178
+ </ TabPanel >
179
+ < TabPanel >
180
+ < Highlight className = 'json' >
181
+ { uiSchemaAsString }
182
+ </ Highlight >
183
+ </ TabPanel >
184
+ </ div >
185
+ </ Tabs >
186
+ </ div >
187
+ }
218
188
< div className = 'demoform' >
219
189
< div className = 'buttons' >
220
190
{ actions ?. map ( ( action : Action , index : number ) => (
221
191
< button className = 'action-button' onClick = { ( ) => setProps ( ( oldProps : JsonFormsInitStateProps ) => action . apply ( oldProps ) ) } key = { index } > { action . label } </ button >
222
192
) ) }
223
193
</ div >
224
194
< div className = 'demo' >
225
- < h4 > < span > Example</ span > </ h4 >
226
195
< ResolvedJsonForms
227
196
key = { currentIndex }
228
197
{ ...props }
0 commit comments