Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Pagination/PaginationOptionsMenu.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.PaginationOptionsMenu = 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 react_styles_1 = require("@patternfly/react-styles");
8 const pagination_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Pagination/pagination"));
9 const Menu_1 = require("../Menu");
10 const MenuToggle_1 = require("../MenuToggle");
11 const Popper_1 = require("../../helpers/Popper/Popper");
12 const ToggleTemplate_1 = require("./ToggleTemplate");
13 const helpers_1 = require("../../helpers");
14 const PaginationOptionsMenu = ({ className, widgetId, page: pageProp, itemCount, isDisabled = false, minWidth, dropDirection = 'down', perPageOptions = [],
15 // eslint-disable-next-line @typescript-eslint/no-unused-vars
16 itemsPerPageTitle = 'Items per page', perPageSuffix = 'per page', optionsToggleAriaLabel, ofWord = 'of', perPage = 0, firstIndex = 0, lastIndex = 0, isLastFullPageShown = false, itemsTitle = '', toggleTemplate, onPerPageSelect = () => null, containerRef, appendTo, shouldPreventScrollOnItemFocus = true, focusTimeoutDelay = 0 }) => {
17 const [isOpen, setIsOpen] = (0, react_1.useState)(false);
18 const toggleRef = (0, react_1.useRef)(null);
19 const menuRef = (0, react_1.useRef)(null);
20 const onToggle = () => {
21 setIsOpen((prevState) => !prevState);
22 };
23 const onSelect = () => {
24 var _a;
25 setIsOpen((prevState) => !prevState);
26 (_a = toggleRef.current) === null || _a === void 0 ? void 0 : _a.focus();
27 };
28 const handleNewPerPage = (_evt, newPerPage) => {
29 let newPage = pageProp;
30 while (Math.ceil(itemCount / newPerPage) < newPage) {
31 newPage--;
32 }
33 if (isLastFullPageShown) {
34 if (itemCount / newPerPage !== newPage) {
35 while (newPage > 1 && itemCount - newPerPage * newPage < 0) {
36 newPage--;
37 }
38 }
39 }
40 const startIdx = (newPage - 1) * newPerPage;
41 const endIdx = newPage * newPerPage;
42 return onPerPageSelect(_evt, newPerPage, newPage, startIdx, endIdx);
43 };
44 (0, react_1.useEffect)(() => {
45 const handleMenuKeys = (event) => {
46 var _a, _b, _c;
47 // Close the menu on tab or escape
48 if ((isOpen && ((_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target))) ||
49 ((_b = toggleRef.current) === null || _b === void 0 ? void 0 : _b.contains(event.target))) {
50 if (event.key === 'Escape' || event.key === 'Tab') {
51 setIsOpen(false);
52 (_c = toggleRef.current) === null || _c === void 0 ? void 0 : _c.focus();
53 }
54 }
55 };
56 const handleClick = (event) => {
57 var _a, _b, _c;
58 // Focus the first non-disabled menu item on toggle 'click'
59 if (isOpen && ((_a = toggleRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target))) {
60 setTimeout(() => {
61 var _a;
62 const firstElement = (_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('li button:not(:disabled)');
63 firstElement && firstElement.focus({ preventScroll: shouldPreventScrollOnItemFocus });
64 }, focusTimeoutDelay);
65 }
66 // If the event is not on the toggle, close the menu
67 if (isOpen &&
68 !((_b = toggleRef === null || toggleRef === void 0 ? void 0 : toggleRef.current) === null || _b === void 0 ? void 0 : _b.contains(event.target)) &&
69 !((_c = menuRef.current) === null || _c === void 0 ? void 0 : _c.contains(event.target))) {
70 setIsOpen(false);
71 }
72 };
73 window.addEventListener('keydown', handleMenuKeys);
74 window.addEventListener('click', handleClick);
75 return () => {
76 window.removeEventListener('keydown', handleMenuKeys);
77 window.removeEventListener('click', handleClick);
78 };
79 }, [focusTimeoutDelay, isOpen, menuRef, shouldPreventScrollOnItemFocus]);
80 const renderItems = () => perPageOptions.map(({ value, title }) => ((0, jsx_runtime_1.jsxs)(Menu_1.MenuItem, { "data-action": `per-page-${value}`, isSelected: perPage === value, onClick: (event) => handleNewPerPage(event, value), children: [title, ` ${perPageSuffix}`] }, value)));
81 const toggle = ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(pagination_1.default.paginationPageMenu), children: (0, jsx_runtime_1.jsxs)(MenuToggle_1.MenuToggle, Object.assign({ ref: toggleRef, onClick: onToggle }, (optionsToggleAriaLabel && { 'aria-label': optionsToggleAriaLabel }), { isDisabled: isDisabled || (itemCount && itemCount <= 0), isExpanded: isOpen }, (widgetId && { id: `${widgetId}-toggle` }), { variant: "plainText", "aria-haspopup": "listbox", children: [toggleTemplate &&
82 typeof toggleTemplate === 'string' &&
83 (0, helpers_1.fillTemplate)(toggleTemplate, { firstIndex, lastIndex, ofWord, itemCount, itemsTitle }), toggleTemplate &&
84 typeof toggleTemplate !== 'string' &&
85 toggleTemplate({
86 firstIndex,
87 lastIndex,
88 ofWord,
89 itemCount,
90 itemsTitle
91 }), !toggleTemplate && ((0, jsx_runtime_1.jsx)(ToggleTemplate_1.ToggleTemplate, { firstIndex: firstIndex, lastIndex: lastIndex, ofWord: ofWord, itemCount: itemCount, itemsTitle: itemsTitle }))] })) }));
92 const menu = ((0, jsx_runtime_1.jsx)(Menu_1.Menu, { className: (0, react_styles_1.css)(className), onSelect: onSelect, ref: menuRef, children: (0, jsx_runtime_1.jsx)(Menu_1.MenuContent, { children: (0, jsx_runtime_1.jsx)(Menu_1.MenuList, { children: renderItems() }) }) }));
93 const containerToAppendTo = appendTo !== null && appendTo !== void 0 ? appendTo : ((containerRef === null || containerRef === void 0 ? void 0 : containerRef.current) || undefined);
94 return ((0, jsx_runtime_1.jsx)(Popper_1.Popper, { trigger: toggle, triggerRef: toggleRef, popper: menu, popperRef: menuRef, isVisible: isOpen, direction: dropDirection, appendTo: containerToAppendTo, minWidth: minWidth !== undefined ? minWidth : 'revert' }));
95 };
96 exports.PaginationOptionsMenu = PaginationOptionsMenu;
97 exports.PaginationOptionsMenu.displayName = 'PaginationOptionsMenu';
98 //# sourceMappingURL=PaginationOptionsMenu.js.map