Debian Codegraph @

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

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Pagination = exports.PaginationVariant = 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 ToggleTemplate_1 = require("./ToggleTemplate");
8 const pagination_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Pagination/pagination"));
9 const react_styles_1 = require("@patternfly/react-styles");
10 const helpers_1 = require("../../helpers");
11 const PaginationNavigation_1 = require("./PaginationNavigation");
12 const PaginationOptionsMenu_1 = require("./PaginationOptionsMenu");
13 const helpers_2 = require("../../helpers");
14 const util_1 = require("../../helpers/util");
15 const c_pagination__nav_page_select_c_form_control_width_chars_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_pagination__nav_page_select_c_form_control_width_chars'));
16 var PaginationVariant;
17 (function (PaginationVariant) {
18 PaginationVariant["bottom"] = "bottom";
19 PaginationVariant["top"] = "top";
20 })(PaginationVariant || (exports.PaginationVariant = PaginationVariant = {}));
21 const defaultPerPageOptions = [
22 {
23 title: '10',
24 value: 10
25 },
26 {
27 title: '20',
28 value: 20
29 },
30 {
31 title: '50',
32 value: 50
33 },
34 {
35 title: '100',
36 value: 100
37 }
38 ];
39 const handleInputWidth = (lastPage, node) => {
40 if (!node) {
41 return;
42 }
43 const len = String(lastPage).length;
44 if (len >= 3) {
45 node.style.setProperty(c_pagination__nav_page_select_c_form_control_width_chars_1.default.name, `${len}`);
46 }
47 else {
48 node.style.setProperty(c_pagination__nav_page_select_c_form_control_width_chars_1.default.name, '2');
49 }
50 };
51 const Pagination = (_a) => {
52 var { children, className = '', variant = PaginationVariant.top, isDisabled = false, isCompact = false, isStatic = false, isSticky = false, isStickyBase = false, isStickyStuck = false, dropDirection: dropDirectionProp, toggleTemplate, perPage = defaultPerPageOptions[0].value, titles = {
53 items: '',
54 page: '',
55 pages: '',
56 itemsPerPage: 'Items per page',
57 perPageSuffix: 'per page',
58 toFirstPageAriaLabel: 'Go to first page',
59 toPreviousPageAriaLabel: 'Go to previous page',
60 toLastPageAriaLabel: 'Go to last page',
61 toNextPageAriaLabel: 'Go to next page',
62 optionsToggleAriaLabel: '',
63 currPageAriaLabel: 'Current page',
64 paginationAriaLabel: 'Pagination',
65 ofWord: 'of'
66 }, firstPage = 1, page: pageProp = 1, offset = null, isLastFullPageShown = false, itemsStart = null, itemsEnd = null, itemCount, perPageOptions = defaultPerPageOptions, widgetId = 'options-menu', onSetPage = () => undefined, onPerPageSelect = () => undefined, onFirstClick = () => undefined, onPreviousClick = () => undefined, onNextClick = () => undefined, onPageInput = () => undefined, onLastClick = () => undefined, ouiaId, ouiaSafe = true, usePageInsets, inset, menuAppendTo } = _a, props = tslib_1.__rest(_a, ["children", "className", "variant", "isDisabled", "isCompact", "isStatic", "isSticky", "isStickyBase", "isStickyStuck", "dropDirection", "toggleTemplate", "perPage", "titles", "firstPage", "page", "offset", "isLastFullPageShown", "itemsStart", "itemsEnd", "itemCount", "perPageOptions", "widgetId", "onSetPage", "onPerPageSelect", "onFirstClick", "onPreviousClick", "onNextClick", "onPageInput", "onLastClick", "ouiaId", "ouiaSafe", "usePageInsets", "inset", "menuAppendTo"]);
67 const paginationRef = (0, react_1.useRef)(null);
68 const containerRef = (0, react_1.useRef)(null);
69 const getLastPage = () =>
70 // when itemCount is not known let's set lastPage as page+1 as we don't know the total count
71 itemCount || itemCount === 0 ? Math.ceil(itemCount / perPage) || 0 : page + 1;
72 (0, react_1.useEffect)(() => {
73 const node = paginationRef.current;
74 handleInputWidth(getLastPage(), node);
75 }, [perPage, itemCount]);
76 const dropDirection = dropDirectionProp || (variant === 'bottom' && !isStatic ? 'up' : 'down');
77 let page = pageProp;
78 if (offset !== null) {
79 itemsStart = offset + 1;
80 page = Math.max(Math.ceil(itemsStart / perPage), 1);
81 itemsEnd = offset + perPage;
82 }
83 const lastPage = getLastPage();
84 let firstIndex = (page - 1) * perPage + 1;
85 let lastIndex = page * perPage;
86 if (itemCount || itemCount === 0) {
87 firstIndex = itemCount <= 0 ? 0 : (page - 1) * perPage + 1;
88 if (page < firstPage && itemCount > 0) {
89 page = firstPage;
90 }
91 else if (page > lastPage) {
92 page = lastPage;
93 }
94 if (itemCount >= 0) {
95 lastIndex = page === lastPage || itemCount === 0 ? itemCount : page * perPage;
96 }
97 }
98 const PaginationToggleTemplateProps = {
99 firstIndex,
100 lastIndex,
101 itemCount,
102 itemsTitle: titles.items,
103 ofWord: titles.ofWord
104 };
105 return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ ref: paginationRef, className: (0, react_styles_1.css)(pagination_1.default.pagination, variant === PaginationVariant.bottom && pagination_1.default.modifiers.bottom, usePageInsets && pagination_1.default.modifiers.pageInsets, (0, util_1.formatBreakpointMods)(inset, pagination_1.default), isStatic && pagination_1.default.modifiers.static, isSticky && !isStickyBase && !isStickyStuck && pagination_1.default.modifiers.sticky, isStickyBase && pagination_1.default.modifiers.stickyBase, isStickyStuck && pagination_1.default.modifiers.stickyStuck, className) }, (widgetId && { id: `${widgetId}-${variant}-pagination` }), (0, helpers_2.useOUIAProps)(exports.Pagination.displayName, ouiaId, ouiaSafe, variant), props, { children: [variant === PaginationVariant.top && ((0, jsx_runtime_1.jsxs)("div", { className: (0, react_styles_1.css)(pagination_1.default.paginationTotalItems), children: [toggleTemplate &&
106 typeof toggleTemplate === 'string' &&
107 (0, helpers_1.fillTemplate)(toggleTemplate, PaginationToggleTemplateProps), toggleTemplate &&
108 typeof toggleTemplate !== 'string' &&
109 toggleTemplate(PaginationToggleTemplateProps), !toggleTemplate && ((0, jsx_runtime_1.jsx)(ToggleTemplate_1.ToggleTemplate, { firstIndex: firstIndex, lastIndex: lastIndex, itemCount: itemCount, itemsTitle: titles.items, ofWord: titles.ofWord }))] })), perPageOptions && perPageOptions.length > 0 && ((0, jsx_runtime_1.jsx)(PaginationOptionsMenu_1.PaginationOptionsMenu, { itemsPerPageTitle: titles.itemsPerPage, perPageSuffix: titles.perPageSuffix, itemsTitle: isCompact ? '' : titles.items, optionsToggleAriaLabel: titles.optionsToggleAriaLabel, perPageOptions: perPageOptions, firstIndex: itemsStart !== null ? itemsStart : firstIndex, lastIndex: itemsEnd !== null ? itemsEnd : lastIndex, ofWord: titles.ofWord, isLastFullPageShown: isLastFullPageShown, itemCount: itemCount, page: page, perPage: perPage, lastPage: lastPage, onPerPageSelect: onPerPageSelect, dropDirection: dropDirection, widgetId: `${widgetId}-${variant}`, toggleTemplate: toggleTemplate, isDisabled: isDisabled, containerRef: containerRef, appendTo: menuAppendTo })), (0, jsx_runtime_1.jsx)(PaginationNavigation_1.PaginationNavigation, { pagesTitle: titles.page, pagesTitlePlural: titles.pages, toLastPageAriaLabel: titles.toLastPageAriaLabel, toPreviousPageAriaLabel: titles.toPreviousPageAriaLabel, toNextPageAriaLabel: titles.toNextPageAriaLabel, toFirstPageAriaLabel: titles.toFirstPageAriaLabel, currPageAriaLabel: titles.currPageAriaLabel, paginationAriaLabel: titles.paginationAriaLabel, ofWord: titles.ofWord, page: itemCount && itemCount <= 0 ? 0 : page, perPage: perPage, itemCount: itemCount, firstPage: itemsStart !== null ? itemsStart : 1, lastPage: lastPage, onSetPage: onSetPage, onFirstClick: onFirstClick, onPreviousClick: onPreviousClick, onNextClick: onNextClick, onLastClick: onLastClick, onPageInput: onPageInput, isDisabled: isDisabled, isCompact: isCompact }), children] })));
110 };
111 exports.Pagination = Pagination;
112 exports.Pagination.displayName = 'Pagination';
113 //# sourceMappingURL=Pagination.js.map