Table
A table displays data in rows and columns and enables a user to navigate its contents via directional navigation keys, and optionally supports row selection and sorting.
Name | Token | Env | ||
---|---|---|---|---|
Dev | ****1822 | preview | ||
Production | ****3345 | prod | ||
beta | ****6431 | preview |
Installation
Install via VScode or copy and paste the code below into a new file, preferably in a folder components/base.
BaseLayer
Examples
Default
Name | Token | Env | ||
---|---|---|---|---|
Dev | ****1822 | preview | ||
Production | ****3345 | prod | ||
beta | ****6431 | preview |
bl-table
import {
Column,
Table,
TableBody,
TableCell,
TableHeader,
TableRow,
} from "@/components/base/table";
export const TableExample = () => {
return (
<Table aria-label="Tokens" selectionMode="multiple">
<TableHeader>
<Column isRowHeader>Name</Column>
<Column>Token</Column>
<Column>Env</Column>
<Column></Column>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>Dev</TableCell>
<TableCell>****1822</TableCell>
<TableCell>preview</TableCell>
</TableRow>
<TableRow>
<TableCell>Production</TableCell>
<TableCell>****3345</TableCell>
<TableCell>prod</TableCell>
</TableRow>
<TableRow>
<TableCell>beta</TableCell>
<TableCell>****6431</TableCell>
<TableCell>preview</TableCell>
</TableRow>
</TableBody>
</Table>
);
};
Sortable
Name | Price | Brand |
---|
bl-table-sortable
import {
Column,
Table,
TableBody,
TableCell,
TableHeader,
TableRow,
} from "@/components/base/table";
import { useAsyncList } from "@react-stately/data";
interface Invoice {
[key: string]: any;
title: string;
price: number;
brand: string;
}
export function TableSortable() {
let list = useAsyncList<Invoice>({
async load({ signal }) {
let res = await fetch(`https://dummyjson.com/products?limit=10`, {
signal,
});
let json = await res.json();
return {
items: json.products,
};
},
async sort({ items, sortDescriptor }) {
return {
items: items.sort((a, b) => {
if (!sortDescriptor.column) {
return 0;
}
let first = a[sortDescriptor.column];
let second = b[sortDescriptor.column];
let cmp =
(parseInt(first) || first) < (parseInt(second) || second) ? -1 : 1;
if (sortDescriptor.direction === "descending") {
cmp *= -1;
}
return cmp;
}),
};
},
});
return (
<Table
aria-label="Example table with client side sorting"
sortDescriptor={list.sortDescriptor}
onSortChange={list.sort}
>
<TableHeader>
<Column id="item" isRowHeader allowsSorting>
Name
</Column>
<Column id="price" allowsSorting>
Price
</Column>
<Column id="brand" allowsSorting>
Brand
</Column>
</TableHeader>
<TableBody items={list.items}>
{(item) => (
<TableRow id={item.title}>
<TableCell>{item.title}</TableCell>
<TableCell>{item.price}</TableCell>
<TableCell>{item.brand}</TableCell>
</TableRow>
)}
</TableBody>
</Table>
);
}
Reorderable
Name | Token | Env | ||
---|---|---|---|---|
Dev | ****1822 | prod | ||
Production | ****3345 | preview | ||
beta | ****6431 | preview |
bl-table-reorderable
import {
Column,
Table,
TableBody,
TableCell,
TableHeader,
TableRow,
} from "@/components/base/table";
import { useDragAndDrop } from "react-aria-components";
import { useListData } from "react-stately";
export function TableReorderable() {
let list = useListData({
initialItems: [
{ id: 1, name: "Dev", token: "****1822", env: "prod" },
{ id: 2, name: "Production", token: "****3345", env: "preview" },
{ id: 3, name: "beta", token: "****6431", env: "preview" },
],
});
let { dragAndDropHooks } = useDragAndDrop({
getItems: (keys) =>
[...keys].map((key) => ({
"text/plain": list.getItem(key).name,
})),
onReorder(e) {
if (e.target.dropPosition === "before") {
list.moveBefore(e.target.key, e.keys);
} else if (e.target.dropPosition === "after") {
list.moveAfter(e.target.key, e.keys);
}
},
});
return (
<Table
aria-label="Tokens"
selectionMode="multiple"
dragAndDropHooks={dragAndDropHooks}
>
<TableHeader>
<Column isRowHeader>Name</Column>
<Column>Token</Column>
<Column>Env</Column>
</TableHeader>
<TableBody items={list.items}>
{(item) => (
<TableRow>
<TableCell>{item.name}</TableCell>
<TableCell>{item.token}</TableCell>
<TableCell>{item.env}</TableCell>
</TableRow>
)}
</TableBody>
</Table>
);
}