Skip to content

Commit c19c7ff

Browse files
authored
Merge pull request #569 from rvsia/refactorWizards
feat(common): wizard - use context to share props
2 parents 5fb3b44 + 537c015 commit c19c7ff

File tree

21 files changed

+266
-776
lines changed

21 files changed

+266
-776
lines changed

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import PropTypes from 'prop-types';
33
import WizardCommon from '@data-driven-forms/common/src/wizard/wizard';
4-
import { FormSpy } from '@data-driven-forms/react-form-renderer';
4+
import { FormSpy, WizardContext } from '@data-driven-forms/react-form-renderer';
55

66
import StepButtons from './wizard/step-buttons';
77

8-
const WizardInternal = ({ currentStep, formOptions, onKeyDown, WizardProps, ...props }) => (
9-
<div onKeyDown={onKeyDown} {...WizardProps}>
10-
{currentStep.fields.map((item) => formOptions.renderForm([item], formOptions))}
11-
<FormSpy subscription={{ valid: true, submitting: true, validating: true }}>
12-
{({ valid, submitting, validating }) => (
13-
<StepButtons isNextDisabled={!valid || submitting || validating} {...props} currentStep={currentStep} formOptions={formOptions} />
14-
)}
15-
</FormSpy>
16-
</div>
17-
);
8+
const WizardInternal = ({ WizardProps, ...props }) => {
9+
const { formOptions, currentStep, onKeyDown, ...rest } = useContext(WizardContext);
10+
11+
return (
12+
<div onKeyDown={onKeyDown} {...WizardProps}>
13+
{currentStep.fields.map((item) => formOptions.renderForm([item], formOptions))}
14+
<FormSpy subscription={{ valid: true, submitting: true, validating: true }}>
15+
{({ valid, submitting, validating }) => (
16+
<StepButtons isNextDisabled={!valid || submitting || validating} {...props} {...rest} currentStep={currentStep} formOptions={formOptions} />
17+
)}
18+
</FormSpy>
19+
</div>
20+
);
21+
};
1822

1923
WizardInternal.propTypes = {
20-
currentStep: PropTypes.object,
21-
onKeyDown: PropTypes.func,
22-
formOptions: PropTypes.shape({
23-
onCancel: PropTypes.func,
24-
renderForm: PropTypes.func,
25-
getState: PropTypes.func
26-
}),
2724
WizardProps: PropTypes.object
2825
};
2926

packages/common/src/wizard/reducer.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ const handleNext = (state, nextStep, formOptions, fields) => {
5353
maxStepIndex: newActiveIndex > state.maxStepIndex ? newActiveIndex : state.maxStepIndex,
5454
navSchema: state.isDynamic
5555
? createSchema({
56-
...state,
5756
fields,
58-
formOptions,
59-
currentIndex: newActiveIndex
57+
formOptions
6058
})
6159
: state.navSchema
6260
};
@@ -103,10 +101,8 @@ const jumpToStep = (state, index, valid, fields, crossroads, formOptions) => {
103101
updatedState = {
104102
...updatedState,
105103
navSchema: createSchema({
106-
...updatedState,
107104
formOptions,
108-
fields,
109-
currentIndex: index
105+
fields
110106
}),
111107
prevSteps: newState.prevSteps.slice(0, index),
112108
maxStepIndex: index
@@ -138,10 +134,8 @@ const reducer = (state, { type, payload }) => {
138134
...state,
139135
loading: false,
140136
navSchema: createSchema({
141-
...state,
142137
fields: payload.fields,
143-
formOptions: payload.formOptions,
144-
currentIndex: 0
138+
formOptions: payload.formOptions
145139
})
146140
};
147141
case 'handleNext':
@@ -152,10 +146,8 @@ const reducer = (state, { type, payload }) => {
152146
prevSteps: state.prevSteps.slice(0, state.activeStepIndex),
153147
maxStepIndex: state.activeStepIndex,
154148
navSchema: createSchema({
155-
...state,
156149
fields: payload.fields,
157-
formOptions: payload.formOptions,
158-
currentIndex: state.activeStepIndex
150+
formOptions: payload.formOptions
159151
})
160152
};
161153
case 'jumpToStep':

packages/common/src/wizard/wizard.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import React, { useReducer, useEffect } from 'react';
22
import PropTypes from 'prop-types';
3-
import { useFormApi } from '@data-driven-forms/react-form-renderer';
3+
import { useFormApi, WizardContext } from '@data-driven-forms/react-form-renderer';
44

55
import get from 'lodash/get';
66
import set from 'lodash/set';
77
import flattenDeep from 'lodash/flattenDeep';
88
import handleEnter from './enter-handler';
99
import reducer, { DYNAMIC_WIZARD_TYPES, findCurrentStep } from './reducer';
10+
import selectNext from './select-next';
1011

11-
const Wizard = ({ fields, isDynamic, crossroads, Wizard, component, ...props }) => {
12+
const Wizard = ({ fields, isDynamic, crossroads, Wizard, component, initialState, ...props }) => {
1213
const formOptions = useFormApi();
1314

1415
const [state, dispatch] = useReducer(reducer, {
1516
activeStep: fields[0].name,
1617
prevSteps: [],
1718
activeStepIndex: 0,
1819
maxStepIndex: 0,
20+
...initialState,
1921
isDynamic: isDynamic || fields.some(({ nextStep }) => DYNAMIC_WIZARD_TYPES.includes(typeof nextStep)),
2022
loading: true
2123
});
@@ -45,10 +47,13 @@ const Wizard = ({ fields, isDynamic, crossroads, Wizard, component, ...props })
4547
return finalObject;
4648
};
4749

50+
const onCancel = () => formOptions.onCancel(state);
51+
4852
const handleSubmit = () =>
4953
formOptions.onSubmit(
5054
prepareValues(formOptions.getState().values, [...state.prevSteps, state.activeStep], formOptions.getRegisteredFields),
51-
formOptions
55+
formOptions,
56+
state
5257
);
5358

5459
const jumpToStep = (index, valid) => dispatch({ type: 'jumpToStep', payload: { index, valid, fields, crossroads, formOptions } });
@@ -64,25 +69,30 @@ const Wizard = ({ fields, isDynamic, crossroads, Wizard, component, ...props })
6469
const onKeyDown = (e) => handleEnter(e, formOptions, state.activeStep, findCurrentStepWrapped, handleNext, handleSubmit);
6570

6671
return (
67-
<Wizard
68-
{...props}
69-
handleNext={handleNext}
70-
onKeyDown={onKeyDown}
71-
setPrevSteps={setPrevSteps}
72-
currentStep={findCurrentStep(state.activeStep, fields)}
73-
jumpToStep={jumpToStep}
74-
handlePrev={handlePrev}
75-
formOptions={{
76-
...formOptions,
77-
handleSubmit
72+
<WizardContext.Provider
73+
value={{
74+
handleNext,
75+
onKeyDown,
76+
setPrevSteps,
77+
currentStep: findCurrentStep(state.activeStep, fields),
78+
jumpToStep,
79+
handlePrev,
80+
formOptions: {
81+
...formOptions,
82+
onCancel,
83+
handleSubmit
84+
},
85+
navSchema: state.navSchema,
86+
activeStepIndex: state.activeStepIndex,
87+
maxStepIndex: state.maxStepIndex,
88+
isDynamic: state.isDynamic,
89+
crossroads,
90+
prevSteps: state.prevSteps,
91+
selectNext
7892
}}
79-
navSchema={state.navSchema}
80-
activeStepIndex={state.activeStepIndex}
81-
maxStepIndex={state.maxStepIndex}
82-
isDynamic={state.isDynamic}
83-
crossroads={crossroads}
84-
prevSteps={state.prevSteps}
85-
/>
93+
>
94+
<Wizard {...props} />
95+
</WizardContext.Provider>
8696
);
8797
};
8898

@@ -95,7 +105,8 @@ Wizard.propTypes = {
95105
isDynamic: PropTypes.bool,
96106
crossroads: PropTypes.arrayOf(PropTypes.string),
97107
Wizard: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
98-
component: PropTypes.any
108+
component: PropTypes.any,
109+
initialState: PropTypes.object
99110
};
100111

101112
export default Wizard;

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

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
44

5+
import { WizardContext } from '@data-driven-forms/react-form-renderer';
6+
57
import { Grid } from '@material-ui/core';
68
import { makeStyles } from '@material-ui/core/styles';
79

@@ -16,21 +18,9 @@ const useStyles = makeStyles(() => ({
1618
}
1719
}));
1820

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-
}) => {
21+
const WizardInternal = ({ buttonLabels, stepsInfo, ButtonContainerProps, StepperProps, WizardBodyProps, WizardProps }) => {
22+
const { formOptions, currentStep, handlePrev, onKeyDown, handleNext, activeStepIndex, prevSteps } = useContext(WizardContext);
23+
3424
const classes = useStyles();
3525

3626
const buttonLabelsFinal = {
@@ -61,19 +51,6 @@ const WizardInternal = ({
6151
};
6252

6353
WizardInternal.propTypes = {
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
7754
buttonLabels: PropTypes.object,
7855
stepsInfo: PropTypes.arrayOf(
7956
PropTypes.shape({

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import WizardStep from './wizard/wizard-step';
33
import PropTypes from 'prop-types';
44
import { Wizard as PfWizard, Modal, Icon } from 'patternfly-react';
5-
import Wizard, { wizardProps } from '@data-driven-forms/common/src/wizard/wizard';
5+
import { WizardContext } from '@data-driven-forms/react-form-renderer';
6+
import Wizard from '@data-driven-forms/common/src/wizard/wizard';
67

78
const defaultButtonLabels = {
89
cancel: 'Cancel',
@@ -11,7 +12,9 @@ const defaultButtonLabels = {
1112
submit: 'Submit'
1213
};
1314

14-
const WizardInternal = ({ title, buttonLabels, stepsInfo, inModal, onKeyDown, formOptions, handleNext, handlePrev, prevSteps, currentStep }) => {
15+
const WizardInternal = ({ title, buttonLabels, stepsInfo, inModal }) => {
16+
const { formOptions, currentStep, handlePrev, onKeyDown, handleNext, prevSteps } = useContext(WizardContext);
17+
1518
const renderSteps = () =>
1619
stepsInfo.map((step, stepIndex) => (
1720
<PfWizard.Step
@@ -58,14 +61,7 @@ WizardInternal.propTypes = {
5861
title: PropTypes.node,
5962
buttonLabels: PropTypes.object,
6063
stepsInfo: PropTypes.array,
61-
inModal: PropTypes.bool,
62-
...wizardProps
63-
};
64-
65-
WizardInternal.defaultProps = {
66-
title: undefined,
67-
stepsInfo: undefined,
68-
inModal: false
64+
inModal: PropTypes.bool
6965
};
7066

7167
const WizardFinal = (props) => <Wizard Wizard={WizardInternal} {...props} />;

0 commit comments

Comments
 (0)