227 lines
9.2 KiB
TypeScript
227 lines
9.2 KiB
TypeScript
'use client'
|
||
|
||
import type { ReactNode } from 'react'
|
||
import { ArrowDown, ArrowUp, ChevronLeft, ChevronRight, ChevronsUpDown, Inbox } from 'lucide-react'
|
||
import { Checkbox } from './checkbox'
|
||
import { cx } from './cx'
|
||
|
||
export type Column<T> = { key: string; header: string; sortKey?: string; cell: (row: T) => ReactNode }
|
||
export type Sort = { key: string; dir: 'asc' | 'desc' }
|
||
export type TableView = { key: string; label: string; count?: number }
|
||
|
||
export interface TableCardProps<T> {
|
||
columns: Column<T>[]
|
||
rows: T[]
|
||
rowKey: (row: T) => string
|
||
onRowOpen?: (row: T) => void
|
||
rowActions?: (row: T) => ReactNode
|
||
views?: { items: TableView[]; active: string; onChange: (key: string) => void }
|
||
toolbar?: ReactNode
|
||
sort?: Sort
|
||
onSortChange?: (sortKey: string) => void
|
||
selection?: { selected: Set<string>; onChange: (next: Set<string>) => void }
|
||
bulkActions?: ReactNode
|
||
footer?: { from: number; to: number; total: number; page: number; pageCount: number; onPage: (page: number) => void }
|
||
empty?: { title: string; action?: ReactNode }
|
||
mobileRow?: (row: T) => ReactNode
|
||
}
|
||
|
||
const TH = 'px-3 py-[9px] text-xs font-[550] text-muted text-left bg-surface-2 border-b border-rule-2 whitespace-nowrap'
|
||
const TD = 'px-3 py-3 text-[13px] border-b border-rule'
|
||
|
||
export function TableCard<T>({
|
||
columns, rows, rowKey, onRowOpen, rowActions, views, toolbar, sort, onSortChange,
|
||
selection, bulkActions, footer, empty, mobileRow,
|
||
}: TableCardProps<T>) {
|
||
const selectedOnPage = selection ? rows.filter(r => selection.selected.has(rowKey(r))).length : 0
|
||
const allSelected = rows.length > 0 && selectedOnPage === rows.length
|
||
|
||
const setAll = (next: boolean) => {
|
||
if (!selection) return
|
||
const s = new Set(selection.selected)
|
||
rows.forEach(r => (next ? s.add(rowKey(r)) : s.delete(rowKey(r))))
|
||
selection.onChange(s)
|
||
}
|
||
const setOne = (id: string, next: boolean) => {
|
||
if (!selection) return
|
||
const s = new Set(selection.selected)
|
||
if (next) s.add(id)
|
||
else s.delete(id)
|
||
selection.onChange(s)
|
||
}
|
||
|
||
const open = (row: T) => onRowOpen?.(row)
|
||
const isEmpty = rows.length === 0 && empty
|
||
|
||
return (
|
||
<div className="relative bg-surface rounded-xl shadow-card overflow-hidden">
|
||
{views && (
|
||
<div className="flex items-center gap-0.5 p-2 border-b border-rule">
|
||
{views.items.map(v => (
|
||
<button
|
||
key={v.key}
|
||
type="button"
|
||
onClick={() => views.onChange(v.key)}
|
||
className={cx(
|
||
'inline-flex items-center gap-1.5 h-7 px-2.5 rounded-md text-[13px] cursor-pointer',
|
||
v.key === views.active ? 'bg-rule-2 font-semibold text-ink' : 'font-medium text-ink-3 hover:bg-surface-2',
|
||
)}
|
||
>
|
||
{v.label}
|
||
{v.count !== undefined && <span className="text-xs font-medium text-faint">{v.count}</span>}
|
||
</button>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
{toolbar && <div className="flex flex-wrap items-center gap-2 px-3 py-2.5 border-b border-rule">{toolbar}</div>}
|
||
|
||
{isEmpty ? (
|
||
<div className="flex flex-col items-center gap-3 px-4 py-14 text-center">
|
||
<span className="inline-grid place-items-center w-16 h-16 rounded-xl bg-surface-2 text-faint">
|
||
<Inbox size={28} strokeWidth={1.8} aria-hidden />
|
||
</span>
|
||
<p className="text-sm text-ink-3">{empty.title}</p>
|
||
{empty.action}
|
||
</div>
|
||
) : (
|
||
<>
|
||
<div className="hidden md:block overflow-x-auto">
|
||
<table className="w-full border-collapse">
|
||
<thead>
|
||
<tr>
|
||
{selection && (
|
||
<th className={cx(TH, 'w-7 pl-4')}>
|
||
<Checkbox
|
||
checked={allSelected}
|
||
indeterminate={selectedOnPage > 0 && !allSelected}
|
||
onChange={setAll}
|
||
label="Select all rows"
|
||
/>
|
||
</th>
|
||
)}
|
||
{columns.map(c => {
|
||
const sorted = sort && c.sortKey && sort.key === c.sortKey ? sort.dir : null
|
||
return (
|
||
<th
|
||
key={c.key}
|
||
className={TH}
|
||
aria-sort={sorted ? (sorted === 'asc' ? 'ascending' : 'descending') : undefined}
|
||
>
|
||
{c.sortKey && onSortChange ? (
|
||
<button
|
||
type="button"
|
||
onClick={() => onSortChange(c.sortKey!)}
|
||
className="inline-flex items-center gap-1 cursor-pointer"
|
||
>
|
||
{c.header}
|
||
{sorted === 'asc' ? (
|
||
<ArrowUp size={12} strokeWidth={2.5} className="text-ink" aria-hidden />
|
||
) : sorted === 'desc' ? (
|
||
<ArrowDown size={12} strokeWidth={2.5} className="text-ink" aria-hidden />
|
||
) : (
|
||
<ChevronsUpDown size={12} strokeWidth={2} className="text-neutral-400" aria-hidden />
|
||
)}
|
||
</button>
|
||
) : (
|
||
c.header
|
||
)}
|
||
</th>
|
||
)
|
||
})}
|
||
{rowActions && <th className={cx(TH, 'w-24')} />}
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{rows.map(row => {
|
||
const id = rowKey(row)
|
||
const selected = Boolean(selection?.selected.has(id))
|
||
return (
|
||
<tr
|
||
key={id}
|
||
tabIndex={onRowOpen ? 0 : undefined}
|
||
onClick={() => open(row)}
|
||
onKeyDown={e => { if (e.key === 'Enter') open(row) }}
|
||
className={cx(
|
||
'group',
|
||
onRowOpen && 'cursor-pointer',
|
||
selected ? 'bg-accent-tint' : 'bg-surface hover:bg-surface-2',
|
||
)}
|
||
>
|
||
{selection && (
|
||
<td className={cx(TD, 'pl-4')} onClick={e => e.stopPropagation()}>
|
||
<Checkbox checked={selected} onChange={next => setOne(id, next)} label="Select row" />
|
||
</td>
|
||
)}
|
||
{columns.map(c => (
|
||
<td key={c.key} className={TD}>{c.cell(row)}</td>
|
||
))}
|
||
{rowActions && (
|
||
<td className={TD} onClick={e => e.stopPropagation()}>
|
||
<div className="flex justify-end gap-1 opacity-0 group-hover:opacity-100 focus-within:opacity-100">
|
||
{rowActions(row)}
|
||
</div>
|
||
</td>
|
||
)}
|
||
</tr>
|
||
)
|
||
})}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
{mobileRow && (
|
||
<ul className="md:hidden">
|
||
{rows.map(row => (
|
||
<li
|
||
key={rowKey(row)}
|
||
onClick={() => open(row)}
|
||
className={cx('px-4 py-3 border-b border-rule', onRowOpen && 'cursor-pointer')}
|
||
>
|
||
{mobileRow(row)}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
)}
|
||
</>
|
||
)}
|
||
|
||
{footer && (
|
||
<div className="flex items-center justify-between px-4 py-2.5 bg-surface-2 text-[13px] text-muted">
|
||
<span>{`Showing ${footer.from}–${footer.to} of ${footer.total}`}</span>
|
||
<div className="flex">
|
||
<button
|
||
type="button"
|
||
aria-label="Previous page"
|
||
disabled={footer.page <= 1}
|
||
onClick={() => footer.onPage(footer.page - 1)}
|
||
className="inline-flex items-center h-8 px-[7px] rounded-l-md bg-surface text-ink shadow-chip disabled:text-neutral-400 cursor-pointer"
|
||
>
|
||
<ChevronLeft size={16} strokeWidth={2} aria-hidden />
|
||
</button>
|
||
<button
|
||
type="button"
|
||
aria-label="Next page"
|
||
disabled={footer.page >= footer.pageCount}
|
||
onClick={() => footer.onPage(footer.page + 1)}
|
||
className="inline-flex items-center h-8 px-[7px] rounded-r-md bg-surface text-ink shadow-chip -ml-px disabled:text-neutral-400 cursor-pointer"
|
||
>
|
||
<ChevronRight size={16} strokeWidth={2} aria-hidden />
|
||
</button>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{selection && bulkActions && selection.selected.size > 0 && (
|
||
<div className="sticky bottom-3 flex justify-center px-4 pb-3 pt-5 bg-gradient-to-t from-surface">
|
||
<div className="flex items-center gap-1.5 py-1.5 pr-1.5 pl-3 rounded-xl bg-surface shadow-float">
|
||
<span className="text-[13px] font-semibold px-2">{`${selection.selected.size} selected`}</span>
|
||
<span className="w-px h-5 bg-rule-2" />
|
||
{bulkActions}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|