@@ -7,6 +7,7 @@ import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"
7
7
import { Maybe } from "components/Conditionals/Maybe"
8
8
import { CSSProperties } from "react"
9
9
import { PageButton } from "./PageButton"
10
+ import { buildPagedList , DEFAULT_RECORDS_PER_PAGE } from "./utils"
10
11
11
12
export type PaginationWidgetProps = {
12
13
prevLabel : string
@@ -20,64 +21,6 @@ export type PaginationWidgetProps = {
20
21
containerStyle ?: CSSProperties
21
22
}
22
23
23
- /**
24
- * Generates a ranged array with an option to step over values.
25
- * Shamelessly stolen from:
26
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#sequence_generator_range
27
- */
28
- const range = ( start : number , stop : number , step = 1 ) =>
29
- Array . from ( { length : ( stop - start ) / step + 1 } , ( _ , i ) => start + i * step )
30
-
31
- export const DEFAULT_RECORDS_PER_PAGE = 25
32
- // Number of pages to the left or right of the current page selection.
33
- const PAGE_NEIGHBORS = 1
34
- // Number of pages displayed for cases where there are multiple ellipsis showing. This can be
35
- // thought of as the minimum number of page numbers to display when multiple ellipsis are showing.
36
- const PAGES_TO_DISPLAY = PAGE_NEIGHBORS * 2 + 3
37
- // Total page blocks(page numbers or ellipsis) displayed, including the maximum number of ellipsis (2).
38
- // This gives us maximum number of 7 page blocks to be displayed when the page neighbors value is 1.
39
- const NUM_PAGE_BLOCKS = PAGES_TO_DISPLAY + 2
40
-
41
- /**
42
- * Builds a list of pages based on how many pages exist and where the user is in their navigation of those pages.
43
- * List result is used to from the buttons that make up the Pagination Widget
44
- */
45
- export const buildPagedList = (
46
- numPages : number ,
47
- activePage : number ,
48
- ) : ( string | number ) [ ] => {
49
- if ( numPages > NUM_PAGE_BLOCKS ) {
50
- let pages = [ ]
51
- const leftBound = activePage - PAGE_NEIGHBORS
52
- const rightBound = activePage + PAGE_NEIGHBORS
53
- const beforeLastPage = numPages - 1
54
- const startPage = leftBound > 2 ? leftBound : 2
55
- const endPage = rightBound < beforeLastPage ? rightBound : beforeLastPage
56
-
57
- pages = range ( startPage , endPage )
58
-
59
- const singleSpillOffset = PAGES_TO_DISPLAY - pages . length - 1
60
- const hasLeftOverflow = startPage > 2
61
- const hasRightOverflow = endPage < beforeLastPage
62
- const leftOverflowPage = "left"
63
- const rightOverflowPage = "right"
64
-
65
- if ( hasLeftOverflow && ! hasRightOverflow ) {
66
- const extraPages = range ( startPage - singleSpillOffset , startPage - 1 )
67
- pages = [ leftOverflowPage , ...extraPages , ...pages ]
68
- } else if ( ! hasLeftOverflow && hasRightOverflow ) {
69
- const extraPages = range ( endPage + 1 , endPage + singleSpillOffset )
70
- pages = [ ...pages , ...extraPages , rightOverflowPage ]
71
- } else if ( hasLeftOverflow && hasRightOverflow ) {
72
- pages = [ leftOverflowPage , ...pages , rightOverflowPage ]
73
- }
74
-
75
- return [ 1 , ...pages , numPages ]
76
- }
77
-
78
- return range ( 1 , numPages )
79
- }
80
-
81
24
export const PaginationWidget = ( {
82
25
prevLabel,
83
26
nextLabel,
0 commit comments