UNPKG

@atlaskit/pagination

Version:

Pagination allows you to divide large amounts of content into smaller chunks across multiple pages.

65 lines (64 loc) 2.99 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _memoizeOne = _interopRequireDefault(require("memoize-one")); // TODO: comma needed after generic declaration below, or the .tsx file thinks // it is a JSX element. Tidy up in a future refactor. var collapseRange = function collapseRange(pages, current, _ref, testId) { var max = _ref.max, ellipsis = _ref.ellipsis, transform = _ref.transform; var total = pages.length; // only need ellipsis if we have more pages than we can display var needEllipsis = total > max; // show start ellipsis if the current page is further away than max - 4 from the first page var hasStartEllipsis = needEllipsis && max - 4 < current; // show end ellipsis if the current page is further than total - max + 3 from the last page var hasEndEllipsis = needEllipsis && current < total - max + 3; var getPageComponents = (0, _memoizeOne.default)(function () { var startIndex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var lastIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : total; return pages.slice(startIndex, lastIndex).map(function (page, index) { return transform(page, startIndex + index, testId); }); }); if (!needEllipsis) { return getPageComponents(0, total); } if (hasStartEllipsis && !hasEndEllipsis) { var _pageCount = max - 2; return [].concat((0, _toConsumableArray2.default)(getPageComponents(0, 1)), [ellipsis({ key: 'ellipsis-1', testId: testId && "".concat(testId, "-ellipsis"), from: 2, to: total - _pageCount })], (0, _toConsumableArray2.default)(getPageComponents(total - _pageCount))); } if (!hasStartEllipsis && hasEndEllipsis) { var _pageCount2 = max - 2; return [].concat((0, _toConsumableArray2.default)(getPageComponents(0, _pageCount2)), [ellipsis({ key: 'ellipsis-1', testId: testId && "".concat(testId, "-ellipsis"), from: _pageCount2 + 1, to: total - 1 })], (0, _toConsumableArray2.default)(getPageComponents(total - 1))); } // we have both start and end ellipsis var pageCount = max - 4; return [].concat((0, _toConsumableArray2.default)(getPageComponents(0, 1)), [ellipsis({ key: 'ellipsis-1', testId: testId && "".concat(testId, "-ellipsis"), from: 2, to: current - Math.floor(pageCount / 2) })], (0, _toConsumableArray2.default)(getPageComponents(current - Math.floor(pageCount / 2), current + pageCount - 1)), [ellipsis({ key: 'ellipsis-2', testId: testId && "".concat(testId, "-ellipsis"), from: current + 3, to: total - 1 })], (0, _toConsumableArray2.default)(getPageComponents(total - 1))); }; var _default = exports.default = collapseRange;