Skip to content

Commit 801ce2b

Browse files
committed
Add a feature for multiple schema in component examples
1 parent 72cbceb commit 801ce2b

File tree

3 files changed

+74
-22
lines changed

3 files changed

+74
-22
lines changed

packages/react-renderer-demo/src/components/component-example-text.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const useStyles = makeStyles((theme) => ({
2424
}
2525
}));
2626

27-
const ComponentExampleText = ({ linkText, schema, variants }) => {
27+
const ComponentExampleText = ({ linkText, schema, variants, schemaVariants }) => {
2828
const classes = useStyles();
2929
const [activeMapper, component] = useComponentExample();
3030

@@ -34,7 +34,7 @@ const ComponentExampleText = ({ linkText, schema, variants }) => {
3434
<Heading level="4" component="h1">
3535
{`${avalableMappers.find(({ mapper }) => mapper === activeMapper)?.title} ${linkText}`}
3636
</Heading>
37-
<ComponentExample variants={variants} schema={schema} activeMapper={activeMapper} component={component} />
37+
<ComponentExample variants={variants} schema={schema} activeMapper={activeMapper} component={component} schemaVariants={schemaVariants} />
3838
<br />
3939
<AdditionalComponentText activeMapper={activeMapper} component={component} />
4040
</div>
@@ -45,7 +45,8 @@ const ComponentExampleText = ({ linkText, schema, variants }) => {
4545
ComponentExampleText.propTypes = {
4646
linkText: PropTypes.string.isRequired,
4747
schema: PropTypes.object.isRequired,
48-
variants: PropTypes.arrayOf(PropTypes.object)
48+
variants: PropTypes.arrayOf(PropTypes.object),
49+
schemaVariants: PropTypes.object
4950
};
5051

5152
ComponentExampleText.defaultProps = {

packages/react-renderer-demo/src/components/component-example.js

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { useRouter } from 'next/router';
33
import CircularProgress from '@material-ui/core/CircularProgress';
44
import { makeStyles } from '@material-ui/core/styles';
55
import PropTypes from 'prop-types';
6-
import Typography from '@material-ui/core/Typography';
76
import Card from '@material-ui/core/Card';
87
import CardContent from '@material-ui/core/CardContent';
98
import 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(/<NEWLINE>/g, '\n')
142+
.replace(/"<FUNCTION/g, '')
143+
.replace(/FUNCTION>"/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

264310
export default ComponentExample;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const updateFieldSchema = (schema, attributes) => ({
2+
fields: [{ ...schema.fields[0], ...attributes }]
3+
});
4+
5+
export default updateFieldSchema;

0 commit comments

Comments
 (0)