@@ -163,15 +163,17 @@ const stringifyWithFunctions = (string) =>
163
163
. replace ( / " < F U N C T I O N / g, '' )
164
164
. replace ( / F U N C T I O N > " / g, '' ) ;
165
165
166
- const ComponentExample = ( { variants, schema, activeMapper, component, schemaVariants } ) => {
167
- const [ variant , setVariant ] = useState ( 'basic' ) ;
166
+ const ComponentExample = ( { variants, schema, activeMapper, component, schemaVariants, activeSchema } ) => {
168
167
const [ expanded , setExpanded ] = useState ( true ) ;
169
168
170
169
const { pathname, push } = useRouter ( ) ;
171
170
const classes = useStyles ( ) ;
172
171
173
172
const availableVariants = schemaVariants ?. [ activeMapper ] ;
174
- const selectedSchema = availableVariants ?. find ( ( { value } ) => value === variant ) ?. schema || schema ;
173
+ const selectedSchema =
174
+ availableVariants ?. find ( ( { value } ) => value === activeSchema ) ?. schema ||
175
+ availableVariants ?. find ( ( { value } ) => value === 'basic' ) ?. schema ||
176
+ schema ;
175
177
const basicConfiguration = {
176
178
...project ,
177
179
dependencies : metadata [ activeMapper ] . dependencies ,
@@ -185,35 +187,23 @@ const ComponentExample = ({ variants, schema, activeMapper, component, schemaVar
185
187
const basicEditorSettings = { height : '100%' , hideNavigation : true , forceEmbedLayout : true , openFile : 'schema.js' } ;
186
188
187
189
useEffect ( ( ) => {
188
- if ( availableVariants && ! availableVariants . map ( ( { value } ) => value ) . includes ( variant ) ) {
189
- setVariant ( ' basic' ) ;
190
+ if ( activeSchema && ! availableVariants ?. find ( ( { value } ) => value === activeSchema ) ) {
191
+ push ( ` ${ pathname } ?mapper= ${ activeMapper } &schema= basic` ) ;
190
192
}
191
193
192
194
sdk . embedProject ( 'code-target' , basicConfiguration , basicEditorSettings ) ;
193
- } , [ activeMapper , schema ] ) ;
194
-
195
- const handleVariantChange = ( _e , newVariant ) => {
196
- setVariant ( newVariant ) ;
197
-
198
- const schema = availableVariants . find ( ( { value } ) => value === newVariant ) . schema ;
199
-
200
- sdk . embedProject (
201
- 'code-target' ,
202
- { ...basicConfiguration , files : { ...basicConfiguration . files , 'schema.js' : `export default ${ stringifyWithFunctions ( schema ) } ;` } } ,
203
- basicEditorSettings
204
- ) ;
205
- } ;
195
+ } , [ activeMapper , schema , activeSchema ] ) ;
206
196
207
197
const renderMapperTabs = ( ) =>
208
198
avalableMappers . map ( ( { title, mapper } ) => (
209
199
< Tab
210
200
key = { mapper }
211
201
value = { mapper }
212
- onClick = { ( ) => push ( `${ pathname } ?mapper=${ mapper } ` ) }
202
+ onClick = { ( ) => push ( `${ pathname } ?mapper=${ mapper } ${ activeSchema ? `&schema= ${ activeSchema } ` : '' } ` ) }
213
203
className = { clsx ( classes . tab , { active : activeMapper === mapper } ) }
214
204
label = {
215
- < Link href = { `${ pathname } ?mapper=${ mapper } ` } >
216
- < a href = { `${ pathname } ?mapper=${ mapper } ` } className = { classes . tabLink } >
205
+ < Link href = { `${ pathname } ?mapper=${ mapper } ${ activeSchema ? `&schema= ${ activeSchema } ` : '' } ` } >
206
+ < a href = { `${ pathname } ?mapper=${ mapper } ${ activeSchema ? `&schema= ${ activeSchema } ` : '' } ` } className = { classes . tabLink } >
217
207
{ title }
218
208
</ a >
219
209
</ Link >
@@ -292,12 +282,22 @@ const ComponentExample = ({ variants, schema, activeMapper, component, schemaVar
292
282
< div className = { classes . spinnerCheat } >
293
283
< Tabs
294
284
hidden = { ! availableVariants }
295
- value = { variant }
285
+ value = { activeSchema || 'basic' }
296
286
className = { clsx ( availableVariants && classes . variantTabs , availableVariants ? 'longer' : classes . hidden ) }
297
- onChange = { handleVariantChange }
298
287
>
299
288
{ ( availableVariants || [ ] ) . map ( ( variant ) => (
300
- < Tab label = { variant . label } value = { variant . value } key = { variant . value } />
289
+ < Tab
290
+ onClick = { ( ) => push ( `${ pathname } ?mapper=${ activeMapper } &schema=${ variant . value } ` ) }
291
+ label = {
292
+ < Link href = { `${ pathname } ?mapper=${ activeMapper } &schema=${ variant . value } ` } >
293
+ < a href = { `${ pathname } ?mapper=${ activeMapper } &schema=${ variant . value } ` } className = { classes . tabLink } >
294
+ { variant . label }
295
+ </ a >
296
+ </ Link >
297
+ }
298
+ value = { variant . value }
299
+ key = { variant . value }
300
+ />
301
301
) ) }
302
302
</ Tabs >
303
303
< div id = "code-target" className = "pepa" > </ div >
@@ -342,7 +342,8 @@ ComponentExample.propTypes = {
342
342
required : PropTypes . bool
343
343
} )
344
344
) . isRequired ,
345
- schemaVariants : PropTypes . object
345
+ schemaVariants : PropTypes . object ,
346
+ activeSchema : PropTypes . string
346
347
} ;
347
348
348
349
export default ComponentExample ;
0 commit comments