1 | import React, { Component } from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 |
|
4 | function _classCallCheck(instance, Constructor) {
|
5 | if (!(instance instanceof Constructor)) {
|
6 | throw new TypeError("Cannot call a class as a function");
|
7 | }
|
8 | }
|
9 |
|
10 | function _defineProperties(target, props) {
|
11 | for (var i = 0; i < props.length; i++) {
|
12 | var descriptor = props[i];
|
13 | descriptor.enumerable = descriptor.enumerable || false;
|
14 | descriptor.configurable = true;
|
15 | if ("value" in descriptor) descriptor.writable = true;
|
16 | Object.defineProperty(target, descriptor.key, descriptor);
|
17 | }
|
18 | }
|
19 |
|
20 | function _createClass(Constructor, protoProps, staticProps) {
|
21 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
22 | if (staticProps) _defineProperties(Constructor, staticProps);
|
23 | return Constructor;
|
24 | }
|
25 |
|
26 | function _defineProperty(obj, key, value) {
|
27 | if (key in obj) {
|
28 | Object.defineProperty(obj, key, {
|
29 | value: value,
|
30 | enumerable: true,
|
31 | configurable: true,
|
32 | writable: true
|
33 | });
|
34 | } else {
|
35 | obj[key] = value;
|
36 | }
|
37 |
|
38 | return obj;
|
39 | }
|
40 |
|
41 | function _objectSpread(target) {
|
42 | for (var i = 1; i < arguments.length; i++) {
|
43 | var source = arguments[i] != null ? arguments[i] : {};
|
44 | var ownKeys = Object.keys(source);
|
45 |
|
46 | if (typeof Object.getOwnPropertySymbols === 'function') {
|
47 | ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
|
48 | return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
49 | }));
|
50 | }
|
51 |
|
52 | ownKeys.forEach(function (key) {
|
53 | _defineProperty(target, key, source[key]);
|
54 | });
|
55 | }
|
56 |
|
57 | return target;
|
58 | }
|
59 |
|
60 | function _inherits(subClass, superClass) {
|
61 | if (typeof superClass !== "function" && superClass !== null) {
|
62 | throw new TypeError("Super expression must either be null or a function");
|
63 | }
|
64 |
|
65 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
66 | constructor: {
|
67 | value: subClass,
|
68 | writable: true,
|
69 | configurable: true
|
70 | }
|
71 | });
|
72 | if (superClass) _setPrototypeOf(subClass, superClass);
|
73 | }
|
74 |
|
75 | function _getPrototypeOf(o) {
|
76 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
77 | return o.__proto__ || Object.getPrototypeOf(o);
|
78 | };
|
79 | return _getPrototypeOf(o);
|
80 | }
|
81 |
|
82 | function _setPrototypeOf(o, p) {
|
83 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
84 | o.__proto__ = p;
|
85 | return o;
|
86 | };
|
87 |
|
88 | return _setPrototypeOf(o, p);
|
89 | }
|
90 |
|
91 | function _objectWithoutPropertiesLoose(source, excluded) {
|
92 | if (source == null) return {};
|
93 | var target = {};
|
94 | var sourceKeys = Object.keys(source);
|
95 | var key, i;
|
96 |
|
97 | for (i = 0; i < sourceKeys.length; i++) {
|
98 | key = sourceKeys[i];
|
99 | if (excluded.indexOf(key) >= 0) continue;
|
100 | target[key] = source[key];
|
101 | }
|
102 |
|
103 | return target;
|
104 | }
|
105 |
|
106 | function _objectWithoutProperties(source, excluded) {
|
107 | if (source == null) return {};
|
108 |
|
109 | var target = _objectWithoutPropertiesLoose(source, excluded);
|
110 |
|
111 | var key, i;
|
112 |
|
113 | if (Object.getOwnPropertySymbols) {
|
114 | var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
115 |
|
116 | for (i = 0; i < sourceSymbolKeys.length; i++) {
|
117 | key = sourceSymbolKeys[i];
|
118 | if (excluded.indexOf(key) >= 0) continue;
|
119 | if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
120 | target[key] = source[key];
|
121 | }
|
122 | }
|
123 |
|
124 | return target;
|
125 | }
|
126 |
|
127 | function _assertThisInitialized(self) {
|
128 | if (self === void 0) {
|
129 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
130 | }
|
131 |
|
132 | return self;
|
133 | }
|
134 |
|
135 | function _possibleConstructorReturn(self, call) {
|
136 | if (call && (typeof call === "object" || typeof call === "function")) {
|
137 | return call;
|
138 | }
|
139 |
|
140 | return _assertThisInitialized(self);
|
141 | }
|
142 |
|
143 | function _toConsumableArray(arr) {
|
144 | return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
|
145 | }
|
146 |
|
147 | function _arrayWithoutHoles(arr) {
|
148 | if (Array.isArray(arr)) {
|
149 | for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
|
150 |
|
151 | return arr2;
|
152 | }
|
153 | }
|
154 |
|
155 | function _iterableToArray(iter) {
|
156 | if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
|
157 | }
|
158 |
|
159 | function _nonIterableSpread() {
|
160 | throw new TypeError("Invalid attempt to spread non-iterable instance");
|
161 | }
|
162 |
|
163 | function getRange(start, end) {
|
164 | return _toConsumableArray(Array(end - start + 1)).map(function (_, i) {
|
165 | return start + i;
|
166 | });
|
167 | }
|
168 | function getPageInfo(_ref) {
|
169 | var limit = _ref.limit,
|
170 | pageCount = _ref.pageCount,
|
171 | total = _ref.total,
|
172 | page = _ref.page;
|
173 | var totalPages = Math.ceil(total / limit);
|
174 | var totalResults = parseInt(total, 10);
|
175 | var currentPage = parseInt(page, 10);
|
176 |
|
177 | if (currentPage < 1) {
|
178 | currentPage = 1;
|
179 | }
|
180 |
|
181 | if (currentPage > totalPages) {
|
182 | currentPage = totalPages;
|
183 | }
|
184 |
|
185 | var firstPage = Math.max(1, currentPage - Math.floor(pageCount / 2));
|
186 | var lastPage = Math.min(totalPages, currentPage + Math.floor(pageCount / 2));
|
187 |
|
188 | if (lastPage - firstPage + 1 < pageCount) {
|
189 | if (currentPage < totalPages / 2) {
|
190 | lastPage = Math.min(totalPages, lastPage + (pageCount - (lastPage - firstPage)));
|
191 | } else {
|
192 | firstPage = Math.max(1, firstPage - (pageCount - (lastPage - firstPage)));
|
193 | }
|
194 | }
|
195 |
|
196 | if (lastPage - firstPage + 1 > pageCount) {
|
197 | if (currentPage > totalPages / 2) {
|
198 |
|
199 | firstPage++;
|
200 | } else {
|
201 |
|
202 | lastPage--;
|
203 | }
|
204 | }
|
205 |
|
206 | var firstResult = limit * (currentPage - 1);
|
207 | var lastResult = limit * currentPage - 1;
|
208 | return {
|
209 | totalPages: totalPages,
|
210 | pages: Math.min(lastPage - firstPage + 1, totalPages),
|
211 | currentPage: currentPage,
|
212 | firstPage: firstPage,
|
213 | lastPage: lastPage,
|
214 | previousPage: currentPage - 1,
|
215 | nextPage: currentPage + 1,
|
216 | hasPreviousPage: currentPage > 1,
|
217 | hasNextPage: currentPage < totalPages,
|
218 | totalResults: totalResults,
|
219 | results: Math.min(lastResult - firstResult + 1, totalResults),
|
220 | firstResult: firstResult,
|
221 | lastResult: lastResult
|
222 | };
|
223 | }
|
224 |
|
225 | var Pagination =
|
226 |
|
227 | function (_Component) {
|
228 | _inherits(Pagination, _Component);
|
229 |
|
230 | function Pagination() {
|
231 | var _this;
|
232 |
|
233 | _classCallCheck(this, Pagination);
|
234 |
|
235 | _this = _possibleConstructorReturn(this, _getPrototypeOf(Pagination).call(this));
|
236 |
|
237 | _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_getPageItemProps", function (props) {
|
238 | var pageValue = props.pageValue,
|
239 | handlePageChange = props.onPageChange,
|
240 | rest = _objectWithoutProperties(props, ["pageValue", "onPageChange"]);
|
241 |
|
242 | var onPageChange = function onPageChange(e) {
|
243 | if (typeof handlePageChange === 'function') {
|
244 | handlePageChange(pageValue, e);
|
245 | }
|
246 |
|
247 | _this.setState({
|
248 | currentPage: pageValue
|
249 | });
|
250 | };
|
251 |
|
252 | return _objectSpread({
|
253 | onClick: onPageChange
|
254 | }, rest);
|
255 | });
|
256 |
|
257 | _this.state = {
|
258 | currentPage: 0
|
259 | };
|
260 | return _this;
|
261 | }
|
262 |
|
263 | _createClass(Pagination, [{
|
264 | key: "componentWillMount",
|
265 | value: function componentWillMount() {
|
266 | if (this.props.currentPage) {
|
267 | this.setState({
|
268 | currentPage: parseInt(this.props.currentPage, 10)
|
269 | });
|
270 | }
|
271 | }
|
272 | }, {
|
273 | key: "componentWillReceiveProps",
|
274 | value: function componentWillReceiveProps(nextProps) {
|
275 | if (nextProps.currentPage !== this.props.currentPage) {
|
276 | this.setState({
|
277 | currentPage: parseInt(nextProps.currentPage, 10)
|
278 | });
|
279 | }
|
280 | }
|
281 | }, {
|
282 | key: "render",
|
283 | value: function render() {
|
284 | var _this$props = this.props,
|
285 | total = _this$props.total,
|
286 | limit = _this$props.limit,
|
287 | pageCount = _this$props.pageCount;
|
288 | var currentPage = this.state.currentPage;
|
289 | var pageInfo = getPageInfo({
|
290 | limit: limit,
|
291 | pageCount: pageCount,
|
292 | total: total,
|
293 | page: currentPage
|
294 | });
|
295 | var firstPage = pageInfo.firstPage,
|
296 | lastPage = pageInfo.lastPage,
|
297 | hasNextPage = pageInfo.hasNextPage,
|
298 | hasPreviousPage = pageInfo.hasPreviousPage,
|
299 | previousPage = pageInfo.previousPage,
|
300 | nextPage = pageInfo.nextPage,
|
301 | totalPages = pageInfo.totalPages;
|
302 | var pages = total ? getRange(firstPage, lastPage) : [];
|
303 | return React.createElement("div", null, this.props.children({
|
304 | pages: pages,
|
305 | previousPage: previousPage,
|
306 | nextPage: nextPage,
|
307 | totalPages: totalPages,
|
308 | currentPage: currentPage,
|
309 | hasNextPage: hasNextPage,
|
310 | hasPreviousPage: hasPreviousPage,
|
311 | getPageItemProps: this._getPageItemProps
|
312 | }));
|
313 | }
|
314 | }]);
|
315 |
|
316 | return Pagination;
|
317 | }(Component);
|
318 |
|
319 | Pagination.propTypes = {
|
320 | total: PropTypes.number.isRequired,
|
321 | limit: PropTypes.number,
|
322 | pageCount: PropTypes.number,
|
323 | currentPage: PropTypes.number,
|
324 | pageValue: PropTypes.number,
|
325 | children: PropTypes.func.isRequired,
|
326 | onPageChange: PropTypes.func
|
327 | };
|
328 | Pagination.defaultProps = {
|
329 | limit: 10,
|
330 | pageCount: 5,
|
331 | currentPage: 0,
|
332 | pageValue: 0
|
333 | };
|
334 |
|
335 | export default Pagination;
|