-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.jsx
101 lines (93 loc) · 3.43 KB
/
index.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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['react'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('react'));
} else {
root.ReactPaginate = factory(root.React);
}
}(this, function(React) {
return React.createClass({
propTypes: {
max: React.PropTypes.number.isRequired,
maxVisible: React.PropTypes.number,
onChange: React.PropTypes.func.isRequired
},
componentDidUpdate: function(prevProps, prevState) {
if (prevState.currentPage !== this.state.currentPage) {
this.props.onChange(this.state.currentPage);
}
},
getDefaultProps: function() {
return {
maxVisible: 5
};
},
getInitialState: function() {
return {
currentPage: 1,
items: []
};
},
goTo: function(page, e) {
if (e) {
e.preventDefault();
}
this.setState({currentPage: page});
},
onClickNext: function(e) {
e.preventDefault();
var page = this.state.currentPage;
if (page < this.props.max) {
this.goTo(page + 1);
}
},
onClickPrev: function(e) {
e.preventDefault();
if (this.state.currentPage > 1) {
this.goTo(this.state.currentPage - 1);
}
},
render: function() {
var className = this.props.className || '',
p = this.props,
s = this.state,
skip = 0;
if (s.currentPage > p.maxVisible - 1 && s.currentPage < p.max) {
skip = s.currentPage - p.maxVisible + 1;
} else if (s.currentPage === p.max) {
skip = s.currentPage - p.maxVisible;
}
var iterator = Array.apply(null, Array(p.maxVisible)).map(function(v, i) {
return skip + i + 1;
});
return (
<nav>
<ul className={'pagination ' + className}>
<li className={s.currentPage === 1 ? 'disabled' : ''}>
<a href="#" onClick={this.onClickPrev}>
<span aria-hidden="true">«</span>
<span className="sr-only">Prev</span>
</a>
</li>
{iterator.map(function(page) {
return (
<li key={page}
onClick={this.goTo.bind(this, page)}
className={s.currentPage === page ? 'active' : ''}>
<a href="#">{page}</a>
</li>
);
}, this)}
<li className={s.currentPage === p.max ? 'disabled' : ''}>
<a href="#" onClick={this.onClickNext}>
<span aria-hidden="true">»</span>
<span className="sr-only">Next</span>
</a>
</li>
</ul>
</nav>
);
}
});
}));