@@ -27,7 +27,7 @@ class Wizard extends React.Component {
27
27
activeStepIndex : 0 ,
28
28
maxStepIndex : 0 ,
29
29
isDynamic, // wizard contains nextStep mapper
30
- navSchema : this . createSchema ( ) ,
30
+ navSchema : this . createSchema ( { currentIndex : 0 , isDynamic } ) ,
31
31
loading : true ,
32
32
} ;
33
33
}
@@ -48,15 +48,14 @@ class Wizard extends React.Component {
48
48
}
49
49
50
50
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
+ } ) ) ;
60
59
61
60
handlePrev = ( ) => this . jumpToStep ( this . state . activeStepIndex - 1 ) ;
62
61
@@ -93,15 +92,24 @@ class Wizard extends React.Component {
93
92
activeStepIndex : index ,
94
93
} ) ) ;
95
94
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 ) ) {
98
99
this . setState ( ( prevState ) => ( {
99
100
navSchema : prevState . navSchema . slice ( 0 , index + 1 ) ,
100
101
prevSteps : prevState . prevSteps . slice ( 0 , index + 1 ) ,
101
102
maxStepIndex : prevState . prevSteps . slice ( 0 , index + 1 ) . length ,
102
103
} ) ) ;
103
104
}
104
105
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
+
105
113
// invalid state disables jumping forward until it fixed (!)
106
114
if ( valid === false ) {
107
115
this . setState ( ( prevState ) => ( {
@@ -113,26 +121,34 @@ class Wizard extends React.Component {
113
121
} ;
114
122
115
123
// 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 ( ) ;
118
131
let schema = [ ] ;
119
132
let field = this . props . fields . find ( ( { stepKey } ) => stepKey === 1 || stepKey === '1' ) ; // find first wizard step
120
- let index = 0 ;
133
+ let index = - 1 ;
121
134
122
135
while ( field ) {
136
+ index += 1 ;
123
137
schema = [
124
138
...schema ,
125
139
{ title : field . title ,
126
140
substepOf : field . substepOf ,
127
- index : index ++ ,
141
+ index,
128
142
primary : ( ! schema [ schema . length - 1 ] || ! field . substepOf || field . substepOf !== schema [ schema . length - 1 ] . substepOf ) } ,
129
143
] ;
130
144
145
+ if ( isDynamic && ! predictSteps && currentIndex === index ) {
146
+ break ;
147
+ }
148
+
131
149
let nextStep = field . nextStep ;
132
150
133
151
if ( typeof field . nextStep === 'object' ) {
134
- const { values } = formOptions . getState ( ) ;
135
-
136
152
nextStep = nextStep . stepMapper [ get ( values , nextStep . when ) ] ;
137
153
}
138
154
@@ -259,6 +275,7 @@ Wizard.propTypes = {
259
275
setFullHeight : PropTypes . bool ,
260
276
isDynamic : PropTypes . bool ,
261
277
showTitles : PropTypes . bool ,
278
+ predictSteps : PropTypes . bool ,
262
279
} ;
263
280
264
281
const defaultLabels = {
0 commit comments