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