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