Skip to content

Commit c8ea497

Browse files
committed
feat(pf4): predict the future in the wizard nav
1 parent 39e2148 commit c8ea497

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const ValidateButtons = ({ disableBack, handlePrev, buttonLabels: { back, cancel
77

88
const setValidating = () => {
99
setState('validating');
10-
setTimeout(() => setState('done'), 2000);
10+
setTimeout(() => setState('done'), 0);
1111
};
1212

1313
return (
@@ -97,6 +97,9 @@ export const wizardSchema = {
9797
component: componentTypes.TEXT_FIELD,
9898
name: 'google.google-field',
9999
label: 'Google field part',
100+
validate: [{
101+
type: validatorTypes.REQUIRED,
102+
}],
100103
}],
101104
}, {
102105
fields: [{
@@ -201,6 +204,7 @@ export const wizardSchemaSubsteps = {
201204
export const wizardSchemaMoreSubsteps = {
202205
fields: [{
203206
component: componentTypes.WIZARD,
207+
isDynamic: true,
204208
name: 'wizzard',
205209
title: 'Title',
206210
description: 'Description',

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

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,13 @@ class Wizard extends React.Component {
2121
// find if wizard contains any dynamic steps (nextStep is mapper object)
2222
const isDynamic = this.props.isDynamic ? true : this.props.fields.find(({ nextStep }) => typeof nextStep === 'object') ? true : false;
2323

24-
// insert into navigation schema first step if dynamic, otherwise create the whole schema
25-
// if the wizard is dynamic, the navigation is build progressively
26-
const firstStep = this.props.fields.find(({ stepKey }) => stepKey === 1 || stepKey === '1');
27-
28-
const navSchema = isDynamic ?
29-
[{ title: firstStep.title, index: 0, primary: true, substepOf: firstStep.substepOf }]
30-
: this.createSchema();
31-
3224
this.state = {
3325
activeStep: this.props.fields[0].stepKey,
3426
prevSteps: [],
3527
activeStepIndex: 0,
3628
maxStepIndex: 0,
3729
isDynamic, // wizard contains nextStep mapper
38-
navSchema, // schema of navigation
30+
navSchema: this.createSchema(),
3931
loading: true,
4032
};
4133
}
@@ -55,21 +47,6 @@ class Wizard extends React.Component {
5547
}
5648
}
5749

58-
insertDynamicStep = (nextStep, navSchema) => {
59-
const { title, substepOf } = this.props.fields.find(({ stepKey }) => stepKey === nextStep);
60-
const lastStep = navSchema[navSchema.length - 1];
61-
62-
return [
63-
...navSchema,
64-
{
65-
title,
66-
substepOf,
67-
index: lastStep.index + 1,
68-
primary: (!substepOf) || (substepOf && substepOf !== lastStep.substepOf),
69-
},
70-
];
71-
}
72-
7350
handleNext = (nextStep, getRegisteredFields) =>
7451
this.setState(prevState =>
7552
({
@@ -78,7 +55,7 @@ class Wizard extends React.Component {
7855
prevSteps: prevState.prevSteps.includes(prevState.activeStep) ? prevState.prevSteps : [ ...prevState.prevSteps, prevState.activeStep ],
7956
activeStepIndex: prevState.activeStepIndex + 1,
8057
maxStepIndex: (prevState.activeStepIndex + 1) > prevState.maxStepIndex ? prevState.maxStepIndex + 1 : prevState.maxStepIndex,
81-
navSchema: this.state.isDynamic ? this.insertDynamicStep(nextStep, prevState.navSchema) : prevState.navSchema,
58+
navSchema: this.state.isDynamic ? this.createSchema() : prevState.navSchema,
8259
}));
8360

8461
handlePrev = () => this.jumpToStep(this.state.activeStepIndex - 1);
@@ -116,11 +93,12 @@ class Wizard extends React.Component {
11693
activeStepIndex: index,
11794
}));
11895

119-
// jumping in dynamic form disables returning to back (!)
120-
if (this.state.isDynamic) {
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') {
12198
this.setState((prevState) => ({
12299
navSchema: prevState.navSchema.slice(0, index + 1),
123100
prevSteps: prevState.prevSteps.slice(0, index + 1),
101+
maxStepIndex: prevState.prevSteps.slice(0, index + 1).length,
124102
}));
125103
}
126104

@@ -136,6 +114,7 @@ class Wizard extends React.Component {
136114

137115
// builds schema used for generating of the navigation links
138116
createSchema = () => {
117+
const { formOptions } = this.props;
139118
let schema = [];
140119
let field = this.props.fields.find(({ stepKey }) => stepKey === 1 || stepKey === '1'); // find first wizard step
141120
let index = 0;
@@ -149,7 +128,19 @@ class Wizard extends React.Component {
149128
primary: (!schema[schema.length - 1] || !field.substepOf || field.substepOf !== schema[schema.length - 1].substepOf) },
150129
];
151130

152-
field = this.props.fields.find(({ stepKey }) => stepKey === field.nextStep);
131+
let nextStep = field.nextStep;
132+
133+
if (typeof field.nextStep === 'object') {
134+
const { values } = formOptions.getState();
135+
136+
nextStep = nextStep.stepMapper[get(values, nextStep.when)];
137+
}
138+
139+
if (nextStep) {
140+
field = this.props.fields.find(({ stepKey }) => stepKey === nextStep);
141+
} else {
142+
field = undefined;
143+
}
153144
}
154145

155146
return schema;

0 commit comments

Comments
 (0)