Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(paginator): coerce string values #8946

Merged
merged 1 commit into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/lib/paginator/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('MatPaginator', () => {
MatPaginatorWithoutPageSizeApp,
MatPaginatorWithoutOptionsApp,
MatPaginatorWithoutInputsApp,
MatPaginatorWithStringValues
],
providers: [MatPaginatorIntl]
}).compileComponents();
Expand Down Expand Up @@ -251,6 +252,18 @@ describe('MatPaginator', () => {
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('.mat-select')).toBeNull();
});

it('should handle the number inputs being passed in as strings', () => {
const withStringFixture = TestBed.createComponent(MatPaginatorWithStringValues);
const withStringPaginator = withStringFixture.componentInstance.paginator;

withStringFixture.detectChanges();

expect(withStringPaginator.pageIndex).toEqual(0);
expect(withStringPaginator.length).toEqual(100);
expect(withStringPaginator.pageSize).toEqual(10);
expect(withStringPaginator.pageSizeOptions).toEqual([5, 10, 25, 100]);
});
});

function getPreviousButton(fixture: ComponentFixture<any>) {
Expand All @@ -264,10 +277,10 @@ function getNextButton(fixture: ComponentFixture<any>) {
@Component({
template: `
<mat-paginator [pageIndex]="pageIndex"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
[length]="length"
(page)="latestPageEvent = $event">
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
[length]="length"
(page)="latestPageEvent = $event">
</mat-paginator>
`,
})
Expand Down Expand Up @@ -312,3 +325,16 @@ class MatPaginatorWithoutPageSizeApp {
class MatPaginatorWithoutOptionsApp {
@ViewChild(MatPaginator) paginator: MatPaginator;
}

@Component({
template: `
<mat-paginator pageIndex="0"
pageSize="10"
[pageSizeOptions]="['5', '10', '25', '100']"
length="100">
</mat-paginator>
`
})
class MatPaginatorWithStringValues {
@ViewChild(MatPaginator) paginator: MatPaginator;
}
9 changes: 5 additions & 4 deletions src/lib/paginator/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {coerceNumberProperty} from '@angular/cdk/coercion';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Expand Down Expand Up @@ -64,7 +65,7 @@ export class MatPaginator implements OnInit, OnDestroy {
@Input()
get pageIndex(): number { return this._pageIndex; }
set pageIndex(pageIndex: number) {
this._pageIndex = pageIndex;
this._pageIndex = coerceNumberProperty(pageIndex);
this._changeDetectorRef.markForCheck();
}
_pageIndex: number = 0;
Expand All @@ -73,7 +74,7 @@ export class MatPaginator implements OnInit, OnDestroy {
@Input()
get length(): number { return this._length; }
set length(length: number) {
this._length = length;
this._length = coerceNumberProperty(length);
this._changeDetectorRef.markForCheck();
}
_length: number = 0;
Expand All @@ -82,7 +83,7 @@ export class MatPaginator implements OnInit, OnDestroy {
@Input()
get pageSize(): number { return this._pageSize; }
set pageSize(pageSize: number) {
this._pageSize = pageSize;
this._pageSize = coerceNumberProperty(pageSize);
this._updateDisplayedPageSizeOptions();
}
private _pageSize: number;
Expand All @@ -91,7 +92,7 @@ export class MatPaginator implements OnInit, OnDestroy {
@Input()
get pageSizeOptions(): number[] { return this._pageSizeOptions; }
set pageSizeOptions(pageSizeOptions: number[]) {
this._pageSizeOptions = pageSizeOptions;
this._pageSizeOptions = (pageSizeOptions || []).map(p => coerceNumberProperty(p));
this._updateDisplayedPageSizeOptions();
}
private _pageSizeOptions: number[] = [];
Expand Down