1
- import React , { useEffect } from 'react' ;
1
+ import React , { useEffect , useState } from 'react' ;
2
2
import { useRouter } from 'next/router' ;
3
3
import CircularProgress from '@material-ui/core/CircularProgress' ;
4
4
import { makeStyles } from '@material-ui/core/styles' ;
5
5
import PropTypes from 'prop-types' ;
6
- import Typography from '@material-ui/core/Typography' ;
7
6
import Card from '@material-ui/core/Card' ;
8
7
import CardContent from '@material-ui/core/CardContent' ;
9
8
import sdk from '@stackblitz/sdk' ;
@@ -95,7 +94,10 @@ const useStyles = makeStyles((theme) => ({
95
94
spinnerCheat : {
96
95
flex : 1 ,
97
96
position : 'relative' ,
98
- boxShadow : theme . shadows [ 1 ]
97
+ boxShadow : theme . shadows [ 1 ] ,
98
+ '& .longer + #code-target' : {
99
+ maxHeight : 'calc(100% - 49px)'
100
+ }
99
101
} ,
100
102
spinner : {
101
103
position : 'absolute' ,
@@ -123,28 +125,62 @@ const useStyles = makeStyles((theme) => ({
123
125
} ,
124
126
alert : {
125
127
marginBottom : 8
128
+ } ,
129
+ variantTabs : {
130
+ height : 49 ,
131
+ background : '#eaeaea'
132
+ } ,
133
+ hidden : {
134
+ height : 0 ,
135
+ minHeight : 0
126
136
}
127
137
} ) ) ;
128
138
129
- const ComponentExample = ( { variants, schema, activeMapper, component } ) => {
139
+ const stringifyWithFunctions = ( string ) =>
140
+ JSON . stringify ( string , null , 2 )
141
+ . replace ( / < N E W L I N E > / g, '\n' )
142
+ . replace ( / " < F U N C T I O N / g, '' )
143
+ . replace ( / F U N C T I O N > " / g, '' ) ;
144
+
145
+ const ComponentExample = ( { variants, schema, activeMapper, component, schemaVariants } ) => {
146
+ const [ variant , setVariant ] = useState ( 'basic' ) ;
147
+
130
148
const { pathname, push } = useRouter ( ) ;
131
149
const classes = useStyles ( ) ;
150
+
151
+ const availableVariants = schemaVariants ?. [ activeMapper ] ;
152
+ const selectedSchema = availableVariants ?. find ( ( { value } ) => value === variant ) ?. schema || schema ;
153
+ const basicConfiguration = {
154
+ ...project ,
155
+ dependencies : metadata [ activeMapper ] . dependencies ,
156
+ files : {
157
+ 'index.html' : metadata [ activeMapper ] . html ,
158
+ 'index.js' : metadata [ activeMapper ] . code ,
159
+ ...( component === 'wizard' && { 'index.js' : metadata [ activeMapper ] . wizardCode } ) ,
160
+ 'schema.js' : `export default ${ stringifyWithFunctions ( selectedSchema ) } ;`
161
+ }
162
+ } ;
163
+ const basicEditorSettings = { height : '100%' , hideNavigation : true , forceEmbedLayout : true , openFile : 'schema.js' } ;
164
+
132
165
useEffect ( ( ) => {
166
+ if ( availableVariants && ! availableVariants . map ( ( { value } ) => value ) . includes ( variant ) ) {
167
+ setVariant ( 'basic' ) ;
168
+ }
169
+
170
+ sdk . embedProject ( 'code-target' , basicConfiguration , basicEditorSettings ) ;
171
+ } , [ activeMapper , schema ] ) ;
172
+
173
+ const handleVariantChange = ( _e , newVariant ) => {
174
+ setVariant ( newVariant ) ;
175
+
176
+ const schema = availableVariants . find ( ( { value } ) => value === newVariant ) . schema ;
177
+
133
178
sdk . embedProject (
134
179
'code-target' ,
135
- {
136
- ...project ,
137
- dependencies : metadata [ activeMapper ] . dependencies ,
138
- files : {
139
- 'index.html' : metadata [ activeMapper ] . html ,
140
- 'index.js' : metadata [ activeMapper ] . code ,
141
- ...( component === 'wizard' && { 'index.js' : metadata [ activeMapper ] . wizardCode } ) ,
142
- 'schema.js' : `export default ${ JSON . stringify ( schema , null , 2 ) } ;`
143
- }
144
- } ,
145
- { height : '100%' , hideNavigation : true , forceEmbedLayout : true , openFile : 'schema.js' }
180
+ { ...basicConfiguration , files : { ...basicConfiguration . files , 'schema.js' : `export default ${ stringifyWithFunctions ( schema ) } ;` } } ,
181
+ basicEditorSettings
146
182
) ;
147
- } , [ activeMapper , schema ] ) ;
183
+ } ;
148
184
149
185
const renderMapperTabs = ( ) =>
150
186
avalableMappers . map ( ( { title, mapper } ) => (
@@ -170,7 +206,6 @@ const ComponentExample = ({ variants, schema, activeMapper, component }) => {
170
206
< Box display = "flex" className = { classes . box } >
171
207
< Card style = { { minHeight : 500 } } square >
172
208
< CardContent >
173
- < Typography component = "h3" > Options</ Typography >
174
209
< Table >
175
210
< TableHead >
176
211
< TableRow >
@@ -217,7 +252,17 @@ const ComponentExample = ({ variants, schema, activeMapper, component }) => {
217
252
</ Tabs >
218
253
</ div >
219
254
< div className = { classes . spinnerCheat } >
220
- < div id = "code-target" > </ div >
255
+ < Tabs
256
+ hidden = { ! availableVariants }
257
+ value = { variant }
258
+ className = { clsx ( availableVariants && classes . variantTabs , availableVariants ? 'longer' : classes . hidden ) }
259
+ onChange = { handleVariantChange }
260
+ >
261
+ { ( availableVariants || [ ] ) . map ( ( variant ) => (
262
+ < Tab label = { variant . label } value = { variant . value } key = { variant . value } />
263
+ ) ) }
264
+ </ Tabs >
265
+ < div id = "code-target" className = "pepa" > </ div >
221
266
< div className = { classes . spinner } >
222
267
< CircularProgress color = "secondary" size = { 80 } />
223
268
</ div >
@@ -258,7 +303,8 @@ ComponentExample.propTypes = {
258
303
type : PropTypes . string . isRequired ,
259
304
required : PropTypes . bool
260
305
} )
261
- ) . isRequired
306
+ ) . isRequired ,
307
+ schemaVariants : PropTypes . object
262
308
} ;
263
309
264
310
export default ComponentExample ;
0 commit comments