1- import React , { useEffect } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { useRouter } from 'next/router' ;
33import CircularProgress from '@material-ui/core/CircularProgress' ;
44import { makeStyles } from '@material-ui/core/styles' ;
55import PropTypes from 'prop-types' ;
6- import Typography from '@material-ui/core/Typography' ;
76import Card from '@material-ui/core/Card' ;
87import CardContent from '@material-ui/core/CardContent' ;
98import sdk from '@stackblitz/sdk' ;
@@ -95,7 +94,10 @@ const useStyles = makeStyles((theme) => ({
9594 spinnerCheat : {
9695 flex : 1 ,
9796 position : 'relative' ,
98- boxShadow : theme . shadows [ 1 ]
97+ boxShadow : theme . shadows [ 1 ] ,
98+ '& .longer + #code-target' : {
99+ maxHeight : 'calc(100% - 49px)'
100+ }
99101 } ,
100102 spinner : {
101103 position : 'absolute' ,
@@ -123,28 +125,62 @@ const useStyles = makeStyles((theme) => ({
123125 } ,
124126 alert : {
125127 marginBottom : 8
128+ } ,
129+ variantTabs : {
130+ height : 49 ,
131+ background : '#eaeaea'
132+ } ,
133+ hidden : {
134+ height : 0 ,
135+ minHeight : 0
126136 }
127137} ) ) ;
128138
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+
130148 const { pathname, push } = useRouter ( ) ;
131149 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+
132165 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+
133178 sdk . embedProject (
134179 '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
146182 ) ;
147- } , [ activeMapper , schema ] ) ;
183+ } ;
148184
149185 const renderMapperTabs = ( ) =>
150186 avalableMappers . map ( ( { title, mapper } ) => (
@@ -170,7 +206,6 @@ const ComponentExample = ({ variants, schema, activeMapper, component }) => {
170206 < Box display = "flex" className = { classes . box } >
171207 < Card style = { { minHeight : 500 } } square >
172208 < CardContent >
173- < Typography component = "h3" > Options</ Typography >
174209 < Table >
175210 < TableHead >
176211 < TableRow >
@@ -217,7 +252,17 @@ const ComponentExample = ({ variants, schema, activeMapper, component }) => {
217252 </ Tabs >
218253 </ div >
219254 < 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 >
221266 < div className = { classes . spinner } >
222267 < CircularProgress color = "secondary" size = { 80 } />
223268 </ div >
@@ -258,7 +303,8 @@ ComponentExample.propTypes = {
258303 type : PropTypes . string . isRequired ,
259304 required : PropTypes . bool
260305 } )
261- ) . isRequired
306+ ) . isRequired ,
307+ schemaVariants : PropTypes . object
262308} ;
263309
264310export default ComponentExample ;
0 commit comments