carbon-react 106.6.1 → 106.6.4

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 (108) hide show
  1. package/esm/__spec_helper__/expect.d.ts +8 -1
  2. package/esm/__spec_helper__/expect.js +1 -5
  3. package/esm/__spec_helper__/test-utils.d.ts +71 -33
  4. package/esm/__spec_helper__/test-utils.js +63 -65
  5. package/esm/components/action-popover/action-popover-context.d.ts +7 -2
  6. package/esm/components/action-popover/action-popover-context.js +1 -1
  7. package/esm/components/action-popover/action-popover-divider/action-popover-divider.component.d.ts +1 -1
  8. package/esm/components/action-popover/action-popover-item/action-popover-item.component.d.ts +28 -41
  9. package/esm/components/action-popover/action-popover-item/action-popover-item.component.js +99 -118
  10. package/esm/components/action-popover/action-popover-menu/action-popover-menu.component.d.ts +35 -2
  11. package/esm/components/action-popover/action-popover-menu/action-popover-menu.component.js +42 -53
  12. package/esm/components/action-popover/action-popover-menu-button/action-popover-menu-button.component.d.ts +20 -18
  13. package/esm/components/action-popover/action-popover-menu-button/action-popover-menu-button.component.js +13 -5
  14. package/esm/components/action-popover/action-popover-test.stories.js +76 -0
  15. package/esm/components/action-popover/action-popover.component.d.ts +30 -53
  16. package/esm/components/action-popover/action-popover.component.js +205 -67
  17. package/esm/components/action-popover/action-popover.style.d.ts +17 -11
  18. package/esm/components/action-popover/action-popover.style.js +20 -26
  19. package/esm/components/action-popover/index.d.ts +9 -5
  20. package/esm/components/box/box.component.d.ts +12 -1
  21. package/esm/components/box/box.component.js +12 -23
  22. package/esm/components/box/box.config.d.ts +10 -12
  23. package/esm/components/box/index.d.ts +2 -1
  24. package/esm/components/button-bar/button-bar-test.stories.js +122 -0
  25. package/esm/components/button-bar/button-bar.component.d.ts +12 -15
  26. package/esm/components/button-bar/button-bar.component.js +331 -33
  27. package/esm/components/button-bar/button-bar.config.d.ts +2 -2
  28. package/esm/components/button-bar/button-bar.style.d.ts +4 -4
  29. package/esm/components/button-bar/button-bar.style.js +18 -33
  30. package/esm/components/button-bar/index.d.ts +2 -1
  31. package/esm/components/dismissible-box/dismissible-box.style.d.ts +1 -2
  32. package/esm/components/drawer/drawer.style.d.ts +1 -2
  33. package/esm/components/duelling-picklist/picklist/picklist.style.d.ts +2 -3
  34. package/esm/components/flat-table/flat-table.style.d.ts +1 -2
  35. package/esm/components/form/form.component.js +2 -1
  36. package/esm/components/link/link.component.js +1 -5
  37. package/esm/components/menu/__internal__/spec-helper/index.js +2 -1
  38. package/esm/components/menu/__internal__/submenu/submenu.component.js +21 -4
  39. package/esm/components/menu/menu.component.js +2 -1
  40. package/esm/components/menu/scrollable-block/scrollable-block.component.js +4 -2
  41. package/esm/components/modal/modal.component.js +3 -60
  42. package/esm/components/toast/toast.component.js +3 -13
  43. package/esm/hooks/__internal__/useModalManager/index.d.ts +1 -0
  44. package/esm/hooks/__internal__/useModalManager/index.js +1 -0
  45. package/esm/hooks/__internal__/useModalManager/useModalManager.d.ts +3 -0
  46. package/esm/hooks/__internal__/useModalManager/useModalManager.js +66 -0
  47. package/lib/__spec_helper__/expect.d.ts +8 -1
  48. package/lib/__spec_helper__/expect.js +2 -4
  49. package/lib/__spec_helper__/test-utils.d.ts +71 -33
  50. package/lib/__spec_helper__/test-utils.js +63 -65
  51. package/lib/components/action-popover/action-popover-context.d.ts +7 -2
  52. package/lib/components/action-popover/action-popover-context.js +1 -1
  53. package/lib/components/action-popover/action-popover-divider/action-popover-divider.component.d.ts +1 -1
  54. package/lib/components/action-popover/action-popover-item/action-popover-item.component.d.ts +28 -41
  55. package/lib/components/action-popover/action-popover-item/action-popover-item.component.js +100 -119
  56. package/lib/components/action-popover/action-popover-menu/action-popover-menu.component.d.ts +35 -2
  57. package/lib/components/action-popover/action-popover-menu/action-popover-menu.component.js +44 -55
  58. package/lib/components/action-popover/action-popover-menu-button/action-popover-menu-button.component.d.ts +20 -18
  59. package/lib/components/action-popover/action-popover-menu-button/action-popover-menu-button.component.js +13 -5
  60. package/lib/components/action-popover/action-popover-test.stories.js +95 -0
  61. package/lib/components/action-popover/action-popover.component.d.ts +30 -53
  62. package/lib/components/action-popover/action-popover.component.js +206 -71
  63. package/lib/components/action-popover/action-popover.style.d.ts +17 -11
  64. package/lib/components/action-popover/action-popover.style.js +21 -28
  65. package/lib/components/action-popover/index.d.ts +9 -5
  66. package/lib/components/box/box.component.d.ts +12 -1
  67. package/lib/components/box/box.component.js +13 -26
  68. package/lib/components/box/box.config.d.ts +10 -12
  69. package/lib/components/box/index.d.ts +2 -1
  70. package/lib/components/button-bar/button-bar-test.stories.js +145 -0
  71. package/lib/components/button-bar/button-bar.component.d.ts +12 -15
  72. package/lib/components/button-bar/button-bar.component.js +336 -35
  73. package/lib/components/button-bar/button-bar.config.d.ts +2 -2
  74. package/lib/components/button-bar/button-bar.style.d.ts +4 -4
  75. package/lib/components/button-bar/button-bar.style.js +18 -35
  76. package/lib/components/button-bar/index.d.ts +2 -1
  77. package/lib/components/dismissible-box/dismissible-box.style.d.ts +1 -2
  78. package/lib/components/drawer/drawer.style.d.ts +1 -2
  79. package/lib/components/duelling-picklist/picklist/picklist.style.d.ts +2 -3
  80. package/lib/components/flat-table/flat-table.style.d.ts +1 -2
  81. package/lib/components/form/form.component.js +2 -1
  82. package/lib/components/link/link.component.js +1 -5
  83. package/lib/components/menu/__internal__/spec-helper/index.js +2 -1
  84. package/lib/components/menu/__internal__/submenu/submenu.component.js +21 -4
  85. package/lib/components/menu/menu.component.js +2 -1
  86. package/lib/components/menu/scrollable-block/scrollable-block.component.js +4 -2
  87. package/lib/components/modal/modal.component.js +3 -62
  88. package/lib/components/toast/toast.component.js +4 -17
  89. package/lib/hooks/__internal__/useModalManager/index.d.ts +1 -0
  90. package/lib/hooks/__internal__/useModalManager/index.js +15 -0
  91. package/lib/hooks/__internal__/useModalManager/package.json +6 -0
  92. package/lib/hooks/__internal__/useModalManager/useModalManager.d.ts +3 -0
  93. package/lib/hooks/__internal__/useModalManager/useModalManager.js +79 -0
  94. package/package.json +5 -1
  95. package/esm/components/action-popover/action-popover-divider/action-popover-divider.d.ts +0 -12
  96. package/esm/components/action-popover/action-popover-item/action-popover-item.d.ts +0 -26
  97. package/esm/components/action-popover/action-popover-menu/action-popover-menu.d.ts +0 -33
  98. package/esm/components/action-popover/action-popover-menu-button/action-popover-menu-button.d.ts +0 -13
  99. package/esm/components/action-popover/action-popover.d.ts +0 -39
  100. package/esm/components/box/box.d.ts +0 -31
  101. package/esm/components/button-bar/button-bar.d.ts +0 -24
  102. package/lib/components/action-popover/action-popover-divider/action-popover-divider.d.ts +0 -12
  103. package/lib/components/action-popover/action-popover-item/action-popover-item.d.ts +0 -26
  104. package/lib/components/action-popover/action-popover-menu/action-popover-menu.d.ts +0 -33
  105. package/lib/components/action-popover/action-popover-menu-button/action-popover-menu-button.d.ts +0 -13
  106. package/lib/components/action-popover/action-popover.d.ts +0 -39
  107. package/lib/components/box/box.d.ts +0 -31
  108. package/lib/components/button-bar/button-bar.d.ts +0 -24
@@ -1,32 +1,26 @@
1
1
  import styled, { css } from "styled-components";
2
2
  import { space } from "styled-system";
3
- import PropTypes from "prop-types";
4
- import propTypes from "@styled-system/prop-types";
5
3
  import BaseTheme from "../../style/themes/base";
6
4
  import StyledIcon from "../icon/icon.style";
7
- const ButtonBar = styled.div`
5
+ const StyledButtonBar = styled.div`
8
6
  ${space}
9
- ${stylingForType}
10
- `;
11
7
 
12
- function stylingForType({
8
+ ${({
9
+ fullWidth,
13
10
  size
14
- }) {
15
- return css`
16
- ${({
17
- fullWidth
18
- }) => fullWidth && css`
11
+ }) => css`
12
+ ${fullWidth && css`
13
+ width: 100%;
14
+ display: flex;
15
+ button {
16
+ box-sizing: content-box;
17
+ padding: 0;
19
18
  width: 100%;
20
- display: flex;
21
- button {
22
- box-sizing: content-box;
23
- padding: 0;
24
- width: 100%;
25
- ${size === "small" && "min-height: 28px"}
26
- ${size === "medium" && "min-height: 36px"}
19
+ ${size === "small" && "min-height: 28px"}
20
+ ${size === "medium" && "min-height: 36px"}
27
21
  ${size === "large" && "min-height: 44px"}
28
- }
29
- `}
22
+ }
23
+ `}
30
24
 
31
25
  button {
32
26
  margin: 0;
@@ -57,19 +51,10 @@ function stylingForType({
57
51
  color: var(--colorsActionMajor500);
58
52
  }
59
53
  }
60
- `;
61
- }
62
-
63
- ButtonBar.defaultProps = {
54
+ `}
55
+ `;
56
+ StyledButtonBar.defaultProps = {
64
57
  theme: BaseTheme,
65
58
  size: "medium"
66
59
  };
67
- ButtonBar.propTypes = {
68
- /** Styled system spacing props */
69
- ...propTypes.space,
70
-
71
- /** Assigns a size to the button: "small" | "medium" | "large" */
72
- size: PropTypes.oneOf(["small", "medium", "large"]),
73
- children: PropTypes.node.isRequired
74
- };
75
- export default ButtonBar;
60
+ export default StyledButtonBar;
@@ -1 +1,2 @@
1
- export { default } from "./button-bar";
1
+ export { default } from "./button-bar.component";
2
+ export type { ButtonBarProps } from "./button-bar.component";
@@ -1,3 +1,2 @@
1
- declare var _default: import("styled-components").StyledComponent<typeof Box, any, {}, never>;
1
+ declare var _default: import("styled-components").StyledComponent<"div", any, import("../box").BoxProps, never>;
2
2
  export default _default;
3
- import Box from "../box";
@@ -2,9 +2,8 @@ export const StyledSidebarHeader: import("styled-components").StyledComponent<"d
2
2
  export const StyledDrawerWrapper: import("styled-components").StyledComponent<"div", any, {}, never>;
3
3
  export const StyledDrawerContent: import("styled-components").StyledComponent<"div", any, {}, never>;
4
4
  export const StyledDrawerChildren: import("styled-components").StyledComponent<"div", any, {}, never>;
5
- export const StyledDrawerSidebar: import("styled-components").StyledComponent<typeof Box, any, {}, never>;
5
+ export const StyledDrawerSidebar: import("styled-components").StyledComponent<"div", any, import("../box").BoxProps, never>;
6
6
  export const StyledSidebarTitle: import("styled-components").StyledComponent<"div", any, {}, never>;
7
7
  export const StyledButton: import("styled-components").StyledComponent<"button", any, {
8
8
  type: "button";
9
9
  }, "type">;
10
- import Box from "../box";
@@ -1,5 +1,4 @@
1
- export const StyledPicklist: import("styled-components").StyledComponent<typeof Box, any, {
2
- as: "ul";
1
+ export const StyledPicklist: import("styled-components").StyledComponent<"div", any, import("../../box").BoxProps & {
2
+ as: string;
3
3
  }, "as">;
4
4
  export const StyledEmptyContainer: import("styled-components").StyledComponent<"li", any, {}, never>;
5
- import Box from "../../box";
@@ -1,5 +1,4 @@
1
- export const StyledFlatTableWrapper: import("styled-components").StyledComponent<typeof Box, any, {}, never>;
1
+ export const StyledFlatTableWrapper: import("styled-components").StyledComponent<"div", any, import("../box").BoxProps, never>;
2
2
  export const StyledFlatTable: import("styled-components").StyledComponent<"table", any, {}, never>;
3
3
  export const StyledFlatTableFooter: import("styled-components").StyledComponent<"div", any, {}, never>;
4
4
  export const StyledTableContainer: import("styled-components").StyledComponent<"div", any, {}, never>;
5
- import Box from "../box";
@@ -27,6 +27,7 @@ const Form = ({
27
27
  } = useContext(SidebarContext);
28
28
  const formRef = useRef();
29
29
  const formFooterRef = useRef();
30
+ const renderFooter = !!(saveButton || leftSideButtons || rightSideButtons || errorCount || warningCount);
30
31
  return /*#__PURE__*/React.createElement(StyledForm, _extends({
31
32
  ref: formRef,
32
33
  stickyFooter: stickyFooter,
@@ -40,7 +41,7 @@ const Form = ({
40
41
  "data-element": "form-content",
41
42
  stickyFooter: stickyFooter,
42
43
  className: stickyFooter ? "sticky" : ""
43
- }, children), /*#__PURE__*/React.createElement(StyledFormFooter, {
44
+ }, children), renderFooter && /*#__PURE__*/React.createElement(StyledFormFooter, {
44
45
  "data-element": "form-footer",
45
46
  className: stickyFooter ? "sticky" : "",
46
47
  ref: formFooterRef,
@@ -86,11 +86,7 @@ const Link = /*#__PURE__*/React.forwardRef(({
86
86
  type = "button";
87
87
  }
88
88
 
89
- return /*#__PURE__*/React.createElement(type, { ...componentProps,
90
- ...(type === "button" && {
91
- role: "link"
92
- })
93
- }, /*#__PURE__*/React.createElement(React.Fragment, null, renderLinkIcon(), /*#__PURE__*/React.createElement(StyledContent, null, isSkipLink ? l.link.skipLinkLabel() : children), renderLinkIcon("right")));
89
+ return /*#__PURE__*/React.createElement(type, componentProps, /*#__PURE__*/React.createElement(React.Fragment, null, renderLinkIcon(), /*#__PURE__*/React.createElement(StyledContent, null, isSkipLink ? l.link.skipLinkLabel() : children), renderLinkIcon("right")));
94
90
  };
95
91
 
96
92
  return /*#__PURE__*/React.createElement(StyledLink, _extends({
@@ -9,7 +9,8 @@ const events = {
9
9
  };
10
10
 
11
11
  const openSubmenu = wrapper => {
12
- const menuItem = wrapper.find('[data-component="submenu-wrapper"]').find("a");
12
+ const menuWrapper = wrapper.find('[data-component="submenu-wrapper"]');
13
+ const menuItem = menuWrapper.exists("a") ? menuWrapper.find("a") : menuWrapper.find("button");
13
14
  menuItem.getDOMNode().focus();
14
15
  act(() => {
15
16
  wrapper.find(StyledMenuItemWrapper).at(0).props().onKeyDown(events.space);
@@ -27,6 +27,7 @@ const Submenu = /*#__PURE__*/React.forwardRef(({
27
27
  asPassiveItem,
28
28
  onSubmenuOpen,
29
29
  onSubmenuClose,
30
+ onClick,
30
31
  ...rest
31
32
  }, ref) => {
32
33
  const [blockDoubleFocus, setBlockDoubleFocus] = useState(false);
@@ -188,6 +189,17 @@ const Submenu = /*#__PURE__*/React.forwardRef(({
188
189
  closeSubmenu();
189
190
  }
190
191
  }, [closeSubmenu]);
192
+
193
+ const handleClick = event => {
194
+ if (clickToOpen) {
195
+ openSubmenu();
196
+ }
197
+
198
+ if (onClick) {
199
+ onClick(event);
200
+ }
201
+ };
202
+
191
203
  useEffect(() => {
192
204
  if (characterString !== "") {
193
205
  const nextIndex = characterNavigation(characterString, React.Children.toArray(formattedChildren), submenuFocusIndex);
@@ -241,7 +253,6 @@ const Submenu = /*#__PURE__*/React.forwardRef(({
241
253
  "data-component": "submenu-wrapper",
242
254
  onMouseOver: !clickToOpen ? () => openSubmenu() : undefined,
243
255
  onMouseLeave: () => closeSubmenu(),
244
- onClick: clickToOpen ? () => openSubmenu() : undefined,
245
256
  ref: submenuRef,
246
257
  isSubmenuOpen: submenuOpen
247
258
  }, /*#__PURE__*/React.createElement(StyledMenuItemWrapper, _extends({}, rest, {
@@ -256,14 +267,17 @@ const Submenu = /*#__PURE__*/React.forwardRef(({
256
267
  hasSubmenu: true,
257
268
  showDropdownArrow: showDropdownArrow,
258
269
  onKeyDown: handleKeyDown,
270
+ onClick: handleClick,
259
271
  clickToOpen: clickToOpen,
260
272
  href: href,
261
- maxWidth: maxWidth
273
+ maxWidth: maxWidth,
274
+ "aria-expanded": submenuOpen
262
275
  }), title), submenuOpen && /*#__PURE__*/React.createElement(StyledSubmenu, {
263
276
  "data-component": "submenu",
264
277
  submenuDirection: submenuDirection,
265
278
  variant: variant,
266
- menuType: menuContext.menuType
279
+ menuType: menuContext.menuType,
280
+ role: "list"
267
281
  }, React.Children.map(children, (child, index) => /*#__PURE__*/React.createElement(SubmenuContext.Provider, {
268
282
  value: {
269
283
  isFocused: !blockDoubleFocus && submenuFocusIndex === index,
@@ -320,6 +334,9 @@ Submenu.propTypes = {
320
334
  onSubmenuOpen: PropTypes.func,
321
335
 
322
336
  /** Callback triggered when submenu closes. Only valid with submenu prop */
323
- onSubmenuClose: PropTypes.func
337
+ onSubmenuClose: PropTypes.func,
338
+
339
+ /** Callback triggered when the top-level menu item is clicked */
340
+ onClick: PropTypes.func
324
341
  };
325
342
  export default Submenu;
@@ -36,7 +36,8 @@ const Menu = ({
36
36
  "data-component": "menu",
37
37
  menuType: menuType
38
38
  }, rest, {
39
- ref: ref
39
+ ref: ref,
40
+ role: "list"
40
41
  }), React.Children.map(children, (child, index) => {
41
42
  const isFocused = focusedItemIndex === index;
42
43
  return /*#__PURE__*/React.createElement(MenuContext.Provider, {
@@ -29,12 +29,14 @@ const ScrollableBlock = ({
29
29
  return /*#__PURE__*/React.createElement(StyledScrollableBlock, _extends({
30
30
  "data-component": "submenu-scrollable-block",
31
31
  menuType: menuContext.menuType,
32
- variant: variant
32
+ variant: variant,
33
+ role: "presentation"
33
34
  }, rest), /*#__PURE__*/React.createElement(Box, {
34
35
  overflowY: "scroll",
35
36
  scrollVariant: scrollVariants[menuContext.menuType],
36
37
  height: height,
37
- p: 0
38
+ p: 0,
39
+ role: "presentation"
38
40
  }, React.Children.map(children, (child, index) => {
39
41
  let isFocused = false;
40
42
  const blockItemFocused = focusIndex >= blockIndex;
@@ -6,7 +6,7 @@ import { TransitionGroup, CSSTransition } from "react-transition-group";
6
6
  import useScrollBlock from "../../hooks/__internal__/useScrollBlock";
7
7
  import Portal from "../portal";
8
8
  import Events from "../../__internal__/utils/helpers/events";
9
- import ModalManager from "./__internal__/modal-manager";
9
+ import useModalManager from "../../hooks/__internal__/useModalManager";
10
10
  import { StyledModal, StyledModalBackground } from "./modal.style";
11
11
  export const ModalContext = /*#__PURE__*/React.createContext({});
12
12
 
@@ -23,8 +23,6 @@ const Modal = ({
23
23
  const ref = useRef();
24
24
  const backgroundNodeRef = useRef();
25
25
  const contentNodeRef = useRef();
26
- const listenerAdded = useRef(false);
27
- const modalRegistered = useRef(false);
28
26
  const [isAnimationComplete, setAnimationComplete] = useState(false);
29
27
  const [triggerRefocusFlag, setTriggerRefocusFlag] = useState(false);
30
28
  const {
@@ -50,67 +48,12 @@ const Modal = ({
50
48
  };
51
49
  }, [allowScroll, enableBackgroundUI]);
52
50
  const closeModal = useCallback(ev => {
53
- const isTopmost = ModalManager.isTopmost(ref.current);
54
-
55
- if (onCancel && !disableClose && !disableEscKey && Events.isEscKey(ev) && isTopmost) {
51
+ if (onCancel && !disableClose && !disableEscKey && Events.isEscKey(ev)) {
56
52
  ev.stopImmediatePropagation();
57
53
  onCancel(ev);
58
54
  }
59
55
  }, [disableClose, disableEscKey, onCancel]);
60
- const addListener = useCallback(() => {
61
- /* istanbul ignore else */
62
- if (!listenerAdded.current) {
63
- document.addEventListener("keyup", closeModal);
64
- listenerAdded.current = true;
65
- }
66
- }, [closeModal]);
67
- const removeListener = useCallback(() => {
68
- if (listenerAdded.current) {
69
- document.removeEventListener("keyup", closeModal);
70
- listenerAdded.current = false;
71
- }
72
- }, [closeModal]);
73
- useEffect(() => {
74
- if (open) {
75
- addListener();
76
- }
77
-
78
- if (!open) {
79
- removeListener();
80
- }
81
- }, [addListener, open, removeListener]);
82
- useEffect(() => {
83
- return () => {
84
- removeListener();
85
- };
86
- }, [removeListener]);
87
- const registerModal = useCallback(() => {
88
- /* istanbul ignore else */
89
- if (!modalRegistered.current) {
90
- ModalManager.addModal(ref.current, setTriggerRefocusFlag);
91
- modalRegistered.current = true;
92
- }
93
- }, []);
94
- const unregisterModal = useCallback(() => {
95
- if (modalRegistered.current) {
96
- ModalManager.removeModal(ref.current);
97
- modalRegistered.current = false;
98
- }
99
- }, []);
100
- useEffect(() => {
101
- if (open) {
102
- registerModal();
103
- }
104
-
105
- if (!open) {
106
- unregisterModal();
107
- }
108
- }, [open, registerModal, unregisterModal]);
109
- useEffect(() => {
110
- return () => {
111
- unregisterModal();
112
- };
113
- }, [unregisterModal]);
56
+ useModalManager(open, closeModal, ref, setTriggerRefocusFlag);
114
57
  let background;
115
58
  let content;
116
59
 
@@ -8,9 +8,9 @@ import Icon from "../icon";
8
8
  import tagComponent from "../../__internal__/utils/helpers/tags/tags";
9
9
  import { ToastStyle, TypeIcon, ToastContentStyle, ToastWrapper, StyledPortal } from "./toast.style";
10
10
  import IconButton from "../icon-button";
11
- import ModalManager from "../modal/__internal__/modal-manager";
12
11
  import Events from "../../__internal__/utils/helpers/events";
13
12
  import useLocale from "../../hooks/__internal__/useLocale";
13
+ import useModalManager from "../../hooks/__internal__/useModalManager";
14
14
  import Logger from "../../__internal__/utils/logger";
15
15
  let deprecatedWarnTriggered = false;
16
16
 
@@ -42,22 +42,12 @@ const Toast = ({
42
42
  return classNames(className);
43
43
  }, [className]);
44
44
  const dismissToast = useCallback(ev => {
45
- const isTopmost = ModalManager.isTopmost(toastRef.current);
46
-
47
- if (onDismiss && Events.isEscKey(ev) && isTopmost) {
45
+ if (onDismiss && Events.isEscKey(ev)) {
48
46
  ev.stopImmediatePropagation();
49
47
  onDismiss(ev);
50
48
  }
51
49
  }, [onDismiss]);
52
- useEffect(() => {
53
- const currentElement = toastRef.current;
54
- ModalManager.addModal(currentElement);
55
- document.addEventListener("keyup", dismissToast);
56
- return () => {
57
- ModalManager.removeModal(currentElement);
58
- document.removeEventListener("keyup", dismissToast);
59
- };
60
- }, [dismissToast]);
50
+ useModalManager(open, dismissToast, toastRef);
61
51
  useEffect(() => {
62
52
  clearTimeout(timer.current);
63
53
 
@@ -0,0 +1 @@
1
+ export { default } from "./useModalManager";
@@ -0,0 +1 @@
1
+ export { default } from "./useModalManager";
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ declare const useModalManager: (open: boolean, closeModal: (e: KeyboardEvent) => void, modalRef: React.RefObject<HTMLElement>, setTriggerRefocusFlag?: ((flag: boolean) => void) | undefined) => void;
3
+ export default useModalManager;
@@ -0,0 +1,66 @@
1
+ import { useEffect, useRef, useCallback } from "react";
2
+ import ModalManager from "../../../components/modal/__internal__/modal-manager";
3
+
4
+ const useModalManager = (open, closeModal, modalRef, setTriggerRefocusFlag) => {
5
+ const listenerAdded = useRef(false);
6
+ const modalRegistered = useRef(false);
7
+ const handleClose = useCallback(ev => {
8
+ const isTopmost = ModalManager.isTopmost(modalRef.current);
9
+
10
+ if (isTopmost) {
11
+ closeModal(ev);
12
+ }
13
+ }, [modalRef, closeModal]);
14
+ const addListener = useCallback(() => {
15
+ /* istanbul ignore else */
16
+ if (!listenerAdded.current) {
17
+ document.addEventListener("keyup", handleClose);
18
+ listenerAdded.current = true;
19
+ }
20
+ }, [handleClose]);
21
+ const removeListener = useCallback(() => {
22
+ if (listenerAdded.current) {
23
+ document.removeEventListener("keyup", handleClose);
24
+ listenerAdded.current = false;
25
+ }
26
+ }, [handleClose]);
27
+ useEffect(() => {
28
+ if (open) {
29
+ addListener();
30
+ } else {
31
+ removeListener();
32
+ }
33
+ }, [addListener, open, removeListener]);
34
+ useEffect(() => {
35
+ return () => {
36
+ removeListener();
37
+ };
38
+ }, [removeListener]);
39
+ const registerModal = useCallback(() => {
40
+ /* istanbul ignore else */
41
+ if (!modalRegistered.current) {
42
+ ModalManager.addModal(modalRef.current, setTriggerRefocusFlag);
43
+ modalRegistered.current = true;
44
+ }
45
+ }, [modalRef, setTriggerRefocusFlag]);
46
+ const unregisterModal = useCallback(() => {
47
+ if (modalRegistered.current) {
48
+ ModalManager.removeModal(modalRef.current);
49
+ modalRegistered.current = false;
50
+ }
51
+ }, [modalRef]);
52
+ useEffect(() => {
53
+ if (open) {
54
+ registerModal();
55
+ } else {
56
+ unregisterModal();
57
+ }
58
+ }, [open, registerModal, unregisterModal]);
59
+ useEffect(() => {
60
+ return () => {
61
+ unregisterModal();
62
+ };
63
+ }, [unregisterModal]);
64
+ };
65
+
66
+ export default useModalManager;
@@ -1 +1,8 @@
1
- export {};
1
+ import "jest-styled-components";
2
+ declare global {
3
+ namespace jest {
4
+ interface Matchers<R> {
5
+ toBeFocused(): R;
6
+ }
7
+ }
8
+ }
@@ -4,9 +4,7 @@ require("jest-styled-components");
4
4
 
5
5
  var _enzyme = require("enzyme");
6
6
 
7
- const {
8
- diff
9
- } = require("jest-matcher-utils");
7
+ var _jestMatcherUtils = require("jest-matcher-utils");
10
8
 
11
9
  expect.extend({
12
10
  toBeFocused(received) {
@@ -19,7 +17,7 @@ expect.extend({
19
17
  const hint = this.utils.matcherHint("toBeFocused", "DOMNode", "", options);
20
18
 
21
19
  const positive = () => {
22
- const diffString = diff(expected, DOMNode, {
20
+ const diffString = (0, _jestMatcherUtils.diff)(expected, DOMNode, {
23
21
  expand: this.expand
24
22
  });
25
23
  return `${hint}\n\n${// eslint-disable-next-line multiline-ternary
@@ -1,34 +1,72 @@
1
- export function getDefaultValue(value: any): any;
2
- export function assertStyleMatch(styleSpec: any, component: any, opts: any): void;
3
- export function toCSSCase(str: any): any;
4
- export function hoverList(wrapper: any): (item: any) => void;
5
- export function selectedItemOf(wrapper: any): any;
6
- export function childrenFrom(node: any): any;
7
- export function makeArrayKeys(n: any): number[];
8
- export const keyboard: {};
9
- export function assertKeyboardTraversal({ num, nonSelectables, }: {
10
- num: any;
11
- nonSelectables?: any[] | undefined;
12
- }): (wrapper: any) => void;
13
- export function assertHoverTraversal({ num, nonSelectables, }: {
14
- num: any;
15
- nonSelectables?: any[] | undefined;
16
- }): (wrapper: any) => void;
17
- export function listFrom(wrapper: any): any;
18
- export function click(wrapper: any): any;
19
- export namespace simulate {
20
- export { keydown };
21
- }
22
- export const carbonThemesJestTable: (string | Partial<import("style/themes/base/base-theme.config").ThemeObject> | undefined)[][];
1
+ /// <reference types="react" />
2
+ import { ReactWrapper, ShallowWrapper } from "enzyme";
3
+ import { LayoutProps, FlexboxProps, BackgroundProps } from "styled-system";
4
+ import { ReactTestRendererJSON } from "react-test-renderer";
23
5
  import { mockMatchMedia } from "./mock-match-media";
24
- export function testStyledSystemSpacing(component: any, defaults: any, styleContainer: any, assertOpts: any): void;
25
- export function testStyledSystemMargin(component: any, defaults: any, styleContainer: any, assertOpts: any): void;
26
- export function testStyledSystemPadding(component: any, defaults: any, styleContainer: any, assertOpts: any): void;
27
- export function testStyledSystemColor(component: any, styleContainer: any): void;
28
- export function testStyledSystemWidth(component: any, styleContainer: any): void;
29
- export function testStyledSystemLayout(component: any, styleContainer: any): void;
30
- export function testStyledSystemFlexBox(component: any, styleContainer: any): void;
31
- export function testStyledSystemBackground(component: any, styleContainer: any): void;
32
- export function expectConsoleOutput(message: any, type?: string): () => void;
33
- declare const keydown: {};
34
- export { mockMatchMedia };
6
+ declare const toCSSCase: (str: string) => string;
7
+ declare const assertStyleMatch: <Props>(styleSpec: {
8
+ [key: string]: string | number | undefined;
9
+ }, component: ReactWrapper<Props, {}, import("react").Component<{}, {}, any>> | ShallowWrapper<Props, {}, import("react").Component<{}, {}, any>> | ReactTestRendererJSON | ReactTestRendererJSON[] | null, opts?: jest.Options | undefined) => void;
10
+ declare const makeArrayKeys: (n: number) => number[];
11
+ declare const keyMap: {
12
+ readonly UpArrow: 38;
13
+ readonly DownArrow: 40;
14
+ readonly RightArrow: 39;
15
+ readonly LeftArrow: 37;
16
+ readonly Enter: 13;
17
+ readonly Tab: 9;
18
+ readonly Space: 32;
19
+ readonly Escape: 27;
20
+ readonly End: 35;
21
+ readonly Home: 36;
22
+ readonly D: 68;
23
+ readonly E: 69;
24
+ readonly P: 80;
25
+ readonly Z: 90;
26
+ readonly 1: 49;
27
+ };
28
+ declare type Keys = keyof typeof keyMap;
29
+ declare type MappedKeys = `press${Keys}`;
30
+ declare type KeyboardAccumulatorType = Record<MappedKeys, () => void>;
31
+ declare const keyboard: KeyboardAccumulatorType;
32
+ declare type KeydownAccumulatorType = Record<MappedKeys, (wrapper: ReactWrapper<any>, options?: {
33
+ shiftKey: boolean;
34
+ }) => void>;
35
+ declare const simulate: {
36
+ keydown: KeydownAccumulatorType;
37
+ };
38
+ declare const listFrom: (wrapper: ReactWrapper) => ReactWrapper<import("enzyme").HTMLAttributes, any, import("react").Component<{}, {}, any>>;
39
+ declare const childrenFrom: (node: ReactWrapper) => ReactWrapper<any, any, import("react").Component<{}, {}, any>>;
40
+ declare const hoverList: (wrapper: ReactWrapper) => (item: number) => void;
41
+ declare const click: (wrapper: ReactWrapper) => ReactWrapper<{}, {}, import("react").Component<{}, {}, any>>;
42
+ declare const selectedItemOf: (wrapper: ReactWrapper) => number;
43
+ declare const assertKeyboardTraversal: ({ num, nonSelectables, }: {
44
+ num: number;
45
+ nonSelectables?: number[] | undefined;
46
+ }) => (wrapper: ReactWrapper) => void;
47
+ declare const assertHoverTraversal: ({ num, nonSelectables, }: {
48
+ num: number;
49
+ nonSelectables?: number[] | undefined;
50
+ }) => (wrapper: ReactWrapper) => void;
51
+ declare const carbonThemesJestTable: (string | Partial<import("style/themes/base/base-theme.config").ThemeObject> | undefined)[][];
52
+ declare const marginProps: readonly [readonly ["m", "margin"], readonly ["ml", "marginLeft"], readonly ["mr", "marginRight"], readonly ["mt", "marginTop"], readonly ["mb", "marginBottom"], readonly ["mx", "marginLeft"], readonly ["mx", "marginRight"], readonly ["my", "marginTop"], readonly ["my", "marginBottom"]];
53
+ declare type MarginProps = {
54
+ [K in typeof marginProps[number][0]]?: string | number;
55
+ };
56
+ declare const paddingProps: readonly [readonly ["p", "padding"], readonly ["pl", "paddingLeft"], readonly ["pr", "paddingRight"], readonly ["pt", "paddingTop"], readonly ["pb", "paddingBottom"], readonly ["px", "paddingLeft"], readonly ["px", "paddingRight"], readonly ["py", "paddingTop"], readonly ["py", "paddingBottom"]];
57
+ declare type PaddingProps = {
58
+ [K in typeof paddingProps[number][0]]?: string | number;
59
+ };
60
+ export declare const getDefaultValue: (value?: string | number | undefined) => string | number | undefined;
61
+ declare const testStyledSystemMargin: (component: (spacingProps?: MarginProps | undefined) => JSX.Element, defaults?: MarginProps | undefined, styleContainer?: ((wrapper: ReactWrapper) => ReactWrapper) | undefined, assertOpts?: jest.Options | undefined) => void;
62
+ declare const testStyledSystemPadding: (component: (spacingProps?: PaddingProps | undefined) => JSX.Element, defaults?: PaddingProps | undefined, styleContainer?: ((wrapper: ReactWrapper) => ReactWrapper) | undefined, assertOpts?: jest.Options | undefined) => void;
63
+ declare const testStyledSystemSpacing: (component: (spacingProps?: MarginProps | PaddingProps | undefined) => JSX.Element, defaults?: MarginProps | PaddingProps | undefined, styleContainer?: ((wrapper: ReactWrapper) => ReactWrapper) | undefined, assertOpts?: jest.Options | undefined) => void;
64
+ declare const testStyledSystemColor: (component: (colorProperties?: any) => JSX.Element, styleContainer?: ((wrapper: ReactWrapper) => void) | undefined) => void;
65
+ declare const testStyledSystemWidth: (component: (widthProperties?: {
66
+ width: string;
67
+ } | undefined) => JSX.Element, styleContainer?: ((wrapper: ReactWrapper) => ReactWrapper) | undefined) => void;
68
+ declare const testStyledSystemLayout: (component: (layoutProperties?: LayoutProps<Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>> | undefined) => JSX.Element, styleContainer?: ((wrapper: ReactWrapper) => ReactWrapper) | undefined) => void;
69
+ declare const testStyledSystemFlexBox: (component: (flexboxProperties?: FlexboxProps<Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>> | undefined) => JSX.Element, styleContainer?: ((wrapper: ReactWrapper) => ReactWrapper) | undefined) => void;
70
+ declare const testStyledSystemBackground: (component: (backgroundProperties?: BackgroundProps<Required<import("styled-system").Theme<import("styled-system").TLengthStyledSystem>>, import("csstype").Property.Background<import("styled-system").TLengthStyledSystem>> | undefined) => JSX.Element, styleContainer?: ((wrapper: ReactWrapper) => ReactWrapper) | undefined) => void;
71
+ declare const expectConsoleOutput: (message: string, type?: "warn" | "error") => () => void;
72
+ export { assertStyleMatch, toCSSCase, hoverList, selectedItemOf, childrenFrom, makeArrayKeys, keyboard, assertKeyboardTraversal, assertHoverTraversal, listFrom, click, simulate, carbonThemesJestTable, mockMatchMedia, testStyledSystemSpacing, testStyledSystemMargin, testStyledSystemPadding, testStyledSystemColor, testStyledSystemWidth, testStyledSystemLayout, testStyledSystemFlexBox, testStyledSystemBackground, expectConsoleOutput, };