Skip to content

Commit cd83248

Browse files
authored
Merge pull request #1097 from rvsia/addSchemasVariantForDemo
Add schemas variant for demo
2 parents cf66cc0 + f16a758 commit cd83248

File tree

5 files changed

+103
-25
lines changed

5 files changed

+103
-25
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;

packages/react-renderer-demo/src/doc-components/examples-texts/select.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ When is `true`, the select will store only values of the selected options. Other
2222

2323
## selectAll
2424

25-
*boolean* | <Chip label="Experimental" color="secondary" />
25+
*boolean* | <Chip label="Experimental" color="secondary" component="span" />
2626

2727
When provided to an option object, this option will select all available options.
2828

@@ -40,7 +40,7 @@ options: [{
4040

4141
## selectNone
4242

43-
*boolean* | <Chip label="Experimental" color="secondary" />
43+
*boolean* | <Chip label="Experimental" color="secondary" component="span" />
4444

4545

4646
When provided to an option object, this option will clear the selection.
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;

packages/react-renderer-demo/src/pages/provided-mappers/select.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import React from 'react';
22
import ComponentText from '@docs/components/component-example-text';
33
import componentTypes from '@data-driven-forms/react-form-renderer/component-types';
44
import baseFieldProps from '../../helpers/base-field-props';
5+
import updateFieldSchema from '../../helpers/update-field-schema';
6+
7+
const loadOptions = `<FUNCTION () =>
8+
new Promise((res) =>
9+
setTimeout(
10+
() =>
11+
res([
12+
{ value: 'first-option', label: 'First async option' },
13+
{ value: 'second-option', label: 'Second async option' }
14+
]),
15+
2500
16+
)
17+
) FUNCTION>`.replace(/\n/g, '<NEWLINE>');
518

619
const schema = {
720
fields: [
@@ -19,6 +32,19 @@ const schema = {
1932
]
2033
};
2134

35+
const basicVariant = { schema, label: 'Basic', value: 'basic' };
36+
const multiVariant = { schema: updateFieldSchema(schema, { isMulti: true }), label: 'Multi', value: 'multi' };
37+
const loadOptionsVariant = { schema: updateFieldSchema(schema, { options: [], loadOptions }), label: 'Load options', value: 'load-options' };
38+
39+
const schemaVariants = {
40+
mui: [basicVariant, multiVariant, loadOptionsVariant],
41+
pf4: [basicVariant, multiVariant, loadOptionsVariant],
42+
blueprint: [basicVariant, multiVariant],
43+
suir: [basicVariant, multiVariant, loadOptionsVariant],
44+
ant: [basicVariant, multiVariant, loadOptionsVariant],
45+
carbon: [basicVariant, multiVariant, loadOptionsVariant]
46+
};
47+
2248
const variants = [
2349
...baseFieldProps,
2450
{
@@ -55,6 +81,6 @@ const variants = [
5581
}
5682
];
5783

58-
const Select = () => <ComponentText schema={schema} variants={variants} linkText="Select" />;
84+
const Select = () => <ComponentText schema={schema} variants={variants} linkText="Select" schemaVariants={schemaVariants} />;
5985

6086
export default Select;

0 commit comments

Comments
 (0)