Also: copy public/ into the runtime image, exclude /images from the login redirect, and fix 768px horizontal overflow. Co-Authored-By: Claude Opus 5.5 <[email protected]>
92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import type { ReactNode } from "react";
|
|
import { EmptyState } from "./empty-state";
|
|
|
|
export interface Column<T> {
|
|
key: string;
|
|
header: string;
|
|
render: (row: T) => ReactNode;
|
|
/** 手機卡片模式也要顯示的關鍵欄位(都未指定時取前三欄)。 */
|
|
essential?: boolean;
|
|
align?: "left" | "right";
|
|
}
|
|
|
|
/**
|
|
* 資料表格:平板與桌機為表格;手機寬度(< 768px)改為只保留關鍵欄位的卡片清單,避免水平捲動。
|
|
* `mode` 可強制指定(例如窄欄位的輔助區)。沒有資料時顯示空狀態。
|
|
*/
|
|
export function DataTable<T>({
|
|
caption,
|
|
columns,
|
|
rows,
|
|
rowKey,
|
|
empty,
|
|
mode = "responsive",
|
|
}: {
|
|
caption: string;
|
|
columns: Column<T>[];
|
|
rows: T[];
|
|
rowKey: (row: T) => string | number;
|
|
empty: { title: string; description?: ReactNode; action?: ReactNode };
|
|
mode?: "responsive" | "table" | "cards";
|
|
}) {
|
|
if (rows.length === 0) return <EmptyState {...empty} />;
|
|
const essential = columns.filter((c) => c.essential);
|
|
const cardColumns = essential.length > 0 ? essential : columns.slice(0, 3);
|
|
const tableClass = mode === "cards" ? "hidden" : mode === "table" ? "block" : "hidden md:block";
|
|
const cardsClass = mode === "table" ? "hidden" : mode === "cards" ? "" : "md:hidden";
|
|
|
|
return (
|
|
<>
|
|
<div className={`relative overflow-x-auto ${tableClass}`} data-testid="table-mode">
|
|
<table className="w-full border-collapse text-left">
|
|
<caption className="sr-only">{caption}</caption>
|
|
<thead>
|
|
<tr className="text-sm text-muted">
|
|
{columns.map((c) => (
|
|
<th
|
|
key={c.key}
|
|
scope="col"
|
|
className={`px-4 py-3 font-medium ${c.align === "right" ? "text-right" : ""}`}
|
|
>
|
|
{c.header}
|
|
</th>
|
|
))}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{rows.map((row) => (
|
|
<tr key={rowKey(row)} className="border-b border-border last:border-0">
|
|
{columns.map((c) => (
|
|
<td
|
|
key={c.key}
|
|
className={`px-4 py-4 align-middle ${c.align === "right" ? "text-right" : ""}`}
|
|
>
|
|
{c.render(row)}
|
|
</td>
|
|
))}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<ul className={`space-y-3 ${cardsClass}`} aria-label={caption} data-testid="card-mode">
|
|
{rows.map((row) => (
|
|
<li
|
|
key={rowKey(row)}
|
|
className="rounded-card border border-border bg-surface p-4 shadow-card"
|
|
>
|
|
<dl className="space-y-1.5">
|
|
{cardColumns.map((c) => (
|
|
<div key={c.key} className="flex justify-between gap-4">
|
|
<dt className="shrink-0 text-sm text-muted">{c.header}</dt>
|
|
<dd className="min-w-0 text-right">{c.render(row)}</dd>
|
|
</div>
|
|
))}
|
|
</dl>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</>
|
|
);
|
|
}
|