| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.PaginationNavigation = 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 pagination_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Pagination/pagination")); |
| 8 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 9 |
const angle_left_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/angle-left-icon')); |
| 10 |
const rh_microns_double_caret_left_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-double-caret-left-icon')); |
| 11 |
const angle_right_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/angle-right-icon')); |
| 12 |
const angle_double_right_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/angle-double-right-icon')); |
| 13 |
const Button_1 = require("../Button"); |
| 14 |
const TextInput_1 = require("../TextInput"); |
| 15 |
const helpers_1 = require("../../helpers"); |
| 16 |
const constants_1 = require("../../helpers/constants"); |
| 17 |
class PaginationNavigation extends react_1.Component { |
| 18 |
constructor(props) { |
| 19 |
super(props); |
| 20 |
this.handleNewPage = (_evt, newPage) => { |
| 21 |
const { perPage, onSetPage } = this.props; |
| 22 |
const startIdx = (newPage - 1) * perPage; |
| 23 |
const endIdx = newPage * perPage; |
| 24 |
return onSetPage(_evt, newPage, perPage, startIdx, endIdx); |
| 25 |
}; |
| 26 |
this.state = { userInputPage: this.props.page }; |
| 27 |
} |
| 28 |
static parseInteger(input, lastPage) { |
| 29 |
// eslint-disable-next-line radix |
| 30 |
let inputPage = Number.parseInt(input, 10); |
| 31 |
if (!Number.isNaN(inputPage)) { |
| 32 |
inputPage = inputPage > lastPage ? lastPage : inputPage; |
| 33 |
inputPage = inputPage < 1 ? 1 : inputPage; |
| 34 |
} |
| 35 |
return inputPage; |
| 36 |
} |
| 37 |
onChange(event, lastPage) { |
| 38 |
const inputPage = PaginationNavigation.parseInteger(event.currentTarget.value, lastPage); |
| 39 |
this.setState({ userInputPage: Number.isNaN(inputPage) ? event.currentTarget.value : inputPage }); |
| 40 |
} |
| 41 |
onKeyDown(event, page, lastPage, onPageInput) { |
| 42 |
const allowedKeys = [ |
| 43 |
'Tab', |
| 44 |
'Backspace', |
| 45 |
'Delete', |
| 46 |
'ArrowLeft', |
| 47 |
'ArrowRight', |
| 48 |
'Home', |
| 49 |
'End', |
| 50 |
'ArrowUp', |
| 51 |
'ArrowDown' |
| 52 |
]; |
| 53 |
if (event.key === constants_1.KeyTypes.Enter) { |
| 54 |
const inputPage = PaginationNavigation.parseInteger(this.state.userInputPage, lastPage); |
| 55 |
onPageInput(event, Number.isNaN(inputPage) ? page : inputPage); |
| 56 |
this.handleNewPage(event, Number.isNaN(inputPage) ? page : inputPage); |
| 57 |
} |
| 58 |
else if (!/^\d*$/.test(event.key) && !allowedKeys.includes(event.key)) { |
| 59 |
event.preventDefault(); |
| 60 |
} |
| 61 |
} |
| 62 |
componentDidUpdate(lastState) { |
| 63 |
if (this.props.page !== lastState.page && |
| 64 |
this.props.page <= this.props.lastPage && |
| 65 |
this.state.userInputPage !== this.props.page) { |
| 66 |
this.setState({ userInputPage: this.props.page }); |
| 67 |
} |
| 68 |
} |
| 69 |
render() { |
| 70 |
const _a = this.props, { page, |
| 71 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 72 |
perPage, |
| 73 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 74 |
onSetPage, isDisabled, itemCount, lastPage, firstPage, pagesTitle, pagesTitlePlural, toLastPageAriaLabel, toNextPageAriaLabel, toFirstPageAriaLabel, toPreviousPageAriaLabel, currPageAriaLabel, paginationAriaLabel, ofWord, onNextClick, onPreviousClick, onFirstClick, onLastClick, onPageInput, className, isCompact } = _a, props = tslib_1.__rest(_a, ["page", "perPage", "onSetPage", "isDisabled", "itemCount", "lastPage", "firstPage", "pagesTitle", "pagesTitlePlural", "toLastPageAriaLabel", "toNextPageAriaLabel", "toFirstPageAriaLabel", "toPreviousPageAriaLabel", "currPageAriaLabel", "paginationAriaLabel", "ofWord", "onNextClick", "onPreviousClick", "onFirstClick", "onLastClick", "onPageInput", "className", "isCompact"]); |
| 75 |
const { userInputPage } = this.state; |
| 76 |
return ((0, jsx_runtime_1.jsxs)("nav", Object.assign({ className: (0, react_styles_1.css)(pagination_1.default.paginationNav, className), "aria-label": paginationAriaLabel }, props, { children: [!isCompact && ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(pagination_1.default.paginationNavControl, pagination_1.default.modifiers.first), children: (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, isDisabled: isDisabled || page === firstPage || page === 0, "aria-label": toFirstPageAriaLabel, "data-action": "first", onClick: (event) => { |
| 77 |
onFirstClick(event, 1); |
| 78 |
this.handleNewPage(event, 1); |
| 79 |
this.setState({ userInputPage: 1 }); |
| 80 |
}, icon: (0, jsx_runtime_1.jsx)(rh_microns_double_caret_left_icon_1.default, {}) }) })), (0, jsx_runtime_1.jsx)("div", { className: pagination_1.default.paginationNavControl, children: (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, isDisabled: isDisabled || page === firstPage || page === 0, "data-action": "previous", onClick: (event) => { |
| 81 |
const newPage = page - 1 >= 1 ? page - 1 : 1; |
| 82 |
onPreviousClick(event, newPage); |
| 83 |
this.handleNewPage(event, newPage); |
| 84 |
this.setState({ userInputPage: newPage }); |
| 85 |
}, "aria-label": toPreviousPageAriaLabel, icon: (0, jsx_runtime_1.jsx)(angle_left_icon_1.default, {}) }) }), !isCompact && ((0, jsx_runtime_1.jsxs)("div", { className: pagination_1.default.paginationNavPageSelect, children: [(0, jsx_runtime_1.jsx)(TextInput_1.TextInput, { "aria-label": currPageAriaLabel, type: "number", isDisabled: isDisabled || (itemCount && page === firstPage && page === lastPage && itemCount >= 0) || page === 0, min: lastPage <= 0 && firstPage <= 0 ? 0 : 1, max: lastPage, value: userInputPage, onKeyDown: (event) => this.onKeyDown(event, page, lastPage, onPageInput), onChange: (event) => this.onChange(event, lastPage) }), (itemCount || itemCount === 0) && ((0, jsx_runtime_1.jsxs)("span", { "aria-hidden": "true", children: [ofWord, " ", pagesTitle ? (0, helpers_1.pluralize)(lastPage, pagesTitle, pagesTitlePlural) : lastPage] }))] })), (0, jsx_runtime_1.jsx)("div", { className: pagination_1.default.paginationNavControl, children: (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, isDisabled: isDisabled || page === lastPage, "aria-label": toNextPageAriaLabel, "data-action": "next", onClick: (event) => { |
| 86 |
const newPage = page + 1 <= lastPage ? page + 1 : lastPage; |
| 87 |
onNextClick(event, newPage); |
| 88 |
this.handleNewPage(event, newPage); |
| 89 |
this.setState({ userInputPage: newPage }); |
| 90 |
}, icon: (0, jsx_runtime_1.jsx)(angle_right_icon_1.default, {}) }) }), !isCompact && ((0, jsx_runtime_1.jsx)("div", { className: (0, react_styles_1.css)(pagination_1.default.paginationNavControl, pagination_1.default.modifiers.last), children: (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, isDisabled: isDisabled || page === lastPage, "aria-label": toLastPageAriaLabel, "data-action": "last", onClick: (event) => { |
| 91 |
onLastClick(event, lastPage); |
| 92 |
this.handleNewPage(event, lastPage); |
| 93 |
this.setState({ userInputPage: lastPage }); |
| 94 |
}, icon: (0, jsx_runtime_1.jsx)(angle_double_right_icon_1.default, {}) }) }))] }))); |
| 95 |
} |
| 96 |
} |
| 97 |
exports.PaginationNavigation = PaginationNavigation; |
| 98 |
PaginationNavigation.displayName = 'PaginationNavigation'; |
| 99 |
PaginationNavigation.defaultProps = { |
| 100 |
className: '', |
| 101 |
isDisabled: false, |
| 102 |
isCompact: false, |
| 103 |
lastPage: 0, |
| 104 |
firstPage: 0, |
| 105 |
pagesTitle: '', |
| 106 |
pagesTitlePlural: '', |
| 107 |
toLastPageAriaLabel: 'Go to last page', |
| 108 |
toNextPageAriaLabel: 'Go to next page', |
| 109 |
toFirstPageAriaLabel: 'Go to first page', |
| 110 |
toPreviousPageAriaLabel: 'Go to previous page', |
| 111 |
currPageAriaLabel: 'Current page', |
| 112 |
paginationAriaLabel: 'Pagination', |
| 113 |
ofWord: 'of', |
| 114 |
onNextClick: () => undefined, |
| 115 |
onPreviousClick: () => undefined, |
| 116 |
onFirstClick: () => undefined, |
| 117 |
onLastClick: () => undefined, |
| 118 |
onPageInput: () => undefined |
| 119 |
}; |
| 120 |
//# sourceMappingURL=PaginationNavigation.js.map |