Skip to content

Commit

Permalink
fix(pagination): emit page change only when collection size is set
Browse files Browse the repository at this point in the history
Fixes #1841

Closes #2257
  • Loading branch information
maxokorokov authored and pkozlowski-opensource committed Mar 28, 2018
1 parent 5ffaabc commit 51a2a29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/pagination/pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,21 @@ describe('ngb-pagination', () => {

expect(fixture.componentInstance.onPageChange).not.toHaveBeenCalled();
}));

it('should not emit "pageChange" when collection size is not set', fakeAsync(() => {
const html = `<ngb-pagination [page]="page" (pageChange)="onPageChange($event)"></ngb-pagination>`;
const fixture = createTestComponent(html);
tick();

spyOn(fixture.componentInstance, 'onPageChange');

fixture.componentInstance.page = 5;
fixture.detectChanges();
tick();

expect(fixture.componentInstance.onPageChange).not.toHaveBeenCalled();
}));

it('should set classes correctly for disabled state', fakeAsync(() => {
const html = `<ngb-pagination [collectionSize]="collectionSize" [pageSize]="pageSize" [maxSize]="maxSize"
[disabled]=true></ngb-pagination>`;
Expand Down
8 changes: 5 additions & 3 deletions src/pagination/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ export class NgbPagination implements OnChanges {
@Input() maxSize: number;

/**
* Current page.
* Current page. Page numbers start with 1
*/
@Input() page = 0;
@Input() page = 1;

/**
* Number of items per page.
Expand All @@ -99,6 +99,8 @@ export class NgbPagination implements OnChanges {
/**
* An event fired when the page is changed.
* Event's payload equals to the newly selected page.
* Will fire only if collection size is set and all values are valid.
* Page numbers start with 1
*/
@Output() pageChange = new EventEmitter<number>(true);

Expand Down Expand Up @@ -192,7 +194,7 @@ export class NgbPagination implements OnChanges {
const prevPageNo = this.page;
this.page = getValueInRange(newPageNo, this.pageCount, 1);

if (this.page !== prevPageNo) {
if (this.page !== prevPageNo && isNumber(this.collectionSize)) {
this.pageChange.emit(this.page);
}
}
Expand Down

0 comments on commit 51a2a29

Please sign in to comment.