Table
A responsive table component for displaying data in rows and columns.
Invoice | Status | Method | Amount |
---|---|---|---|
INV001 | Paid | Credit Card | $250.00 |
INV002 | Pending | PayPal | $150.00 |
INV003 | Unpaid | Bank Transfer | $350.00 |
INV004 | Paid | Credit Card | $450.00 |
INV005 | Paid | PayPal | $550.00 |
INV006 | Pending | Bank Transfer | $200.00 |
INV007 | Unpaid | Credit Card | $300.00 |
tsx
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow,} from "@/components/ui/table"
const invoices = [ { invoice: "INV001", paymentStatus: "Paid", totalAmount: "$250.00", paymentMethod: "Credit Card", }, { invoice: "INV002", paymentStatus: "Pending", totalAmount: "$150.00", paymentMethod: "PayPal", }, { invoice: "INV003", paymentStatus: "Unpaid", totalAmount: "$350.00", paymentMethod: "Bank Transfer", },]
export function TableDemo() { return ( <Table> <TableCaption>A list of your recent invoices.</TableCaption> <TableHeader> <TableRow> <TableHead className="w-[100px]">Invoice</TableHead> <TableHead>Status</TableHead> <TableHead>Method</TableHead> <TableHead className="text-right">Amount</TableHead> </TableRow> </TableHeader> <TableBody> {invoices.map((invoice) => ( <TableRow key={invoice.invoice}> <TableCell className="font-medium">{invoice.invoice}</TableCell> <TableCell>{invoice.paymentStatus}</TableCell> <TableCell>{invoice.paymentMethod}</TableCell> <TableCell className="text-right">{invoice.totalAmount}</TableCell> </TableRow> ))} </TableBody> </Table> )}
Installation
CLI
bash
npx fivui add table
Manual
Copy and paste the following code into your project.
components/ui/table.tsx
"use client"
import * as React from "react"
import { cn } from "@/lib/utils"
const Table = React.forwardRef< HTMLTableElement, React.HTMLAttributes<HTMLTableElement>>(({ className, ...props }, ref) => ( <div className="relative w-full overflow-auto"> <table ref={ref} className={cn("w-full caption-bottom text-sm", className)} {...props} /> </div>))Table.displayName = "Table"
const TableHeader = React.forwardRef< HTMLTableSectionElement, React.HTMLAttributes<HTMLTableSectionElement>>(({ className, ...props }, ref) => ( <thead ref={ref} className={cn("[&_tr]:border-b", className)} {...props} />))TableHeader.displayName = "TableHeader"
const TableBody = React.forwardRef< HTMLTableSectionElement, React.HTMLAttributes<HTMLTableSectionElement>>(({ className, ...props }, ref) => ( <tbody ref={ref} className={cn("[&_tr:last-child]:border-0", className)} {...props} />))TableBody.displayName = "TableBody"
const TableFooter = React.forwardRef< HTMLTableSectionElement, React.HTMLAttributes<HTMLTableSectionElement>>(({ className, ...props }, ref) => ( <tfoot ref={ref} className={cn( "border-t bg-muted/50 font-medium [&>tr]:last:border-b-0", className )} {...props} />))TableFooter.displayName = "TableFooter"
const TableRow = React.forwardRef< HTMLTableRowElement, React.HTMLAttributes<HTMLTableRowElement>>(({ className, ...props }, ref) => ( <tr ref={ref} className={cn( "border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted", className )} {...props} />))TableRow.displayName = "TableRow"
const TableHead = React.forwardRef< HTMLTableCellElement, React.ThHTMLAttributes<HTMLTableCellElement>>(({ className, ...props }, ref) => ( <th ref={ref} className={cn( "h-12 px-4 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0", className )} {...props} />))TableHead.displayName = "TableHead"
const TableCell = React.forwardRef< HTMLTableCellElement, React.TdHTMLAttributes<HTMLTableCellElement>>(({ className, ...props }, ref) => ( <td ref={ref} className={cn("p-4 align-middle [&:has([role=checkbox])]:pr-0", className)} {...props} />))TableCell.displayName = "TableCell"
const TableCaption = React.forwardRef< HTMLTableCaptionElement, React.HTMLAttributes<HTMLTableCaptionElement>>(({ className, ...props }, ref) => ( <caption ref={ref} className={cn("mt-4 text-sm text-muted-foreground", className)} {...props} />))TableCaption.displayName = "TableCaption"
export { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell, TableCaption,}
Usage
tsx
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow,} from "@/components/ui/table"
tsx
<Table> <TableCaption>A list of your recent invoices.</TableCaption> <TableHeader> <TableRow> <TableHead>Invoice</TableHead> <TableHead>Status</TableHead> <TableHead>Method</TableHead> <TableHead>Amount</TableHead> </TableRow> </TableHeader> <TableBody> <TableRow> <TableCell>INV001</TableCell> <TableCell>Paid</TableCell> <TableCell>Credit Card</TableCell> <TableCell>$250.00</TableCell> </TableRow> </TableBody></Table>
Examples
Basic Table
Name | Status | Role |
---|---|---|
John Doe | Active | Developer |
Jane Smith | Inactive | Designer |
Bob Johnson | Active | Manager |
tsx
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow,} from "@/components/ui/table"
export function BasicTable() { return ( <Table> <TableHeader> <TableRow> <TableHead>Name</TableHead> <TableHead>Status</TableHead> <TableHead>Role</TableHead> </TableRow> </TableHeader> <TableBody> <TableRow> <TableCell>John Doe</TableCell> <TableCell>Active</TableCell> <TableCell>Developer</TableCell> </TableRow> <TableRow> <TableCell>Jane Smith</TableCell> <TableCell>Inactive</TableCell> <TableCell>Designer</TableCell> </TableRow> <TableRow> <TableCell>Bob Johnson</TableCell> <TableCell>Active</TableCell> <TableCell>Manager</TableCell> </TableRow> </TableBody> </Table> )}
With Caption
Invoice | Status | Amount |
---|---|---|
INV001 | Paid | $250.00 |
INV002 | Pending | $150.00 |
tsx
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow,} from "@/components/ui/table"
export function TableWithCaption() { return ( <Table> <TableCaption>A list of your recent invoices.</TableCaption> <TableHeader> <TableRow> <TableHead>Invoice</TableHead> <TableHead>Status</TableHead> <TableHead className="text-right">Amount</TableHead> </TableRow> </TableHeader> <TableBody> <TableRow> <TableCell className="font-medium">INV001</TableCell> <TableCell>Paid</TableCell> <TableCell className="text-right">$250.00</TableCell> </TableRow> <TableRow> <TableCell className="font-medium">INV002</TableCell> <TableCell>Pending</TableCell> <TableCell className="text-right">$150.00</TableCell> </TableRow> </TableBody> </Table> )}
API Reference
Table Components
Component | Description | HTML Element |
---|---|---|
Table | The root table component with overflow handling | table |
TableHeader | Contains the table header rows | thead |
TableBody | Contains the table body rows | tbody |
TableFooter | Contains the table footer rows | tfoot |
TableRow | A table row with hover effects | tr |
TableHead | A table header cell | th |
TableCell | A table data cell | td |
TableCaption | A table caption for accessibility | caption |
All table components inherit props from their respective HTML elements.
View MDN Docs