Skip to content

Commit 5758020

Browse files
authored
Merge pull request #170 from rvsia/refactor_pf4_wizard
fix(pf4wizard): refactor and fix number logic
2 parents f09a379 + 6e1df03 commit 5758020

File tree

2 files changed

+61
-42
lines changed

2 files changed

+61
-42
lines changed

packages/pf4-component-mapper/src/form-fields/wizard/wizard.js

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Wizard extends React.Component {
2626
prevSteps: [],
2727
activeStepIndex: 0,
2828
maxStepIndex: 0,
29-
isDynamic, // wizard contains nextStep mapper
29+
isDynamic,
3030
navSchema: this.createSchema({ currentIndex: 0, isDynamic }),
3131
loading: true,
3232
};
@@ -47,15 +47,19 @@ class Wizard extends React.Component {
4747
}
4848
}
4949

50-
handleNext = (nextStep, getRegisteredFields) =>
50+
handleNext = (nextStep, getRegisteredFields) => {
51+
const newActiveIndex = this.state.activeStepIndex + 1;
52+
const shouldInsertStepIntoHistory = this.state.prevSteps.includes(this.state.activeStep);
53+
5154
this.setState(prevState => ({
5255
registeredFieldsHistory: { ...prevState.registeredFieldsHistory, [prevState.activeStep]: getRegisteredFields() },
5356
activeStep: nextStep,
54-
prevSteps: prevState.prevSteps.includes(prevState.activeStep) ? prevState.prevSteps : [ ...prevState.prevSteps, prevState.activeStep ],
55-
activeStepIndex: prevState.activeStepIndex + 1,
56-
maxStepIndex: (prevState.activeStepIndex + 1) > prevState.maxStepIndex ? prevState.maxStepIndex + 1 : prevState.maxStepIndex,
57-
navSchema: this.state.isDynamic ? this.createSchema({ currentIndex: prevState.activeStepIndex + 1 }) : prevState.navSchema,
57+
prevSteps: shouldInsertStepIntoHistory ? prevState.prevSteps : [ ...prevState.prevSteps, prevState.activeStep ],
58+
activeStepIndex: newActiveIndex,
59+
maxStepIndex: newActiveIndex > prevState.maxStepIndex ? newActiveIndex : prevState.maxStepIndex,
60+
navSchema: this.state.isDynamic ? this.createSchema({ currentIndex: newActiveIndex }) : prevState.navSchema,
5861
}));
62+
}
5963

6064
handlePrev = () => this.jumpToStep(this.state.activeStepIndex - 1);
6165

@@ -78,45 +82,59 @@ class Wizard extends React.Component {
7882

7983
findCurrentStep = activeStep => this.props.fields.find(({ stepKey }) => stepKey === activeStep)
8084

81-
// jumping in the wizzard by clicking on nav links
8285
jumpToStep = (index, valid) => {
83-
if (this.state.prevSteps[index]) {
84-
this.setState((prevState) =>
85-
({
86+
const clickOnPreviousStep = this.state.prevSteps[index];
87+
if (clickOnPreviousStep) {
88+
let originalActiveStep;
89+
this.setState((prevState) => {
90+
const includeActiveStep = prevState.prevSteps.includes(prevState.activeStep);
91+
originalActiveStep = prevState.activeStep;
92+
93+
return ({
8694
activeStep: this.state.prevSteps[index],
87-
prevSteps: prevState.prevSteps.includes(prevState.activeStep) ? prevState.prevSteps : [ ...prevState.prevSteps, prevState.activeStep ],
95+
prevSteps: includeActiveStep ? prevState.prevSteps : [ ...prevState.prevSteps, prevState.activeStep ],
8896
activeStepIndex: index,
89-
}));
90-
91-
const currentStep = this.findCurrentStep(this.state.prevSteps[index]);
92-
const currentStepHasStepMapper = typeof currentStep.nextStep === 'object';
93-
94-
if (this.state.isDynamic && (currentStepHasStepMapper || !this.props.predictSteps)) {
95-
this.setState((prevState) => ({
96-
navSchema: prevState.navSchema.slice(0, index + 1),
97-
prevSteps: prevState.prevSteps.slice(0, index + 1),
98-
maxStepIndex: prevState.prevSteps.slice(0, index + 1).length,
99-
}));
100-
}
101-
102-
if (currentStep.disableForwardJumping) {
103-
this.setState((prevState) => ({
104-
prevSteps: prevState.prevSteps.slice(0, index + 1),
105-
maxStepIndex: prevState.prevSteps.slice(0, index).length,
106-
}));
107-
}
108-
109-
// invalid state disables jumping forward until it fixed (!)
110-
if (valid === false) {
111-
this.setState((prevState) => ({
112-
prevSteps: prevState.prevSteps.slice(0, index + 2),
113-
maxStepIndex: prevState.prevSteps.slice(0, index + 1).length,
114-
}));
115-
}
97+
});
98+
}, () => this.setState((prevState) => {
99+
const INDEXING_BY_ZERO = 1;
100+
101+
let newState;
102+
103+
const currentStep = this.findCurrentStep(prevState.prevSteps[index]);
104+
const currentStepHasStepMapper = typeof currentStep.nextStep === 'object';
105+
106+
const dynamicStepShouldDisableNav = prevState.isDynamic && (currentStepHasStepMapper || !this.props.predictSteps);
107+
108+
if (dynamicStepShouldDisableNav) {
109+
const newPrevSteps = prevState.prevSteps.slice(0, index);
110+
newState = {
111+
navSchema: this.props.predictSteps ? this.createSchema({ currentIndex: index }) : prevState.navSchema.slice(0, index + INDEXING_BY_ZERO),
112+
prevSteps: newPrevSteps,
113+
maxStepIndex: newPrevSteps.length,
114+
};
115+
} else if (currentStep.disableForwardJumping) {
116+
const newPrevSteps = prevState.prevSteps.slice(0, index);
117+
newState = {
118+
prevSteps: newPrevSteps,
119+
maxStepIndex: newPrevSteps.length,
120+
};
121+
}
122+
123+
if (valid === false) {
124+
const indexOfCurrentStep = prevState.prevSteps.indexOf(originalActiveStep);
125+
126+
newState = {
127+
...newState,
128+
prevSteps: prevState.prevSteps.slice(0, indexOfCurrentStep + INDEXING_BY_ZERO),
129+
maxStepIndex: prevState.prevSteps.slice(0, indexOfCurrentStep + INDEXING_BY_ZERO).length - INDEXING_BY_ZERO,
130+
};
131+
}
132+
133+
return newState;
134+
}));
116135
}
117136
};
118137

119-
// builds schema used for generating of the navigation links
120138
createSchema = ({ currentIndex, isDynamic }) => {
121139
if (typeof isDynamic === 'undefined'){
122140
isDynamic = this.state.isDynamic;

packages/pf4-component-mapper/src/tests/wizard/wizard.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ describe('<Wizard />', () => {
571571
expect(wrapper.find('WizardNavItem').at(2).text()).toEqual(THIRD_TITLE);
572572
});
573573

574-
it('reset nav when jumped into compileMapper step', () => {
574+
it('disable nav when jumped into compileMapper step', () => {
575575
const wrapper = mount(<FormRenderer
576576
schema={ wizardSchema }
577577
formFieldsMapper={ formFieldsMapper }
@@ -588,8 +588,9 @@ describe('<Wizard />', () => {
588588

589589
backButtonClick(wrapper);
590590

591-
expect(wrapper.find('WizardNavItem')).toHaveLength(1);
592-
expect(wrapper.find('WizardNavItem').at(0).text()).toEqual(FIRST_TITLE);
591+
expect(wrapper.find('WizardNavItem').at(0).props().isDisabled).toEqual(false);
592+
expect(wrapper.find('WizardNavItem').at(1).props().isDisabled).toEqual(true);
593+
expect(wrapper.find('WizardNavItem').at(2).props().isDisabled).toEqual(true);
593594
});
594595

595596
it('disable nav when jumped into disableForwardJumping step', () => {

0 commit comments

Comments
 (0)