Skip to content

Commit dddb25d

Browse files
authored
Merge pull request #179 from devrnt/135-gottostep-fails-with-dynamically-rendered-step
135 gottostep fails with dynamically rendered step
2 parents 83d97f4 + 49c65d5 commit dddb25d

File tree

2 files changed

+72
-17
lines changed

2 files changed

+72
-17
lines changed

src/wizard.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,25 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
2929
}
3030
});
3131

32-
const goToStep = React.useRef((stepIndex: number) => {
33-
if (stepIndex >= 0 && stepIndex < stepCount) {
34-
nextStepHandler.current = null;
35-
setActiveStep(stepIndex);
36-
} else {
37-
if (__DEV__) {
38-
logger.log(
39-
'warn',
40-
[
41-
`Invalid step index [${stepIndex}] passed to 'goToStep'. `,
42-
`Ensure the given stepIndex is not out of boundaries.`,
43-
].join(''),
44-
);
32+
const goToStep = React.useCallback(
33+
(stepIndex: number) => {
34+
if (stepIndex >= 0 && stepIndex < stepCount) {
35+
nextStepHandler.current = null;
36+
setActiveStep(stepIndex);
37+
} else {
38+
if (__DEV__) {
39+
logger.log(
40+
'warn',
41+
[
42+
`Invalid step index [${stepIndex}] passed to 'goToStep'. `,
43+
`Ensure the given stepIndex is not out of boundaries.`,
44+
].join(''),
45+
);
46+
}
4547
}
46-
}
47-
});
48+
},
49+
[stepCount],
50+
);
4851

4952
// Callback to attach the step handler
5053
const handleStep = React.useRef((handler: Handler) => {
@@ -78,9 +81,9 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
7881
stepCount,
7982
isFirstStep: !hasPreviousStep.current,
8083
isLastStep: !hasNextStep.current,
81-
goToStep: goToStep.current,
84+
goToStep,
8285
}),
83-
[activeStep, stepCount, isLoading],
86+
[isLoading, activeStep, stepCount, goToStep],
8487
);
8588

8689
const activeStepContent = React.useMemo(() => {

test/useWizard.test.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,56 @@ describe('useWizard', () => {
224224
}
225225
});
226226
});
227+
228+
test('should update step count when dynamic step is added', () => {
229+
const steps = ['one', 'two', 'three'];
230+
231+
const { result, rerender } = renderHook(() => useWizard(), {
232+
initialProps: {
233+
startIndex: 0,
234+
},
235+
wrapper: ({ children, startIndex }) => (
236+
<Wizard startIndex={startIndex}>
237+
{steps.map((step) => (
238+
<p key={step}>{children}</p>
239+
))}
240+
</Wizard>
241+
),
242+
});
243+
244+
expect(result.current.stepCount).toBe(3);
245+
246+
steps.push('four');
247+
rerender();
248+
249+
expect(result.current.stepCount).toBe(4);
250+
});
251+
252+
test('should go to step index of dynamic step', () => {
253+
const steps = ['one', 'two', 'three'];
254+
255+
const { result, rerender } = renderHook(() => useWizard(), {
256+
initialProps: {
257+
startIndex: 0,
258+
},
259+
wrapper: ({ children, startIndex }) => (
260+
<Wizard startIndex={startIndex}>
261+
{steps.map((step) => (
262+
<p key={step}>{children}</p>
263+
))}
264+
</Wizard>
265+
),
266+
});
267+
268+
steps.push('four');
269+
rerender();
270+
271+
act(() => {
272+
result.current.goToStep(3);
273+
});
274+
275+
expect(result.current.activeStep).toBe(3);
276+
expect(result.current.isFirstStep).toBe(false);
277+
expect(result.current.isLastStep).toBe(true);
278+
});
227279
});

0 commit comments

Comments
 (0)