Skip to content

Commit e82abff

Browse files
committed
feat: expose Step Names
1 parent 2af9d8f commit e82abff

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export type WizardProps = {
2323
onStepChange?: (stepIndex: number) => void;
2424
};
2525

26+
export type StepName = {
27+
number: number;
28+
name: string;
29+
};
30+
2631
export type WizardValues = {
2732
/**
2833
* Go to the next step
@@ -57,6 +62,10 @@ export type WizardValues = {
5762
isFirstStep: boolean;
5863
/** Indicate if the current step is the last step (aka no next step) */
5964
isLastStep: boolean;
65+
/** The labels of each step */
66+
stepNames: ({
67+
name: any;
68+
} | null)[];
6069
};
6170

6271
/** Console log levels */

src/wizard.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22

33
import * as logger from './logger';
4-
import { Handler, WizardProps } from './types';
4+
import { Handler, StepName, WizardProps } from './types';
55
import WizardContext from './wizardContext';
66

77
const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
@@ -20,6 +20,18 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
2020
const hasPreviousStep = React.useRef(false);
2121
const nextStepHandler = React.useRef<Handler>(() => {});
2222
const stepCount = React.Children.toArray(children).length;
23+
const stepsArray = React.Children.toArray(children);
24+
const stepNames = stepsArray
25+
.map((child) => {
26+
if (React.isValidElement(child)) {
27+
const number = child.props.number;
28+
const name = child.props.name || `Step ${number}`;
29+
30+
return { name, number };
31+
}
32+
return null;
33+
})
34+
.filter(Boolean) as StepName[];
2335

2436
hasNextStep.current = activeStep < stepCount - 1;
2537
hasPreviousStep.current = activeStep > 0;
@@ -97,6 +109,7 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
97109
isFirstStep: !hasPreviousStep.current,
98110
isLastStep: !hasNextStep.current,
99111
goToStep,
112+
stepNames,
100113
}),
101114
[
102115
doNextStep,
@@ -105,6 +118,7 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
105118
activeStep,
106119
stepCount,
107120
goToStep,
121+
stepNames,
108122
],
109123
);
110124

0 commit comments

Comments
 (0)