-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pagination.js
137 lines (121 loc) · 3.89 KB
/
Pagination.js
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
import React from "react";
import PropTypes from "prop-types";
const defaultButton = props => <button {...props}>{props.children}</button>;
export default class Pagination extends React.Component {
constructor(props) {
super();
this.changePage = this.changePage.bind(this);
this.state = {
visiblePages: this.getVisiblePages(null, props.pages),
totalPages:props.pages
};
}
static propTypes = {
pages: PropTypes.number,
page: PropTypes.number,
PageButtonComponent: PropTypes.any,
onPageChange: PropTypes.func,
previousText: PropTypes.string,
nextText: PropTypes.string
};
componentWillReceiveProps(nextProps) {
if (this.props.pages !== nextProps.pages) {
this.setState({
visiblePages: this.getVisiblePages(null, nextProps.pages)
});
}
this.changePage(nextProps.page + 1);
}
filterPages = (visiblePages, totalPages) => {
return visiblePages.filter(page => page <= totalPages);
};
getVisiblePages = (page, total) => {
if (total < 7) {
return this.filterPages([1, 2, 3, 4, 5, 6], total);
} else {
if (page % 5 >= 0 && page > 4 && page + 2 < total) {
return [1, page - 1, page, page + 1, total];
} else if (page % 5 >= 0 && page > 4 && page + 2 >= total) {
return [1, total - 3, total - 2, total - 1, total];
} else {
return [1, 2, 3, 4, 5, total];
}
}
};
changePage(page) {
if(page==='first'){
page=1;
}
if(page==='last'){
page=this.props.pages;
}
const activePage = this.props.page + 1;
if (page === activePage) {
return;
}
const visiblePages = this.getVisiblePages(page, this.props.pages);
this.setState({
visiblePages: this.filterPages(visiblePages, this.props.pages)
});
this.props.onPageChange(page - 1);
var x = document.getElementsByClassName("checkbox");
var i = 0;
for(i=0; i<=x.length-1; i++) {
if(typeof x[i].checked !== undefined){
x[i].checked = false;
}
}
}
render() {
const { PageButtonComponent = defaultButton } = this.props;
const { visiblePages } = this.state;
const activePage = this.props.page + 1;
return (
<div className="Table__pagination">
<button className="previous-arrow" onClick={this.changePage.bind(null,'first')} disabled={activePage === 1}>«</button>
<div className="Table__prevPageWrapper">
<PageButtonComponent
className="Table__pageButton"
onClick={() => {
if (activePage === 1) return;
this.changePage(activePage - 1);
}}
disabled={activePage === 1}
>
<
</PageButtonComponent>
</div>
<div className="Table__visiblePagesWrapper">
{visiblePages.map((page, index, array) => {
return (
<PageButtonComponent
key={page}
className={
activePage === page
? "Table__pageButton Table__pageButton--active"
: "Table__pageButton"
}
onClick={this.changePage.bind(null, page)}
>
{array[index - 1] + 2 < page ? `...${page}` : page}
</PageButtonComponent>
);
})}
</div>
<div className="Table__nextPageWrapper">
<PageButtonComponent
className="Table__pageButton"
onClick={() => {
if (activePage === this.props.pages) return;
this.changePage(activePage + 1);
}}
disabled={activePage === this.props.pages}
>
>
</PageButtonComponent>
</div>
<button className="next-arrow" onClick={this.changePage.bind(null,'last')} disabled={activePage === this.props.pages}>»</button>
</div>
);
}
}