Skip to content

Commit bca4207

Browse files
authored
Merge pull request #90 from ulken/feat-step_count
Add step count
2 parents ea41b9e + 81f094f commit bca4207

File tree

9 files changed

+36
-11
lines changed

9 files changed

+36
-11
lines changed

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Used to retrieve all methods and properties related to your wizard. Make sure `W
124124
| handleStep | (handler: Handler) => void | Attach a callback that will be called when calling `nextStep`. `handler` can be either sync or async |
125125
| isLoading | boolean | \* Will reflect the handler promise state: will be `true` if the handler promise is pending and `false` when the handler is either fulfilled or rejected |
126126
| activeStep | number | The current active step of the wizard |
127+
| stepCount | number | The total number of steps of the wizard |
127128
| isFirstStep | boolean | Indicate if the current step is the first step (aka no previous step) |
128129
| isLastStep | boolean | Indicate if the current step is the last step (aka no next step) |
129130
| |
@@ -149,6 +150,7 @@ const Step1 = () => {
149150
isLastStep,
150151
isFirstStep,
151152
activeStep,
153+
stepCount,
152154
previousStep,
153155
nextStep,
154156
goToStep,

examples/gatsby/src/pages/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Footer = () => {
1919
previousStep,
2020
isLoading,
2121
activeStep,
22+
stepCount,
2223
isLastStep,
2324
isFirstStep
2425
} = useWizard();
@@ -33,6 +34,10 @@ const Footer = () => {
3334
<p>
3435
Active step: {activeStep + 1} <br />
3536
</p>
37+
<br />
38+
<p>
39+
Total steps: {stepCount} <br />
40+
</p>
3641
</div>
3742
<div>
3843
<button

examples/nextjs/pages/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const Footer = () => {
1717
previousStep,
1818
isLoading,
1919
activeStep,
20+
stepCount,
2021
isLastStep,
2122
isFirstStep,
2223
} = useWizard();
@@ -31,6 +32,10 @@ const Footer = () => {
3132
<p>
3233
Active step: {activeStep + 1} <br />
3334
</p>
35+
<br />
36+
<p>
37+
Total steps: {stepCount} <br />
38+
</p>
3439
</div>
3540
<div>
3641
<button

playground/components/footer/footer.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const Info = styled('div')`
2222
margin: 0.25rem 0;
2323
}
2424
25-
@media screen and (min-width: 600px) {
25+
@media screen and (min-width: 768px) {
2626
flex-direction: row;
2727
gap: 1rem;
2828
@@ -38,6 +38,7 @@ const Footer: React.FC = () => {
3838
previousStep,
3939
isLoading,
4040
activeStep,
41+
stepCount,
4142
isLastStep,
4243
isFirstStep,
4344
} = useWizard();
@@ -53,6 +54,10 @@ const Footer: React.FC = () => {
5354
<p>
5455
Active step: {activeStep + 1} <br />
5556
</p>
57+
<br />
58+
<p>
59+
Total steps: {stepCount} <br />
60+
</p>
5661
</Info>
5762
<Actions>
5863
<Button

playground/components/footer/footerStepIndex.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const FooterGoToStepIndex: React.FC = () => {
1010
previousStep,
1111
isLoading,
1212
activeStep,
13+
stepCount,
1314
isLastStep,
1415
isFirstStep,
1516
} = useWizard();
@@ -25,6 +26,10 @@ const FooterGoToStepIndex: React.FC = () => {
2526
<p>
2627
Active step: {activeStep + 1} <br />
2728
</p>
29+
<br />
30+
<p>
31+
Total steps: {stepCount} <br />
32+
</p>
2833
</Info>
2934
<Actions>
3035
<Button

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export type WizardValues = {
3939
isLoading: boolean;
4040
/** The current active step of the wizard */
4141
activeStep: number;
42+
/** The total number of steps of the wizard */
43+
stepCount: number;
4244
/** Indicate if the current step is the first step (aka no previous step) */
4345
isFirstStep: boolean;
4446
/** Indicate if the current step is the last step (aka no next step) */

src/wizard.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const Wizard: React.FC<WizardProps> = React.memo(
1111
const hasNextStep = React.useRef(true);
1212
const hasPreviousStep = React.useRef(false);
1313
const nextStepHandler = React.useRef<Handler>(() => {});
14+
const stepCount = React.Children.count(children);
1415

15-
hasNextStep.current =
16-
activeStep < React.Children.toArray(children).length - 1;
16+
hasNextStep.current = activeStep < stepCount - 1;
1717
hasPreviousStep.current = activeStep > 0;
1818

1919
const goToNextStep = React.useRef(() => {
@@ -29,10 +29,7 @@ const Wizard: React.FC<WizardProps> = React.memo(
2929
});
3030

3131
const goToStep = React.useRef((stepIndex: number) => {
32-
if (
33-
stepIndex >= 0 &&
34-
stepIndex < React.Children.toArray(children).length
35-
) {
32+
if (stepIndex >= 0 && stepIndex < stepCount) {
3633
setActiveStep(stepIndex);
3734
} else {
3835
if (__DEV__) {
@@ -76,11 +73,12 @@ const Wizard: React.FC<WizardProps> = React.memo(
7673
handleStep: handleStep.current,
7774
isLoading,
7875
activeStep,
76+
stepCount,
7977
isFirstStep: !hasPreviousStep.current,
8078
isLastStep: !hasNextStep.current,
8179
goToStep: goToStep.current,
8280
}),
83-
[activeStep, isLoading],
81+
[activeStep, stepCount, isLoading],
8482
);
8583

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

test/useWizard.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ describe('useWizard', () => {
3131
expect(result.current.activeStep).toBe(0);
3232
});
3333

34+
test('should set step count to two', () => {
35+
const { result } = renderUseWizardHook();
36+
37+
expect(result.current.stepCount).toBe(2);
38+
});
39+
3440
test('should set active step to one', () => {
3541
const { result } = renderUseWizardHook(1);
3642

0 commit comments

Comments
 (0)