Skip to content

Commit b3b3c93

Browse files
committed
feat(mui): move mui wizard from demo to mapper
1 parent e41833e commit b3b3c93

File tree

8 files changed

+445
-85
lines changed

8 files changed

+445
-85
lines changed

packages/mui-component-mapper/src/components/component-mapper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import DatePicker from './time-picker';
1010
import PlainText from './plain-text';
1111
import Select from './select';
1212
import Radio from './radio';
13+
import Wizard from './wizard';
1314

1415
export const components = {
1516
TextField,
@@ -21,7 +22,8 @@ export const components = {
2122
DatePicker,
2223
TimePicker,
2324
PlainText,
24-
SubForm
25+
SubForm,
26+
Wizard
2527
};
2628

2729
const componentMapper = {
@@ -35,7 +37,8 @@ const componentMapper = {
3537
[componentTypes.DATE_PICKER]: DatePicker,
3638
[componentTypes.TIME_PICKER]: TimePicker,
3739
[componentTypes.SWITCH]: Switch,
38-
[componentTypes.PLAIN_TEXT]: PlainText
40+
[componentTypes.PLAIN_TEXT]: PlainText,
41+
[componentTypes.WIZARD]: Wizard
3942
};
4043

4144
export default componentMapper;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React, { useState, cloneElement } from 'react';
2+
import PropTypes from 'prop-types';
3+
import WizardStep from './wizard/wizard-step';
4+
import Grid from '@material-ui/core/Grid';
5+
import Typography from '@material-ui/core/Typography';
6+
import { useFormApi } from '@data-driven-forms/react-form-renderer';
7+
8+
const Wizard = ({ fields, title, description }) => {
9+
const [activeStep, setActiveStep] = useState(fields[0].name);
10+
const [prevSteps, setPrevSteps] = useState([]);
11+
12+
const formOptions = useFormApi();
13+
14+
const handleNext = (nextStep) => {
15+
setPrevSteps([...prevSteps, activeStep]);
16+
setActiveStep(nextStep);
17+
};
18+
19+
const handlePrev = () => {
20+
setActiveStep(prevSteps[prevSteps.length - 1]);
21+
22+
const newSteps = prevSteps;
23+
newSteps.pop();
24+
setPrevSteps(newSteps);
25+
};
26+
27+
const findCurrentStep = (activeStep) => fields.find(({ name }) => name === activeStep);
28+
29+
const findActiveFields = (visitedSteps) =>
30+
visitedSteps.map((key) => findCurrentStep(key).fields.map(({ name }) => name)).reduce((acc, curr) => curr.concat(acc.map((item) => item)), []);
31+
32+
const getValues = (values, visitedSteps) =>
33+
Object.keys(values)
34+
.filter((key) => findActiveFields(visitedSteps).includes(key))
35+
.reduce((acc, curr) => ({ ...acc, [curr]: values[curr] }), {});
36+
37+
const handleSubmit = () => formOptions.onSubmit(getValues(formOptions.getState().values, [...prevSteps, activeStep]));
38+
39+
const currentStep = (
40+
<WizardStep
41+
{...findCurrentStep(activeStep)}
42+
formOptions={{
43+
...formOptions,
44+
handleSubmit
45+
}}
46+
/>
47+
);
48+
49+
return (
50+
<Grid container spacing={6}>
51+
<Grid item xs={12}>
52+
<Typography component="h3">{title}</Typography>
53+
<Typography paragraph>{description}</Typography>
54+
<Typography component="h5">{`Step ${prevSteps.length + 1}`}</Typography>
55+
</Grid>
56+
<Grid item xs={12}>
57+
{cloneElement(currentStep, {
58+
handleNext,
59+
handlePrev,
60+
disableBack: prevSteps.length === 0
61+
})}
62+
</Grid>
63+
</Grid>
64+
);
65+
};
66+
67+
Wizard.propTypes = {
68+
title: PropTypes.node,
69+
description: PropTypes.node,
70+
fields: PropTypes.arrayOf(
71+
PropTypes.shape({
72+
name: PropTypes.string
73+
})
74+
).isRequired
75+
};
76+
77+
export default Wizard;
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import PropTypes from 'prop-types';
33
import WizardStepButtons from './step-buttons';
44
import Typography from '@material-ui/core/Typography';
55

6-
const WizardStep = ({ title, description, fields, formOptions, ...rest }) => {
7-
return (
8-
<Fragment>
9-
<Typography component="h5">{title}</Typography>
10-
<Typography paragraph>{description}</Typography>
11-
{fields.map((item) => formOptions.renderForm([item], formOptions))}
12-
<WizardStepButtons formOptions={formOptions} {...rest} />
13-
</Fragment>
14-
);
15-
};
6+
const WizardStep = ({ title, description, fields, formOptions, ...rest }) => (
7+
<Fragment>
8+
<Typography component="h5">{title}</Typography>
9+
<Typography paragraph>{description}</Typography>
10+
{fields.map((item) => formOptions.renderForm([item], formOptions))}
11+
<WizardStepButtons formOptions={formOptions} {...rest} />
12+
</Fragment>
13+
);
1614

1715
WizardStep.propTypes = {
1816
title: PropTypes.string,

0 commit comments

Comments
 (0)