feat(design): add TableCard (views, toolbar, sortable header, selection, bulk bar, footer pager, empty, mobile list)
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,226 @@
|
|||||||
|
'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>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
import { describe, it, expect, vi } from 'vitest'
|
||||||
|
import { render, screen, fireEvent, within } from '@testing-library/react'
|
||||||
|
import { TableCard, type Column } from '@/components/ui/table-card'
|
||||||
|
|
||||||
|
type Row = { id: string; ref: string; status: string }
|
||||||
|
const rows: Row[] = [
|
||||||
|
{ id: '1', ref: 'IMS-0001', status: 'Open' },
|
||||||
|
{ id: '2', ref: 'IMS-0002', status: 'Closed' },
|
||||||
|
{ id: '3', ref: 'IMS-0003', status: 'Open' },
|
||||||
|
]
|
||||||
|
const columns: Column<Row>[] = [
|
||||||
|
{ key: 'ref', header: 'Reference', sortKey: 'ref', cell: r => r.ref },
|
||||||
|
{ key: 'status', header: 'Status', cell: r => r.status },
|
||||||
|
]
|
||||||
|
|
||||||
|
const base = { columns, rows, rowKey: (r: Row) => r.id }
|
||||||
|
|
||||||
|
describe('TableCard', () => {
|
||||||
|
it('renders a 12px card with a grey header and the rows', () => {
|
||||||
|
const { container } = render(<TableCard {...base} />)
|
||||||
|
expect(container.firstElementChild?.className).toContain('rounded-xl')
|
||||||
|
expect(container.firstElementChild?.className).toContain('shadow-card')
|
||||||
|
expect(screen.getByText('Reference').closest('th')?.className).toContain('bg-surface-2')
|
||||||
|
expect(screen.getByText('IMS-0002')).toBeTruthy()
|
||||||
|
expect(screen.getAllByRole('row')).toHaveLength(4)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('opens a row on click and on Enter, but not from the actions cell', () => {
|
||||||
|
const onRowOpen = vi.fn()
|
||||||
|
render(<TableCard {...base} onRowOpen={onRowOpen} rowActions={() => <button>quick</button>} />)
|
||||||
|
fireEvent.click(screen.getByText('IMS-0001'))
|
||||||
|
expect(onRowOpen).toHaveBeenCalledWith(rows[0])
|
||||||
|
fireEvent.keyDown(screen.getByText('IMS-0002').closest('tr')!, { key: 'Enter' })
|
||||||
|
expect(onRowOpen).toHaveBeenCalledWith(rows[1])
|
||||||
|
onRowOpen.mockClear()
|
||||||
|
fireEvent.click(screen.getAllByText('quick')[0])
|
||||||
|
expect(onRowOpen).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('sorts a single column: sortable header is a button, aria-sort reflects state', () => {
|
||||||
|
const onSortChange = vi.fn()
|
||||||
|
render(<TableCard {...base} sort={{ key: 'ref', dir: 'asc' }} onSortChange={onSortChange} />)
|
||||||
|
expect(screen.getByText('Reference').closest('th')?.getAttribute('aria-sort')).toBe('ascending')
|
||||||
|
expect(screen.getByText('Status').closest('th')?.getAttribute('aria-sort')).toBeNull()
|
||||||
|
expect(screen.queryByRole('button', { name: /Status/ })).toBeNull()
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: /Reference/ }))
|
||||||
|
expect(onSortChange).toHaveBeenCalledWith('ref')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('selects rows and all rows, and shows the bulk bar with the count', () => {
|
||||||
|
const onChange = vi.fn()
|
||||||
|
const { rerender } = render(
|
||||||
|
<TableCard {...base} selection={{ selected: new Set(), onChange }} bulkActions={<button>Export</button>} />,
|
||||||
|
)
|
||||||
|
expect(screen.queryByText(/selected/)).toBeNull()
|
||||||
|
fireEvent.click(screen.getByRole('checkbox', { name: 'Select all rows' }))
|
||||||
|
expect(onChange).toHaveBeenLastCalledWith(new Set(['1', '2', '3']))
|
||||||
|
fireEvent.click(screen.getAllByRole('checkbox', { name: 'Select row' })[1])
|
||||||
|
expect(onChange).toHaveBeenLastCalledWith(new Set(['2']))
|
||||||
|
|
||||||
|
rerender(<TableCard {...base} selection={{ selected: new Set(['1']), onChange }} bulkActions={<button>Export</button>} />)
|
||||||
|
expect(screen.getByText('1 selected')).toBeTruthy()
|
||||||
|
expect(screen.getByRole('button', { name: 'Export' })).toBeTruthy()
|
||||||
|
expect(screen.getByRole('checkbox', { name: 'Select all rows' }).getAttribute('aria-checked')).toBe('mixed')
|
||||||
|
expect(screen.getByText('IMS-0001').closest('tr')?.className).toContain('bg-accent-tint')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders saved views with counts and reports changes', () => {
|
||||||
|
const onChange = vi.fn()
|
||||||
|
render(
|
||||||
|
<TableCard
|
||||||
|
{...base}
|
||||||
|
views={{ items: [{ key: 'all', label: 'All', count: 248 }, { key: 'open', label: 'Open', count: 37 }], active: 'all', onChange }}
|
||||||
|
/>,
|
||||||
|
)
|
||||||
|
expect(screen.getByRole('button', { name: /All/ }).className).toContain('bg-rule-2')
|
||||||
|
expect(screen.getByText('248')).toBeTruthy()
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: /Open/ }))
|
||||||
|
expect(onChange).toHaveBeenCalledWith('open')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders the footer count and pager', () => {
|
||||||
|
const onPage = vi.fn()
|
||||||
|
render(<TableCard {...base} footer={{ from: 1, to: 3, total: 248, page: 1, pageCount: 83, onPage }} />)
|
||||||
|
expect(screen.getByText('Showing 1–3 of 248')).toBeTruthy()
|
||||||
|
expect((screen.getByRole('button', { name: 'Previous page' }) as HTMLButtonElement).disabled).toBe(true)
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Next page' }))
|
||||||
|
expect(onPage).toHaveBeenCalledWith(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('shows the empty state instead of the table', () => {
|
||||||
|
render(<TableCard {...base} rows={[]} empty={{ title: 'No incidents found', action: <button>Report incident</button> }} />)
|
||||||
|
expect(screen.queryByRole('table')).toBeNull()
|
||||||
|
expect(screen.getByText('No incidents found')).toBeTruthy()
|
||||||
|
expect(screen.getByRole('button', { name: 'Report incident' })).toBeTruthy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders a stacked mobile list beside the desktop table', () => {
|
||||||
|
const onRowOpen = vi.fn()
|
||||||
|
const { container } = render(<TableCard {...base} onRowOpen={onRowOpen} mobileRow={r => <span>mobile {r.ref}</span>} />)
|
||||||
|
const list = container.querySelector('ul')!
|
||||||
|
expect(list.className).toContain('md:hidden')
|
||||||
|
expect(within(list).getByText('mobile IMS-0003')).toBeTruthy()
|
||||||
|
expect(container.querySelector('table')!.parentElement!.className).toContain('hidden md:block')
|
||||||
|
fireEvent.click(within(list).getByText('mobile IMS-0003'))
|
||||||
|
expect(onRowOpen).toHaveBeenCalledWith(rows[2])
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user