Skip to content

Commit 5ca6a5f

Browse files
authored
feat(pagination): add navigation methods (#9154)
**Related Issue:** #6344 ## Summary Adds a method to navigate to the first, last, and an arbitrary page.
1 parent 6d269b3 commit 5ca6a5f

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

packages/calcite-components/src/components/pagination/pagination.e2e.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,58 @@ describe("calcite-pagination", () => {
279279
}
280280
});
281281
});
282+
283+
describe("navigation methods", () => {
284+
let page: E2EPage;
285+
beforeEach(async () => {
286+
page = await newE2EPage();
287+
await page.setContent(
288+
`<calcite-pagination start-item="1" total-items="124" page-size="20"></calcite-pagination>`,
289+
);
290+
});
291+
292+
it("navigates to last page", async () => {
293+
const element = await page.find("calcite-pagination");
294+
await element.callMethod("goTo", "end");
295+
await page.waitForChanges();
296+
const item = await element.getProperty("startItem");
297+
expect(item).toEqual(121);
298+
});
299+
300+
it("navigates to first page", async () => {
301+
const element = await page.find("calcite-pagination");
302+
await element.callMethod("goTo", "end");
303+
await page.waitForChanges();
304+
let item = await element.getProperty("startItem");
305+
expect(item).toEqual(121);
306+
await element.callMethod("goTo", "start");
307+
await page.waitForChanges();
308+
item = await element.getProperty("startItem");
309+
expect(item).toEqual(1);
310+
});
311+
312+
it("navigates middle page", async () => {
313+
const element = await page.find("calcite-pagination");
314+
await element.callMethod("goTo", 3);
315+
await page.waitForChanges();
316+
const item = await element.getProperty("startItem");
317+
expect(item).toEqual(41);
318+
});
319+
320+
it("navigates beyond last page", async () => {
321+
const element = await page.find("calcite-pagination");
322+
await element.callMethod("goTo", 20);
323+
await page.waitForChanges();
324+
const item = await element.getProperty("startItem");
325+
expect(item).toEqual(121);
326+
});
327+
328+
it("navigates before first page", async () => {
329+
const element = await page.find("calcite-pagination");
330+
await element.callMethod("goTo", -1);
331+
await page.waitForChanges();
332+
const item = await element.getProperty("startItem");
333+
expect(item).toEqual(1);
334+
});
335+
});
282336
});

packages/calcite-components/src/components/pagination/pagination.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,32 @@ export class Pagination
242242
this.startItem = Math.max(1, this.startItem - this.pageSize);
243243
}
244244

245+
/**
246+
* Set a specified page as active.
247+
*
248+
* @param page
249+
*/
250+
@Method()
251+
async goTo(page: number | "start" | "end"): Promise<void> {
252+
switch (page) {
253+
case "start":
254+
this.startItem = 1;
255+
break;
256+
case "end":
257+
this.startItem = this.lastStartItem;
258+
break;
259+
default: {
260+
if (page >= Math.ceil(this.totalPages)) {
261+
this.startItem = this.lastStartItem;
262+
} else if (page <= 0) {
263+
this.startItem = 1;
264+
} else {
265+
this.startItem = (page - 1) * this.pageSize + 1;
266+
}
267+
}
268+
}
269+
}
270+
245271
// --------------------------------------------------------------------------
246272
//
247273
// Private Methods

0 commit comments

Comments
 (0)