-
Notifications
You must be signed in to change notification settings - Fork 8
/
ReactTablePagination.jsx
138 lines (133 loc) · 4.35 KB
/
ReactTablePagination.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React from 'react';
import PropTypes from 'prop-types';
import {
Pagination,
PaginationItem,
PaginationLink,
FormGroup,
Input,
} from 'reactstrap';
import ChevronRightIcon from 'mdi-react/ChevronRightIcon';
import ChevronDoubleRightIcon from 'mdi-react/ChevronDoubleRightIcon';
import ChevronLeftIcon from 'mdi-react/ChevronLeftIcon';
import ChevronDoubleLeftIcon from 'mdi-react/ChevronDoubleLeftIcon';
const ReactTablePagination = ({
dataLength,
page,
gotoPage,
canPreviousPage,
pageOptions,
pageSize,
pageIndex,
previousPage,
nextPage,
canNextPage,
setPageSize,
manualPageSize,
}) => {
// console.log(pageOptions)
const arrayPageIndex = (pageIndex - 2) < 0
? pageOptions.slice(0, pageIndex + 3)
: pageOptions.slice((pageIndex - 2), (pageIndex + 3));
return (
<Pagination className="pagination" dir="ltr">
<div className="pagination">
<PaginationLink
className="pagination__link pagination__link--arrow"
type="button"
onClick={() => gotoPage(0)}
disabled={!canPreviousPage}
>
<ChevronDoubleLeftIcon className="pagination__link-icon" />
</PaginationLink>
<PaginationLink
className="pagination__link pagination__link--arrow"
type="button"
onClick={previousPage}
disabled={!canPreviousPage}
>
<ChevronLeftIcon className="pagination__link-icon" />
</PaginationLink>
{arrayPageIndex.map(i => (
<PaginationItem
className="pagination__item"
active={pageIndex === i}
key={i}
>
<PaginationLink
key={i}
className="pagination__link"
type="button"
onClick={() => gotoPage(i)}
>
{i + 1}
</PaginationLink>
</PaginationItem>
))}
<PaginationItem className="pagination__item">
<PaginationLink
className="pagination__link pagination__link--arrow"
type="button"
onClick={nextPage}
disabled={!canNextPage}
>
<ChevronRightIcon className="pagination__link-icon" />
</PaginationLink>
</PaginationItem>
<PaginationItem className="pagination__item">
<PaginationLink
className="pagination__link pagination__link--arrow"
type="button"
onClick={() => gotoPage(pageOptions.length - 1)}
disabled={!canNextPage}
>
<ChevronDoubleRightIcon className="pagination__link-icon" />
</PaginationLink>
</PaginationItem>
<PaginationItem className="pagination__item pagination-info">
Showing {pageSize * pageIndex + 1} to {pageSize * pageIndex + page.length} of {dataLength}
</PaginationItem>
{manualPageSize.length > 1 && (
<PaginationItem className="pagination__item">
<FormGroup className="pagination__select-form ">
<Input
className="pagination__item pagination-info"
type="select"
name="select"
id="exampleSelect"
value={pageSize}
onChange={(event) => {
setPageSize(Number(event.target.value));
}}
>
{manualPageSize.map(item => (
<option className="pagination__item pagination__item-option" key={item} value={item}>
Show {item}
</option>
))}
</Input>
</FormGroup>
</PaginationItem>
)}
</div>
</Pagination>
);
};
ReactTablePagination.propTypes = {
dataLength: PropTypes.number.isRequired,
page: PropTypes.arrayOf(PropTypes.shape()).isRequired,
gotoPage: PropTypes.func.isRequired,
canNextPage: PropTypes.bool.isRequired,
canPreviousPage: PropTypes.bool.isRequired,
pageOptions: PropTypes.arrayOf(PropTypes.number).isRequired,
pageSize: PropTypes.number.isRequired,
pageIndex: PropTypes.number.isRequired,
previousPage: PropTypes.func.isRequired,
nextPage: PropTypes.func.isRequired,
setPageSize: PropTypes.func.isRequired,
manualPageSize: PropTypes.arrayOf(PropTypes.number),
};
ReactTablePagination.defaultProps = {
manualPageSize: [10, 20, 30, 40],
};
export default ReactTablePagination;