Skip to content

Commit ee5bd97

Browse files
committed
Add backwards compatibility props to wizard
1 parent c8ea497 commit ee5bd97

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

packages/pf4-component-mapper/demo/demo-schemas/wizard-schema.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const wizardSchema = {
3232
fields: [{
3333
component: componentTypes.WIZARD,
3434
name: 'wizzard',
35+
predictSteps: true,
3536
//inModal: true,
3637
title: 'Title',
3738
showTitles: true,
@@ -206,9 +207,10 @@ export const wizardSchemaMoreSubsteps = {
206207
component: componentTypes.WIZARD,
207208
isDynamic: true,
208209
name: 'wizzard',
209-
title: 'Title',
210+
title: 'Dynamic with steps predicting',
210211
description: 'Description',
211212
buttonsPosition: 'left',
213+
predictSteps: true,
212214
fields: [{
213215
title: 'Get started with adding source',
214216
name: 'step-1',
@@ -232,15 +234,16 @@ export const wizardSchemaMoreSubsteps = {
232234
label: 'Aws field part',
233235
}],
234236
}, {
235-
title: 'Configure AWS part 2',
237+
title: 'Configure AWS part 2 - disabled jumping',
236238
name: 'step-88',
239+
disableForwardJumping: true,
237240
stepKey: 'aws2',
238241
nextStep: 'summary',
239242
substepOf: 'Summary',
240243
fields: [{
241244
component: componentTypes.TEXT_FIELD,
242-
name: 'aws-field',
243-
label: 'Aws field part',
245+
name: 'aws-field-1',
246+
label: 'Aws field part 1',
244247
}],
245248
},
246249
{

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

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Wizard extends React.Component {
2727
activeStepIndex: 0,
2828
maxStepIndex: 0,
2929
isDynamic, // wizard contains nextStep mapper
30-
navSchema: this.createSchema(),
30+
navSchema: this.createSchema({ currentIndex: 0, isDynamic }),
3131
loading: true,
3232
};
3333
}
@@ -48,15 +48,14 @@ class Wizard extends React.Component {
4848
}
4949

5050
handleNext = (nextStep, getRegisteredFields) =>
51-
this.setState(prevState =>
52-
({
53-
registeredFieldsHistory: { ...prevState.registeredFieldsHistory, [prevState.activeStep]: getRegisteredFields() },
54-
activeStep: nextStep,
55-
prevSteps: prevState.prevSteps.includes(prevState.activeStep) ? prevState.prevSteps : [ ...prevState.prevSteps, prevState.activeStep ],
56-
activeStepIndex: prevState.activeStepIndex + 1,
57-
maxStepIndex: (prevState.activeStepIndex + 1) > prevState.maxStepIndex ? prevState.maxStepIndex + 1 : prevState.maxStepIndex,
58-
navSchema: this.state.isDynamic ? this.createSchema() : prevState.navSchema,
59-
}));
51+
this.setState(prevState => ({
52+
registeredFieldsHistory: { ...prevState.registeredFieldsHistory, [prevState.activeStep]: getRegisteredFields() },
53+
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,
58+
}));
6059

6160
handlePrev = () => this.jumpToStep(this.state.activeStepIndex - 1);
6261

@@ -93,15 +92,24 @@ class Wizard extends React.Component {
9392
activeStepIndex: index,
9493
}));
9594

96-
// jumping in dynamic form disables returning to back if jumped on compileMapper step (!)
97-
if (this.state.isDynamic && typeof this.findCurrentStep(this.state.prevSteps[index]).nextStep === 'object') {
95+
const currentStep = this.findCurrentStep(this.state.prevSteps[index]);
96+
const currentStepHasStepMapper = typeof currentStep.nextStep === 'object';
97+
98+
if (this.state.isDynamic && (currentStepHasStepMapper || !this.props.predictSteps)) {
9899
this.setState((prevState) => ({
99100
navSchema: prevState.navSchema.slice(0, index + 1),
100101
prevSteps: prevState.prevSteps.slice(0, index + 1),
101102
maxStepIndex: prevState.prevSteps.slice(0, index + 1).length,
102103
}));
103104
}
104105

106+
if (currentStep.disableForwardJumping) {
107+
this.setState((prevState) => ({
108+
prevSteps: prevState.prevSteps.slice(0, index + 1),
109+
maxStepIndex: prevState.prevSteps.slice(0, index).length,
110+
}));
111+
}
112+
105113
// invalid state disables jumping forward until it fixed (!)
106114
if (valid === false) {
107115
this.setState((prevState) => ({
@@ -113,26 +121,34 @@ class Wizard extends React.Component {
113121
};
114122

115123
// builds schema used for generating of the navigation links
116-
createSchema = () => {
117-
const { formOptions } = this.props;
124+
createSchema = ({ currentIndex, isDynamic }) => {
125+
if (typeof isDynamic === 'undefined'){
126+
isDynamic = this.state.isDynamic;
127+
}
128+
129+
const { formOptions, predictSteps } = this.props;
130+
const { values } = formOptions.getState();
118131
let schema = [];
119132
let field = this.props.fields.find(({ stepKey }) => stepKey === 1 || stepKey === '1'); // find first wizard step
120-
let index = 0;
133+
let index = -1;
121134

122135
while (field){
136+
index += 1;
123137
schema = [
124138
...schema,
125139
{ title: field.title,
126140
substepOf: field.substepOf,
127-
index: index++,
141+
index,
128142
primary: (!schema[schema.length - 1] || !field.substepOf || field.substepOf !== schema[schema.length - 1].substepOf) },
129143
];
130144

145+
if (isDynamic && !predictSteps && currentIndex === index) {
146+
break;
147+
}
148+
131149
let nextStep = field.nextStep;
132150

133151
if (typeof field.nextStep === 'object') {
134-
const { values } = formOptions.getState();
135-
136152
nextStep = nextStep.stepMapper[get(values, nextStep.when)];
137153
}
138154

@@ -259,6 +275,7 @@ Wizard.propTypes = {
259275
setFullHeight: PropTypes.bool,
260276
isDynamic: PropTypes.bool,
261277
showTitles: PropTypes.bool,
278+
predictSteps: PropTypes.bool,
262279
};
263280

264281
const defaultLabels = {

0 commit comments

Comments
 (0)