UNPKG

7.84 kBJavaScriptView Raw
1import { __rest } from "tslib";
2import * as React from 'react';
3import { ToggleTemplate } from './ToggleTemplate';
4import styles from '@patternfly/react-styles/css/components/Pagination/pagination';
5import { css } from '@patternfly/react-styles';
6import { fillTemplate } from '../../helpers';
7import { Navigation } from './Navigation';
8import { PaginationOptionsMenu } from './PaginationOptionsMenu';
9import { getOUIAProps, getDefaultOUIAId } from '../../helpers';
10import widthChars from '@patternfly/react-tokens/dist/esm/c_pagination__nav_page_select_c_form_control_width_chars';
11export var PaginationVariant;
12(function (PaginationVariant) {
13 PaginationVariant["top"] = "top";
14 PaginationVariant["bottom"] = "bottom";
15})(PaginationVariant || (PaginationVariant = {}));
16const defaultPerPageOptions = [
17 {
18 title: '10',
19 value: 10
20 },
21 {
22 title: '20',
23 value: 20
24 },
25 {
26 title: '50',
27 value: 50
28 },
29 {
30 title: '100',
31 value: 100
32 }
33];
34const handleInputWidth = (lastPage, node) => {
35 if (!node) {
36 return;
37 }
38 const len = String(lastPage).length;
39 if (len >= 3) {
40 node.style.setProperty(widthChars.name, `${len}`);
41 }
42 else {
43 node.style.setProperty(widthChars.name, '2');
44 }
45};
46let paginationId = 0;
47export class Pagination extends React.Component {
48 constructor() {
49 super(...arguments);
50 this.paginationRef = React.createRef();
51 this.state = {
52 ouiaStateId: getDefaultOUIAId(Pagination.displayName, this.props.variant)
53 };
54 }
55 getLastPage() {
56 const { itemCount, perPage, page } = this.props;
57 // when itemCount is not known let's set lastPage as page+1 as we don't know the total count
58 return itemCount || itemCount === 0 ? Math.ceil(itemCount / perPage) || 0 : page + 1;
59 }
60 componentDidMount() {
61 const node = this.paginationRef.current;
62 handleInputWidth(this.getLastPage(), node);
63 }
64 componentDidUpdate(prevProps) {
65 const node = this.paginationRef.current;
66 if (prevProps.perPage !== this.props.perPage || prevProps.itemCount !== this.props.itemCount) {
67 handleInputWidth(this.getLastPage(), node);
68 }
69 }
70 render() {
71 const _a = this.props, { children, className, variant, isDisabled, isCompact, isStatic, isSticky, perPage, titles, firstPage, page: propPage, offset, defaultToFullPage, itemCount, itemsStart, itemsEnd, perPageOptions, dropDirection: dropDirectionProp, widgetId, toggleTemplate, onSetPage, onPerPageSelect, onFirstClick, onPreviousClick, onNextClick, onPageInput, onLastClick, ouiaId, ouiaSafe, perPageComponent } = _a, props = __rest(_a, ["children", "className", "variant", "isDisabled", "isCompact", "isStatic", "isSticky", "perPage", "titles", "firstPage", "page", "offset", "defaultToFullPage", "itemCount", "itemsStart", "itemsEnd", "perPageOptions", "dropDirection", "widgetId", "toggleTemplate", "onSetPage", "onPerPageSelect", "onFirstClick", "onPreviousClick", "onNextClick", "onPageInput", "onLastClick", "ouiaId", "ouiaSafe", "perPageComponent"]);
72 const dropDirection = dropDirectionProp || (variant === 'bottom' && !isStatic ? 'up' : 'down');
73 let page = propPage;
74 if (!page && offset) {
75 page = Math.ceil(offset / perPage);
76 }
77 if (page === 0 && !itemCount) {
78 page = 1;
79 }
80 const lastPage = this.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 toggleTemplateProps = { firstIndex, lastIndex, itemCount, itemsTitle: titles.items, ofWord: titles.ofWord };
96 return (React.createElement("div", Object.assign({ ref: this.paginationRef, className: css(styles.pagination, variant === PaginationVariant.bottom && styles.modifiers.bottom, isCompact && styles.modifiers.compact, isStatic && styles.modifiers.static, isSticky && styles.modifiers.sticky, className), id: `${widgetId}-${paginationId++}` }, getOUIAProps(Pagination.displayName, ouiaId !== undefined ? ouiaId : this.state.ouiaStateId, ouiaSafe), props),
97 variant === PaginationVariant.top && (React.createElement("div", { className: css(styles.paginationTotalItems) },
98 toggleTemplate && typeof toggleTemplate === 'string' && fillTemplate(toggleTemplate, toggleTemplateProps),
99 toggleTemplate &&
100 typeof toggleTemplate !== 'string' &&
101 toggleTemplate(toggleTemplateProps),
102 !toggleTemplate && (React.createElement(ToggleTemplate, { firstIndex: firstIndex, lastIndex: lastIndex, itemCount: itemCount, itemsTitle: titles.items, ofWord: titles.ofWord })))),
103 React.createElement(PaginationOptionsMenu, { itemsPerPageTitle: titles.itemsPerPage, perPageSuffix: titles.perPageSuffix, itemsTitle: isCompact ? '' : titles.items, optionsToggle: titles.optionsToggle, perPageOptions: perPageOptions, firstIndex: itemsStart !== null ? itemsStart : firstIndex, lastIndex: itemsEnd !== null ? itemsEnd : lastIndex, ofWord: titles.ofWord, defaultToFullPage: defaultToFullPage, itemCount: itemCount, page: page, perPage: perPage, lastPage: lastPage, onPerPageSelect: onPerPageSelect, dropDirection: dropDirection, widgetId: widgetId, toggleTemplate: toggleTemplate, isDisabled: isDisabled, perPageComponent: perPageComponent }),
104 React.createElement(Navigation, { pagesTitle: titles.page, pagesTitlePlural: titles.pages, toLastPage: titles.toLastPage, toPreviousPage: titles.toPreviousPage, toNextPage: titles.toNextPage, toFirstPage: titles.toFirstPage, currPage: titles.currPage, paginationTitle: titles.paginationTitle, 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 }),
105 children));
106 }
107}
108Pagination.displayName = 'Pagination';
109Pagination.defaultProps = {
110 children: null,
111 className: '',
112 variant: PaginationVariant.top,
113 isDisabled: false,
114 isCompact: false,
115 isSticky: false,
116 perPage: defaultPerPageOptions[0].value,
117 titles: {
118 items: '',
119 page: '',
120 pages: '',
121 itemsPerPage: 'Items per page',
122 perPageSuffix: 'per page',
123 toFirstPage: 'Go to first page',
124 toPreviousPage: 'Go to previous page',
125 toLastPage: 'Go to last page',
126 toNextPage: 'Go to next page',
127 optionsToggle: '',
128 currPage: 'Current page',
129 paginationTitle: 'Pagination',
130 ofWord: 'of'
131 },
132 firstPage: 1,
133 page: 0,
134 offset: 0,
135 defaultToFullPage: false,
136 itemsStart: null,
137 itemsEnd: null,
138 perPageOptions: defaultPerPageOptions,
139 widgetId: 'pagination-options-menu',
140 onSetPage: () => undefined,
141 onPerPageSelect: () => undefined,
142 onFirstClick: () => undefined,
143 onPreviousClick: () => undefined,
144 onNextClick: () => undefined,
145 onPageInput: () => undefined,
146 onLastClick: () => undefined,
147 ouiaSafe: true,
148 perPageComponent: 'div'
149};
150//# sourceMappingURL=Pagination.js.map
\No newline at end of file