Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

add simpler page selector with text input (#8532)

Shahmir Varqhae89338754c5b
diff --git a/frontend/src/components/data-table/__tests__/pagination.test.tsx b/frontend/src/components/data-table/__tests__/pagination.test.tsx
index 26d2a0b8ed..bea0597c9e 100644
--- a/frontend/src/components/data-table/__tests__/pagination.test.tsx
+++ b/frontend/src/components/data-table/__tests__/pagination.test.tsx
@@ -1,26 +1,18 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { render } from "@testing-library/react";
import { expect, test } from "vitest";
-import { Functions } from "@/utils/functions";
-import { PageSelector } from "../pagination";
+import { getPageRanges } from "../pagination";
+import type { PageRange } from "../types";
-function getOptions(currentPage: number) {
- const { container } = render(
- <PageSelector
- currentPage={currentPage}
- totalPages={200}
- onPageChange={Functions.NOOP}
- />,
+function getLabels(currentPage: number): string[] {
+ const ranges = getPageRanges(currentPage, 200);
+ return ranges.map((item: PageRange) =>
+ item.type === "ellipsis" ? "..." : String(item.page),
);
-
- const options = container.querySelectorAll("option");
- const optionValues = [...options].map((option) => option.textContent);
- return optionValues;
}
test("pagination start / middle / end", () => {
- expect(getOptions(1)).toMatchInlineSnapshot(`
+ expect(getLabels(1)).toMatchInlineSnapshot(`
[
"1",
"2",
@@ -58,19 +50,19 @@ test("pagination start / middle / end", () => {
`);
// all fall in the top/middle/bottom 10
- expect(getOptions(1)).toEqual(getOptions(10));
- expect(getOptions(96)).toEqual(getOptions(105));
- expect(getOptions(191)).toEqual(getOptions(200));
+ expect(getLabels(1)).toEqual(getLabels(10));
+ expect(getLabels(96)).toEqual(getLabels(105));
+ expect(getLabels(191)).toEqual(getLabels(200));
// Check off by one
- expect(getOptions(1)).not.toEqual(getOptions(11));
- expect(getOptions(1)).not.toEqual(getOptions(95));
- expect(getOptions(1)).not.toEqual(getOptions(106));
- expect(getOptions(1)).not.toEqual(getOptions(190));
+ expect(getLabels(1)).not.toEqual(getLabels(11));
+ expect(getLabels(1)).not.toEqual(getLabels(95));
+ expect(getLabels(1)).not.toEqual(getLabels(106));
+ expect(getLabels(1)).not.toEqual(getLabels(190));
});
test("pagination lower middle", () => {
- expect(getOptions(50)).toMatchInlineSnapshot(`
+ expect(getLabels(50)).toMatchInlineSnapshot(`
[
"1",
"2",
@@ -111,7 +103,7 @@ test("pagination lower middle", () => {
});
test("pagination upper middle", () => {
- expect(getOptions(150)).toMatchInlineSnapshot(`
+ expect(getLabels(150)).toMatchInlineSnapshot(`
[
"1",
"2",
diff --git a/frontend/src/components/data-table/pagination.tsx b/frontend/src/components/data-table/pagination.tsx
index 99ccf55c32..5a7a994b8e 100644
--- a/frontend/src/components/data-table/pagination.tsx
+++ b/frontend/src/components/data-table/pagination.tsx
@@ -4,13 +4,25 @@
import type { Table } from "@tanstack/react-table";
import { range } from "lodash-es";
import {
+ ChevronDown,
ChevronLeft,
ChevronRight,
ChevronsLeft,
ChevronsRight,
} from "lucide-react";
+import React from "react";
import { useLocale } from "react-aria";
import { Button } from "@/components/ui/button";
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuLabel,
+ DropdownMenuSeparator,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu";
+import { Input } from "@/components/ui/input";
+import { cn } from "@/utils/cn";
import { Events } from "@/utils/events";
import { prettyNumber } from "@/utils/numbers";
import { PluralWord } from "@/utils/pluralize";
@@ -23,7 +35,7 @@ import {
SelectTrigger,
SelectValue,
} from "../ui/select";
-import type { DataTableSelection } from "./types";
+import type { DataTableSelection, PageRange } from "./types";
interface DataTablePaginationProps<TData> {
table: Table<TData>;
@@ -251,73 +263,143 @@ export const PageSelector = ({
totalPages: number;
onPageChange: (page: number) => void;
}) => {
- const renderOption = (i: number) => (
- <option key={i} value={i + 1}>
- {i + 1}
- </option>
- );
+ const [open, setOpen] = React.useState(false);
+ const [jumpValue, setJumpValue] = React.useState("");
+ const jumpInputId = React.useId();
- const renderEllipsis = (key: number) => (
- <option key={`__${key}__`} disabled={true} value={`__${key}__`}>
- ...
- </option>
+ const pageRanges = React.useMemo(
+ () => getPageRanges(currentPage, totalPages),
+ [currentPage, totalPages],
);
- const renderPageOptions = () => {
- /* If this is too large, this can cause the browser to hang. */
- if (totalPages <= 100) {
- return range(totalPages).map((i) => renderOption(i));
+ const handleJump = () => {
+ const page = Number.parseInt(jumpValue, 10);
+ if (page >= 1 && page <= totalPages) {
+ onPageChange(page - 1);
+ setJumpValue("");
+ setOpen(false);
}
+ };
- const middle = Math.floor(totalPages / 2);
+ return (
+ <DropdownMenu open={open} onOpenChange={setOpen}>
+ <DropdownMenuTrigger asChild={true}>
+ <button
+ type="button"
+ className="border rounded justify-between pl-1.5 pr-0.5 min-w-9 text-xs items-center hover:bg-accent inline-flex gap-0.5"
+ data-testid="page-select"
+ >
+ {currentPage}
+ <ChevronDown className="h-3 w-3 opacity-50 mb-px" />
+ </button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent
+ className="w-36 overflow-hidden flex flex-col"
+ scrollable={false}
+ align="center"
+ sideOffset={6}
+ style={{ maxHeight: "22rem" }}
+ >
+ <div className="overflow-y-auto flex-1 min-h-0">
+ {pageRanges.map((item) =>
+ item.type === "ellipsis" ? (
+ <DropdownMenuLabel
+ key={item.key}
+ className="text-center text-xs text-muted-foreground"
+ >
+ ...
+ </DropdownMenuLabel>
+ ) : (
+ <DropdownMenuItem
+ key={item.page}
+ data-testid="page-option"
+ className={cn(
+ "text-xs cursor-pointer",
+ item.page === currentPage && "font-semibold bg-accent",
+ )}
+ onSelect={() => onPageChange(item.page - 1)}
+ onMouseDown={Events.preventFocus}
+ >
+ {item.page}
+ </DropdownMenuItem>
+ ),
+ )}
+ </div>
+ <DropdownMenuSeparator />
+ <div
+ className="px-2 pt-0.5 shrink-0"
+ onKeyDown={(e) => e.stopPropagation()}
+ >
+ <label
+ htmlFor={jumpInputId}
+ className="text-xs text-muted-foreground block mb-1"
+ >
+ Jump to page
+ </label>
+ <Input
+ id={jumpInputId}
+ type="number"
+ min={1}
+ max={totalPages}
+ placeholder={`1-${totalPages}`}
+ value={jumpValue}
+ onChange={(e) => setJumpValue(e.target.value)}
+ onKeyDown={(e) => {
+ if (e.key === "Enter") {
+ handleJump();
+ }
+ e.stopPropagation();
+ }}
+ className="h-6 text-xs"
+ data-testid="page-jump-input"
+ />
+ </div>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ );
+};
- // Show the first 10 pages, the middle 10 pages, and the last 10 pages.
- const firstPages = range(10).map((i) => renderOption(i));
- const middlePages = range(10).map((i) => renderOption(middle - 5 + i));
- const lastPages = range(10).map((i) => renderOption(totalPages - 10 + i));
+export function getPageRanges(
+ currentPage: number,
+ totalPages: number,
+): PageRange[] {
+ if (totalPages <= 100) {
+ return range(totalPages).map((i) => ({ type: "page", page: i + 1 }));
+ }
- const result = [
- ...firstPages,
- renderEllipsis(1),
- ...middlePages,
- renderEllipsis(2),
- ...lastPages,
- ];
+ const middle = Math.floor(totalPages / 2);
- if (currentPage > 10 && currentPage <= middle - 5) {
- result.splice(
- 10,
- 1, // delete the first ellipsis
- renderEllipsis(1),
- renderOption(currentPage - 1),
- renderEllipsis(11),
- );
+ const items: PageRange[] = [];
+ const addPages = (start: number, count: number) => {
+ for (let i = 0; i < count; i++) {
+ items.push({ type: "page", page: start + i });
}
+ };
- if (currentPage > middle + 5 && currentPage <= totalPages - 10) {
- result.splice(
- -11,
- 1, // delete the first ellipsis
- renderEllipsis(2),
- renderOption(currentPage - 1),
- renderEllipsis(22),
- );
- }
+ addPages(1, 10);
+ items.push({ type: "ellipsis", key: "e1" });
- return result;
- };
+ if (currentPage > 10 && currentPage <= middle - 5) {
+ items.push(
+ { type: "page", page: currentPage },
+ { type: "ellipsis", key: "e1b" },
+ );
+ }
- return (
- <select
- className="cursor-pointer border rounded"
- value={currentPage}
- data-testid="page-select"
- onChange={(e) => onPageChange(Number(e.target.value) - 1)}
- >
- {renderPageOptions()}
- </select>
- );
-};
+ addPages(middle - 4, 10);
+ items.push({ type: "ellipsis", key: "e2" });
+
+ if (currentPage > middle + 5 && currentPage <= totalPages - 10) {
+ items.push(
+ { type: "page", page: currentPage },
+ { type: "ellipsis", key: "e2b" },
+ );
+ }
+
+ addPages(totalPages - 9, 10);
+
+ return items;
+}
export function prettifyRowCount(rowCount: number, locale: string): string {
return `${prettyNumber(rowCount, locale)} ${new PluralWord("row").pluralize(rowCount)}`;
diff --git a/frontend/src/components/data-table/types.ts b/frontend/src/components/data-table/types.ts
index e4dc90e792..a3354fd5f4 100644
--- a/frontend/src/components/data-table/types.ts
+++ b/frontend/src/components/data-table/types.ts
@@ -80,3 +80,7 @@ export function extractTimezone(dtype: string | undefined): string | undefined {
const match = /^datetime(?:64)?\[[^,]+,([^,]+)]$/.exec(dtype);
return match?.[1]?.trim();
}
+
+export type PageRange =
+ | { type: "page"; page: number }
+ | { type: "ellipsis"; key: string };