@@ -42,8 +42,6 @@ export interface WizardProps extends React.HTMLProps<HTMLDivElement> {
42
42
height ?: number | string ;
43
43
/** Disables navigation items that haven't been visited. Defaults to false */
44
44
isStepVisitRequired ?: boolean ;
45
- /** Flag to unmount inactive steps instead of hiding. Defaults to true */
46
- hasUnmountedSteps ?: boolean ;
47
45
/** Callback function when a step in the navigation is clicked */
48
46
onNavByIndex ?: WizardNavStepFunction ;
49
47
/** Callback function after next button is clicked */
@@ -66,50 +64,44 @@ export const Wizard = ({
66
64
nav,
67
65
startIndex = 1 ,
68
66
isStepVisitRequired = false ,
69
- hasUnmountedSteps = true ,
70
67
onNavByIndex,
71
68
onNext,
72
69
onBack,
73
70
onSave,
74
71
onClose,
75
72
...wrapperProps
76
73
} : WizardProps ) => {
77
- const [ currentStepIndex , setCurrentStepIndex ] = React . useState ( startIndex ) ;
74
+ const [ activeStepIndex , setActiveStepIndex ] = React . useState ( startIndex ) ;
78
75
const initialSteps = buildSteps ( children ) ;
79
76
80
77
const goToNextStep = ( steps : WizardControlStep [ ] = initialSteps ) => {
81
- const newStepIndex =
82
- steps . findIndex ( ( step , index ) => index + 1 > currentStepIndex && ! step . isHidden && ! isWizardParentStep ( step ) ) + 1 ;
78
+ const newStepIndex = steps . find ( step => step . index > activeStepIndex && ! step . isHidden && ! isWizardParentStep ( step ) )
79
+ ?. index ;
83
80
84
- if ( currentStepIndex >= steps . length || ! newStepIndex ) {
81
+ if ( activeStepIndex >= steps . length || ! newStepIndex ) {
85
82
return onSave ? onSave ( ) : onClose ?.( ) ;
86
83
}
87
84
88
- const currStep = isWizardParentStep ( steps [ currentStepIndex ] )
89
- ? steps [ currentStepIndex + 1 ]
90
- : steps [ currentStepIndex ] ;
91
- const prevStep = steps [ currentStepIndex - 1 ] ;
92
-
93
- setCurrentStepIndex ( newStepIndex ) ;
85
+ const currStep = isWizardParentStep ( steps [ activeStepIndex ] ) ? steps [ activeStepIndex + 1 ] : steps [ activeStepIndex ] ;
86
+ const prevStep = steps [ activeStepIndex - 1 ] ;
94
87
95
- return onNext ?.( normalizeNavStep ( currStep , steps ) , normalizeNavStep ( prevStep , steps ) ) ;
88
+ setActiveStepIndex ( newStepIndex ) ;
89
+ return onNext ?.( normalizeNavStep ( currStep ) , normalizeNavStep ( prevStep ) ) ;
96
90
} ;
97
91
98
92
const goToPrevStep = ( steps : WizardControlStep [ ] = initialSteps ) => {
99
93
const newStepIndex =
100
94
findLastIndex (
101
95
steps ,
102
- ( step : WizardControlStep , index : number ) =>
103
- index + 1 < currentStepIndex && ! step . isHidden && ! isWizardParentStep ( step )
96
+ ( step : WizardControlStep ) => step . index < activeStepIndex && ! step . isHidden && ! isWizardParentStep ( step )
104
97
) + 1 ;
105
- const currStep = isWizardParentStep ( steps [ currentStepIndex - 2 ] )
106
- ? steps [ currentStepIndex - 3 ]
107
- : steps [ currentStepIndex - 2 ] ;
108
- const prevStep = steps [ currentStepIndex - 1 ] ;
109
-
110
- setCurrentStepIndex ( newStepIndex ) ;
98
+ const currStep = isWizardParentStep ( steps [ activeStepIndex - 2 ] )
99
+ ? steps [ activeStepIndex - 3 ]
100
+ : steps [ activeStepIndex - 2 ] ;
101
+ const prevStep = steps [ activeStepIndex - 1 ] ;
111
102
112
- return onBack ?.( normalizeNavStep ( currStep , steps ) , normalizeNavStep ( prevStep , steps ) ) ;
103
+ setActiveStepIndex ( newStepIndex ) ;
104
+ return onBack ?.( normalizeNavStep ( currStep ) , normalizeNavStep ( prevStep ) ) ;
113
105
} ;
114
106
115
107
const goToStepByIndex = ( steps : WizardControlStep [ ] = initialSteps , index : number ) => {
@@ -120,46 +112,40 @@ export const Wizard = ({
120
112
index = 1 ;
121
113
} else if ( index > lastStepIndex ) {
122
114
index = lastStepIndex ;
123
- } else if ( steps [ index - 1 ] . isHidden ) {
124
- // eslint-disable-next-line no-console
125
- console . error ( 'Wizard: Unable to navigate to hidden step.' ) ;
126
115
}
127
116
128
117
const currStep = steps [ index - 1 ] ;
129
- const prevStep = steps [ currentStepIndex - 1 ] ;
130
- setCurrentStepIndex ( index ) ;
118
+ const prevStep = steps [ activeStepIndex - 1 ] ;
131
119
132
- return onNavByIndex ?.( normalizeNavStep ( currStep , steps ) , normalizeNavStep ( prevStep , steps ) ) ;
120
+ setActiveStepIndex ( index ) ;
121
+ return onNavByIndex ?.( normalizeNavStep ( currStep ) , normalizeNavStep ( prevStep ) ) ;
133
122
} ;
134
123
135
124
const goToStepById = ( steps : WizardControlStep [ ] = initialSteps , id : number | string ) => {
136
- const stepIndex = steps . findIndex ( step => step . id === id ) + 1 ;
125
+ const step = steps . find ( step => step . id === id ) ;
126
+ const stepIndex = step ?. index ;
127
+ const lastStepIndex = steps . length + 1 ;
137
128
138
- if ( stepIndex > 0 && stepIndex < steps . length + 1 && ! steps [ stepIndex ] . isHidden ) {
139
- setCurrentStepIndex ( stepIndex ) ;
140
- } else {
141
- // eslint-disable-next-line no-console
142
- console . error ( `Wizard: Unable to navigate to step with id: ${ id } .` ) ;
129
+ if ( stepIndex > 0 && stepIndex < lastStepIndex && ! step . isHidden ) {
130
+ setActiveStepIndex ( stepIndex ) ;
143
131
}
144
132
} ;
145
133
146
134
const goToStepByName = ( steps : WizardControlStep [ ] = initialSteps , name : string ) => {
147
- const stepIndex = initialSteps . findIndex ( step => step . name === name ) + 1 ;
135
+ const step = steps . find ( step => step . name === name ) ;
136
+ const stepIndex = step ?. index ;
137
+ const lastStepIndex = steps . length + 1 ;
148
138
149
- if ( stepIndex > 0 && stepIndex < steps . length + 1 && ! steps [ stepIndex ] . isHidden ) {
150
- setCurrentStepIndex ( stepIndex ) ;
151
- } else {
152
- // eslint-disable-next-line no-console
153
- console . error ( `Wizard: Unable to navigate to step with name: ${ name } .` ) ;
139
+ if ( stepIndex > 0 && stepIndex < lastStepIndex && ! step . isHidden ) {
140
+ setActiveStepIndex ( stepIndex ) ;
154
141
}
155
142
} ;
156
143
157
144
return (
158
145
< WizardContextProvider
159
146
steps = { initialSteps }
160
- currentStepIndex = { currentStepIndex }
147
+ activeStepIndex = { activeStepIndex }
161
148
footer = { footer }
162
- isStepVisitRequired = { isStepVisitRequired }
163
149
onNext = { goToNextStep }
164
150
onBack = { goToPrevStep }
165
151
onClose = { onClose }
@@ -176,37 +162,32 @@ export const Wizard = ({
176
162
{ ...wrapperProps }
177
163
>
178
164
{ header }
179
- < WizardInternal nav = { nav } hasUnmountedSteps = { hasUnmountedSteps } isStepVisitRequired = { isStepVisitRequired } />
165
+ < WizardInternal nav = { nav } isStepVisitRequired = { isStepVisitRequired } />
180
166
</ div >
181
167
</ WizardContextProvider >
182
168
) ;
183
169
} ;
184
170
185
- const WizardInternal = ( {
186
- nav,
187
- hasUnmountedSteps,
188
- isStepVisitRequired
189
- } : Pick < WizardProps , 'nav' | 'hasUnmountedSteps' | 'isStepVisitRequired' > ) => {
190
- const { currentStep, steps, footer, goToStepByIndex } = useWizardContext ( ) ;
171
+ const WizardInternal = ( { nav, isStepVisitRequired } : Pick < WizardProps , 'nav' | 'isStepVisitRequired' > ) => {
172
+ const { activeStep, steps, footer, goToStepByIndex } = useWizardContext ( ) ;
191
173
const [ isNavExpanded , setIsNavExpanded ] = React . useState ( false ) ;
192
174
193
175
const wizardNav = React . useMemo ( ( ) => {
194
176
if ( isCustomWizardNav ( nav ) ) {
195
- return typeof nav === 'function' ? nav ( isNavExpanded , steps , currentStep , goToStepByIndex ) : nav ;
177
+ return typeof nav === 'function' ? nav ( isNavExpanded , steps , activeStep , goToStepByIndex ) : nav ;
196
178
}
197
179
198
180
return < WizardNavInternal nav = { nav } isNavExpanded = { isNavExpanded } isStepVisitRequired = { isStepVisitRequired } /> ;
199
- } , [ currentStep , isStepVisitRequired , goToStepByIndex , isNavExpanded , nav , steps ] ) ;
181
+ } , [ activeStep , isStepVisitRequired , goToStepByIndex , isNavExpanded , nav , steps ] ) ;
200
182
201
183
return (
202
184
< WizardToggle
203
185
nav = { wizardNav }
204
186
footer = { footer }
205
187
steps = { steps }
206
- currentStep = { currentStep }
188
+ activeStep = { activeStep }
207
189
isNavExpanded = { isNavExpanded }
208
190
toggleNavExpanded = { ( ) => setIsNavExpanded ( prevIsExpanded => ! prevIsExpanded ) }
209
- hasUnmountedSteps = { hasUnmountedSteps }
210
191
/>
211
192
) ;
212
193
} ;
0 commit comments