Skip to content

Commit

Permalink
Merge pull request #84 from sematext/SC-3056
Browse files Browse the repository at this point in the history
SC-3056 Add ellipsis to indicate missing pages
  • Loading branch information
lujomon authored Dec 18, 2018
2 parents dc2490d + 47c7492 commit 13757a9
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ const pageItem = (idx, page, onPageChange) => (
</a>
</li>
);

const ellipsisItem = (idx) => (
<li key={`ellipsis-${idx}`} className="page-item">
<div className="page-link">
...
</div>
</li>
);
// shown pages: first, pagesBeforeCurrent, current, pagesAfterCurrent, last
const pagesBeforeCurrent = 5;
const pagesAfterCurrent = 5;
Expand All @@ -37,6 +43,14 @@ const currentPageRange = (page, pageCount) => {
firstPage = Math.min(firstPage, firstPageMaxValue);
return _.range(firstPage, firstPage + rangeCount);
};
const middlePages = (page, pageCount, onPageChange) => {
const pageRange = currentPageRange(page, pageCount);
const ret = [];
if (pageRange[0] > 1) ret.push(ellipsisItem(0));
ret.push(pageRange.map(idx => pageItem(idx, page, onPageChange)));
if (pageRange[pageRange.length - 1] < pageCount - 2) ret.push(ellipsisItem(1));
return ret;
};

class Pagination extends Component {
render() {
Expand Down Expand Up @@ -75,9 +89,7 @@ class Pagination extends Component {
</a>
</li>
{ pageItem(0, page, onPageChange) }
{ pageCount > 2 &&
currentPageRange(page, pageCount).map(idx => pageItem(idx, page, onPageChange))
}
{ pageCount > 2 && middlePages(page, pageCount, onPageChange) }
{ pageCount > 1 && pageItem(pageCount - 1, page, onPageChange) }
<li className={`page-item ${hasNext ? '' : 'disabled'}`}>
<a
Expand Down

0 comments on commit 13757a9

Please sign in to comment.