Skip to content

Commit

Permalink
fix(paginator): fixed a bug where setting the offset property befor…
Browse files Browse the repository at this point in the history
…e `total` would result in the page size not changing when `total` was updated
  • Loading branch information
DRiFTy17 committed Oct 6, 2023
1 parent 4b1589f commit 7062230
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/lib/paginator/paginator-foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class PaginatorFoundation {
// Backing models
private _pageIndex = PAGINATOR_CONSTANTS.numbers.DEFAULT_PAGE_INDEX;
private _pageSize = PAGINATOR_CONSTANTS.numbers.DEFAULT_PAGE_SIZE;
private _offset = 0;
private _total = PAGINATOR_CONSTANTS.numbers.DEFAULT_TOTAL;
private _pageSizeOptions: ISelectOption[] = [];
private _label = PAGINATOR_CONSTANTS.strings.DEFAULT_LABEL;
Expand Down Expand Up @@ -82,24 +83,24 @@ export class PaginatorFoundation {

/** Sets page index by providing the number of items to skip. */
public set offset(value: number) {
if (value >= this._total) {
if (this._total >= this._pageSize) {
value = this._total - this._pageSize;
} else {
value = 0;
}
if (this._offset !== value) {
this._offset = value;
this._computePageIndexFromOffset(value);
}
const clampedValue = Math.min(Math.max(value, 0), this._total);
this.pageIndex = Math.floor(clampedValue / this._pageSize);
}
public get offset(): number {
return this._pageIndex * this._pageSize;
return this._offset;
}

/** The total number of items to be paginated. Default is 0. */
public set total(value: number) {
if (this._total !== value) {
this._total = value;

if (this._offset > 0 && this._total > 0) {
this._computePageIndexFromOffset(this._offset);
}

this._update();
this._adapter.setHostAttribute(PAGINATOR_CONSTANTS.attributes.TOTAL, this._total.toString());
}
Expand Down Expand Up @@ -362,6 +363,8 @@ export class PaginatorFoundation {
* Updates our internal state as well as updating the UI.
*/
private _update(): void {
this._offset = this._pageIndex * this._pageSize;

// Create and update the range label
if (this.pageSize > 1) {
const startIndex = this._pageIndex * this._pageSize;
Expand Down Expand Up @@ -469,4 +472,16 @@ export class PaginatorFoundation {
// same as has next page
return this._hasNextPage();
}

private _computePageIndexFromOffset(value: number): void {
if (value >= this._total) {
if (this._total >= this._pageSize) {
value = this._total - this._pageSize;
} else {
value = 0;
}
}
const clampedValue = Math.min(Math.max(value, 0), this._total);
this.pageIndex = Math.floor(clampedValue / this._pageSize);
}
}
41 changes: 41 additions & 0 deletions src/test/spec/paginator/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,47 @@ describe('PaginatorComponent', function(this: ITestContext) {
expect(this.context.paginator.pageIndex).toBe(0);
});

it('should set page index via offset property if total is not > 0 initially', function(this: ITestContext) {
this.context = setupTestContext();
const pageSizeOptions = [5, 10, 25];
this.context.paginator.pageSizeOptions = pageSizeOptions;
this.context.paginator.pageSize = 25;

expect(this.context.paginator.total).toBe(0);

this.context.paginator.offset = 25;
expect(this.context.paginator.pageIndex).toBe(0);

this.context.paginator.total = 100;
expect(this.context.paginator.pageIndex).toBe(1);
});

it('should update offset when page index changes', function(this: ITestContext) {
this.context = setupTestContext();
const pageSizeOptions = [5, 10, 25];
this.context.paginator.pageSizeOptions = pageSizeOptions;
this.context.paginator.pageSize = 25;
this.context.paginator.total = 100;

expect(this.context.paginator.pageIndex).toBe(0);
expect(this.context.paginator.offset).toBe(0);

this.context.paginator.pageIndex = 1;

expect(this.context.paginator.pageIndex).toBe(1);
expect(this.context.paginator.offset).toBe(25);

this.context.paginator.pageIndex = 3;

expect(this.context.paginator.pageIndex).toBe(3);
expect(this.context.paginator.offset).toBe(75);

this.context.paginator.pageIndex = 0;

expect(this.context.paginator.pageIndex).toBe(0);
expect(this.context.paginator.offset).toBe(0);
});

it('should get offset', function(this: ITestContext) {
this.context = setupTestContext();
const pageSizeOptions = [5, 10, 25];
Expand Down

0 comments on commit 7062230

Please sign in to comment.