forked from carlosrocha/react-data-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagination.js
More file actions
129 lines (111 loc) · 3.73 KB
/
Copy pathPagination.js
File metadata and controls
129 lines (111 loc) · 3.73 KB
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
'use strict';
var React = require('react');
var { PropTypes } = React;
// Used to cancel events.
var preventDefault = e => e.preventDefault();
class Pagination {
shouldComponentUpdate(nextProps) {
var props = this.props;
return props.totalPages !== nextProps.totalPages ||
props.currentPage !== nextProps.currentPage ||
props.showPages !== nextProps.showPages;
}
onChangePage(pageNumber, event) {
event.preventDefault();
this.props.onChangePage(pageNumber);
}
render() {
var { totalPages, showPages, currentPage } = this.props;
if (totalPages === 0) {
return null;
}
var diff = Math.floor(showPages / 2),
start = Math.max(currentPage - diff, 0),
end = Math.min(start + showPages, totalPages);
if (totalPages >= showPages && end >= totalPages) {
start = totalPages - showPages;
}
var buttons = [], btnEvent, isCurrent;
for (var i = start; i < end; i++) {
isCurrent = currentPage === i;
// If the button is for the current page then disable the event.
if (isCurrent) {
btnEvent = preventDefault;
} else {
btnEvent = this.onChangePage.bind(this, i);
}
buttons.push(
<li key={i} className={isCurrent ? 'active' : null}>
<a role="button" href="#" onClick={btnEvent} tabIndex="0">
<span>{i + 1}</span>
{isCurrent ?
<span className="sr-only">(current)</span> : null}
</a>
</li>
);
}
// First and Prev button handlers and class.
var firstHandler = preventDefault;
var prevHandler = preventDefault;
var isNotFirst = currentPage > 0;
if (isNotFirst) {
firstHandler = this.onChangePage.bind(this, 0);
prevHandler = this.onChangePage.bind(this, currentPage - 1);
}
// Next and Last button handlers and class.
var nextHandler = preventDefault;
var lastHandler = preventDefault;
var isNotLast = currentPage < totalPages - 1;
if (isNotLast) {
nextHandler = this.onChangePage.bind(this, currentPage + 1);
lastHandler = this.onChangePage.bind(this, totalPages - 1);
}
return (
<ul className={this.props.className} aria-label="Pagination">
<li className={!isNotFirst ? 'disabled' : null}>
<a role="button" href="#" tabIndex="0"
onClick={firstHandler}
aria-disabled={!isNotFirst}
aria-label="First">
<span className="fa fa-angle-double-left" aria-hidden="true" />
</a>
</li>
<li className={!isNotFirst ? 'disabled' : null}>
<a role="button" href="#" tabIndex="0"
onClick={prevHandler}
aria-disabled={!isNotFirst}
aria-label="Previous">
<span className="fa fa-angle-left" aria-hidden="true" />
</a>
</li>
{buttons}
<li className={!isNotLast ? 'disabled' : null}>
<a role="button" href="#" tabIndex="0"
onClick={nextHandler}
aria-disabled={!isNotLast}
aria-label="Next">
<span className="fa fa-angle-right" aria-hidden="true" />
</a>
</li>
<li className={!isNotLast ? 'disabled' : null}>
<a role="button" href="#" tabIndex="0"
onClick={lastHandler}
aria-disabled={!isNotLast}
aria-label="Last">
<span className="fa fa-angle-double-right" aria-hidden="true" />
</a>
</li>
</ul>
);
}
}
Pagination.propTypes = {
onChangePage: PropTypes.func.isRequired,
totalPages: PropTypes.number.isRequired,
currentPage: PropTypes.number.isRequired,
showPages: PropTypes.number
};
Pagination.defaultProps = {
showPages: 5
};
module.exports = Pagination;