Skip to content

Commit 8ce60c7

Browse files
committed
(feat) add onIndexChange
1 parent b256c5a commit 8ce60c7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type WizardProps = {
1818
* ```
1919
*/
2020
wrapper?: React.ReactElement;
21+
onIndexChange?: (stepIndex: number) => void;
2122
};
2223

2324
export type WizardValues = {

src/wizard.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { Handler, WizardProps } from './types';
55
import WizardContext from './wizardContext';
66

77
const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
8-
({ header, footer, children, wrapper: Wrapper, startIndex = 0 }) => {
8+
({
9+
header,
10+
footer,
11+
children,
12+
onIndexChange,
13+
wrapper: Wrapper,
14+
startIndex = 0,
15+
}) => {
916
const [activeStep, setActiveStep] = React.useState(startIndex);
1017
const [isLoading, setIsLoading] = React.useState(false);
1118
const hasNextStep = React.useRef(true);
@@ -16,6 +23,12 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
1623
hasNextStep.current = activeStep < stepCount - 1;
1724
hasPreviousStep.current = activeStep > 0;
1825

26+
React.useEffect(() => {
27+
if (onIndexChange) {
28+
onIndexChange(activeStep);
29+
}
30+
}, [activeStep, onIndexChange]);
31+
1932
const goToNextStep = React.useRef(() => {
2033
if (hasNextStep.current) {
2134
setActiveStep((activeStep) => activeStep + 1);

0 commit comments

Comments
 (0)