Leaf and Kettle's tea list can run to dozens of pages, so its page links show only the pages around the current one. Build them: app/teas/pagination.tsx, whose default export is Pagination({ pageCount }). It's a Client Component that reads the current page and the rest of the URL with useSearchParams() and usePathname(), so any list page can show it, passing only how many pages there are.
The current page is the URL's page when that's a whole number from 1 up, and 1 otherwise. A page past pageCount means the last page. With one page or none, render nothing.
Render a <nav aria-label="Pagination"> holding a <ul>, with each item in an <li>: a Previous link (not on the first page), the page links, and a Next link (not on the last page). The page links are for the first page, the last page, the current page, and the pages right before and after it. Where pages are left out, put an <li> with …, but when a gap would hide just one page, show a link to that page instead. Each page link's text is its number, and the current page's link has aria-current="page".
Make every link with <Link href={{ pathname, query }}>. Keep every other param in the URL in its order, repeated ones like type=green&type=black included, then put page last, leaving it out for page 1, so /teas?page=1 is never linked.
/teas?type=green&type=black&page=6&sort=price, with pageCount 12
→ Previous 1 … 5 6 7 … 12 Next
7 → /teas?type=green&type=black&sort=price&page=7
1 → /teas?type=green&type=black&sort=price
const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
renderClient(<Pagination pageCount={5} />, { url: "/teas?page=3" });
expect(items()).toEqual(["Previous", "1", "2", "3", "4", "5", "Next"]);
expect(screen.getByRole("link", { name: "3" })).toHaveAttribute("aria-current", "page");
expect(screen.getAllByRole("link").filter((link) => link.hasAttribute("aria-current")).length).toBe(1);
const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
renderClient(<Pagination pageCount={12} />, { url: "/teas?page=6" });
expect(items()).toEqual(["Previous", "1", "…", "5", "6", "7", "…", "12", "Next"]);
cleanup();
renderClient(<Pagination pageCount={12} />, { url: "/teas" });
expect(items()).toEqual(["1", "2", "…", "12", "Next"]);
cleanup();
renderClient(<Pagination pageCount={12} />, { url: "/teas?page=12" });
expect(items()).toEqual(["Previous", "1", "…", "11", "12"]);
const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
renderClient(<Pagination pageCount={12} />, { url: "/teas?page=4" });
expect(items()).toEqual(["Previous", "1", "2", "3", "4", "5", "…", "12", "Next"]);
cleanup();
renderClient(<Pagination pageCount={12} />, { url: "/teas?page=9" });
expect(items()).toEqual(["Previous", "1", "…", "8", "9", "10", "11", "12", "Next"]);
const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
renderClient(<Pagination pageCount={12} />, { url: "/teas?type=green&type=black&page=6&sort=price" });
expect(href("7")).toBe("/teas?type=green&type=black&sort=price&page=7");
expect(href("Next")).toBe("/teas?type=green&type=black&sort=price&page=7");
expect(href("Previous")).toBe("/teas?type=green&type=black&sort=price&page=5");
expect(href("12")).toBe("/teas?type=green&type=black&sort=price&page=12");
expect(href("1")).toBe("/teas?type=green&type=black&sort=price");
cleanup();
renderClient(<Pagination pageCount={3} />, { url: "/teas?page=2" });
expect(href("1")).toBe("/teas");
const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
for (const page of ["abc", "0", "-2", "2.5", ""]) {
renderClient(<Pagination pageCount={3} />, { url: "/teas?page=" + page });
expect(items()).toEqual(["1", "2", "3", "Next"]);
expect(screen.getByRole("link", { name: "1" })).toHaveAttribute("aria-current", "page");
cleanup();
}
renderClient(<Pagination pageCount={3} />, { url: "/teas?page=40" });
expect(items()).toEqual(["Previous", "1", "2", "3"]);
expect(screen.getByRole("link", { name: "3" })).toHaveAttribute("aria-current", "page");
cleanup();
for (const pageCount of [1, 0]) {
renderClient(<Pagination pageCount={pageCount} />, { url: "/teas?page=2" });
expect(screen.queryByRole("navigation")).toBe(null);
cleanup();
}
const items = () => within(screen.getByRole("navigation", { name: "Pagination" })).getAllByRole("listitem").map((item) => item.textContent);
const href = (name) => screen.getByRole("link", { name }).getAttribute("href");
renderClient(<Pagination pageCount={12} />, { url: "/teas?sort=name" });
await user.click(screen.getByRole("link", { name: "2" }));
expect(router.history).toEqual(["/teas?sort=name", "/teas?sort=name&page=2"]);
expect(items()).toEqual(["Previous", "1", "2", "3", "…", "12", "Next"]);
expect(screen.getByRole("link", { name: "2" })).toHaveAttribute("aria-current", "page");
await user.click(screen.getByRole("link", { name: "Previous" }));
expect(router.url).toBe("/teas?sort=name");
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Page links with gaps that keep the filters. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.