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