|
| 1 | +// RecordCardList — a stacked card layout for tabular data on narrow |
| 2 | +// viewports (#421). It consumes the SAME `TableColumn<Row>[]` descriptors, |
| 3 | +// rows, selection, and navigation props as `<Table>`, so a page defines |
| 4 | +// its columns once and renders either layout from a single source. The |
| 5 | +// first column is the record's identity (the card title); the rest become |
| 6 | +// a label/value list. Generic and model-agnostic (CLAUDE.md §7). |
| 7 | + |
| 8 | +import type { ReactNode } from 'react'; |
| 9 | + |
| 10 | +import { Checkbox } from './Checkbox'; |
| 11 | +import { Skeleton } from './Skeleton'; |
| 12 | +import type { TableColumn } from './Table'; |
| 13 | + |
| 14 | +export interface RecordCardListProps<Row> { |
| 15 | + /** Same column descriptors as `<Table>`; the first is the card title. */ |
| 16 | + columns: TableColumn<Row>[]; |
| 17 | + rows: Row[]; |
| 18 | + rowKey: (row: Row) => string | number; |
| 19 | + /** Tap a card to open the record (in-app navigation). */ |
| 20 | + onRowClick?: (row: Row) => void; |
| 21 | + /** |
| 22 | + * When set, the card title becomes a real `<a href>` so the browser's |
| 23 | + * native open-in-new-tab works (Cmd/Ctrl/middle-click); a plain tap is |
| 24 | + * intercepted for in-app nav via `onRowClick`. Mirrors `<Table>` (#253). |
| 25 | + * Should be the full app path including the router basename. |
| 26 | + */ |
| 27 | + rowHref?: (row: Row) => string; |
| 28 | + /** Render a per-card selection checkbox wired to the props below. */ |
| 29 | + selectable?: boolean; |
| 30 | + selectedKeys?: Set<string | number>; |
| 31 | + onToggleRow?: (key: string | number) => void; |
| 32 | + /** Show shimmer placeholder cards instead of `rows` during a refetch. */ |
| 33 | + loading?: boolean; |
| 34 | + /** Placeholder card count while `loading` (defaults to a stable count). */ |
| 35 | + skeletonRows?: number; |
| 36 | + emptyLabel?: string; |
| 37 | +} |
| 38 | + |
| 39 | +export function RecordCardList<Row>({ |
| 40 | + columns, |
| 41 | + rows, |
| 42 | + rowKey, |
| 43 | + onRowClick, |
| 44 | + rowHref, |
| 45 | + selectable = false, |
| 46 | + selectedKeys, |
| 47 | + onToggleRow, |
| 48 | + loading = false, |
| 49 | + skeletonRows, |
| 50 | + emptyLabel = 'No results.', |
| 51 | +}: RecordCardListProps<Row>) { |
| 52 | + // Only fall back to the empty-state when genuinely idle-and-empty — not |
| 53 | + // mid-fetch, where skeleton cards are shown instead (matches `<Table>`). |
| 54 | + if (!loading && rows.length === 0) { |
| 55 | + return <div className="py-8 text-center text-sm text-gray-500">{emptyLabel}</div>; |
| 56 | + } |
| 57 | + |
| 58 | + const selected = selectedKeys ?? new Set<string | number>(); |
| 59 | + const skeletonCount = skeletonRows ?? Math.min(Math.max(rows.length || 6, 3), 12); |
| 60 | + // First column = identity → title; the rest become the label/value body. |
| 61 | + const [titleCol, ...detailCols] = columns; |
| 62 | + |
| 63 | + if (loading) { |
| 64 | + return ( |
| 65 | + <ul className="space-y-2" aria-busy="true"> |
| 66 | + {Array.from({ length: skeletonCount }).map((_, i) => ( |
| 67 | + <li key={`dar-card-skeleton-${i}`} className="rounded-lg border border-gray-300 bg-white p-4"> |
| 68 | + <Skeleton className="mb-3 h-5 w-1/3" /> |
| 69 | + <div className="space-y-2"> |
| 70 | + {Array.from({ length: Math.max(detailCols.length, 2) }).map((__, j) => ( |
| 71 | + <Skeleton key={j} className="h-4 w-2/3" /> |
| 72 | + ))} |
| 73 | + </div> |
| 74 | + </li> |
| 75 | + ))} |
| 76 | + </ul> |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <ul className="space-y-2"> |
| 82 | + {rows.map((row) => { |
| 83 | + const key = rowKey(row); |
| 84 | + const isSelected = selected.has(key); |
| 85 | + const title: ReactNode = titleCol ? titleCol.render(row) : null; |
| 86 | + return ( |
| 87 | + <li |
| 88 | + key={key} |
| 89 | + onClick={ |
| 90 | + onRowClick |
| 91 | + ? (e) => { |
| 92 | + // A modified click (Cmd/Ctrl/Shift/Alt) is the browser's |
| 93 | + // open-in-new-tab gesture — let the title anchor handle |
| 94 | + // it; don't also navigate in-app. |
| 95 | + if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return; |
| 96 | + onRowClick(row); |
| 97 | + } |
| 98 | + : undefined |
| 99 | + } |
| 100 | + className={`rounded-lg border bg-white p-4 ${ |
| 101 | + isSelected ? 'border-primary' : 'border-gray-300' |
| 102 | + } ${onRowClick ? 'cursor-pointer hover:bg-gray-50' : ''}`} |
| 103 | + > |
| 104 | + <div className="flex items-start justify-between gap-3"> |
| 105 | + <div className="min-w-0 flex-1 font-medium text-gray-900"> |
| 106 | + {rowHref && titleCol ? ( |
| 107 | + // Real anchor so native open-in-new-tab works; a plain |
| 108 | + // left-click is intercepted for in-app nav (#253). |
| 109 | + <a |
| 110 | + href={rowHref(row)} |
| 111 | + className="text-inherit no-underline hover:underline" |
| 112 | + onClick={(e) => { |
| 113 | + if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return; |
| 114 | + e.preventDefault(); |
| 115 | + e.stopPropagation(); |
| 116 | + onRowClick?.(row); |
| 117 | + }} |
| 118 | + > |
| 119 | + {title} |
| 120 | + </a> |
| 121 | + ) : ( |
| 122 | + title |
| 123 | + )} |
| 124 | + </div> |
| 125 | + {selectable && ( |
| 126 | + // stopPropagation so toggling selection doesn't also open |
| 127 | + // the record (the card's onClick navigates). |
| 128 | + <span onClick={(e) => e.stopPropagation()} className="shrink-0"> |
| 129 | + <Checkbox |
| 130 | + aria-label="Select row" |
| 131 | + checked={isSelected} |
| 132 | + onChange={() => onToggleRow?.(key)} |
| 133 | + /> |
| 134 | + </span> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + {detailCols.length > 0 && ( |
| 138 | + <dl className="mt-3 space-y-2 text-sm"> |
| 139 | + {detailCols.map((col) => ( |
| 140 | + <div key={col.key}> |
| 141 | + <dt className="text-xs uppercase tracking-wide text-gray-500">{col.header}</dt> |
| 142 | + <dd className="mt-0.5 text-gray-700">{col.render(row)}</dd> |
| 143 | + </div> |
| 144 | + ))} |
| 145 | + </dl> |
| 146 | + )} |
| 147 | + </li> |
| 148 | + ); |
| 149 | + })} |
| 150 | + </ul> |
| 151 | + ); |
| 152 | +} |
0 commit comments