TSK-1641: Modify pagination behavior when input is out of range
This commit is contained in:
parent
8b8e54644a
commit
0b682d3301
|
|
@ -1,13 +1,13 @@
|
||||||
<div class="pagination" #pagination>
|
<div class="pagination" #pagination>
|
||||||
<mat-paginator class="pagination__mat-paginator" [length]="page?.totalElements" [pageIndex]="pageSelected - 1 "
|
<mat-paginator class="pagination__mat-paginator" [length]="page?.totalElements"
|
||||||
hidePageSize="true" [pageSize]="page?.size" (page)="changeToPage($event)" [showFirstLastButtons]="true" [ngClass]="
|
hidePageSize="true" [pageSize]="page?.size" (page)="changeToPage($event)" [showFirstLastButtons]="true" [ngClass]="
|
||||||
{'pagination__mat-paginator--expanded': expanded,
|
{'pagination__mat-paginator--expanded': expanded,
|
||||||
'pagination__mat-paginator--collapsed': !expanded }"></mat-paginator>
|
'pagination__mat-paginator--collapsed': !expanded }"></mat-paginator>
|
||||||
<div class="pagination__go-to" *ngIf="expanded">
|
<div class="pagination__go-to" *ngIf="expanded">
|
||||||
<div class="pagination__go-to-label">Page:</div>
|
<div class="pagination__go-to-label">Page:</div>
|
||||||
<mat-form-field>
|
<mat-form-field>
|
||||||
<input #inputTypeAhead matInput type="text" [matAutocomplete]="auto" [(ngModel)]="pageSelected" name="accessId"
|
<input id="inputTypeAhead" matInput type="text" [matAutocomplete]="auto" [(ngModel)]="pageSelected" name="accessId"
|
||||||
(ngModelChange)="filter(pageSelected)" (focus)="filter(pageSelected)" />
|
(ngModelChange)="filter(pageSelected)" (focus)="filter(pageSelected)" (click)="onSelectText()" />
|
||||||
<mat-autocomplete #autoComplete autoActiveFirstOption (optionSelected)="goToPage($event.option.value)"
|
<mat-autocomplete #autoComplete autoActiveFirstOption (optionSelected)="goToPage($event.option.value)"
|
||||||
#auto="matAutocomplete">
|
#auto="matAutocomplete">
|
||||||
<mat-option *ngFor="let pageNumber of filteredPages" [value]="pageNumber">{{ pageNumber }}</mat-option>
|
<mat-option *ngFor="let pageNumber of filteredPages" [value]="pageNumber">{{ pageNumber }}</mat-option>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
import { PaginationComponent } from './pagination.component';
|
||||||
|
import { DebugElement } from '@angular/core';
|
||||||
|
import { MatPaginatorModule } from '@angular/material/paginator';
|
||||||
|
import { MatAutocompleteModule } from '@angular/material/autocomplete';
|
||||||
|
import { FormsModule } from '@angular/forms';
|
||||||
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||||
|
import { MatInputModule } from '@angular/material/input';
|
||||||
|
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
||||||
|
|
||||||
|
describe('PaginationComponent', () => {
|
||||||
|
let fixture: ComponentFixture<PaginationComponent>;
|
||||||
|
let debugElement: DebugElement;
|
||||||
|
let component: PaginationComponent;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
imports: [
|
||||||
|
MatPaginatorModule,
|
||||||
|
MatAutocompleteModule,
|
||||||
|
FormsModule,
|
||||||
|
MatFormFieldModule,
|
||||||
|
MatInputModule,
|
||||||
|
BrowserAnimationsModule
|
||||||
|
],
|
||||||
|
declarations: [PaginationComponent],
|
||||||
|
providers: []
|
||||||
|
}).compileComponents();
|
||||||
|
|
||||||
|
fixture = TestBed.createComponent(PaginationComponent);
|
||||||
|
debugElement = fixture.debugElement;
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
component.page = { totalPages: 10 };
|
||||||
|
component.pageNumbers = [];
|
||||||
|
for (let i = 1; i <= component.page.totalPages; i++) {
|
||||||
|
component.pageNumbers.push(i);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should create component', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should suggest page 3 when filter() is called with 3', () => {
|
||||||
|
component.filter(3);
|
||||||
|
expect(component.filteredPages).toEqual(['3']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should suggest all pages when input of filter() is out of range', () => {
|
||||||
|
component.filter(11);
|
||||||
|
expect(component.filteredPages).toEqual(component.pageNumbers.map(String));
|
||||||
|
component.filter(-1);
|
||||||
|
expect(component.filteredPages).toEqual(component.pageNumbers.map(String));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should suggest all pages when input of filter() is not a number', () => {
|
||||||
|
component.filter('abc');
|
||||||
|
expect(component.filteredPages).toEqual(component.pageNumbers.map(String));
|
||||||
|
component.filter('');
|
||||||
|
expect(component.filteredPages).toEqual(component.pageNumbers.map(String));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -110,8 +110,17 @@ export class PaginationComponent implements OnInit, OnChanges {
|
||||||
this.changePage.emit(page);
|
this.changePage.emit(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
filter(filterVal) {
|
filter(filterValue) {
|
||||||
const filterValue = filterVal.toString();
|
const pageNumbers = this.pageNumbers.map(String);
|
||||||
this.filteredPages = this.pageNumbers.map(String).filter((value) => value.includes(filterValue));
|
this.filteredPages = pageNumbers.filter((value) => value.includes(filterValue.toString()));
|
||||||
|
if (this.filteredPages.length === 0) {
|
||||||
|
this.filteredPages = pageNumbers;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onSelectText() {
|
||||||
|
const input = document.getElementById('inputTypeAhead') as HTMLInputElement;
|
||||||
|
input.focus();
|
||||||
|
input.select();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue