Skip to Content
TelamonJS

DataTable

Advanced data table with sorting, filtering, pagination, and row selection

Advanced data table component built on TanStack Table with sorting, filtering, pagination, row selection, and mobile responsiveness.

Lower-Level Component

DataTable is a lower-level component. For most use cases, use ListPage which wraps DataTable with additional features.


Basic Usage

import { DataTable } from '@telamonjs/ui'
import type { ColumnDef } from '@tanstack/react-table'

interface User {
  id: string
  name: string
  email: string
  role: string
}

const columns: ColumnDef<User>[] = [
  { accessorKey: 'name', header: 'Name' },
  { accessorKey: 'email', header: 'Email' },
  { accessorKey: 'role', header: 'Role' },
]

export default function UsersTable({ users }: { users: User[] }) {
  return <DataTable columns={columns} data={users} />
}

Props

Most-used props

This table covers the props most consumers reach for; DataTableProps has the full list.

Prop

Type


Examples

With Sorting

<DataTable
  columns={columns}
  data={users}
  // Sorting enabled by default - click headers to sort
/>

Column configuration:

const columns: ColumnDef<User>[] = [
  {
    accessorKey: 'name',
    header: 'Name',
    enableSorting: true, // ✅ Sortable (default)
  },
  {
    accessorKey: 'email',
    header: 'Email',
    enableSorting: false, // ❌ Not sortable
  },
]

With Pagination

<DataTable columns={columns} data={users} pageSize={25} />

Pagination controls render while enablePagination is true (the default).

With Row Selection

import { DataTable } from '@telamonjs/ui'

export default function UsersTable({ users }: { users: User[] }) {
  return <DataTable columns={columns} data={users} enableRowSelection />
}

enableRowSelection renders checkboxes and manages selection state internally (Zustand store keyed by tableId) — there is no rowSelection/onRowSelectionChange prop to control it externally.

With Row Actions

import { DataTable } from '@telamonjs/ui'

;<DataTable
  columns={columns}
  data={users}
  actionsConfig={{
    standard: {
      view: { href: row => `/users/${row.id}` },
      edit: { href: row => `/users/${row.id}/edit` },
      delete: {
        onClick: row => handleDelete(row.id),
        confirmDialog: true,
      },
    },
  }}
/>

actionsConfig (type DataTableActionsConfig<TData>) injects the actions column — there is no rowActions render-prop.

Custom Cell Rendering

import { Badge } from '@telamonjs/ui'

const columns: ColumnDef<User>[] = [
  {
    accessorKey: 'name',
    header: 'Name',
  },
  {
    accessorKey: 'role',
    header: 'Role',
    cell: ({ row }) => {
      const role = row.original.role
      return (
        <Badge variant={role === 'ADMIN' ? 'default' : 'secondary'}>
          {role}
        </Badge>
      )
    },
  },
  {
    accessorKey: 'createdAt',
    header: 'Created',
    cell: ({ row }) => {
      return new Date(row.original.createdAt).toLocaleDateString()
    },
  },
]

Use cell function to customize rendering.

enableSearch (default true) renders the search input and filters client-side automatically — no external filter state is needed:

<DataTable columns={columns} data={users} searchPlaceholder='Search users...' />

For server-side pagination, wire onSearchChange instead (debounced 300ms):

<DataTable
  columns={columns}
  data={users}
  manualPagination
  onSearchChange={search => refetch({ search })}
/>

Custom Mobile Card

<DataTable
  columns={columns}
  data={users}
  cardComponent={({ data }) => (
    <div className='p-4 space-y-2'>
      <div className='font-semibold'>{data.name}</div>
      <div className='text-sm text-muted-foreground'>{data.email}</div>
      <Badge>{data.role}</Badge>
    </div>
  )}
/>

cardComponent (type React.ComponentType<{ data: TData; onClick?: (data: TData) => void }>) replaces the default mobile card renderer.

Loading State

<DataTable columns={columns} data={users} loading={isLoading} />

Shows skeleton loading state.

Empty State

<DataTable
  columns={columns}
  data={[]}
  emptyTitle='No users found'
  emptyDescription='Create your first user to get started.'
/>

emptyTitle/emptyDescription customize the empty state — there is no single emptyMessage prop.


Complete Example

app/admin/users/components/users-table.tsx
'use client'

import { DataTable, Badge } from '@telamonjs/ui'
import type { ColumnDef } from '@tanstack/react-table'

interface User {
  id: string
  name: string
  email: string
  role: 'USER' | 'ADMIN'
  active: boolean
  createdAt: string
}

const columns: ColumnDef<User>[] = [
  {
    accessorKey: 'name',
    header: 'Name',
    enableSorting: true,
  },
  {
    accessorKey: 'email',
    header: 'Email',
    enableSorting: true,
  },
  {
    accessorKey: 'role',
    header: 'Role',
    cell: ({ row }) => (
      <Badge variant={row.original.role === 'ADMIN' ? 'default' : 'secondary'}>
        {row.original.role}
      </Badge>
    ),
  },
  {
    accessorKey: 'active',
    header: 'Status',
    cell: ({ row }) => (
      <Badge variant={row.original.active ? 'default' : 'secondary'}>
        {row.original.active ? 'Active' : 'Inactive'}
      </Badge>
    ),
  },
  {
    accessorKey: 'createdAt',
    header: 'Created',
    cell: ({ row }) => new Date(row.original.createdAt).toLocaleDateString(),
    enableSorting: true,
  },
]

export default function UsersTable({ users }: { users: User[] }) {
  async function handleDelete(id: string) {
    if (confirm('Are you sure you want to delete this user?')) {
      // Delete logic
    }
  }

  return (
    <DataTable
      columns={columns}
      data={users}
      pageSize={10}
      enableRowSelection
      actionsConfig={{
        standard: {
          view: { href: row => `/admin/users/${row.id}` },
          edit: { href: row => `/admin/users/${row.id}/edit` },
          delete: {
            onClick: row => handleDelete(row.id),
            confirmDialog: true,
          },
        },
      }}
      emptyTitle='No users found'
    />
  )
}

Column Definition API

Common properties:

{
  accessorKey: 'fieldName',        // Data field key
  header: 'Display Name',          // Column header
  cell: ({ row }) => <Custom />,   // Custom cell renderer
  enableSorting: true,             // Allow sorting
  enableHiding: true,              // Allow hiding column
  size: 200,                       // Column width in px
}

Advanced features:

  • accessorFn - Custom accessor function
  • sortingFn - Custom sorting logic
  • filterFn - Custom filtering logic
  • meta - Store custom metadata

See TanStack Table Docs for full API.


Responsive Behavior

Screen SizeBehavior
Desktop (≥ 768px)Full table with all columns
Mobile (< 768px)Switches to card view automatically

Mobile cards show:

  • Custom renderer if provided via cardComponent
  • Default card with all fields if no custom renderer

Best Practices

  1. Define columns in config.tsx - Follow Page Config Pattern
  2. Use cell renderers - For badges, dates, custom formatting
  3. Enable sorting - For better UX on large datasets
  4. Add row actions - Common operations easily accessible
  5. Provide empty state - Custom message for empty data
  6. Custom mobile cards - Optimize mobile UX

Performance

Optimization tips:

  • ✅ Use useMemo for columns definition
  • ✅ Virtualize rows for 1000+ records (react-virtual)
  • ✅ Server-side pagination for 10,000+ records
  • ✅ Debounce search input
  • ✅ Lazy load row actions menu

Example:

const columns = useMemo<ColumnDef<User>[]>(
  () => [
    { accessorKey: 'name', header: 'Name' },
    // ...
  ],
  []
)

Accessibility

  • Semantic <table> element
  • Sortable headers keyboard accessible (Enter to sort)
  • Row selection with keyboard (Space to toggle)
  • ARIA labels for sorting state
  • Focus visible states
  • Screen reader announcements for updates

  • ListPage - Higher-level list with filters (recommended)
  • MobileCard - Mobile card component

See Also


Status: ✅ Stable

On this page