Skip to content

Commit

Permalink
Make pagination page buffer configurable (#2032)
Browse files Browse the repository at this point in the history
* Pagination-Talon tileBuffer through props

* Included tileBuffer as dependency in hooks
  • Loading branch information
niklaswolf authored and dpatil-magento committed Dec 17, 2019
1 parent 9f7d1c9 commit 1d2d32b
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions packages/peregrine/lib/talons/Pagination/usePagination.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { useCallback, useMemo } from 'react';
const tileBuffer = 2;

export const usePagination = props => {
const { currentPage, setPage, totalPages } = props;
const { currentPage, setPage, totalPages, tileBuffer = 2 } = props;

const getLeadTile = useCallback((currentPage, totalPages) => {
const selectedTile = currentPage;
const leftBound = 1 + tileBuffer;
const rightBound = totalPages - tileBuffer;
const getLeadTile = useCallback(
(currentPage, totalPages) => {
const selectedTile = currentPage;
const leftBound = 1 + tileBuffer;
const rightBound = totalPages - tileBuffer;

let leadTile = selectedTile - tileBuffer;
if (selectedTile < leftBound) {
leadTile = 1;
} else if (selectedTile > rightBound) {
leadTile = Math.max(totalPages - tileBuffer * 2, 1);
}
return leadTile;
}, []);
let leadTile = selectedTile - tileBuffer;
if (selectedTile < leftBound) {
leadTile = 1;
} else if (selectedTile > rightBound) {
leadTile = Math.max(totalPages - tileBuffer * 2, 1);
}
return leadTile;
},
[tileBuffer]
);

const handleLeftSkip = useCallback(() => {
const leadTile = getLeadTile(currentPage, totalPages);

const leftSkip = Math.max(1, leadTile - (tileBuffer + 1));

setPage(leftSkip);
}, [currentPage, getLeadTile, setPage, totalPages]);
}, [currentPage, getLeadTile, setPage, totalPages, tileBuffer]);

const handleRightSkip = useCallback(() => {
const leadTile = getLeadTile(currentPage, totalPages);
Expand All @@ -34,7 +36,7 @@ export const usePagination = props => {
);

setPage(rightSkip);
}, [currentPage, getLeadTile, setPage, totalPages]);
}, [currentPage, getLeadTile, setPage, totalPages, tileBuffer]);

const handleNavBack = useCallback(() => {
if (currentPage > 1) {
Expand All @@ -61,7 +63,7 @@ export const usePagination = props => {
tiles.push(tile);
}
return tiles;
}, [currentPage, getLeadTile, totalPages]);
}, [currentPage, getLeadTile, totalPages, tileBuffer]);

return {
handleLeftSkip,
Expand Down

0 comments on commit 1d2d32b

Please sign in to comment.