Skip to content

Commit 228b850

Browse files
authored
Merge pull request #106 from martinbroos/main
make sure stepCount doesn't count false / null or undefined child items
2 parents b6096cc + 176e317 commit 228b850

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/wizard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ 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);
14+
const stepCount = React.Children.toArray(children).length;
1515

1616
hasNextStep.current = activeStep < stepCount - 1;
1717
hasPreviousStep.current = activeStep > 0;

test/useWizard.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const renderUseWizardHook = (initialStartIndex = 0) => {
1212
wrapper: ({ children, startIndex }) => (
1313
<Wizard startIndex={startIndex}>
1414
<p>step 1 {children}</p>
15+
{false && <p>Optional step</p>}
1516
<p>step 2 {children}</p>
1617
</Wizard>
1718
),
@@ -37,6 +38,20 @@ describe('useWizard', () => {
3738
expect(result.current.stepCount).toBe(2);
3839
});
3940

41+
test('should set step count to one when using falsy step', () => {
42+
const { result } = renderHook(() => useWizard(), {
43+
wrapper: ({ children }) => (
44+
<Wizard>
45+
<p>step 1 {children}</p>
46+
<p>step 2 {children}</p>
47+
{false && <p>step 3 {children}</p>}
48+
</Wizard>
49+
),
50+
});
51+
52+
expect(result.current.stepCount).toBe(2);
53+
});
54+
4055
test('should set active step to one', () => {
4156
const { result } = renderUseWizardHook(1);
4257

0 commit comments

Comments
 (0)