@@ -42,8 +42,6 @@ export interface WizardProps extends React.HTMLProps<HTMLDivElement> {
4242 height ?: number | string ;
4343 /** Disables navigation items that haven't been visited. Defaults to false */
4444 isStepVisitRequired ?: boolean ;
45- /** Flag to unmount inactive steps instead of hiding. Defaults to true */
46- hasUnmountedSteps ?: boolean ;
4745 /** Callback function when a step in the navigation is clicked */
4846 onNavByIndex ?: WizardNavStepFunction ;
4947 /** Callback function after next button is clicked */
@@ -66,50 +64,44 @@ export const Wizard = ({
6664 nav,
6765 startIndex = 1 ,
6866 isStepVisitRequired = false ,
69- hasUnmountedSteps = true ,
7067 onNavByIndex,
7168 onNext,
7269 onBack,
7370 onSave,
7471 onClose,
7572 ...wrapperProps
7673} : WizardProps ) => {
77- const [ currentStepIndex , setCurrentStepIndex ] = React . useState ( startIndex ) ;
74+ const [ activeStepIndex , setActiveStepIndex ] = React . useState ( startIndex ) ;
7875 const initialSteps = buildSteps ( children ) ;
7976
8077 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 ;
8380
84- if ( currentStepIndex >= steps . length || ! newStepIndex ) {
81+ if ( activeStepIndex >= steps . length || ! newStepIndex ) {
8582 return onSave ? onSave ( ) : onClose ?.( ) ;
8683 }
8784
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 ] ;
9487
95- return onNext ?.( normalizeNavStep ( currStep , steps ) , normalizeNavStep ( prevStep , steps ) ) ;
88+ setActiveStepIndex ( newStepIndex ) ;
89+ return onNext ?.( normalizeNavStep ( currStep ) , normalizeNavStep ( prevStep ) ) ;
9690 } ;
9791
9892 const goToPrevStep = ( steps : WizardControlStep [ ] = initialSteps ) => {
9993 const newStepIndex =
10094 findLastIndex (
10195 steps ,
102- ( step : WizardControlStep , index : number ) =>
103- index + 1 < currentStepIndex && ! step . isHidden && ! isWizardParentStep ( step )
96+ ( step : WizardControlStep ) => step . index < activeStepIndex && ! step . isHidden && ! isWizardParentStep ( step )
10497 ) + 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 ] ;
111102
112- return onBack ?.( normalizeNavStep ( currStep , steps ) , normalizeNavStep ( prevStep , steps ) ) ;
103+ setActiveStepIndex ( newStepIndex ) ;
104+ return onBack ?.( normalizeNavStep ( currStep ) , normalizeNavStep ( prevStep ) ) ;
113105 } ;
114106
115107 const goToStepByIndex = ( steps : WizardControlStep [ ] = initialSteps , index : number ) => {
@@ -120,46 +112,40 @@ export const Wizard = ({
120112 index = 1 ;
121113 } else if ( index > lastStepIndex ) {
122114 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.' ) ;
126115 }
127116
128117 const currStep = steps [ index - 1 ] ;
129- const prevStep = steps [ currentStepIndex - 1 ] ;
130- setCurrentStepIndex ( index ) ;
118+ const prevStep = steps [ activeStepIndex - 1 ] ;
131119
132- return onNavByIndex ?.( normalizeNavStep ( currStep , steps ) , normalizeNavStep ( prevStep , steps ) ) ;
120+ setActiveStepIndex ( index ) ;
121+ return onNavByIndex ?.( normalizeNavStep ( currStep ) , normalizeNavStep ( prevStep ) ) ;
133122 } ;
134123
135124 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 ;
137128
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 ) ;
143131 }
144132 } ;
145133
146134 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 ;
148138
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 ) ;
154141 }
155142 } ;
156143
157144 return (
158145 < WizardContextProvider
159146 steps = { initialSteps }
160- currentStepIndex = { currentStepIndex }
147+ activeStepIndex = { activeStepIndex }
161148 footer = { footer }
162- isStepVisitRequired = { isStepVisitRequired }
163149 onNext = { goToNextStep }
164150 onBack = { goToPrevStep }
165151 onClose = { onClose }
@@ -176,37 +162,32 @@ export const Wizard = ({
176162 { ...wrapperProps }
177163 >
178164 { header }
179- < WizardInternal nav = { nav } hasUnmountedSteps = { hasUnmountedSteps } isStepVisitRequired = { isStepVisitRequired } />
165+ < WizardInternal nav = { nav } isStepVisitRequired = { isStepVisitRequired } />
180166 </ div >
181167 </ WizardContextProvider >
182168 ) ;
183169} ;
184170
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 ( ) ;
191173 const [ isNavExpanded , setIsNavExpanded ] = React . useState ( false ) ;
192174
193175 const wizardNav = React . useMemo ( ( ) => {
194176 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 ;
196178 }
197179
198180 return < WizardNavInternal nav = { nav } isNavExpanded = { isNavExpanded } isStepVisitRequired = { isStepVisitRequired } /> ;
199- } , [ currentStep , isStepVisitRequired , goToStepByIndex , isNavExpanded , nav , steps ] ) ;
181+ } , [ activeStep , isStepVisitRequired , goToStepByIndex , isNavExpanded , nav , steps ] ) ;
200182
201183 return (
202184 < WizardToggle
203185 nav = { wizardNav }
204186 footer = { footer }
205187 steps = { steps }
206- currentStep = { currentStep }
188+ activeStep = { activeStep }
207189 isNavExpanded = { isNavExpanded }
208190 toggleNavExpanded = { ( ) => setIsNavExpanded ( prevIsExpanded => ! prevIsExpanded ) }
209- hasUnmountedSteps = { hasUnmountedSteps }
210191 />
211192 ) ;
212193} ;
0 commit comments