@@ -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
@@ -535,19 +547,20 @@ export class TableBase<T extends {}> extends React.Component<
535
547
} ;
536
548
537
549
public incrementPage = ( ) => {
538
- this . setPage ( this . state . page + 1 ) ;
550
+ const newPage = this . state . page + 1 ;
551
+ this . setPage ( newPage ) ;
539
552
} ;
540
553
541
554
public decrementPage = ( ) => {
542
- this . setPage ( this . state . page - 1 ) ;
555
+ const newPage = this . state . page - 1 ;
556
+ this . setPage ( newPage ) ;
543
557
} ;
544
558
545
559
public render ( ) {
546
560
const {
547
561
columns,
548
562
data,
549
563
usePager,
550
- itemsPerPage,
551
564
pagerPosition,
552
565
rowAnchorAttributes,
553
566
rowKey,
@@ -557,20 +570,26 @@ export class TableBase<T extends {}> extends React.Component<
557
570
getRowClass,
558
571
className,
559
572
fuzzyPager,
573
+ pagination,
560
574
} = this . props ;
561
575
562
576
const { page, sort } = this . state ;
563
577
const items = data || [ ] ;
564
- const totalItems = items . length ;
565
- const _itemsPerPage = itemsPerPage || 50 ;
578
+ const totalItems = pagination ?. totalItems ?? items . length ;
579
+ const _itemsPerPage = pagination ?. itemsPerPage || DEFAULT_ITEMS_PER_PAGE ;
566
580
const _pagerPosition = pagerPosition || 'top' ;
567
581
568
- const lowerBound = usePager ? page * _itemsPerPage : 0 ;
569
- const upperBound = usePager
570
- ? Math . min ( ( page + 1 ) * _itemsPerPage , totalItems )
571
- : totalItems ;
582
+ const lowerBound =
583
+ ! pagination ?. serverSide && usePager ? page * _itemsPerPage : 0 ;
584
+ const upperBound =
585
+ ! pagination ?. serverSide && usePager
586
+ ? Math . min ( ( page + 1 ) * _itemsPerPage , totalItems )
587
+ : totalItems ;
572
588
573
- const sortedData = this . sortData ( items ) . slice ( lowerBound , upperBound ) ;
589
+ const sortedData =
590
+ ! pagination ?. serverSide && usePager
591
+ ? this . sortData ( items ) . slice ( lowerBound , upperBound )
592
+ : items ;
574
593
575
594
const shouldShowPaper = ! ! usePager && totalItems > 0 ;
576
595
@@ -586,7 +605,7 @@ export class TableBase<T extends {}> extends React.Component<
586
605
fuzzy = { fuzzyPager }
587
606
totalItems = { totalItems }
588
607
itemsPerPage = { _itemsPerPage }
589
- page = { page }
608
+ page = { pagination ?. currentPage ?? page }
590
609
nextPage = { this . incrementPage }
591
610
prevPage = { this . decrementPage }
592
611
mb = { 2 }
@@ -730,7 +749,7 @@ export interface TableBaseProps<T> {
730
749
/** A function that is called when a column is sorted */
731
750
onSort ?: ( sort : TableSortOptions < T > ) => void ;
732
751
/** A function that is called when the page is incremented, decremented and reset */
733
- onPageChange ?: ( page : number ) => void ;
752
+ onPageChange ?: ( page : number , itemsPerPage : number ) => void ;
734
753
/** sort options to be used both as a default sort, and on subsequent renders if the passed sort changes */
735
754
sort ?: TableSortOptions < T > ;
736
755
/** Attributes to pass to the anchor element used in a row */
@@ -753,6 +772,8 @@ export interface TableBaseProps<T> {
753
772
fuzzyPager ?: boolean ;
754
773
/** The number of items to be shown per page. Only used if `usePager` is true. Defaults to 50. */
755
774
itemsPerPage ?: number ;
775
+ /** Information from a server side pagination */
776
+ pagination ?: Pagination ;
756
777
/** 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
778
pagerPosition ?: 'top' | 'bottom' | 'both' ;
758
779
className ?: string ;
0 commit comments