@onewelcome/react-lib-components 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/dist/Button/IconButton.d.ts +2 -1
  2. package/dist/DataGrid/DataGrid.d.ts +1 -0
  3. package/dist/DataGrid/DataGridActions/DataGridActions.d.ts +2 -1
  4. package/dist/Form/Form.d.ts +3 -3
  5. package/dist/Notifications/SlideInModal/SlideInModal.d.ts +1 -1
  6. package/dist/Tabs/Tab.d.ts +5 -9
  7. package/dist/Tabs/TabButton.d.ts +3 -6
  8. package/dist/Tabs/Tabs.d.ts +1 -2
  9. package/dist/hooks/useDebouncedCallback.d.ts +1 -0
  10. package/dist/index.d.ts +1 -0
  11. package/dist/react-lib-components.cjs.development.js +472 -395
  12. package/dist/react-lib-components.cjs.development.js.map +1 -1
  13. package/dist/react-lib-components.cjs.production.min.js +1 -1
  14. package/dist/react-lib-components.cjs.production.min.js.map +1 -1
  15. package/dist/react-lib-components.esm.js +473 -397
  16. package/dist/react-lib-components.esm.js.map +1 -1
  17. package/package.json +1 -1
  18. package/src/Breadcrumbs/Breadcrumbs.tsx +46 -38
  19. package/src/Button/BaseButton.tsx +23 -20
  20. package/src/Button/Button.module.scss +9 -0
  21. package/src/Button/Button.tsx +40 -40
  22. package/src/Button/IconButton.tsx +28 -28
  23. package/src/ContextMenu/ContextMenu.tsx +161 -160
  24. package/src/ContextMenu/ContextMenuItem.tsx +55 -49
  25. package/src/DataGrid/DataGrid.tsx +1 -0
  26. package/src/DataGrid/DataGridActions/DataGridActions.module.scss +1 -1
  27. package/src/DataGrid/DataGridActions/DataGridActions.test.tsx +4 -2
  28. package/src/DataGrid/DataGridActions/DataGridActions.tsx +95 -87
  29. package/src/DataGrid/DataGridActions/DataGridColumnsToggle.tsx +64 -64
  30. package/src/DataGrid/DataGridBody/DataGridCell.tsx +42 -44
  31. package/src/DataGrid/DataGridBody/DataGridRow.tsx +29 -29
  32. package/src/DataGrid/DataGridHeader/DataGridHeader.tsx +78 -78
  33. package/src/DataGrid/DataGridHeader/DataGridHeaderCell.tsx +48 -48
  34. package/src/Form/Checkbox/Checkbox.tsx +134 -130
  35. package/src/Form/Fieldset/Fieldset.tsx +81 -78
  36. package/src/Form/Form.tsx +9 -4
  37. package/src/Form/FormControl/FormControl.module.scss +1 -20
  38. package/src/Form/FormControl/FormControl.tsx +36 -35
  39. package/src/Form/FormGroup/FormGroup.tsx +62 -62
  40. package/src/Form/FormHelperText/FormHelperText.tsx +19 -18
  41. package/src/Form/FormSelectorWrapper/FormSelectorWrapper.tsx +58 -53
  42. package/src/Form/Input/Input.tsx +90 -87
  43. package/src/Form/Label/Label.tsx +17 -16
  44. package/src/Form/Radio/Radio.tsx +91 -91
  45. package/src/Form/Select/Option.tsx +66 -60
  46. package/src/Form/Select/Select.tsx +207 -209
  47. package/src/Form/Textarea/Textarea.tsx +51 -53
  48. package/src/Form/Toggle/Toggle.tsx +26 -23
  49. package/src/Form/Wrapper/CheckboxWrapper/CheckboxWrapper.tsx +51 -43
  50. package/src/Form/Wrapper/InputWrapper/InputWrapper.tsx +112 -106
  51. package/src/Form/Wrapper/RadioWrapper/RadioWrapper.tsx +67 -62
  52. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.tsx +42 -37
  53. package/src/Form/Wrapper/TextareaWrapper/TextareaWrapper.tsx +94 -94
  54. package/src/Form/Wrapper/Wrapper/Wrapper.tsx +73 -73
  55. package/src/Icon/Icon.module.scss +1 -0
  56. package/src/Icon/Icon.tsx +19 -16
  57. package/src/Link/Link.tsx +68 -63
  58. package/src/Notifications/BaseModal/BaseModal.tsx +68 -68
  59. package/src/Notifications/BaseModal/BaseModalActions/BaseModalActions.tsx +13 -10
  60. package/src/Notifications/BaseModal/BaseModalContent/BaseModalContent.tsx +33 -25
  61. package/src/Notifications/BaseModal/BaseModalHeader/BaseModalHeader.tsx +20 -17
  62. package/src/Notifications/Dialog/Dialog.tsx +83 -83
  63. package/src/Notifications/Dialog/DialogActions/DialogActions.tsx +17 -14
  64. package/src/Notifications/Dialog/DialogTitle/DialogTitle.tsx +15 -12
  65. package/src/Notifications/DiscardChangesModal/DiscardChangesDialog/DiscardChangesDialog.tsx +40 -40
  66. package/src/Notifications/SlideInModal/SlideInModal.module.scss +5 -5
  67. package/src/Notifications/SlideInModal/SlideInModal.test.tsx +7 -2
  68. package/src/Notifications/SlideInModal/SlideInModal.tsx +47 -27
  69. package/src/Pagination/Pagination.tsx +169 -169
  70. package/src/Popover/Popover.module.scss +1 -0
  71. package/src/Popover/Popover.tsx +43 -33
  72. package/src/ProgressBar/ProgressBar.tsx +17 -14
  73. package/src/Skeleton/Skeleton.tsx +23 -20
  74. package/src/StatusIndicator/StatusIndicator.tsx +18 -15
  75. package/src/Tabs/{TabPanel.module.scss → Tab.module.scss} +1 -1
  76. package/src/Tabs/Tab.test.tsx +1 -39
  77. package/src/Tabs/Tab.tsx +16 -10
  78. package/src/Tabs/TabButton.module.scss +0 -4
  79. package/src/Tabs/TabButton.test.tsx +3 -31
  80. package/src/Tabs/TabButton.tsx +35 -49
  81. package/src/Tabs/Tabs.test.tsx +40 -33
  82. package/src/Tabs/Tabs.tsx +107 -101
  83. package/src/TextEllipsis/TextEllipsis.tsx +50 -41
  84. package/src/Tiles/Tile.tsx +58 -56
  85. package/src/Tiles/Tiles.tsx +44 -41
  86. package/src/Tooltip/Tooltip.tsx +101 -100
  87. package/src/Typography/Typography.tsx +47 -44
  88. package/src/Wizard/BaseWizardSteps/BaseWizardSteps.tsx +55 -52
  89. package/src/Wizard/WizardSteps/WizardSteps.tsx +25 -22
  90. package/src/hooks/useDebouncedCallback.test.ts +140 -0
  91. package/src/hooks/useDebouncedCallback.tsx +32 -0
  92. package/src/index.ts +1 -0
  93. package/src/mixins.module.scss +2 -2
  94. package/src/util/helper.test.tsx +0 -28
  95. package/dist/Tabs/TabPanel.d.ts +0 -8
  96. package/src/Tabs/TabPanel.test.tsx +0 -92
  97. package/src/Tabs/TabPanel.tsx +0 -43
@@ -16,6 +16,7 @@
16
16
 
17
17
  import React, {
18
18
  ComponentPropsWithRef,
19
+ ForwardRefRenderFunction,
19
20
  ReactElement,
20
21
  ReactNode,
21
22
  useEffect,
@@ -50,110 +51,110 @@ const defaultPosition: DefaultPosition = {
50
51
  transformOrigin: { horizontal: "left", vertical: "center" }
51
52
  };
52
53
 
53
- export const Tooltip = React.forwardRef<HTMLDivElement, Props>(
54
- (
55
- {
56
- children,
57
- className,
58
- placement = defaultPosition.placement,
59
- offset = defaultPosition.offset,
60
- transformOrigin = defaultPosition.transformOrigin,
61
- domRoot = document.body,
62
- label,
63
- ...rest
64
- }: Props,
65
- ref
66
- ) => {
67
- const [identifier] = useState(generateID());
68
- const [visible, setVisible] = useState(false);
69
-
70
- const relativeElement = useRef<HTMLDivElement>(null);
71
- const elementToBePositioned = useRef<HTMLDivElement>(null);
72
-
73
- const { top, bottom, right, left, calculatePosition } = usePosition({
74
- relativeElement: relativeElement,
75
- elementToBePositioned: elementToBePositioned,
76
- placement: placement,
77
- offset: offset,
78
- transformOrigin: transformOrigin
79
- });
80
-
81
- useEffect(() => {
82
- if (!visible) return;
83
-
84
- function escapePressHandler(event: KeyboardEvent) {
85
- if (event.key === "Escape") {
86
- setVisible(false);
87
- }
88
- }
54
+ const TooltipComponent: ForwardRefRenderFunction<HTMLDivElement, Props> = (
55
+ {
56
+ children,
57
+ className,
58
+ placement = defaultPosition.placement,
59
+ offset = defaultPosition.offset,
60
+ transformOrigin = defaultPosition.transformOrigin,
61
+ domRoot = document.body,
62
+ label,
63
+ ...rest
64
+ }: Props,
65
+ ref
66
+ ) => {
67
+ const [identifier] = useState(generateID());
68
+ const [visible, setVisible] = useState(false);
69
+
70
+ const relativeElement = useRef<HTMLDivElement>(null);
71
+ const elementToBePositioned = useRef<HTMLDivElement>(null);
72
+
73
+ const { top, bottom, right, left, calculatePosition } = usePosition({
74
+ relativeElement: relativeElement,
75
+ elementToBePositioned: elementToBePositioned,
76
+ placement: placement,
77
+ offset: offset,
78
+ transformOrigin: transformOrigin
79
+ });
89
80
 
90
- document.addEventListener("keyup", escapePressHandler);
91
-
92
- return () => {
93
- document.removeEventListener("keyup", escapePressHandler);
94
- };
95
- }, [visible]);
96
-
97
- useLayoutEffect(() => {
98
- calculatePosition();
99
- }, [visible]);
100
-
101
- const renderChildren = () => {
102
- if (React.isValidElement(label)) {
103
- return React.cloneElement(label as ReactElement, {
104
- onFocus: () => setVisible(true),
105
- onBlur: () => setVisible(false),
106
- "aria-describedby": identifier,
107
- tabIndex: 0,
108
- className: classes["label"]
109
- });
81
+ useEffect(() => {
82
+ if (!visible) return;
83
+
84
+ function escapePressHandler(event: KeyboardEvent) {
85
+ if (event.key === "Escape") {
86
+ setVisible(false);
110
87
  }
88
+ }
89
+
90
+ document.addEventListener("keyup", escapePressHandler);
111
91
 
112
- return (
113
- <span
114
- className={classes["label"]}
115
- tabIndex={0}
116
- onFocus={() => setVisible(true)}
117
- onBlur={() => setVisible(false)}
118
- aria-describedby={identifier}
119
- >
120
- {label}
121
- </span>
122
- );
92
+ return () => {
93
+ document.removeEventListener("keyup", escapePressHandler);
123
94
  };
95
+ }, [visible]);
96
+
97
+ useLayoutEffect(() => {
98
+ calculatePosition();
99
+ }, [visible]);
100
+
101
+ const renderChildren = () => {
102
+ if (React.isValidElement(label)) {
103
+ return React.cloneElement(label as ReactElement, {
104
+ onFocus: () => setVisible(true),
105
+ onBlur: () => setVisible(false),
106
+ "aria-describedby": identifier,
107
+ tabIndex: 0,
108
+ className: classes["label"]
109
+ });
110
+ }
124
111
 
125
112
  return (
126
- <div {...rest} ref={ref} className={`${classes.wrapper} ${className ?? ""}`}>
127
- {renderChildren()}
128
- <div className={`${classes["tooltip-wrapper"]}`}>
129
- <Icon
130
- ref={relativeElement}
131
- tag="div"
132
- onMouseEnter={() => setVisible(true)}
133
- onMouseLeave={() => setVisible(false)}
134
- icon={Icons.InfoCircle}
135
- className={classes.icon}
136
- />
137
- {createPortal(
138
- <div
139
- ref={elementToBePositioned}
140
- style={{
141
- ...rest.style,
142
- top: top,
143
- left: left,
144
- right: right,
145
- bottom: bottom
146
- }}
147
- aria-hidden={!visible}
148
- id={identifier}
149
- className={`${classes.tooltip} ${visible ? classes.visible : ""}`}
150
- >
151
- {children}
152
- </div>,
153
- domRoot
154
- )}
155
- </div>
156
- </div>
113
+ <span
114
+ className={classes["label"]}
115
+ tabIndex={0}
116
+ onFocus={() => setVisible(true)}
117
+ onBlur={() => setVisible(false)}
118
+ aria-describedby={identifier}
119
+ >
120
+ {label}
121
+ </span>
157
122
  );
158
- }
159
- );
123
+ };
124
+
125
+ return (
126
+ <div {...rest} ref={ref} className={`${classes.wrapper} ${className ?? ""}`}>
127
+ {renderChildren()}
128
+ <div className={`${classes["tooltip-wrapper"]}`}>
129
+ <Icon
130
+ ref={relativeElement}
131
+ tag="div"
132
+ onMouseEnter={() => setVisible(true)}
133
+ onMouseLeave={() => setVisible(false)}
134
+ icon={Icons.InfoCircle}
135
+ className={classes.icon}
136
+ />
137
+ {createPortal(
138
+ <div
139
+ ref={elementToBePositioned}
140
+ style={{
141
+ ...rest.style,
142
+ top: top,
143
+ left: left,
144
+ right: right,
145
+ bottom: bottom
146
+ }}
147
+ aria-hidden={!visible}
148
+ id={identifier}
149
+ className={`${classes.tooltip} ${visible ? classes.visible : ""}`}
150
+ >
151
+ {children}
152
+ </div>,
153
+ domRoot
154
+ )}
155
+ </div>
156
+ </div>
157
+ );
158
+ };
159
+
160
+ export const Tooltip = React.forwardRef(TooltipComponent);
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import React, { ComponentPropsWithRef, ReactNode } from "react";
17
+ import React, { ForwardRefRenderFunction, ComponentPropsWithRef, ReactNode } from "react";
18
18
  import classes from "./Typography.module.scss";
19
19
  import { Spacing, useSpacing } from "../hooks/useSpacing";
20
20
 
@@ -56,51 +56,54 @@ export interface Props extends ComponentPropsWithRef<any> {
56
56
  spacing?: Spacing;
57
57
  }
58
58
 
59
- export const Typography = React.forwardRef<any, Props>(
60
- ({ children, variant, tag, style, spacing, className = "", ...rest }: Props, ref) => {
61
- if (!validVariants.includes(variant)) {
62
- throw new Error(
63
- `You entered an invalid variant. You can choose from: ${validVariants}, you entered: ${variant}`
64
- );
65
- }
59
+ const TypographyComponent: ForwardRefRenderFunction<any, Props> = (
60
+ { children, variant, tag, style, spacing, className = "", ...rest }: Props,
61
+ ref
62
+ ) => {
63
+ if (!validVariants.includes(variant)) {
64
+ throw new Error(
65
+ `You entered an invalid variant. You can choose from: ${validVariants}, you entered: ${variant}`
66
+ );
67
+ }
66
68
 
67
- const styleWithSpacing = useSpacing(spacing, style);
69
+ const styleWithSpacing = useSpacing(spacing, style);
68
70
 
69
- if (!tag) {
70
- switch (variant) {
71
- case "h1":
72
- case "h2":
73
- case "h3":
74
- case "h4":
75
- case "code":
76
- tag = variant;
77
- break;
78
- case "body":
79
- tag = "p";
80
- break;
81
- case "body-bold":
82
- tag = "p";
83
- break;
84
- case "sub-text":
85
- tag = "span";
86
- break;
87
- default:
88
- tag = "div";
89
- break;
90
- }
71
+ if (!tag) {
72
+ switch (variant) {
73
+ case "h1":
74
+ case "h2":
75
+ case "h3":
76
+ case "h4":
77
+ case "code":
78
+ tag = variant;
79
+ break;
80
+ case "body":
81
+ tag = "p";
82
+ break;
83
+ case "body-bold":
84
+ tag = "p";
85
+ break;
86
+ case "sub-text":
87
+ tag = "span";
88
+ break;
89
+ default:
90
+ tag = "div";
91
+ break;
91
92
  }
93
+ }
92
94
 
93
- let TagName = tag;
95
+ let TagName = tag;
94
96
 
95
- return (
96
- <TagName
97
- {...rest}
98
- ref={ref}
99
- style={styleWithSpacing}
100
- className={`${classes["typography_style_" + variant]} ${className}`}
101
- >
102
- {children}
103
- </TagName>
104
- );
105
- }
106
- );
97
+ return (
98
+ <TagName
99
+ {...rest}
100
+ ref={ref}
101
+ style={styleWithSpacing}
102
+ className={`${classes["typography_style_" + variant]} ${className}`}
103
+ >
104
+ {children}
105
+ </TagName>
106
+ );
107
+ };
108
+
109
+ export const Typography = React.forwardRef(TypographyComponent);
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import React, { ComponentPropsWithRef, Fragment } from "react";
17
+ import React, { ForwardRefRenderFunction, ComponentPropsWithRef, Fragment } from "react";
18
18
  import classes from "./BaseWizardSteps.module.scss";
19
19
  import { Icon, Icons } from "../../Icon/Icon";
20
20
 
@@ -32,59 +32,62 @@ export interface Props extends Omit<ComponentPropsWithRef<"div">, "onClick"> {
32
32
  futureStepsClickable?: boolean;
33
33
  }
34
34
 
35
- export const BaseWizardSteps = React.forwardRef<HTMLDivElement, Props>(
36
- ({ steps, currentStepNo, onClick, futureStepsClickable = false, ...rest }: Props, ref) => {
37
- const getStepState = (stepNo: number): StepState => {
38
- if (currentStepNo === stepNo) {
39
- return "current";
40
- } else if (stepNo < currentStepNo) {
41
- return "finished";
42
- }
43
- return "future";
44
- };
35
+ const BaseWizardStepsComponent: ForwardRefRenderFunction<HTMLDivElement, Props> = (
36
+ { steps, currentStepNo, onClick, futureStepsClickable = false, ...rest }: Props,
37
+ ref
38
+ ) => {
39
+ const getStepState = (stepNo: number): StepState => {
40
+ if (currentStepNo === stepNo) {
41
+ return "current";
42
+ } else if (stepNo < currentStepNo) {
43
+ return "finished";
44
+ }
45
+ return "future";
46
+ };
45
47
 
46
- const getStepContent = (stepState: StepState, index: number, disabled?: boolean) => {
47
- const stepNumberString = String(index + 1);
48
- if (stepState === "finished") {
49
- return disabled ? null : <Icon className={classes["checkmark"]} icon={Icons.Checkmark} />;
50
- } else {
51
- return <Fragment>{stepNumberString}</Fragment>;
52
- }
53
- };
48
+ const getStepContent = (stepState: StepState, index: number, disabled?: boolean) => {
49
+ const stepNumberString = String(index + 1);
50
+ if (stepState === "finished") {
51
+ return disabled ? null : <Icon className={classes["checkmark"]} icon={Icons.Checkmark} />;
52
+ } else {
53
+ return <Fragment>{stepNumberString}</Fragment>;
54
+ }
55
+ };
54
56
 
55
- const generatedSteps = steps.map((step, index) => {
56
- const stepState = getStepState(index);
57
- const disabledStyleClassName = step.disabled ? classes["disabled"] : "";
58
- const clickableClassName = futureStepsClickable ? classes["clickable"] : "";
59
-
60
- return (
61
- <button
62
- key={step.label.toLowerCase().replace(/\s/, "-")}
63
- disabled={
64
- step.disabled ||
65
- (stepState === "future" && !futureStepsClickable) ||
66
- stepState === "current"
67
- }
68
- aria-current={stepState === "current" ? "step" : undefined}
69
- onClick={() => onClick && onClick(index)}
70
- className={`${classes["wizard-element"]} ${classes[stepState]} ${clickableClassName} ${disabledStyleClassName}`}
71
- >
72
- <div className={classes["number-wrapper"]}>
73
- <span className={classes["number"]}>
74
- {getStepContent(stepState, index, step.disabled)}
75
- </span>
76
- </div>
77
- <div className={classes["two-line-text-overflow"]}>
78
- <span className={classes["label"]}>{step.label}</span>
79
- </div>
80
- </button>
81
- );
82
- });
57
+ const generatedSteps = steps.map((step, index) => {
58
+ const stepState = getStepState(index);
59
+ const disabledStyleClassName = step.disabled ? classes["disabled"] : "";
60
+ const clickableClassName = futureStepsClickable ? classes["clickable"] : "";
83
61
 
84
62
  return (
85
- <div {...rest} ref={ref} className={classes["wizard"]}>
86
- {generatedSteps}
87
- </div>
63
+ <button
64
+ key={step.label.toLowerCase().replace(/\s/, "-")}
65
+ disabled={
66
+ step.disabled ||
67
+ (stepState === "future" && !futureStepsClickable) ||
68
+ stepState === "current"
69
+ }
70
+ aria-current={stepState === "current" ? "step" : undefined}
71
+ onClick={() => onClick && onClick(index)}
72
+ className={`${classes["wizard-element"]} ${classes[stepState]} ${clickableClassName} ${disabledStyleClassName}`}
73
+ >
74
+ <div className={classes["number-wrapper"]}>
75
+ <span className={classes["number"]}>
76
+ {getStepContent(stepState, index, step.disabled)}
77
+ </span>
78
+ </div>
79
+ <div className={classes["two-line-text-overflow"]}>
80
+ <span className={classes["label"]}>{step.label}</span>
81
+ </div>
82
+ </button>
88
83
  );
89
- }
90
- );
84
+ });
85
+
86
+ return (
87
+ <div {...rest} ref={ref} className={classes["wizard"]}>
88
+ {generatedSteps}
89
+ </div>
90
+ );
91
+ };
92
+
93
+ export const BaseWizardSteps = React.forwardRef(BaseWizardStepsComponent);
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import React, { ComponentPropsWithRef, useContext } from "react";
17
+ import React, { ForwardRefRenderFunction, ComponentPropsWithRef, useContext } from "react";
18
18
  import { WizardStateContext } from "../WizardStateProvider";
19
19
  import { BaseWizardSteps } from "../BaseWizardSteps/BaseWizardSteps";
20
20
  import { changeCurrentStepNo } from "../wizardStateReducer";
@@ -23,26 +23,29 @@ export interface Props extends ComponentPropsWithRef<"div"> {
23
23
  onStepClick: (currentStepNo: number, selectedStepNo: number) => boolean;
24
24
  }
25
25
 
26
- export const WizardSteps = React.forwardRef<HTMLDivElement, Props>(
27
- ({ onStepClick, ...rest }: Props, ref) => {
28
- const {
29
- state: { currentStepNo, mode, steps },
30
- dispatch
31
- } = useContext(WizardStateContext);
26
+ const WizardStepsComponent: ForwardRefRenderFunction<HTMLDivElement, Props> = (
27
+ { onStepClick, ...rest }: Props,
28
+ ref
29
+ ) => {
30
+ const {
31
+ state: { currentStepNo, mode, steps },
32
+ dispatch
33
+ } = useContext(WizardStateContext);
32
34
 
33
- const onClick = (selectedStepNo: number) => {
34
- onStepClick(currentStepNo, selectedStepNo) && dispatch(changeCurrentStepNo(selectedStepNo));
35
- };
35
+ const onClick = (selectedStepNo: number) => {
36
+ onStepClick(currentStepNo, selectedStepNo) && dispatch(changeCurrentStepNo(selectedStepNo));
37
+ };
36
38
 
37
- return (
38
- <BaseWizardSteps
39
- {...rest}
40
- ref={ref}
41
- onClick={onClick}
42
- steps={steps}
43
- currentStepNo={currentStepNo}
44
- futureStepsClickable={mode === "edit"}
45
- />
46
- );
47
- }
48
- );
39
+ return (
40
+ <BaseWizardSteps
41
+ {...rest}
42
+ ref={ref}
43
+ onClick={onClick}
44
+ steps={steps}
45
+ currentStepNo={currentStepNo}
46
+ futureStepsClickable={mode === "edit"}
47
+ />
48
+ );
49
+ };
50
+
51
+ export const WizardSteps = React.forwardRef(WizardStepsComponent);
@@ -0,0 +1,140 @@
1
+ import { renderHook, act } from "@testing-library/react-hooks";
2
+ import { useDebouncedCallback } from "./useDebouncedCallback";
3
+
4
+ const BASE_DELAY = 300;
5
+ const EXTRA_DELAY = 200;
6
+
7
+ const sleep = (delay: number) => new Promise(resolve => setTimeout(resolve, delay));
8
+
9
+ describe("Testing the useDebouncedCallback hook", () => {
10
+ it("should run callback after specified delay", async () => {
11
+ const delay = BASE_DELAY;
12
+ const callback = jest.fn();
13
+
14
+ const { result } = renderHook(() => useDebouncedCallback(callback, delay));
15
+
16
+ expect(callback).toHaveBeenCalledTimes(0);
17
+ act(result.current);
18
+ await sleep(delay + EXTRA_DELAY);
19
+
20
+ expect(callback).toHaveBeenCalledTimes(1);
21
+
22
+ act(result.current);
23
+ await sleep(delay + EXTRA_DELAY);
24
+
25
+ expect(callback).toHaveBeenCalledTimes(2);
26
+ });
27
+
28
+ it("should pass args through to callback function", async () => {
29
+ let mutableValue = "";
30
+ const delay = BASE_DELAY;
31
+ const callback = jest.fn(arg => (mutableValue = `got arg ${arg}`));
32
+
33
+ const { result } = renderHook(() => useDebouncedCallback(callback, delay));
34
+
35
+ act(() => result.current("passthrough"));
36
+ await sleep(delay + EXTRA_DELAY);
37
+
38
+ expect(callback).toHaveBeenCalledTimes(1);
39
+ expect(mutableValue).toBe(`got arg passthrough`);
40
+ });
41
+
42
+ it("should cancel pending call if fn is called again", async () => {
43
+ const delay = BASE_DELAY;
44
+ const callback = jest.fn();
45
+
46
+ const { result } = renderHook(() => useDebouncedCallback(callback, delay));
47
+
48
+ expect(callback).toHaveBeenCalledTimes(0);
49
+ act(result.current);
50
+ await sleep(delay / 2);
51
+
52
+ act(result.current);
53
+ await sleep(delay + EXTRA_DELAY);
54
+
55
+ expect(callback).toHaveBeenCalledTimes(1);
56
+ });
57
+
58
+ it("should cancel pending call if callback changes", async () => {
59
+ const delay = BASE_DELAY;
60
+ const callback1 = jest.fn();
61
+ const callback2 = jest.fn();
62
+
63
+ const { result, rerender } = renderHook(
64
+ ({ callback, delay }) => useDebouncedCallback(callback, delay),
65
+ { initialProps: { callback: callback1, delay } }
66
+ );
67
+
68
+ act(result.current);
69
+ await sleep(delay / 2);
70
+
71
+ rerender({ callback: callback2, delay });
72
+ act(result.current);
73
+ await sleep(delay + EXTRA_DELAY);
74
+
75
+ expect(callback1).toHaveBeenCalledTimes(0);
76
+ expect(callback2).toHaveBeenCalledTimes(1);
77
+ });
78
+
79
+ it("should cancel pending call if delay changes", async () => {
80
+ const delay1 = BASE_DELAY;
81
+ const delay2 = BASE_DELAY * 2;
82
+ const callback = jest.fn();
83
+
84
+ const { result, rerender } = renderHook(
85
+ ({ callback, delay }) => useDebouncedCallback(callback, delay),
86
+ { initialProps: { callback, delay: delay1 } }
87
+ );
88
+
89
+ act(result.current);
90
+ await sleep(delay1 / 2);
91
+
92
+ rerender({ callback, delay: delay2 });
93
+ act(result.current);
94
+ await sleep(delay2 + EXTRA_DELAY);
95
+
96
+ expect(callback).toHaveBeenCalledTimes(1);
97
+ });
98
+
99
+ it("should cancel pending call if dependencies change", async () => {
100
+ const delay = BASE_DELAY;
101
+ const callback = jest.fn();
102
+
103
+ const { result, rerender } = renderHook(
104
+ ({ callback, delay, dependencies }) => useDebouncedCallback(callback, delay, dependencies),
105
+ { initialProps: { callback, delay, dependencies: ["a"] } }
106
+ );
107
+
108
+ act(result.current);
109
+ await sleep(delay / 2);
110
+
111
+ rerender({ callback, delay, dependencies: ["b"] });
112
+ act(result.current);
113
+ await sleep(delay + EXTRA_DELAY);
114
+
115
+ expect(callback).toHaveBeenCalledTimes(1);
116
+ });
117
+
118
+ it("should support async callbacks", async () => {
119
+ const makeAsyncCall = jest.fn(
120
+ () =>
121
+ new Promise(async resolve => {
122
+ await sleep(delay / 2);
123
+ resolve(null);
124
+ })
125
+ );
126
+
127
+ const delay = BASE_DELAY;
128
+ const callback = jest.fn(async () => {
129
+ await makeAsyncCall();
130
+ });
131
+
132
+ const { result } = renderHook(() => useDebouncedCallback(callback, delay));
133
+
134
+ await act(async () => result.current());
135
+ await sleep(delay + EXTRA_DELAY);
136
+
137
+ expect(callback).toHaveBeenCalledTimes(1);
138
+ expect(makeAsyncCall).toHaveBeenCalledTimes(1);
139
+ });
140
+ });
@@ -0,0 +1,32 @@
1
+ /*
2
+ * Copyright 2022 OneWelcome B.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import React from "react";
18
+
19
+ export const useDebouncedCallback = (callback: Function, delay: number, dependencies?: any[]) => {
20
+ const timeout = React.useRef<ReturnType<typeof setTimeout>>();
21
+ const comboDeps = dependencies ? [callback, delay, ...dependencies] : [callback, delay];
22
+
23
+ return React.useCallback((...args) => {
24
+ if (timeout.current != null) {
25
+ clearTimeout(timeout.current);
26
+ }
27
+
28
+ timeout.current = setTimeout(() => {
29
+ callback(...args);
30
+ }, delay);
31
+ }, comboDeps);
32
+ };