@@ -14,7 +14,7 @@ export const usePagination = (props: {
1414} ) => {
1515 const { totalResults, totalNumberOfPages, currentPage, resultsPerPage } = props ;
1616 const adjacentToCurrentPage = 1 ; // This is the number(s) we see next to the currentPage
17- const totalNumbersShownInPagination = 5 ;
17+ const totalNumbersShownInPagination = 6 ;
1818
1919 const pageNumbersAsArray = ( startRange : number , endRange : number ) => {
2020 return [ ...Array ( endRange + 1 ) . keys ( ) ] . slice ( startRange ) ;
@@ -28,28 +28,26 @@ export const usePagination = (props: {
2828 }
2929
3030 // Otherwise, we show the ellipses
31- // Make sure when calculating it's still within the range
32- const adjacentRightIsWithinRange = Math . min ( currentPage + adjacentToCurrentPage , totalNumberOfPages ) ;
33- const adjacentLeftIsWithinRange = Math . max ( currentPage - adjacentToCurrentPage , 1 ) ;
31+ const toTheRightOfCurrent = Math . min ( currentPage + adjacentToCurrentPage , totalNumberOfPages ) ;
32+ const toTheLeftOfCurrent = Math . max ( currentPage - adjacentToCurrentPage , 1 ) ;
3433
35- const showRightEllipsis =
36- adjacentRightIsWithinRange < totalNumberOfPages - minimumAmountInBetweenToShowEllipsis ; // e.g. "1 2 3 ... 7"
37- const showLeftEllipsis = adjacentLeftIsWithinRange > minimumAmountInBetweenToShowEllipsis ; // e.g. 1 ... 5 6 7"
34+ const showRightEllipsis = toTheRightOfCurrent < totalNumberOfPages - minimumAmountInBetweenToShowEllipsis ; // e.g. "1 2 3 ... 7"
35+ const showLeftEllipsis = toTheLeftOfCurrent > minimumAmountInBetweenToShowEllipsis ; // e.g. 1 ... 5 6 7"
3836
39- if ( showRightEllipsis ) {
37+ if ( showRightEllipsis && ! showLeftEllipsis ) {
4038 let leftSideNumbers = 3 ;
4139 let leftPageNumbersAsArray = pageNumbersAsArray ( 1 , leftSideNumbers ) ;
4240 return [ ...leftPageNumbersAsArray , "..." , totalNumberOfPages ] ;
4341 }
4442
45- if ( showLeftEllipsis ) {
43+ if ( showLeftEllipsis && ! showRightEllipsis ) {
4644 let rightSideNumbers = 3 ;
4745 let rightPageNumbersAsArray = pageNumbersAsArray ( totalNumberOfPages - rightSideNumbers , totalNumberOfPages ) ;
4846 return [ 1 , "..." , ...rightPageNumbersAsArray ] ;
4947 }
5048
5149 if ( showRightEllipsis && showLeftEllipsis ) {
52- let middleNumbers = pageNumbersAsArray ( adjacentLeftIsWithinRange , adjacentRightIsWithinRange ) ;
50+ let middleNumbers = pageNumbersAsArray ( toTheLeftOfCurrent , toTheRightOfCurrent ) ;
5351 return [ 1 , "..." , ...middleNumbers , "..." , totalNumberOfPages ] ;
5452 }
5553 } , [ totalResults , totalNumberOfPages , currentPage , adjacentToCurrentPage , pageNumbersAsArray , resultsPerPage ] ) ;
0 commit comments