Skip to content

Commit 309af84

Browse files
committed
fix(pf3): wizard use common wizard
1 parent 88e308e commit 309af84

File tree

7 files changed

+3341
-1301
lines changed

7 files changed

+3341
-1301
lines changed

packages/common/src/wizard/wizard.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const Wizard = ({ fields, isDynamic, crossroads, Wizard, ...props }) => {
8181
maxStepIndex={state.maxStepIndex}
8282
isDynamic={state.isDynamic}
8383
crossroads={crossroads}
84+
prevSteps={state.prevSteps}
8485
/>
8586
);
8687
};
@@ -97,3 +98,19 @@ Wizard.propTypes = {
9798
};
9899

99100
export default Wizard;
101+
102+
export const wizardProps = {
103+
currentStep: PropTypes.object,
104+
handlePrev: PropTypes.func,
105+
onKeyDown: PropTypes.func,
106+
jumpToStep: PropTypes.func,
107+
setPrevSteps: PropTypes.func,
108+
handleNext: PropTypes.func,
109+
navSchema: PropTypes.array,
110+
activeStepIndex: PropTypes.number,
111+
maxStepIndex: PropTypes.number,
112+
formOptions: PropTypes.shape({
113+
onCancel: PropTypes.func
114+
}),
115+
prevSteps: PropTypes.array
116+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const selectSchema = {
104104
}
105105

106106
const App = () => {
107-
const [schema, setSchema] = useState(sandbox)
107+
const [schema, setSchema] = useState(wizardSchema)
108108

109109
return (
110110
<div>
Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import React, { useState } from 'react';
1+
import React 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 handleEnter from '@data-driven-forms/common/src/wizard/enter-handler';
6-
import { useFormApi } from '@data-driven-forms/react-form-renderer';
5+
import Wizard, { wizardProps } from '@data-driven-forms/common/src/wizard/wizard';
76

87
const defaultButtonLabels = {
98
cancel: 'Cancel',
@@ -12,37 +11,7 @@ const defaultButtonLabels = {
1211
submit: 'Submit'
1312
};
1413

15-
const Wizard = ({ title, buttonLabels, stepsInfo, fields, inModal }) => {
16-
const formOptions = useFormApi();
17-
18-
const [activeStep, setActiveStep] = useState(fields[0].name);
19-
const [prevSteps, setPrevSteps] = useState([]);
20-
21-
const handleNext = (nextStep) => {
22-
setPrevSteps([...prevSteps, activeStep]);
23-
setActiveStep(nextStep);
24-
};
25-
26-
const handlePrev = () => {
27-
setActiveStep(prevSteps[prevSteps.length - 1]);
28-
29-
const newSteps = prevSteps;
30-
newSteps.pop();
31-
setPrevSteps(newSteps);
32-
};
33-
34-
const findCurrentStep = (activeStep) => fields.find(({ name }) => name === activeStep);
35-
36-
const findActiveFields = (visitedSteps) =>
37-
visitedSteps.map((key) => findCurrentStep(key).fields.map(({ name }) => name)).reduce((acc, curr) => curr.concat(acc.map((item) => item)), []);
38-
39-
const getValues = (values, visitedSteps) =>
40-
Object.keys(values)
41-
.filter((key) => findActiveFields(visitedSteps).includes(key))
42-
.reduce((acc, curr) => ({ ...acc, [curr]: values[curr] }), {});
43-
44-
const handleSubmit = () => formOptions.onSubmit(getValues(formOptions.getState().values, [...prevSteps, activeStep]));
45-
14+
const WizardInternal = ({ title, buttonLabels, stepsInfo, inModal, onKeyDown, formOptions, handleNext, handlePrev, prevSteps, currentStep }) => {
4615
const renderSteps = () =>
4716
stepsInfo.map((step, stepIndex) => (
4817
<PfWizard.Step
@@ -61,7 +30,7 @@ const Wizard = ({ title, buttonLabels, stepsInfo, fields, inModal }) => {
6130
};
6231

6332
return (
64-
<div onKeyDown={(e) => handleEnter(e, formOptions, activeStep, findCurrentStep, handleNext, handleSubmit)}>
33+
<div onKeyDown={onKeyDown}>
6534
{title && (
6635
<Modal.Header>
6736
{inModal && (
@@ -78,32 +47,27 @@ const Wizard = ({ title, buttonLabels, stepsInfo, fields, inModal }) => {
7847
handlePrev={handlePrev}
7948
disableBack={prevSteps.length === 0}
8049
buttonLabels={fullButtonLabels}
81-
{...findCurrentStep(activeStep)}
82-
formOptions={{
83-
...formOptions,
84-
handleSubmit
85-
}}
50+
{...currentStep}
51+
formOptions={formOptions}
8652
/>
8753
</div>
8854
);
8955
};
9056

91-
Wizard.propTypes = {
57+
WizardInternal.propTypes = {
9258
title: PropTypes.string,
9359
buttonLabels: PropTypes.object,
9460
stepsInfo: PropTypes.array,
9561
inModal: PropTypes.bool,
96-
fields: PropTypes.arrayOf(
97-
PropTypes.shape({
98-
name: PropTypes.string
99-
})
100-
).isRequired
62+
...wizardProps
10163
};
10264

103-
Wizard.defaultProps = {
65+
WizardInternal.defaultProps = {
10466
title: undefined,
10567
stepsInfo: undefined,
10668
inModal: false
10769
};
10870

109-
export default Wizard;
71+
const WizardFinal = (props) => <Wizard Wizard={WizardInternal} {...props} />;
72+
73+
export default WizardFinal;

0 commit comments

Comments
 (0)