File tree 3 files changed +62
-0
lines changed
material-experimental/mdc-paginator 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -511,6 +511,31 @@ describe('MDC-based MatPaginator', () => {
511
511
const hostElement = fixture . nativeElement . querySelector ( 'mat-paginator' ) ;
512
512
expect ( hostElement . getAttribute ( 'role' ) ) . toBe ( 'group' ) ;
513
513
} ) ;
514
+
515
+ it ( 'should update the page index when switching to a smaller length' , fakeAsync ( ( ) => {
516
+ const fixture = createComponent ( MatPaginatorApp ) ;
517
+ const instance = fixture . componentInstance ;
518
+ instance . length = 50 ;
519
+ instance . pageSize = 10 ;
520
+ instance . pageIndex = 4 ;
521
+ fixture . detectChanges ( ) ;
522
+
523
+ expect ( instance . paginator . pageIndex ) . toBe ( 4 ) ;
524
+
525
+ instance . pageEvent . calls . reset ( ) ;
526
+
527
+ instance . length = 10 ;
528
+ fixture . detectChanges ( ) ;
529
+ tick ( ) ;
530
+
531
+ expect ( instance . paginator . pageIndex ) . toBe ( 0 ) ;
532
+ expect ( instance . pageEvent ) . toHaveBeenCalledWith (
533
+ jasmine . objectContaining ( {
534
+ previousPageIndex : 4 ,
535
+ pageIndex : 0 ,
536
+ } ) ,
537
+ ) ;
538
+ } ) ) ;
514
539
} ) ;
515
540
516
541
function getPreviousButton ( fixture : ComponentFixture < any > ) {
Original file line number Diff line number Diff line change @@ -506,6 +506,31 @@ describe('MatPaginator', () => {
506
506
const hostElement = fixture . nativeElement . querySelector ( 'mat-paginator' ) ;
507
507
expect ( hostElement . getAttribute ( 'role' ) ) . toBe ( 'group' ) ;
508
508
} ) ;
509
+
510
+ it ( 'should update the page index when switching to a smaller length' , fakeAsync ( ( ) => {
511
+ const fixture = createComponent ( MatPaginatorApp ) ;
512
+ const instance = fixture . componentInstance ;
513
+ instance . length = 50 ;
514
+ instance . pageSize = 10 ;
515
+ instance . pageIndex = 4 ;
516
+ fixture . detectChanges ( ) ;
517
+
518
+ expect ( instance . paginator . pageIndex ) . toBe ( 4 ) ;
519
+
520
+ instance . pageEvent . calls . reset ( ) ;
521
+
522
+ instance . length = 10 ;
523
+ fixture . detectChanges ( ) ;
524
+ tick ( ) ;
525
+
526
+ expect ( instance . paginator . pageIndex ) . toBe ( 0 ) ;
527
+ expect ( instance . pageEvent ) . toHaveBeenCalledWith (
528
+ jasmine . objectContaining ( {
529
+ previousPageIndex : 4 ,
530
+ pageIndex : 0 ,
531
+ } ) ,
532
+ ) ;
533
+ } ) ) ;
509
534
} ) ;
510
535
511
536
function getPreviousButton ( fixture : ComponentFixture < any > ) {
Original file line number Diff line number Diff line change @@ -129,6 +129,18 @@ export abstract class _MatPaginatorBase<
129
129
}
130
130
set length ( value : number ) {
131
131
this . _length = coerceNumberProperty ( value ) ;
132
+
133
+ const maxPageIndex = Math . max ( this . getNumberOfPages ( ) - 1 , 0 ) ;
134
+ const currentPageIndex = this . _pageIndex ;
135
+
136
+ if ( currentPageIndex > maxPageIndex && this . initialized ) {
137
+ // Needs to happen on the next tick, in order to avoid "changed after checked" errors.
138
+ Promise . resolve ( ) . then ( ( ) => {
139
+ this . _pageIndex = maxPageIndex ;
140
+ this . _emitPageEvent ( currentPageIndex ) ;
141
+ } ) ;
142
+ }
143
+
132
144
this . _changeDetectorRef . markForCheck ( ) ;
133
145
}
134
146
private _length = 0 ;
You can’t perform that action at this time.
0 commit comments