From 1b8110dbbf155ad95d81e2d4dadb9000a4d3ce1a Mon Sep 17 00:00:00 2001 From: ngqile Date: Wed, 2 Dec 2026 09:24:30 +0800 Subject: [PATCH] feat: data table listings become cards on phones Below md each row is a card: the first column as title, other columns as labelled values, actions at the bottom. A sort select replaces the hidden header; paging is unchanged. One DOM, CSS only, like DashTable. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM --- .../changes/polish-e2e-followups/tasks.md | 2 +- .../components/ui/data-table.cards.test.tsx | 79 +++++++++++++++++++ .../app/components/ui/data-table.tsx | 44 +++++++++-- .../app/lib/i18n/defaultMessages.ts | 1 + 4 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 solvway-admin-portal/app/components/ui/data-table.cards.test.tsx diff --git a/openspec/changes/polish-e2e-followups/tasks.md b/openspec/changes/polish-e2e-followups/tasks.md index 01c597a..b0dd82a 100644 --- a/openspec/changes/polish-e2e-followups/tasks.md +++ b/openspec/changes/polish-e2e-followups/tasks.md @@ -35,7 +35,7 @@ Verify commands: - Depends on: 3 - Scope: `solvway-admin-portal/**` except `app/components/ui/data-table*` - Verify: portal verify -- [ ] 5. Phone cards in `DataTable` below `md`: +- [x] 5. Phone cards in `DataTable` below `md`: - title from the first column, labelled values, actions at the bottom; - a sort select when columns sort, and shared paging; - opt-out for pages with their own cards. diff --git a/solvway-admin-portal/app/components/ui/data-table.cards.test.tsx b/solvway-admin-portal/app/components/ui/data-table.cards.test.tsx new file mode 100644 index 0000000..ac638b6 --- /dev/null +++ b/solvway-admin-portal/app/components/ui/data-table.cards.test.tsx @@ -0,0 +1,79 @@ +import { describe, it, expect, vi } from "vitest"; +import { render, screen, fireEvent } from "@testing-library/react"; +import type { ColumnDef } from "@tanstack/react-table"; +import { DataTable } from "./data-table"; + +type Row = { id: string; name: string; company: string; tag: string }; +const columns: ColumnDef[] = [ + { accessorKey: "name", header: "Name" }, + { accessorKey: "company", header: "Company" }, + { accessorKey: "tag", header: () => Tag, enableSorting: false }, + { id: "actions", header: "Actions", enableSorting: false, cell: () => }, +]; +const data: Row[] = [{ id: "1", name: "Alpha", company: "INS", tag: "x" }]; + +function renderTable(extra: Partial>> = {}) { + return render( + label="t" columns={columns} data={data} total={1} page={1} pageSize={20} + onPageChange={() => {}} rowKey={r => r.id} {...extra} />, + ); +} +const cells = () => Array.from(screen.getByText("Alpha").closest("tr")!.querySelectorAll("td")); + +describe("DataTable phone cards", () => { + it("labels non-first cells with the header, leaves the title cell unlabelled", () => { + renderTable(); + const [title, company] = cells(); + expect(title).not.toHaveAttribute("data-label"); + expect(company).toHaveAttribute("data-label", "Company"); + }); + + it("gives a non-string header no label", () => { + renderTable(); + expect(cells()[2]).not.toHaveAttribute("data-label"); + }); + + it("puts the actions cell last, unlabelled", () => { + renderTable(); + const actions = cells()[3]; + expect(actions).not.toHaveAttribute("data-label"); + expect(actions.className).toContain("max-md:order-last"); + }); + + // jsdom applies no CSS, so these guard the classes that keep a card from overflowing at 360 px. + it("lets the card grow past the table row height", () => { + renderTable(); + expect(screen.getByText("Alpha").closest("tr")!.className).toContain("max-md:h-auto"); + }); + + it("wraps long title and values instead of scrolling sideways", () => { + renderTable(); + const [title, company] = cells(); + expect(title.className).toContain("max-md:whitespace-normal"); + expect(title.className).toContain("max-md:[overflow-wrap:anywhere]"); + expect(company.className).toContain("max-md:[overflow-wrap:anywhere]"); + }); +}); + +describe("DataTable phone sort select", () => { + it("is absent without onSortingChange", () => { + renderTable(); + expect(screen.queryByRole("combobox", { name: "Sort by" })).toBeNull(); + }); + + it("lists sortable columns only, both directions", () => { + renderTable({ onSortingChange: () => {} }); + const select = screen.getByRole("combobox", { name: "Sort by" }); + const texts = Array.from(select.querySelectorAll("option")).map(o => o.textContent); + expect(texts).toEqual(["", "Name ↑", "Name ↓", "Company ↑", "Company ↓"]); + }); + + it("reflects the current sorting and reports changes", () => { + const onSortingChange = vi.fn(); + renderTable({ onSortingChange, sorting: [{ id: "company", desc: true }] }); + const select = screen.getByRole("combobox", { name: "Sort by" }) as HTMLSelectElement; + expect(select.value).toBe("company:desc"); + fireEvent.change(select, { target: { value: "name:asc" } }); + expect(onSortingChange).toHaveBeenCalledWith([{ id: "name", desc: false }]); + }); +}); diff --git a/solvway-admin-portal/app/components/ui/data-table.tsx b/solvway-admin-portal/app/components/ui/data-table.tsx index 7463b2a..637f9dc 100644 --- a/solvway-admin-portal/app/components/ui/data-table.tsx +++ b/solvway-admin-portal/app/components/ui/data-table.tsx @@ -50,6 +50,13 @@ const ROW_CONTROLS = 'a, button, input, select, textarea, label, [role="checkbox const PINNED_CELL = 'sticky left-0 z-[1] bg-card shadow-[inset_-1px_0_0_var(--border)] group-hover:bg-muted group-focus-visible:bg-muted group-aria-selected:bg-accent' +// Below md the table becomes one card per row (one DOM, CSS only; same technique as DashTable). +const CARD_CELL = + 'max-md:flex max-md:justify-between max-md:gap-4 max-md:whitespace-normal max-md:[overflow-wrap:anywhere] max-md:px-1 max-md:py-1 max-md:before:shrink-0 max-md:before:font-medium max-md:before:text-muted-foreground max-md:before:content-[attr(data-label)]' +// The title cell is the pinned one; below md it is a plain block, not pinned. +const CARD_TITLE = 'max-md:static max-md:bg-transparent max-md:shadow-none max-md:block max-md:whitespace-normal max-md:[overflow-wrap:anywhere] max-md:px-1 max-md:py-1 max-md:text-base max-md:font-semibold' +const CARD_ACTIONS = 'max-md:order-last max-md:flex max-md:flex-wrap max-md:justify-end max-md:gap-2 max-md:px-1 max-md:pt-2' + /** * Data table for server-paginated, server-sorted result sets — which is what * every `src/services/*` list function returns. Pagination and sorting state are @@ -128,6 +135,12 @@ export function DataTable({ }) const visibleColumnCount = table.getVisibleLeafColumns().length + // Phone-card labels come from string headers only; other headers get no label. + const labelOf = (column: { columnDef: ColumnDef }) => + typeof column.columnDef.header === 'string' ? column.columnDef.header : undefined + const sortableColumns = onSortingChange + ? table.getVisibleLeafColumns().filter(c => c.getCanSort()) + : [] const hideableColumns = table.getAllLeafColumns().filter(c => c.getCanHide()) return ( @@ -159,10 +172,28 @@ export function DataTable({ )} + {sortableColumns.length > 0 && ( + + )} +
- - +
+ {table.getHeaderGroups().map(headerGroup => ( {headerGroup.headers.map((header, index) => { @@ -192,7 +223,7 @@ export function DataTable({ ))} - + {isLoading ? ( Array.from({ length: Math.min(pageSize, 8) }).map((_, rowIndex) => ( @@ -214,7 +245,7 @@ export function DataTable({ ({ {row.getVisibleCells().map((cell, index) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} diff --git a/solvway-admin-portal/app/lib/i18n/defaultMessages.ts b/solvway-admin-portal/app/lib/i18n/defaultMessages.ts index d45f7ff..8682827 100644 --- a/solvway-admin-portal/app/lib/i18n/defaultMessages.ts +++ b/solvway-admin-portal/app/lib/i18n/defaultMessages.ts @@ -95,6 +95,7 @@ export const DEFAULT_MESSAGES: Record = { UiColumns: "Columns", UiVisibleColumns: "Visible columns", UiNoResults: "No results.", + UiSortBy: "Sort by", UiRetry: "Retry", UiLoading: "Loading", NotAuthorizedTitle: "Not authorised",