Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Tabs/OverflowTab.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.OverflowTab = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const tabs_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Tabs/tabs"));
8 const react_styles_1 = require("@patternfly/react-styles");
9 const rh_microns_caret_down_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-down-icon'));
10 const helpers_1 = require("../../helpers");
11 const Menu_1 = require("../Menu");
12 const TabsContext_1 = require("./TabsContext");
13 const TabTitleText_1 = require("./TabTitleText");
14 const OverflowTab = (_a) => {
15 var { className, overflowingTabs = [], showTabCount, defaultTitleText = 'More', toggleAriaLabel, zIndex = 9999, shouldPreventScrollOnItemFocus = true, focusTimeoutDelay = 0, popperProps } = _a, props = tslib_1.__rest(_a, ["className", "overflowingTabs", "showTabCount", "defaultTitleText", "toggleAriaLabel", "zIndex", "shouldPreventScrollOnItemFocus", "focusTimeoutDelay", "popperProps"]);
16 const menuRef = (0, react_1.useRef)(undefined);
17 const overflowTabRef = (0, react_1.useRef)(undefined);
18 const overflowLIRef = (0, react_1.useRef)(undefined);
19 const [isExpanded, setIsExpanded] = (0, react_1.useState)(false);
20 const { localActiveKey, handleTabClick } = (0, react_1.useContext)(TabsContext_1.TabsContext);
21 const closeMenu = () => {
22 setIsExpanded(false);
23 overflowTabRef.current.focus();
24 };
25 const handleMenuKeys = (ev) => {
26 var _a;
27 const menuContainsEventTarget = (_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) === null || _a === void 0 ? void 0 : _a.contains(ev.target);
28 if (isExpanded && menuContainsEventTarget && ev.key === 'Escape') {
29 closeMenu();
30 }
31 };
32 const handleClick = (ev) => {
33 var _a, _b;
34 const clickIsOutsideMenu = !((_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) === null || _a === void 0 ? void 0 : _a.contains(ev.target));
35 const clickIsOutsideOverflowTab = !((_b = overflowTabRef === null || overflowTabRef === void 0 ? void 0 : overflowTabRef.current) === null || _b === void 0 ? void 0 : _b.contains(ev.target));
36 if (isExpanded && clickIsOutsideMenu && clickIsOutsideOverflowTab) {
37 closeMenu();
38 }
39 };
40 (0, react_1.useEffect)(() => {
41 window.addEventListener('click', handleClick);
42 window.addEventListener('keydown', handleMenuKeys);
43 return () => {
44 window.removeEventListener('click', handleClick);
45 window.removeEventListener('keydown', handleMenuKeys);
46 };
47 }, [isExpanded, menuRef, overflowTabRef]);
48 const selectedTab = overflowingTabs.find((tab) => tab.eventKey === localActiveKey);
49 const tabTitle = (selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.title) ? selectedTab.title : defaultTitleText;
50 const toggleMenu = () => {
51 setIsExpanded((prevIsExpanded) => !prevIsExpanded);
52 setTimeout(() => {
53 if (menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) {
54 const firstElement = menuRef.current.querySelector('li > button,input:not(:disabled)');
55 firstElement && firstElement.focus({ preventScroll: shouldPreventScrollOnItemFocus });
56 }
57 }, focusTimeoutDelay);
58 };
59 const overflowTab = ((0, jsx_runtime_1.jsx)("li", Object.assign({ className: (0, react_styles_1.css)(tabs_1.default.tabsItem, tabs_1.default.modifiers.overflow, selectedTab && tabs_1.default.modifiers.current, className), role: "presentation", ref: overflowLIRef }, props, { children: (0, jsx_runtime_1.jsxs)("button", { type: "button", className: (0, react_styles_1.css)(tabs_1.default.tabsLink, isExpanded && tabs_1.default.modifiers.expanded), onClick: () => toggleMenu(), "aria-label": toggleAriaLabel, "aria-haspopup": "menu", "aria-expanded": isExpanded, role: "tab", ref: overflowTabRef, children: [(0, jsx_runtime_1.jsxs)(TabTitleText_1.TabTitleText, { children: [tabTitle, showTabCount && tabTitle === defaultTitleText && ` (${overflowingTabs.length})`] }), (0, jsx_runtime_1.jsx)("span", { className: tabs_1.default.tabsLinkToggleIcon, children: (0, jsx_runtime_1.jsx)(rh_microns_caret_down_icon_1.default, {}) })] }) })));
60 const tabs = overflowingTabs.map((tab) => ((0, jsx_runtime_1.jsx)(Menu_1.MenuItem, { itemId: tab.eventKey, isSelected: localActiveKey === tab.eventKey, children: tab.title }, tab.eventKey)));
61 const onTabSelect = (event, key) => {
62 closeMenu();
63 const selectedTabRef = overflowingTabs.find((tab) => tab.eventKey === key).tabContentRef;
64 handleTabClick(event, key, selectedTabRef);
65 };
66 const overflowMenu = ((0, jsx_runtime_1.jsx)(Menu_1.Menu, { ref: menuRef, onSelect: (ev, itemId) => onTabSelect(ev, itemId), children: (0, jsx_runtime_1.jsx)(Menu_1.MenuContent, { children: (0, jsx_runtime_1.jsx)(Menu_1.MenuList, { children: tabs }) }) }));
67 return ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [overflowTab, (0, jsx_runtime_1.jsx)(helpers_1.Popper, Object.assign({ triggerRef: overflowTabRef, popper: overflowMenu, popperRef: menuRef, isVisible: isExpanded, minWidth: "revert", appendTo: overflowLIRef.current, zIndex: zIndex }, popperProps))] }));
68 };
69 exports.OverflowTab = OverflowTab;
70 exports.OverflowTab.displayName = 'OverflowTab';
71 //# sourceMappingURL=OverflowTab.js.map