@@ -21,21 +21,13 @@ class Wizard extends React.Component {
21
21
// find if wizard contains any dynamic steps (nextStep is mapper object)
22
22
const isDynamic = this . props . isDynamic ? true : this . props . fields . find ( ( { nextStep } ) => typeof nextStep === 'object' ) ? true : false ;
23
23
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
-
32
24
this . state = {
33
25
activeStep : this . props . fields [ 0 ] . stepKey ,
34
26
prevSteps : [ ] ,
35
27
activeStepIndex : 0 ,
36
28
maxStepIndex : 0 ,
37
29
isDynamic, // wizard contains nextStep mapper
38
- navSchema, // schema of navigation
30
+ navSchema : this . createSchema ( { currentIndex : 0 , isDynamic } ) ,
39
31
loading : true ,
40
32
} ;
41
33
}
@@ -55,38 +47,18 @@ class Wizard extends React.Component {
55
47
}
56
48
}
57
49
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
-
73
50
handleNext = ( nextStep , getRegisteredFields ) =>
74
- this . setState ( prevState =>
75
- ( {
76
- registeredFieldsHistory : { ...prevState . registeredFieldsHistory , [ prevState . activeStep ] : getRegisteredFields ( ) } ,
77
- activeStep : nextStep ,
78
- prevSteps : prevState . prevSteps . includes ( prevState . activeStep ) ? prevState . prevSteps : [ ...prevState . prevSteps , prevState . activeStep ] ,
79
- activeStepIndex : prevState . activeStepIndex + 1 ,
80
- maxStepIndex : ( prevState . activeStepIndex + 1 ) > prevState . maxStepIndex ? prevState . maxStepIndex + 1 : prevState . maxStepIndex ,
81
- navSchema : this . state . isDynamic ? this . insertDynamicStep ( nextStep , prevState . navSchema ) : prevState . navSchema ,
82
- } ) ) ;
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
+ } ) ) ;
83
59
84
60
handlePrev = ( ) => this . jumpToStep ( this . state . activeStepIndex - 1 ) ;
85
61
86
- findActiveFields = visitedSteps =>
87
- visitedSteps . map ( key => this . findCurrentStep ( key ) . fields . map ( ( { name } ) => name ) )
88
- . reduce ( ( acc , curr ) => curr . concat ( acc . map ( item => item ) ) , [ ] ) ;
89
-
90
62
handleSubmit = ( values , visitedSteps , getRegisteredFields ) => {
91
63
// Add the final step fields to history
92
64
const finalRegisteredFieldsHistory = {
@@ -116,11 +88,21 @@ class Wizard extends React.Component {
116
88
activeStepIndex : index ,
117
89
} ) ) ;
118
90
119
- // jumping in dynamic form disables returning to back (!)
120
- if ( this . state . isDynamic ) {
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 ) ) {
121
95
this . setState ( ( prevState ) => ( {
122
96
navSchema : prevState . navSchema . slice ( 0 , index + 1 ) ,
123
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 ,
124
106
} ) ) ;
125
107
}
126
108
@@ -135,21 +117,42 @@ class Wizard extends React.Component {
135
117
} ;
136
118
137
119
// builds schema used for generating of the navigation links
138
- createSchema = ( ) => {
120
+ createSchema = ( { currentIndex, isDynamic } ) => {
121
+ if ( typeof isDynamic === 'undefined' ) {
122
+ isDynamic = this . state . isDynamic ;
123
+ }
124
+
125
+ const { formOptions, predictSteps } = this . props ;
126
+ const { values } = formOptions . getState ( ) ;
139
127
let schema = [ ] ;
140
128
let field = this . props . fields . find ( ( { stepKey } ) => stepKey === 1 || stepKey === '1' ) ; // find first wizard step
141
- let index = 0 ;
129
+ let index = - 1 ;
142
130
143
131
while ( field ) {
132
+ index += 1 ;
144
133
schema = [
145
134
...schema ,
146
135
{ title : field . title ,
147
136
substepOf : field . substepOf ,
148
- index : index ++ ,
137
+ index,
149
138
primary : ( ! schema [ schema . length - 1 ] || ! field . substepOf || field . substepOf !== schema [ schema . length - 1 ] . substepOf ) } ,
150
139
] ;
151
140
152
- field = this . props . fields . find ( ( { stepKey } ) => stepKey === field . nextStep ) ;
141
+ if ( isDynamic && ! predictSteps && currentIndex === index ) {
142
+ break ;
143
+ }
144
+
145
+ let nextStep = field . nextStep ;
146
+
147
+ if ( typeof field . nextStep === 'object' ) {
148
+ nextStep = nextStep . stepMapper [ get ( values , nextStep . when ) ] ;
149
+ }
150
+
151
+ if ( nextStep ) {
152
+ field = this . props . fields . find ( ( { stepKey } ) => stepKey === nextStep ) ;
153
+ } else {
154
+ field = undefined ;
155
+ }
153
156
}
154
157
155
158
return schema ;
@@ -268,6 +271,7 @@ Wizard.propTypes = {
268
271
setFullHeight : PropTypes . bool ,
269
272
isDynamic : PropTypes . bool ,
270
273
showTitles : PropTypes . bool ,
274
+ predictSteps : PropTypes . bool ,
271
275
} ;
272
276
273
277
const defaultLabels = {
0 commit comments