-
Notifications
You must be signed in to change notification settings - Fork 90
[kumo] add pagination Enter key navigation and dropdown page selector #342
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
Merged
mattrothenberg
merged 11 commits into
cloudflare:main
from
ben-pepito:feat/pagination-enter-key-and-dropdown-selector
Apr 6, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b18d71b
feat(pagination): add Enter key navigation and dropdown page selector
ben-pepito 9d9ccc9
chore: add changeset for pagination enhancements
ben-pepito 2971d5a
docs(pagination): add dropdown page selector demo and docs section
ben-pepito e38f1c0
fix(pagination): remove rounded corners on dropdown page selector
ben-pepito 69fe71a
fix(pagination): match dropdown border color to InputGroup buttons
ben-pepito 3b71f46
merge: resolve conflict with upstream main
ben-pepito 1e86555
refactor(pagination): extract clamp utility for page input handlers
ben-pepito 9b13cb7
Merge branch 'main' into feat/pagination-enter-key-and-dropdown-selector
mattrothenberg 96f68c3
address review: add dropdown perf JSDoc + test for dropdown setPage
ben-pepito 071d9cc
chore: retrigger CI checks
ben-pepito fb06a5a
Merge branch 'main' into feat/pagination-enter-key-and-dropdown-selector
mattrothenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| "@cloudflare/kumo": minor | ||
| --- | ||
|
|
||
| Add Enter key navigation to Pagination page number input and new `pageSelector` prop for dropdown mode | ||
|
|
||
| - The page number input in `Pagination.Controls` now navigates on Enter key press (previously only on blur) | ||
| - New `pageSelector` prop on `Pagination.Controls`: set to `"dropdown"` to render a Select dropdown instead of a text input for page selection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
257 changes: 257 additions & 0 deletions
257
packages/kumo/src/components/pagination/pagination.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import { render, screen, fireEvent } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { Pagination } from "./pagination"; | ||
|
|
||
| function renderPagination({ | ||
| page = 1, | ||
| perPage = 10, | ||
| totalCount = 100, | ||
| setPage = vi.fn(), | ||
| controls = "full" as const, | ||
| pageSelector, | ||
| }: { | ||
| page?: number; | ||
| perPage?: number; | ||
| totalCount?: number; | ||
| setPage?: (page: number) => void; | ||
| controls?: "full" | "simple"; | ||
| pageSelector?: "input" | "dropdown"; | ||
| } = {}) { | ||
| return render( | ||
| <Pagination | ||
| page={page} | ||
| setPage={setPage} | ||
| perPage={perPage} | ||
| totalCount={totalCount} | ||
| > | ||
| <Pagination.Info /> | ||
| <Pagination.Controls controls={controls} pageSelector={pageSelector} /> | ||
| </Pagination>, | ||
| ); | ||
| } | ||
|
|
||
| describe("Pagination", () => { | ||
| describe("Controls", () => { | ||
| it("renders navigation buttons", () => { | ||
| renderPagination(); | ||
|
|
||
| expect(screen.getByLabelText("First page")).toBeTruthy(); | ||
| expect(screen.getByLabelText("Previous page")).toBeTruthy(); | ||
| expect(screen.getByLabelText("Next page")).toBeTruthy(); | ||
| expect(screen.getByLabelText("Last page")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("renders page number input by default", () => { | ||
| renderPagination(); | ||
|
|
||
| expect(screen.getByLabelText("Page number")).toBeTruthy(); | ||
| expect(screen.getByLabelText("Page number").tagName.toLowerCase()).toBe( | ||
| "input", | ||
| ); | ||
| }); | ||
|
|
||
| it("disables first/previous buttons on first page", () => { | ||
| renderPagination({ page: 1 }); | ||
|
|
||
| expect(screen.getByLabelText("First page").hasAttribute("disabled")).toBe( | ||
| true, | ||
| ); | ||
| expect( | ||
| screen.getByLabelText("Previous page").hasAttribute("disabled"), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("disables next/last buttons on last page", () => { | ||
| renderPagination({ page: 10, perPage: 10, totalCount: 100 }); | ||
|
|
||
| expect(screen.getByLabelText("Next page").hasAttribute("disabled")).toBe( | ||
| true, | ||
| ); | ||
| expect(screen.getByLabelText("Last page").hasAttribute("disabled")).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it("calls setPage when clicking next", () => { | ||
| const setPage = vi.fn(); | ||
| renderPagination({ page: 1, setPage }); | ||
|
|
||
| fireEvent.click(screen.getByLabelText("Next page")); | ||
| expect(setPage).toHaveBeenCalledWith(2); | ||
| }); | ||
|
|
||
| it("calls setPage when clicking previous", () => { | ||
| const setPage = vi.fn(); | ||
| renderPagination({ page: 3, setPage }); | ||
|
|
||
| fireEvent.click(screen.getByLabelText("Previous page")); | ||
| expect(setPage).toHaveBeenCalledWith(2); | ||
| }); | ||
|
|
||
| it("calls setPage when clicking first page", () => { | ||
| const setPage = vi.fn(); | ||
| renderPagination({ page: 5, setPage }); | ||
|
|
||
| fireEvent.click(screen.getByLabelText("First page")); | ||
| expect(setPage).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it("calls setPage when clicking last page", () => { | ||
| const setPage = vi.fn(); | ||
| renderPagination({ page: 5, perPage: 10, totalCount: 100, setPage }); | ||
|
|
||
| fireEvent.click(screen.getByLabelText("Last page")); | ||
| expect(setPage).toHaveBeenCalledWith(10); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Enter key navigation (input mode)", () => { | ||
| it("navigates to the typed page on Enter", async () => { | ||
| const user = userEvent.setup(); | ||
| const setPage = vi.fn(); | ||
| renderPagination({ page: 1, setPage }); | ||
|
|
||
| const input = screen.getByLabelText("Page number"); | ||
| await user.clear(input); | ||
| await user.type(input, "5"); | ||
| await user.keyboard("{Enter}"); | ||
|
|
||
| expect(setPage).toHaveBeenCalledWith(5); | ||
| }); | ||
|
|
||
| it("clamps value to maxPage on Enter when input exceeds max", async () => { | ||
| const user = userEvent.setup(); | ||
| const setPage = vi.fn(); | ||
| renderPagination({ page: 1, perPage: 10, totalCount: 100, setPage }); | ||
|
|
||
| const input = screen.getByLabelText("Page number"); | ||
| await user.clear(input); | ||
| await user.type(input, "999"); | ||
| await user.keyboard("{Enter}"); | ||
|
|
||
| // maxPage is 10, so it should clamp to 10 | ||
| expect(setPage).toHaveBeenCalledWith(10); | ||
| }); | ||
|
|
||
| it("clamps value to 1 on Enter when input is 0 or negative", async () => { | ||
| const user = userEvent.setup(); | ||
| const setPage = vi.fn(); | ||
| renderPagination({ page: 5, setPage }); | ||
|
|
||
| const input = screen.getByLabelText("Page number"); | ||
| await user.clear(input); | ||
| await user.type(input, "0"); | ||
| await user.keyboard("{Enter}"); | ||
|
|
||
| expect(setPage).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it("still navigates on blur (existing behavior preserved)", async () => { | ||
| const user = userEvent.setup(); | ||
| const setPage = vi.fn(); | ||
| renderPagination({ page: 1, setPage }); | ||
|
|
||
| const input = screen.getByLabelText("Page number"); | ||
| await user.clear(input); | ||
| await user.type(input, "3"); | ||
| await user.tab(); // triggers blur | ||
|
|
||
| expect(setPage).toHaveBeenCalledWith(3); | ||
| }); | ||
| }); | ||
|
|
||
| describe("simple controls mode", () => { | ||
| it("does not render page number input in simple mode", () => { | ||
| renderPagination({ controls: "simple" }); | ||
|
|
||
| expect(screen.queryByLabelText("Page number")).toBeNull(); | ||
| }); | ||
|
|
||
| it("does not render first/last page buttons in simple mode", () => { | ||
| renderPagination({ controls: "simple" }); | ||
|
|
||
| expect(screen.queryByLabelText("First page")).toBeNull(); | ||
| expect(screen.queryByLabelText("Last page")).toBeNull(); | ||
| }); | ||
|
|
||
| it("still renders previous/next buttons in simple mode", () => { | ||
| renderPagination({ controls: "simple" }); | ||
|
|
||
| expect(screen.getByLabelText("Previous page")).toBeTruthy(); | ||
| expect(screen.getByLabelText("Next page")).toBeTruthy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("dropdown mode", () => { | ||
| it("renders a select dropdown instead of an input when pageSelector='dropdown'", () => { | ||
| renderPagination({ pageSelector: "dropdown" }); | ||
|
|
||
| // Should have a combobox (Select renders as combobox role) | ||
| const combobox = screen.getByRole("combobox", { name: "Page number" }); | ||
| expect(combobox).toBeTruthy(); | ||
|
|
||
| // Should NOT have a text input for page number | ||
| expect(screen.queryByRole("textbox", { name: "Page number" })).toBeNull(); | ||
| }); | ||
|
|
||
| it("calls setPage when selecting a page from dropdown", async () => { | ||
| const user = userEvent.setup(); | ||
| const setPage = vi.fn(); | ||
| renderPagination({ | ||
| page: 1, | ||
| perPage: 10, | ||
| totalCount: 100, | ||
| pageSelector: "dropdown", | ||
| setPage, | ||
| }); | ||
|
|
||
| const combobox = screen.getByRole("combobox", { name: "Page number" }); | ||
| await user.click(combobox); | ||
|
|
||
| // Base UI Select renders options in a portal; query from document | ||
| const option = await screen.findByRole("option", { name: "5" }); | ||
| await user.click(option); | ||
|
|
||
| expect(setPage).toHaveBeenCalledWith(5); | ||
| }); | ||
|
|
||
| it("does not render dropdown in simple controls mode even if pageSelector is dropdown", () => { | ||
| renderPagination({ controls: "simple", pageSelector: "dropdown" }); | ||
|
|
||
| expect( | ||
| screen.queryByRole("combobox", { name: "Page number" }), | ||
| ).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Info", () => { | ||
| it("shows page range info", () => { | ||
| renderPagination({ page: 2, perPage: 10, totalCount: 100 }); | ||
|
|
||
| expect(screen.getByText("11-20")).toBeTruthy(); | ||
| expect(screen.getByText("100")).toBeTruthy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("backward compatibility", () => { | ||
| it("defaults to input mode when pageSelector is not specified", () => { | ||
| renderPagination(); | ||
|
|
||
| const input = screen.getByLabelText("Page number"); | ||
| expect(input.tagName.toLowerCase()).toBe("input"); | ||
| }); | ||
|
|
||
| it("defaults to full controls when controls is not specified", () => { | ||
| render( | ||
| <Pagination page={1} setPage={vi.fn()} perPage={10} totalCount={100}> | ||
| <Pagination.Controls /> | ||
| </Pagination>, | ||
| ); | ||
|
|
||
| expect(screen.getByLabelText("First page")).toBeTruthy(); | ||
| expect(screen.getByLabelText("Page number")).toBeTruthy(); | ||
| expect(screen.getByLabelText("Last page")).toBeTruthy(); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.