Data Table

A table component for displaying and interacting with tabular data.

Basic Data Table

ID
Name
Email
Status
Role
Last Login
1John Doejohn@example.com
Active
Admin2023-05-15
2Jane Smithjane@example.com
Inactive
User2023-04-20
3Bob Johnsonbob@example.com
Active
Editor2023-05-10
4Alice Brownalice@example.com
Pending
User2023-05-01
5Charlie Wilsoncharlie@example.com
Active
Admin2023-05-12

Usage

import { DataTable } from "@/components/data-table"
import { Badge } from "@/components/badge"

export default function MyComponent() {
  const data = [
    { id: 1, name: "John Doe", email: "john@example.com", status: "active" },
    { id: 2, name: "Jane Smith", email: "jane@example.com", status: "inactive" },
    // ...more data
  ]
  
  const renderStatus = (status) => {
    return <Badge variant={status === "active" ? "success" : "error"}>
      {status === "active" ? "Active" : "Inactive"}
    </Badge>
  }
  
  const columns = [
    { header: "ID", accessor: "id", sortable: true },
    { header: "Name", accessor: "name", sortable: true },
    { header: "Email", accessor: "email", sortable: true },
    { header: "Status", accessor: (row) => renderStatus(row.status) },
  ]
  
  return (
    <DataTable
      data={data}
      columns={columns}
      pageSize={10}
      onRowClick={(row) => console.log(row)}
    />
  )
}

Props

Name
Type
Default
Description
data
array
required
Array of data objects to display in the table.
columns
array
required
Array of column configuration objects.
pageSize
number
10
Number of rows to display per page.
onRowClick
function
undefined
Function called when a row is clicked.
rowClassName
string | function
undefined
CSS class or function returning a class for each row.