Skip to content

Commit 9f75a7a

Browse files
committed
Rename index callback
1 parent b977aa1 commit 9f75a7a

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Example: pass a footer component that contains a "previous" and "next" button to
8484
| startIndex | number | Indicate the wizard to start at the given step || 0 |
8585
| header | React.ReactNode | Header that is shown above the active step || |
8686
| footer | React.ReactNode | Footer that is shown below the active stepstep || |
87+
| onStepChange | (stepIndex) | Callback that will be invoked with the new step index when the wizard changes steps || |
8788
| wrapper | React.React.ReactElement | Optional wrapper that is exclusively wrapped around the active step component. It is not wrapped around the `header` and `footer` || |
8889
| children | React.ReactNode | Each child component will be treated as an individual step | ✔️ |
8990

src/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export type WizardProps = {
99
startIndex?: number;
1010
/**
1111
* Optional wrapper that is exclusively wrapped around the active step component. It is not wrapped around the `header` and `footer`
12-
*
1312
* @example With `framer-motion` - `<AnimatePresence />`
1413
* ```jsx
1514
* <Wizard wrapper={<AnimatePresence exitBeforeEnter />}>
@@ -18,7 +17,8 @@ export type WizardProps = {
1817
* ```
1918
*/
2019
wrapper?: React.ReactElement;
21-
onIndexChange?: (stepIndex: number) => void;
20+
/** Callback that will be invoked with the new step index when the wizard changes steps */
21+
onStepChange?: (stepIndex: number) => void;
2222
};
2323

2424
export type WizardValues = {
@@ -32,13 +32,11 @@ export type WizardValues = {
3232
previousStep: () => void;
3333
/**
3434
* Go to the given step index
35-
*
3635
* @param stepIndex The step index, starts at 0
3736
*/
3837
goToStep: (stepIndex: number) => void;
3938
/**
4039
* Attach a callback that will be called when calling `nextStep()`
41-
*
4240
* @param handler Can be either sync or async
4341
*/
4442
handleStep: (handler: Handler) => void;

src/wizard.tsx

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
99
header,
1010
footer,
1111
children,
12-
onIndexChange,
12+
onStepChange,
1313
wrapper: Wrapper,
1414
startIndex = 0,
1515
}) => {
@@ -23,30 +23,31 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
2323
hasNextStep.current = activeStep < stepCount - 1;
2424
hasPreviousStep.current = activeStep > 0;
2525

26-
React.useEffect(() => {
27-
if (onIndexChange) {
28-
onIndexChange(activeStep);
29-
}
30-
}, [activeStep]);
31-
32-
const goToNextStep = React.useRef(() => {
26+
const goToNextStep = React.useCallback(() => {
3327
if (hasNextStep.current) {
34-
setActiveStep((activeStep) => activeStep + 1);
28+
const newActiveStepIndex = activeStep + 1;
29+
30+
setActiveStep(newActiveStepIndex);
31+
onStepChange?.(newActiveStepIndex);
3532
}
36-
});
33+
}, [activeStep, onStepChange]);
3734

38-
const goToPreviousStep = React.useRef(() => {
35+
const goToPreviousStep = React.useCallback(() => {
3936
if (hasPreviousStep.current) {
4037
nextStepHandler.current = null;
41-
setActiveStep((activeStep) => activeStep - 1);
38+
const newActiveStepIndex = activeStep - 1;
39+
40+
setActiveStep(newActiveStepIndex);
41+
onStepChange?.(newActiveStepIndex);
4242
}
43-
});
43+
}, [activeStep, onStepChange]);
4444

4545
const goToStep = React.useCallback(
4646
(stepIndex: number) => {
4747
if (stepIndex >= 0 && stepIndex < stepCount) {
4848
nextStepHandler.current = null;
4949
setActiveStep(stepIndex);
50+
onStepChange?.(stepIndex);
5051
} else {
5152
if (__DEV__) {
5253
logger.log(
@@ -59,35 +60,35 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
5960
}
6061
}
6162
},
62-
[stepCount],
63+
[stepCount, onStepChange],
6364
);
6465

6566
// Callback to attach the step handler
6667
const handleStep = React.useRef((handler: Handler) => {
6768
nextStepHandler.current = handler;
6869
});
6970

70-
const doNextStep = React.useRef(async () => {
71+
const doNextStep = React.useCallback(async () => {
7172
if (hasNextStep.current && nextStepHandler.current) {
7273
try {
7374
setIsLoading(true);
7475
await nextStepHandler.current();
7576
setIsLoading(false);
7677
nextStepHandler.current = null;
77-
goToNextStep.current();
78+
goToNextStep();
7879
} catch (error) {
7980
setIsLoading(false);
8081
throw error;
8182
}
8283
} else {
83-
goToNextStep.current();
84+
goToNextStep();
8485
}
85-
});
86+
}, [goToNextStep]);
8687

8788
const wizardValue = React.useMemo(
8889
() => ({
89-
nextStep: doNextStep.current,
90-
previousStep: goToPreviousStep.current,
90+
nextStep: doNextStep,
91+
previousStep: goToPreviousStep,
9192
handleStep: handleStep.current,
9293
isLoading,
9394
activeStep,
@@ -96,7 +97,14 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
9697
isLastStep: !hasNextStep.current,
9798
goToStep,
9899
}),
99-
[isLoading, activeStep, stepCount, goToStep],
100+
[
101+
doNextStep,
102+
goToPreviousStep,
103+
isLoading,
104+
activeStep,
105+
stepCount,
106+
goToStep,
107+
],
100108
);
101109

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

0 commit comments

Comments
 (0)