@@ -13,169 +13,118 @@ export interface IPaginator {
13
13
loadPrevious ( ) : void ;
14
14
loadFirst ( ) : void ;
15
15
loadLast ( ) : void ;
16
- lastPage : number | undefined ;
17
- goToPage ( page : number ) : void ;
16
+ loadPage ( page : number ) : void ;
18
17
hasPrevious ( ) : boolean ;
19
18
hasNext ( ) : boolean ;
20
- hasLast ( ) : boolean ;
21
- }
22
-
23
- export function lastPage ( data : Data , page_size : number ) {
24
- return Math . max ( Math . ceil ( data . length / page_size ) - 1 , 0 ) ;
25
- }
26
-
27
- export function loadPrevious ( page_current : number , setProps : SetProps ) {
28
- if ( page_current <= 0 ) {
29
- return ;
30
- }
31
-
32
- page_current -- ;
33
- setProps ( { page_current, ...clearSelection } ) ;
19
+ isLast ( ) : boolean ;
20
+ lastPage : number | undefined ;
34
21
}
35
22
36
- export function loadFirst ( page_current : number , setProps : SetProps ) {
37
- page_current = 0 ;
38
- setProps ( { page_current, ...clearSelection } ) ;
23
+ export interface IPaginatorParams {
24
+ setProps : SetProps ;
25
+ page_current : number ;
26
+ page_count : number | undefined ;
39
27
}
40
28
41
- export function hasPrevious ( page_current : number ) {
42
- return ( page_current !== 0 ) ;
29
+ export function lastPage (
30
+ data : Data ,
31
+ page_size : number
32
+ ) {
33
+ return Math . ceil ( data . length / page_size ) ;
43
34
}
44
35
45
- function getBackEndPagination (
46
- page_current : number ,
47
- setProps : SetProps ,
48
- page_count : number | undefined
36
+ function makePaginator (
37
+ params : IPaginatorParams | null
49
38
) : IPaginator {
50
39
51
- // adjust for zero-indexing
52
- if ( page_count ) {
53
- page_count = Math . max ( 0 , page_count - 1 ) ;
40
+ if ( params === null ) {
41
+ return {
42
+ loadNext ( ) { } ,
43
+ loadPrevious ( ) { } ,
44
+ loadFirst ( ) { } ,
45
+ loadLast ( ) { } ,
46
+ loadPage ( ) { } ,
47
+ hasPrevious ( ) { return true ; } ,
48
+ hasNext ( ) { return true ; } ,
49
+ isLast ( ) { return false ; } ,
50
+ lastPage : undefined
51
+ } ;
54
52
}
55
53
56
- return {
57
- loadNext : ( ) => {
58
- page_current ++ ;
59
- setProps ( { page_current, ...clearSelection } ) ;
60
- } ,
61
- loadPrevious : ( ) => loadPrevious ( page_current , setProps ) ,
62
- loadFirst : ( ) => loadFirst ( page_current , setProps ) ,
63
- loadLast : ( ) => {
64
- if ( page_count ) {
65
- page_current = page_count ;
66
- setProps ( { page_current, ...clearSelection } ) ;
67
- }
68
- } ,
69
- lastPage : page_count ,
70
- goToPage : ( page : number ) => {
71
-
72
- // adjust for zero-indexing
73
- page -- ;
74
-
75
- page_current = page ;
54
+ let {
55
+ setProps,
56
+ page_current,
57
+ page_count
58
+ } = params ;
59
+
60
+ function updatePage ( ) {
61
+ setProps ( {
62
+ page_current,
63
+ ...clearSelection
64
+ } ) ;
65
+ }
76
66
77
- if ( page < 0 ) {
78
- page_current = 0 ;
79
- }
67
+ function loadPage ( page : number ) {
80
68
81
- if ( page_count && page > page_count ) {
82
- page_current = page_count ;
83
- }
69
+ page = Math . max ( 0 , page ) ;
70
+ page = page_count ? Math . min ( page_count - 1 , page ) : page ;
84
71
85
- setProps ( { page_current, ...clearSelection } ) ;
86
- } ,
87
- hasPrevious : ( ) => hasPrevious ( page_current ) ,
88
- hasNext : ( ) => {
89
- return page_count === undefined || page_current !== page_count ;
90
- } ,
91
- hasLast : ( ) => {
92
- return ! page_count ? false : page_current !== page_count ;
93
- }
94
- } ;
95
- }
72
+ page_current = page ;
73
+ updatePage ( ) ;
74
+ }
96
75
97
- function getFrontEndPagination (
98
- page_current : number ,
99
- page_size : number ,
100
- setProps : SetProps ,
101
- data : Data
102
- ) {
103
76
return {
104
- loadNext : ( ) => {
105
- const maxPageIndex = lastPage ( data , page_size ) ;
106
77
107
- if ( page_current >= maxPageIndex ) {
108
- return ;
109
- }
78
+ loadNext : ( ) => loadPage ( page_current + 1 ) ,
110
79
111
- page_current ++ ;
112
- setProps ( { page_current, ...clearSelection } ) ;
113
- } ,
114
- loadPrevious : ( ) => loadPrevious ( page_current , setProps ) ,
115
- loadFirst : ( ) => loadFirst ( page_current , setProps ) ,
116
- loadLast : ( ) => {
117
- page_current = lastPage ( data , page_size ) ;
118
- setProps ( { page_current, ...clearSelection } ) ;
119
- } ,
120
- lastPage : lastPage ( data , page_size ) ,
121
- goToPage : ( page : number ) => {
80
+ loadPrevious : ( ) => loadPage ( page_current - 1 ) ,
122
81
123
- page -- ;
82
+ loadFirst : ( ) => loadPage ( 0 ) ,
124
83
125
- page_current = page ;
84
+ loadPage ,
126
85
127
- if ( page < 0 ) {
128
- page_current = 0 ;
129
- }
130
-
131
- if ( page > lastPage ( data , page_size ) ) {
132
- page_current = lastPage ( data , page_size ) ;
133
- }
86
+ loadLast : ( ) => {
87
+ if ( page_count ) { loadPage ( page_count - 1 ) ; }
88
+ } ,
134
89
135
- setProps ( { page_current, ...clearSelection } ) ;
90
+ hasPrevious : ( ) => {
91
+ return page_current !== 0 ;
136
92
} ,
137
- hasPrevious : ( ) => hasPrevious ( page_current ) ,
93
+
138
94
hasNext : ( ) => {
139
- return ( page_current !== lastPage ( data , page_size ) ) ;
95
+ return page_count ? page_current !== page_count - 1 : true ;
140
96
} ,
141
- hasLast : ( ) => {
142
- return ( page_current !== lastPage ( data , page_size ) ) ;
143
- }
144
- } ;
145
- }
146
97
147
- function getNoPagination ( ) {
148
- return {
149
- loadNext : ( ) => { } ,
150
- loadPrevious : ( ) => { } ,
151
- loadFirst : ( ) => { } ,
152
- loadLast : ( ) => { } ,
153
- lastPage : 0 ,
154
- goToPage : ( ) => { } ,
155
- hasPrevious : ( ) => false ,
156
- hasNext : ( ) => false ,
157
- hasLast : ( ) => false
98
+ isLast : ( ) => {
99
+ return page_count ? page_current === page_count - 1 : false ;
100
+ } ,
101
+
102
+ lastPage : page_count ? Math . max ( 0 , page_count - 1 ) : undefined
158
103
} ;
159
104
}
160
105
161
106
const getter = (
162
- page_action : TableAction ,
107
+ table_action : TableAction ,
163
108
page_current : number ,
164
109
page_size : number ,
165
110
page_count : number | undefined ,
166
111
setProps : SetProps ,
167
112
data : Data
168
113
) : IPaginator => {
169
- switch ( page_action ) {
170
- case TableAction . None :
171
- return getNoPagination ( ) ;
172
- case TableAction . Native :
173
- return getFrontEndPagination ( page_current , page_size , setProps , data ) ;
174
- case TableAction . Custom :
175
- return getBackEndPagination ( page_current , setProps , page_count ) ;
176
- default :
177
- throw new Error ( `Unknown pagination mode: '${ page_action } '` ) ;
114
+
115
+ if ( table_action === TableAction . Native ) {
116
+ page_count = lastPage ( data , page_size ) ;
178
117
}
118
+
119
+ if ( page_count ) page_count = Math . max ( page_count , 1 ) ;
120
+
121
+ return makePaginator (
122
+ table_action === TableAction . None ? null : {
123
+ setProps,
124
+ page_current,
125
+ page_count
126
+ }
127
+ ) ;
179
128
} ;
180
129
181
130
export default memoizeOneFactory ( getter ) ;
0 commit comments