Skip to content

Commit 35134e1

Browse files
committed
feat: add goToStep
1 parent 2020625 commit 35134e1

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/wizard.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,33 @@ const Wizard: React.FC<WizardProps> = React.memo(
1616
activeStep < React.Children.toArray(children).length - 1;
1717
hasPreviousStep.current = activeStep > 0;
1818

19-
const goToNextStep = React.useRef((stepIndex?: number) => {
20-
if (hasNextStep.current || stepIndex) {
21-
setActiveStep((activeStep) => stepIndex ?? activeStep + 1);
19+
const goToNextStep = React.useRef(() => {
20+
if (hasNextStep.current) {
21+
setActiveStep((activeStep) => activeStep + 1);
2222
}
2323
});
2424

25-
const goToPreviousStep = React.useRef((step?: number) => {
25+
const goToPreviousStep = React.useRef(() => {
2626
if (hasPreviousStep.current) {
27-
setActiveStep((activeStep) => step ?? activeStep - 1);
27+
setActiveStep((activeStep) => activeStep - 1);
28+
}
29+
});
30+
31+
const goToStep = React.useRef((stepIndex: number) => {
32+
if (
33+
stepIndex > 0 &&
34+
stepIndex < React.Children.toArray(children).length
35+
) {
36+
if (__DEV__) {
37+
logger.log(
38+
'warn',
39+
[
40+
`Invalid step index [${stepIndex}] passed to 'goToStep'. `,
41+
`Ensure the given stepIndex is not out of boundaries.`,
42+
].join(''),
43+
);
44+
}
45+
setActiveStep(stepIndex);
2846
}
2947
});
3048

@@ -33,20 +51,20 @@ const Wizard: React.FC<WizardProps> = React.memo(
3351
nextStepHandler.current = handler;
3452
});
3553

36-
const doNextStep = React.useRef(async (stepIndex?: number) => {
54+
const doNextStep = React.useRef(async () => {
3755
if (hasNextStep.current && nextStepHandler.current) {
3856
try {
3957
setIsLoading(true);
4058
await nextStepHandler.current();
4159
setIsLoading(false);
4260
nextStepHandler.current = null;
43-
goToNextStep.current(stepIndex);
61+
goToNextStep.current();
4462
} catch (error) {
4563
setIsLoading(false);
4664
throw error;
4765
}
4866
} else {
49-
goToNextStep.current(stepIndex);
67+
goToNextStep.current();
5068
}
5169
});
5270

@@ -59,6 +77,7 @@ const Wizard: React.FC<WizardProps> = React.memo(
5977
activeStep,
6078
isFirstStep: !hasPreviousStep.current,
6179
isLastStep: !hasNextStep.current,
80+
goToStep: goToStep.current,
6281
}),
6382
[activeStep, isLoading],
6483
);

0 commit comments

Comments
 (0)