Skip to content

Commit 38ec9cc

Browse files
authored
Merge pull request #466 from rvsia/muiWiz
Add stepper to MUI wizard
2 parents 1cd8efc + 0b398d9 commit 38ec9cc

File tree

9 files changed

+420
-178
lines changed

9 files changed

+420
-178
lines changed

.github/labeler.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,3 @@ PF4:
1818

1919
renderer:
2020
- packages/react-form-renderer/**/*
21-
22-
dependencies:
23-
- '**/package.json'
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { componentTypes, validatorTypes } from '@data-driven-forms/react-form-renderer';
2+
3+
const wizardSchema = {
4+
fields: [
5+
{
6+
component: componentTypes.WIZARD,
7+
name: 'wizzard',
8+
buttonLabels: {},
9+
title: 'Cokoliv',
10+
stepsInfo: [{ title: 'Add a source' }, { title: 'Configure a source' }, { title: 'Summary' }],
11+
fields: [
12+
{
13+
title: 'Get started with adding source',
14+
name: 'step-1',
15+
nextStep: {
16+
when: 'source-type',
17+
stepMapper: {
18+
aws: 'aws',
19+
google: 'google'
20+
}
21+
},
22+
fields: [
23+
{
24+
component: componentTypes.TEXTAREA,
25+
name: 'source-name',
26+
type: 'text',
27+
label: 'Source name'
28+
},
29+
{
30+
component: componentTypes.SELECT,
31+
name: 'source-type',
32+
label: 'Source type',
33+
isRequired: true,
34+
options: [
35+
{
36+
value: 'aws',
37+
label: 'Aws'
38+
},
39+
{
40+
value: 'google',
41+
label: 'Google'
42+
}
43+
],
44+
validate: [
45+
{
46+
type: validatorTypes.REQUIRED
47+
}
48+
]
49+
}
50+
]
51+
},
52+
{
53+
title: 'Configure AWS',
54+
name: 'aws',
55+
nextStep: 'summary',
56+
fields: [
57+
{
58+
component: componentTypes.TEXT_FIELD,
59+
name: 'aws-field',
60+
label: 'Aws field part'
61+
}
62+
]
63+
},
64+
{
65+
name: 'google',
66+
title: 'Configure google',
67+
nextStep: 'summary',
68+
fields: [
69+
{
70+
component: componentTypes.TEXT_FIELD,
71+
name: 'google-field',
72+
label: 'Google field part'
73+
}
74+
]
75+
},
76+
{
77+
fields: [
78+
{
79+
name: 'summary',
80+
component: componentTypes.TEXT_FIELD,
81+
isDisabled: true,
82+
label: 'Summary'
83+
}
84+
],
85+
name: 'summary'
86+
}
87+
]
88+
}
89+
]
90+
};
91+
92+
export default wizardSchema;

packages/mui-component-mapper/demo/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import demoSchema from '@data-driven-forms/common/src/demoschema';
1111
import fieldArraySchema from './demo-schemas/field-array-schema';
1212

1313
import Button from '@material-ui/core/Button';
14+
import wizardSchema from './demo-schemas/wizard-schema';
1415

1516
const theme = createMuiTheme({
1617
typography: {
@@ -29,7 +30,7 @@ const compositeMapper = {
2930
};
3031

3132
const App = () => {
32-
const [schema, setSchema] = useState(fieldArraySchema);
33+
const [schema, setSchema] = useState(wizardSchema);
3334

3435
return (
3536
<ThemeProvider theme={theme}>
@@ -40,6 +41,7 @@ const App = () => {
4041
<Grid item xs={12}>
4142
<Button onClick={() => setSchema(demoSchema)}>Demo schema</Button>
4243
<Button onClick={() => setSchema(fieldArraySchema)}>Field array</Button>
44+
<Button onClick={() => setSchema(wizardSchema)}>Wizard</Button>
4345
</Grid>
4446
<Grid item xs={12}>
4547
<FormRenderer

packages/mui-component-mapper/src/files/wizard.js

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
1-
import React, { cloneElement } from 'react';
1+
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import WizardStep from './wizard/wizard-step';
4-
import { Grid, Typography } from '@material-ui/core';
5-
import Wizard, { wizardProps } from '@data-driven-forms/common/src/wizard/wizard';
3+
import clsx from 'clsx';
4+
5+
import { Grid } from '@material-ui/core';
6+
import { makeStyles } from '@material-ui/core/styles';
7+
8+
import Wizard from '@data-driven-forms/common/src/wizard/wizard';
9+
import WizardNav from './wizard/wizard-nav';
10+
import WizardStepButtons from './wizard/step-buttons';
11+
12+
const useStyles = makeStyles(() => ({
13+
wizardBody: {
14+
padding: 24,
15+
margin: 0
16+
}
17+
}));
18+
19+
const WizardInternal = ({
20+
currentStep,
21+
formOptions,
22+
activeStepIndex,
23+
prevSteps,
24+
handleNext,
25+
handlePrev,
26+
buttonLabels,
27+
stepsInfo,
28+
ButtonContainerProps,
29+
StepperProps,
30+
WizardBodyProps,
31+
WizardProps,
32+
onKeyDown
33+
}) => {
34+
const classes = useStyles();
635

7-
const WizardInternal = ({ title, description, currentStep, formOptions, prevSteps, handleNext, handlePrev, buttonLabels }) => {
836
const buttonLabelsFinal = {
937
next: 'Continue',
1038
submit: 'Submit',
@@ -13,30 +41,56 @@ const WizardInternal = ({ title, description, currentStep, formOptions, prevStep
1341
...buttonLabels
1442
};
1543

16-
const step = <WizardStep {...currentStep} formOptions={formOptions} buttonLabels={buttonLabelsFinal} />;
17-
1844
return (
19-
<Grid container spacing={6}>
20-
<Grid item xs={12}>
21-
<Typography component="h3">{title}</Typography>
22-
<Typography paragraph>{description}</Typography>
23-
<Typography component="h5">{`Step ${prevSteps.length + 1}`}</Typography>
24-
</Grid>
25-
<Grid item xs={12}>
26-
{cloneElement(step, {
27-
handleNext,
28-
handlePrev,
29-
disableBack: prevSteps.length === 0
30-
})}
45+
<Grid container spacing={3} {...WizardProps} onKeyDown={onKeyDown}>
46+
{stepsInfo && <WizardNav StepperProps={StepperProps} stepsInfo={stepsInfo} activeStepIndex={activeStepIndex} />}
47+
<Grid container spacing={2} {...WizardBodyProps} className={clsx(classes.wizardBody, WizardBodyProps.className)}>
48+
{currentStep.fields.map((item) => formOptions.renderForm([item], formOptions))}
49+
<WizardStepButtons
50+
{...currentStep}
51+
formOptions={formOptions}
52+
buttonLabels={buttonLabelsFinal}
53+
handleNext={handleNext}
54+
handlePrev={handlePrev}
55+
disableBack={prevSteps.length === 0}
56+
ButtonContainerProps={ButtonContainerProps}
57+
/>
3158
</Grid>
3259
</Grid>
3360
);
3461
};
3562

3663
WizardInternal.propTypes = {
37-
title: PropTypes.node,
38-
description: PropTypes.node,
39-
...wizardProps
64+
currentStep: PropTypes.object,
65+
handlePrev: PropTypes.func,
66+
onKeyDown: PropTypes.func,
67+
jumpToStep: PropTypes.func,
68+
setPrevSteps: PropTypes.func,
69+
handleNext: PropTypes.func,
70+
activeStepIndex: PropTypes.number,
71+
formOptions: PropTypes.shape({
72+
onCancel: PropTypes.func,
73+
renderForm: PropTypes.func
74+
}),
75+
prevSteps: PropTypes.array,
76+
// ^^ common props
77+
buttonLabels: PropTypes.object,
78+
stepsInfo: PropTypes.arrayOf(
79+
PropTypes.shape({
80+
title: PropTypes.node,
81+
label: PropTypes.node,
82+
StepLabelProps: PropTypes.object,
83+
StepProps: PropTypes.object
84+
})
85+
),
86+
ButtonContainerProps: PropTypes.object,
87+
StepperProps: PropTypes.object,
88+
WizardBodyProps: PropTypes.object,
89+
WizardProps: PropTypes.object
90+
};
91+
92+
WizardInternal.defaultProps = {
93+
WizardBodyProps: {}
4094
};
4195

4296
const MuiWizard = (props) => <Wizard Wizard={WizardInternal} {...props} />;

0 commit comments

Comments
 (0)