Table
Rows and columns of data: orders, invoices, users. Columns come as data, cells draw as text or any node, headers sort when sortable, and rows can be pressed. It draws every row, so keep it to a page at a time: pair it with Pagination for long lists.
import { Table } from 'patine-ui';
<VStack gap="md"> <Table caption="Recent invoices" data={INVOICES} onRowPress={(row) => setSelected(row.id)} defaultSort={{ key: 'amount', direction: 'desc' }} columns={[ { key: 'customer', title: 'Customer', flex: 2, sortable: true, render: (row) => ( <HStack gap="sm"> <Avatar name={row.customer} size="sm" /> <Text size="sm">{row.customer}</Text> </HStack> ), }, { key: 'status', title: 'Status', render: (row) => <Badge variant={VARIANT[row.status]}>{row.status}</Badge> }, { key: 'amount', title: 'Amount', align: 'right', sortable: true, render: (row) => <Text size="sm" weight="medium">${row.amount}.00</Text> }, ]} /> <Text size="sm" color="textSecondary">{selected ? `Opened ${selected}` : 'Press a row to open it.'}</Text> </VStack>
Try the props
design<Table columns={columns} data={people} />
Example: a sales leaderboard
A sales team's quarter: sort by deals or revenue, the rank follows the sort, and the table scrolls sideways on a phone instead of squeezing its columns.
<VStack gap="sm" style={{ width: '100%' }}> <Table caption="Q4 so far" style={{ width: '100%' }} data={ranked} keyExtractor={(r) => r.name} sort={sort} onSortChange={setSort} manualSort striped minWidth={520} columns={[ { key: 'rank', title: '#', width: 36, render: (row) => <Text size="sm" color="textSecondary">{ranked.indexOf(row) + 1}</Text> }, { key: 'name', title: 'Rep', flex: 2, render: (row) => ( <HStack gap="sm"> <Avatar name={row.name} size="sm" /> <VStack> <Text size="sm" weight="medium">{row.name}</Text> <Text size="xs" color="textSecondary">{row.region}</Text> </VStack> </HStack> ), }, { key: 'deals', title: 'Deals', align: 'right', sortable: true }, { key: 'revenue', title: 'Revenue', align: 'right', sortable: true, render: (row) => <Text size="sm" weight="medium">{euro(row.revenue)}</Text> }, { key: 'quota', title: 'Quota', flex: 1.5, render: (row) => ( <VStack gap="xs"> <Progress value={Math.min(100, (row.revenue / row.quota) * 100)} accessibilityLabel={`${row.name}, quota`} /> <Text size="xs" color={row.revenue >= row.quota ? 'success' : 'textSecondary'}>{Math.round((row.revenue / row.quota) * 100)}%</Text> </VStack> ), }, ]} /> </VStack>
Columns
Each column has a key and a title. The cell shows row[key], or accessor(row), as text; render(row, index) draws anything instead. width fixes a column in points; otherwise columns share the free width by flex (1 each). Put numbers on the right with align: 'right'.
Sorting
sortable columns sort when their header is pressed: ascending, then descending, then back to the data's order. Numbers sort by size and text the way people expect ("Item 2" before "Item 10"), with empty cells last. sort and onSortChange make it controlled; manualSort leaves data in the order you give it, for sorting on a server.
Narrow screens
Below minWidth, the table scrolls sideways instead of squeezing its columns.
Designs
- web: shadcn's Table: gray header text and a line under each row.
- Material 3: a data table: 56dp header, 52dp rows, in an outlined box.
- iOS: list-like 44pt rows under a small capitalized header.
- brand: an outlined box with a tinted header.
Accessibility
- A
tableofrows, withcolumnheaderandcellroles. A sortable header is a button witharia-sort, and its title shows ↑ or ↓. - A pressable row is one button read as its cells together, with hover and a focus ring on the web.
captionis a heading read before the table.
Props
Also takes View props.
| Prop | Type | Default | Description |
|---|---|---|---|
columns | { key, title, align?, width?, flex?, accessor?, render?, sortable? }[] | — | The columns. |
data | T[] | — | The rows. |
keyExtractor | (row, index) => string | row.id, else the index | Each row's key. |
onRowPress | (row, index) => void | — | Makes rows pressable. |
caption | ReactNode | — | A title, read first. |
striped | boolean | false | Tints every other row. |
sort | { key, direction } | null | — | Controlled sort. |
defaultSort | { key, direction } | null | null | Uncontrolled sort. |
onSortChange | (sort) => void | — | Called when a header is pressed. |
manualSort | boolean | false | Keep data's order. |
emptyText | ReactNode | 'No results.' | Shown with no rows. |
minWidth | number | — | Scrolls sideways below this width. |
styles | { table?, header?, headerCell?, row?, cell?, caption? } | — | Slot styles. |
theme, design | As every component. |