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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
82e6ddc3e6
commit
1b8110dbbf
4 files changed
+119
-7
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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<Row, unknown>[] = [
|
||||
{ accessorKey: "name", header: "Name" },
|
||||
{ accessorKey: "company", header: "Company" },
|
||||
{ accessorKey: "tag", header: () => <span>Tag</span>, enableSorting: false },
|
||||
{ id: "actions", header: "Actions", enableSorting: false, cell: () => <button>Act</button> },
|
||||
];
|
||||
const data: Row[] = [{ id: "1", name: "Alpha", company: "INS", tag: "x" }];
|
||||
|
||||
function renderTable(extra: Partial<React.ComponentProps<typeof DataTable<Row>>> = {}) {
|
||||
return render(
|
||||
<DataTable<Row> 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 }]);
|
||||
});
|
||||
});
|
||||
@@ -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<TData>({
|
||||
})
|
||||
|
||||
const visibleColumnCount = table.getVisibleLeafColumns().length
|
||||
// Phone-card labels come from string headers only; other headers get no label.
|
||||
const labelOf = (column: { columnDef: ColumnDef<TData, unknown> }) =>
|
||||
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<TData>({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{sortableColumns.length > 0 && (
|
||||
<select
|
||||
aria-label={t("UiSortBy")}
|
||||
className="h-9 w-full rounded-md border bg-card px-2 text-sm md:hidden"
|
||||
value={sorting[0] ? `${sorting[0].id}:${sorting[0].desc ? 'desc' : 'asc'}` : ''}
|
||||
onChange={e => {
|
||||
const [id, dir] = e.target.value.split(':')
|
||||
onSortingChange!(id ? [{ id, desc: dir === 'desc' }] : [])
|
||||
}}
|
||||
>
|
||||
<option value="" />
|
||||
{sortableColumns.flatMap(c => [
|
||||
<option key={`${c.id}:asc`} value={`${c.id}:asc`}>{labelOf(c) ?? c.id} ↑</option>,
|
||||
<option key={`${c.id}:desc`} value={`${c.id}:desc`}>{labelOf(c) ?? c.id} ↓</option>,
|
||||
])}
|
||||
</select>
|
||||
)}
|
||||
|
||||
<div className="overflow-hidden rounded-table border bg-card">
|
||||
<div className="overflow-x-auto">
|
||||
<Table aria-label={label}>
|
||||
<TableHeader className="sticky top-0 z-10">
|
||||
<Table aria-label={label} className="max-md:block">
|
||||
<TableHeader className="sticky top-0 z-10 max-md:hidden">
|
||||
{table.getHeaderGroups().map(headerGroup => (
|
||||
<TableRow key={headerGroup.id}>
|
||||
{headerGroup.headers.map((header, index) => {
|
||||
@@ -192,7 +223,7 @@ export function DataTable<TData>({
|
||||
</TableRow>
|
||||
))}
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
<TableBody className="max-md:block max-md:[&_tr:last-child]:border">
|
||||
{isLoading ? (
|
||||
Array.from({ length: Math.min(pageSize, 8) }).map((_, rowIndex) => (
|
||||
<TableRow key={`skeleton-${rowIndex}`} data-testid="table-skeleton-row">
|
||||
@@ -214,7 +245,7 @@ export function DataTable<TData>({
|
||||
<TableRow
|
||||
key={seen ? `${key}~${seen}` : key}
|
||||
className={cn(
|
||||
'group h-9 transition-colors hover:bg-muted',
|
||||
'group h-9 transition-colors max-md:h-auto max-md:m-2 max-md:flex max-md:flex-col max-md:rounded-lg max-md:border max-md:p-2 hover:bg-muted',
|
||||
interactive && 'cursor-pointer focus-visible:bg-muted focus-visible:outline-none',
|
||||
interactive && key === selectedKey && 'bg-accent text-accent-foreground hover:bg-accent',
|
||||
)}
|
||||
@@ -244,10 +275,11 @@ export function DataTable<TData>({
|
||||
{row.getVisibleCells().map((cell, index) => (
|
||||
<TableCell
|
||||
key={cell.id}
|
||||
data-label={index === 0 || cell.column.id === 'actions' ? undefined : labelOf(cell.column)}
|
||||
className={cn(
|
||||
'py-1.5',
|
||||
cell.column.columnDef.meta?.numeric && 'text-right tabular',
|
||||
index === 0 && PINNED_CELL,
|
||||
cell.column.columnDef.meta?.numeric && 'text-right tabular max-md:text-left',
|
||||
index === 0 ? cn(PINNED_CELL, CARD_TITLE) : cell.column.id === 'actions' ? CARD_ACTIONS : CARD_CELL,
|
||||
)}
|
||||
>
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
|
||||
@@ -95,6 +95,7 @@ export const DEFAULT_MESSAGES: Record<string, string> = {
|
||||
UiColumns: "Columns",
|
||||
UiVisibleColumns: "Visible columns",
|
||||
UiNoResults: "No results.",
|
||||
UiSortBy: "Sort by",
|
||||
UiRetry: "Retry",
|
||||
UiLoading: "Loading",
|
||||
NotAuthorizedTitle: "Not authorised",
|
||||
|
||||
Reference in new issue
Block a user