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(pagination): prevents console error when page-size is set to zero #8017

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,25 @@ describe("calcite-pagination", () => {
});
});

describe("pageSize", () => {
it("should set pageSize to one when set to zero via attribute", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-pagination start-item="1" total-items="5" page-size="0"></calcite-pagination>`);
const pagination = await page.find("calcite-pagination");
expect(await pagination.getProperty("pageSize")).toBe(1);
});

it("should set pageSize to one when set to zero programmatically", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-pagination start-item="1" total-items="50" page-size="10"></calcite-pagination>`);
const pagination = await page.find("calcite-pagination");
expect(await pagination.getProperty("pageSize")).toBe(10);
pagination.setProperty("pageSize", 0);
await page.waitForChanges();
expect(await pagination.getProperty("pageSize")).toBe(1);
});
});

describe("number locale support", () => {
let page: E2EPage;
let element: E2EElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class Pagination
@Prop() numberingSystem: NumberingSystem;

/** Specifies the number of items per page. */
@Prop({ reflect: true }) pageSize = 20;
@Prop({ mutable: true, reflect: true }) pageSize = 20;

/** Specifies the size of the component. */
@Prop({ reflect: true }) scale: Scale = "m";
Expand All @@ -112,12 +112,16 @@ export class Pagination
/** Specifies the total number of items. */
@Prop({ reflect: true }) totalItems = 0;

@Watch("pageSize")
@Watch("totalItems")
handleTotalPages(): void {
handleTotalItems(): void {
this.totalPages = this.totalItems / this.pageSize;
}

@Watch("pageSize")
handlePageSize(): void {
this.handleTotalPages();
}

// --------------------------------------------------------------------------
//
// Private Properties
Expand Down Expand Up @@ -297,6 +301,13 @@ export class Pagination
this.emitUpdate();
};

private handleTotalPages(): void {
if (this.pageSize < 1) {
this.pageSize = 1;
}
this.totalPages = this.totalItems / this.pageSize;
}

//--------------------------------------------------------------------------
//
// Render Methods
Expand Down Expand Up @@ -382,6 +393,12 @@ export class Pagination

renderPage(start: number): VNode {
const { pageSize } = this;

// if(pageSize === 0 ) {

// this.pageSize === 1
// console.log("pagesizeeeeee",pageSize)
// }
const page = Math.floor(start / pageSize) + (pageSize === 1 ? 0 : 1);

numberStringFormatter.numberFormatOptions = {
Expand Down