Skip to content

Commit 06fc7e8

Browse files
committed
chore(Wizard) Updates wizard utilities to use hooks and compound-components (#3200)
* install cc * use CC in wiz * useControlled * rm isControlled check * lint
1 parent 3b0cf7c commit 06fc7e8

File tree

10 files changed

+37
-191
lines changed

10 files changed

+37
-191
lines changed

packages/wizard/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"dependencies": {
3131
"@leafygreen-ui/button": "workspace:^",
32+
"@leafygreen-ui/compound-component": "workspace:^",
3233
"@leafygreen-ui/descendants": "workspace:^",
3334
"@leafygreen-ui/emotion": "workspace:^",
3435
"@leafygreen-ui/form-footer": "workspace:^",

packages/wizard/src/Wizard/Wizard.tsx

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import React from 'react';
1+
import React, { useCallback } from 'react';
22

3+
import {
4+
CompoundComponent,
5+
findChild,
6+
findChildren,
7+
} from '@leafygreen-ui/compound-component';
38
import { Direction } from '@leafygreen-ui/descendants';
4-
import { findChild, findChildren } from '@leafygreen-ui/lib';
9+
import { useControlled } from '@leafygreen-ui/hooks';
510

611
import { WizardSubComponentProperties } from '../constants';
7-
import { CompoundComponent } from '../utils/CompoundComponent';
8-
import { useWizardControlledValue } from '../utils/useWizardControlledValue/useWizardControlledValue';
912
import { WizardProvider } from '../WizardContext/WizardContext';
1013
import { WizardFooter } from '../WizardFooter';
1114
import { WizardStep } from '../WizardStep';
@@ -30,11 +33,8 @@ export const Wizard = CompoundComponent(
3033
);
3134

3235
// Controlled/Uncontrolled activeStep value
33-
const {
34-
isControlled,
35-
value: activeStep,
36-
setValue: setActiveStep,
37-
} = useWizardControlledValue<number>(activeStepProp, undefined, 0);
36+
const { value: activeStep, updateValue: setActiveStep } =
37+
useControlled<number>(activeStepProp, onStepChange, 0);
3838

3939
if (
4040
activeStepProp &&
@@ -47,22 +47,23 @@ export const Wizard = CompoundComponent(
4747
);
4848
}
4949

50-
const updateStep = (direction: Direction) => {
51-
const getNextStep = (curr: number) => {
52-
switch (direction) {
53-
case Direction.Next:
54-
return Math.min(curr + 1, stepChildren.length - 1);
55-
case Direction.Prev:
56-
return Math.max(curr - 1, 0);
57-
}
58-
};
50+
const updateStep = useCallback(
51+
(direction: Direction) => {
52+
const getNextStep = (curr: number) => {
53+
switch (direction) {
54+
case Direction.Next:
55+
return Math.min(curr + 1, stepChildren.length - 1);
56+
case Direction.Prev:
57+
return Math.max(curr - 1, 0);
58+
}
59+
};
5960

60-
if (!isControlled) {
61-
setActiveStep(getNextStep);
62-
}
63-
64-
onStepChange?.(getNextStep(activeStep));
65-
};
61+
// TODO pass getNextStep into setter as callback https://jira.mongodb.org/browse/LG-5607
62+
const nextStep = getNextStep(activeStep);
63+
setActiveStep(nextStep);
64+
},
65+
[activeStep, setActiveStep, stepChildren.length],
66+
);
6667

6768
// Get the current step to render
6869
const currentStep = stepChildren[activeStep] || null;

packages/wizard/src/WizardFooter/WizardFooter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { MouseEventHandler } from 'react';
22

3+
import { CompoundSubComponent } from '@leafygreen-ui/compound-component';
34
import { Direction } from '@leafygreen-ui/descendants';
45
import { FormFooter } from '@leafygreen-ui/form-footer';
56
import { consoleOnce } from '@leafygreen-ui/lib';
67

78
import { WizardSubComponentProperties } from '../constants';
8-
import { CompoundSubComponent } from '../utils/CompoundSubComponent';
99
import { useWizardContext } from '../WizardContext';
1010

1111
import { WizardFooterProps } from './WizardFooter.types';

packages/wizard/src/WizardStep/WizardStep.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22

3+
import { CompoundSubComponent } from '@leafygreen-ui/compound-component';
34
import { cx } from '@leafygreen-ui/emotion';
45
import { consoleOnce } from '@leafygreen-ui/lib';
56
import { Description, H3 } from '@leafygreen-ui/typography';
67

78
import { WizardSubComponentProperties } from '../constants';
8-
import { CompoundSubComponent } from '../utils/CompoundSubComponent';
99
import { useWizardContext } from '../WizardContext';
1010

1111
import { TextNode } from './TextNode';

packages/wizard/src/utils/CompoundComponent.tsx

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

packages/wizard/src/utils/CompoundSubComponent.tsx

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

packages/wizard/src/utils/useWizardControlledValue/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/wizard/src/utils/useWizardControlledValue/useWizardControlledValue.ts

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

packages/wizard/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
"include": ["src/**/*"],
1010
"exclude": ["**/*.spec.*", "**/*.stories.*"],
1111
"references": [
12+
{
13+
"path": "../button"
14+
},
15+
{
16+
"path": "../compound-component"
17+
},
1218
{
1319
"path": "../button"
1420
},

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)