diff --git a/components/ui/table-card.tsx b/components/ui/table-card.tsx new file mode 100644 index 0000000..b6d4684 --- /dev/null +++ b/components/ui/table-card.tsx @@ -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 = { 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 { + columns: Column[] + 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; onChange: (next: Set) => 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({ + columns, rows, rowKey, onRowOpen, rowActions, views, toolbar, sort, onSortChange, + selection, bulkActions, footer, empty, mobileRow, +}: TableCardProps) { + 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 ( +
+ {views && ( +
+ {views.items.map(v => ( + + ))} +
+ )} + + {toolbar &&
{toolbar}
} + + {isEmpty ? ( +
+ + + +

{empty.title}

+ {empty.action} +
+ ) : ( + <> +
+ + + + {selection && ( + + )} + {columns.map(c => { + const sorted = sort && c.sortKey && sort.key === c.sortKey ? sort.dir : null + return ( + + ) + })} + {rowActions && + + + {rows.map(row => { + const id = rowKey(row) + const selected = Boolean(selection?.selected.has(id)) + return ( + 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 && ( + + )} + {columns.map(c => ( + + ))} + {rowActions && ( + + )} + + ) + })} + +
+ 0 && !allSelected} + onChange={setAll} + label="Select all rows" + /> + + {c.sortKey && onSortChange ? ( + + ) : ( + c.header + )} + } +
e.stopPropagation()}> + setOne(id, next)} label="Select row" /> + {c.cell(row)} e.stopPropagation()}> +
+ {rowActions(row)} +
+
+
+ + {mobileRow && ( +
    + {rows.map(row => ( +
  • open(row)} + className={cx('px-4 py-3 border-b border-rule', onRowOpen && 'cursor-pointer')} + > + {mobileRow(row)} +
  • + ))} +
+ )} + + )} + + {footer && ( +
+ {`Showing ${footer.from}–${footer.to} of ${footer.total}`} +
+ + +
+
+ )} + + {selection && bulkActions && selection.selected.size > 0 && ( +
+
+ {`${selection.selected.size} selected`} + + {bulkActions} +
+
+ )} +
+ ) +} diff --git a/tests/components/ui/table-card.test.tsx b/tests/components/ui/table-card.test.tsx new file mode 100644 index 0000000..79e2c1f --- /dev/null +++ b/tests/components/ui/table-card.test.tsx @@ -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[] = [ + { 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() + 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( } />) + 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() + 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( + Export} />, + ) + 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(Export} />) + 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( + , + ) + 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() + 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(Report incident }} />) + 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( mobile {r.ref}} />) + 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]) + }) +})