UNPKG

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