@@ -24,6 +24,13 @@ const BaseTableWrapper = styled.div`
24
24
max-width: 100%;
25
25
border-bottom: 1px solid ${ ( props ) => props . theme . colors . quartenary . main } ;
26
26
` ;
27
+ export const DEFAULT_ITEMS_PER_PAGE = 50 ;
28
+ export interface Pagination {
29
+ currentPage ?: number ;
30
+ itemsPerPage : number ;
31
+ totalItems ?: number ;
32
+ serverSide ?: boolean ;
33
+ }
27
34
28
35
interface InternalTableBaseProps {
29
36
hasCheckbox : boolean ;
@@ -170,7 +177,7 @@ export class TableBase<T extends {}> extends React.Component<
170
177
}
171
178
172
179
public componentDidUpdate ( prevProps : TableBaseProps < T > ) {
173
- const { sort, checkedItems, data, itemsPerPage , rowKey } = this . props ;
180
+ const { sort, checkedItems, data, pagination , rowKey } = this . props ;
174
181
if ( sort && ! isEqual ( prevProps . sort , sort ) ) {
175
182
this . setState ( {
176
183
sort,
@@ -186,10 +193,12 @@ export class TableBase<T extends {}> extends React.Component<
186
193
this . setRowSelection ( checkedItems ) ;
187
194
}
188
195
189
- const totalItems = data ?. length ?? 0 ;
196
+ const totalItems = pagination ?. totalItems ?? data ?. length ?? 0 ;
197
+ const currentPage = pagination ?. currentPage ?? this . state . page ;
190
198
if (
191
- this . state . page !== 0 &&
192
- totalItems <= this . state . page * ( itemsPerPage ?? 50 )
199
+ currentPage !== 1 &&
200
+ totalItems <=
201
+ currentPage * ( pagination ?. itemsPerPage ?? DEFAULT_ITEMS_PER_PAGE )
193
202
) {
194
203
this . resetPager ( ) ;
195
204
}
@@ -464,6 +473,7 @@ export class TableBase<T extends {}> extends React.Component<
464
473
public toggleSort = ( e : React . MouseEvent < HTMLButtonElement > ) => {
465
474
const { field } = e . currentTarget . dataset ;
466
475
const { sort } = this . state ;
476
+ const { pagination } = this . props ;
467
477
if ( ! field ) {
468
478
return ;
469
479
}
@@ -476,8 +486,9 @@ export class TableBase<T extends {}> extends React.Component<
476
486
if ( sort . field === field ) {
477
487
nextSort = { field : sort . field , reverse : ! sort . reverse } ;
478
488
}
479
-
480
- this . setState ( { sort : nextSort } ) ;
489
+ if ( ! pagination ?. serverSide ) {
490
+ this . setState ( { sort : nextSort } ) ;
491
+ }
481
492
482
493
if ( this . props . onSort ) {
483
494
this . props . onSort ( nextSort ) ;
@@ -522,11 +533,12 @@ export class TableBase<T extends {}> extends React.Component<
522
533
}
523
534
} ;
524
535
525
- public setPage = ( change : any ) => {
526
- if ( this . props . onPageChange ) {
527
- this . props . onPageChange ( change ) ;
528
- }
529
-
536
+ public setPage = ( change : number ) => {
537
+ const { pagination } = this . props ;
538
+ this . props . onPageChange ?.(
539
+ change ,
540
+ pagination ?. itemsPerPage ?? DEFAULT_ITEMS_PER_PAGE ,
541
+ ) ;
530
542
this . setState ( { page : change } ) ;
531
543
} ;
532
544
@@ -547,7 +559,6 @@ export class TableBase<T extends {}> extends React.Component<
547
559
columns,
548
560
data,
549
561
usePager,
550
- itemsPerPage,
551
562
pagerPosition,
552
563
rowAnchorAttributes,
553
564
rowKey,
@@ -557,20 +568,26 @@ export class TableBase<T extends {}> extends React.Component<
557
568
getRowClass,
558
569
className,
559
570
fuzzyPager,
571
+ pagination,
560
572
} = this . props ;
561
573
562
574
const { page, sort } = this . state ;
563
575
const items = data || [ ] ;
564
- const totalItems = items . length ;
565
- const _itemsPerPage = itemsPerPage || 50 ;
576
+ const totalItems = pagination ?. totalItems ?? items . length ;
577
+ const _itemsPerPage = pagination ?. itemsPerPage || DEFAULT_ITEMS_PER_PAGE ;
566
578
const _pagerPosition = pagerPosition || 'top' ;
567
579
568
- const lowerBound = usePager ? page * _itemsPerPage : 0 ;
569
- const upperBound = usePager
570
- ? Math . min ( ( page + 1 ) * _itemsPerPage , totalItems )
571
- : totalItems ;
580
+ const lowerBound =
581
+ ! pagination ?. serverSide && usePager ? page * _itemsPerPage : 0 ;
582
+ const upperBound =
583
+ ! pagination ?. serverSide && usePager
584
+ ? Math . min ( ( page + 1 ) * _itemsPerPage , totalItems )
585
+ : totalItems ;
572
586
573
- const sortedData = this . sortData ( items ) . slice ( lowerBound , upperBound ) ;
587
+ const sortedData =
588
+ ! pagination ?. serverSide && usePager
589
+ ? this . sortData ( items ) . slice ( lowerBound , upperBound )
590
+ : items ;
574
591
575
592
const shouldShowPaper = ! ! usePager && totalItems > 0 ;
576
593
@@ -586,7 +603,7 @@ export class TableBase<T extends {}> extends React.Component<
586
603
fuzzy = { fuzzyPager }
587
604
totalItems = { totalItems }
588
605
itemsPerPage = { _itemsPerPage }
589
- page = { page }
606
+ page = { pagination ?. currentPage ?? page }
590
607
nextPage = { this . incrementPage }
591
608
prevPage = { this . decrementPage }
592
609
mb = { 2 }
@@ -730,7 +747,7 @@ export interface TableBaseProps<T> {
730
747
/** A function that is called when a column is sorted */
731
748
onSort ?: ( sort : TableSortOptions < T > ) => void ;
732
749
/** A function that is called when the page is incremented, decremented and reset */
733
- onPageChange ?: ( page : number ) => void ;
750
+ onPageChange ?: ( page : number , itemsPerPage : number ) => void ;
734
751
/** sort options to be used both as a default sort, and on subsequent renders if the passed sort changes */
735
752
sort ?: TableSortOptions < T > ;
736
753
/** Attributes to pass to the anchor element used in a row */
@@ -753,6 +770,8 @@ export interface TableBaseProps<T> {
753
770
fuzzyPager ?: boolean ;
754
771
/** The number of items to be shown per page. Only used if `usePager` is true. Defaults to 50. */
755
772
itemsPerPage ?: number ;
773
+ /** Information from a server side pagination */
774
+ pagination ?: Pagination ;
756
775
/** Sets whether the pager is displayed at the top of the table, the bottom of the table or in both positions. Only used if `usePager` is true. Defaults to `top`. */
757
776
pagerPosition ?: 'top' | 'bottom' | 'both' ;
758
777
className ?: string ;
0 commit comments