Skip to content

Commit 885db0a

Browse files
committed
refactor: export BaseWizardStep interface
1 parent 74129d7 commit 885db0a

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Used to retrieve all methods and properties related to your wizard. Make sure `W
147147
| stepCount | number | The total number of steps of the wizard |
148148
| isFirstStep | boolean | Indicate if the current step is the first step (aka no previous step) |
149149
| isLastStep | boolean | Indicate if the current step is the last step (aka no next step) |
150-
| stepNames | { name: string; number: string }[] | An array of objects for each step in the wizard. Each object has a `name` and `number` property corresponding to the step's name and number. |
150+
| steps | { name?: string; number?: string }[] | An array of objects for each step in the wizard. Each object has a `name` and `number` property corresponding to the step's name and number. |
151151
| |
152152

153153
#### Example

playground/components/asyncStep.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { styled } from 'goober';
22
import * as React from 'react';
33

44
import { useWizard } from '../../dist';
5+
import { BaseWizardStep } from '../../dist/types';
56
import { useMockMutation } from '../hooks';
67

7-
type Props = {
8-
number: number;
9-
name?: string;
8+
type Props = BaseWizardStep & {
9+
content: string;
1010
};
1111

1212
const MOCK = [

playground/components/sidebar.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ export const Nav = styled('nav')`
2727
`;
2828

2929
const Sidebar: React.FC = () => {
30-
const { activeStep, stepCount, goToStep, stepNames } = useWizard();
30+
const { activeStep, stepCount, goToStep, steps } = useWizard();
3131

3232
return (
3333
<Nav>
3434
{stepCount > 0 && (
3535
<ul>
36-
{stepNames.map((stepName, index) => (
36+
{steps.map((stepName, index) => (
3737
<li key={index}>
3838
<Button
39-
label={stepName.name}
40-
onClick={() => goToStep(stepName.number - 1)}
41-
disabled={stepName.number - 1 > activeStep}
39+
label={stepName.name || `Step ${stepName.number}`}
40+
onClick={() => goToStep(index)}
41+
disabled={index > activeStep}
4242
>
4343
{stepName.name}
4444
</Button>

playground/components/step.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { styled } from 'goober';
22
import * as React from 'react';
33

44
import { useWizard } from '../../dist';
5+
import { BaseWizardStep } from '../../dist/types';
56

6-
type Props = {
7-
number: number;
7+
type Props = BaseWizardStep & {
88
withCallback?: boolean;
9-
name?: string;
109
};
1110

1211
const Container = styled('div')`

src/types.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ export type WizardProps = {
3333
onStepChange?: (stepIndex: number) => void;
3434
};
3535

36-
export type StepName = {
37-
number: number;
38-
name: string;
39-
};
36+
export interface BaseWizardStep {
37+
/** The step number */
38+
number?: number;
39+
/** The step name */
40+
name?: string;
41+
}
4042

4143
export type WizardValues = {
4244
/**
@@ -73,7 +75,7 @@ export type WizardValues = {
7375
/** Indicate if the current step is the last step (aka no next step) */
7476
isLastStep: boolean;
7577
/** The labels of each step */
76-
stepNames: StepName[];
78+
steps: BaseWizardStep[];
7779
};
7880

7981
/** Console log levels */

src/wizard.tsx

+5-5
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, StepName, WizardProps } from './types';
4+
import { BaseWizardStep, Handler, WizardProps } from './types';
55
import WizardContext from './wizardContext';
66

77
const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
@@ -22,7 +22,7 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
2222
const nextStepHandler = React.useRef<Handler>(() => {});
2323
const stepCount = React.Children.toArray(children).length;
2424
const stepsArray = React.Children.toArray(children);
25-
const stepNames = stepsArray
25+
const steps = stepsArray
2626
.map((child) => {
2727
if (React.isValidElement(child)) {
2828
const number = child.props.number;
@@ -32,7 +32,7 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
3232
}
3333
return null;
3434
})
35-
.filter(Boolean) as StepName[];
35+
.filter(Boolean) as BaseWizardStep[];
3636

3737
hasNextStep.current = activeStep < stepCount - 1;
3838
hasPreviousStep.current = activeStep > 0;
@@ -110,7 +110,7 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
110110
isFirstStep: !hasPreviousStep.current,
111111
isLastStep: !hasNextStep.current,
112112
goToStep,
113-
stepNames,
113+
steps,
114114
}),
115115
[
116116
doNextStep,
@@ -119,7 +119,7 @@ const Wizard: React.FC<React.PropsWithChildren<WizardProps>> = React.memo(
119119
activeStep,
120120
stepCount,
121121
goToStep,
122-
stepNames,
122+
steps,
123123
],
124124
);
125125

0 commit comments

Comments
 (0)