react-data-table-component

repository·master·Indexed 24 days ago

https://github.com/jbetancur/react-data-table-component

A fast, feature-rich React data table component (v8.8.0) providing built-in support for sorting, pagination, selection, and expandable rows. It includes multiple themes, including a Material UI-compatible theme, and is designed to be lightweight (~35 KB) without requiring heavy external dependencies like @mui/material or Emotion.

Tokens
9.5K
Snippets
11
Records
55
Agent score
80%

What's inside react-data-table-component

  1. When to choose Material UI Table

    master

    The @mui/material Table component provides atomic, styled primitives (TableHead, TableBody, etc.) but lacks built-in logic.

    Use Material UI Table if:

    • You want to compose your own table using Material-styled primitives.
    • Your application is already on Material UI and the table requirements are simple enough to hand-roll sorting and pagination.
    • You do not need built-in sorting, pagination, or selection logic.
  2. When to choose react-data-table-component

    master

    react-data-table-component is a production-ready, full component that balances features and ease of use without external dependencies.

    Use react-data-table-component if:

    • You want a working, styled table with sorting, pagination, selection, and expandable rows without building the UI yourself.
    • You want the Material UI look (via the built-in material theme) without adding @mui/material or Emotion as dependencies.
    • You want expandable rows to be included for free (not gated behind a paid tier).
    • You want a lightweight footprint (~35 KB) compared to heavy enterprise grids.
  3. When to choose TanStack Table

    master

    TanStack Table is a headless library. It manages the logic (sorting, pagination, selection, expansion) but provides no markup or CSS. You are responsible for building the entire UI layer.

    Use TanStack Table if:

    • You need absolute pixel-level control over markup and styling.
    • Your design system requires a highly custom implementation.
    • Minimal bundle size is a top priority (~15 KB min+gzip).
  4. Accessibility for Column filters

    master

    Filterable columns use a toggle button and a popup dialog (role="dialog").

    Filter toggle button

    • aria-label: Set to "Filter active" when a filter is applied, or "Filter column" otherwise.
    • aria-pressed: Indicates if the popup is open (true) or closed (false).

    Filter panel (popup)

    • Focus automatically moves to the first focusable element (the operator <select>) when opened.
    • Escape closes the panel.
    • Tab / Shift+Tab navigates between the operator select, value inputs, and action buttons.

    Panel Controls

    • Operator <select>: aria-label="Filter operator"
    • Primary value <input>: aria-label="Filter value"
    • Secondary value <input> (Between): aria-label="Filter second value"
    • AND/OR toggle buttons: aria-pressed reflects active state.
    • Add condition button: aria-label="Add a second filter condition"
    • Remove condition button: aria-label="Remove condition"
  5. Accessibility for Row selection

    master

    When selectableRows is enabled:

    • Data rows use aria-selected={true|false} to announce selection state.
    • The header 'select-all' checkbox uses aria-label="Select all rows".
    • Per-row checkboxes use aria-label="Select row {id}", where {id} is the row's key field value.
    • The indeterminate state is handled via the native indeterminate DOM property.
    • If cellNavigation is enabled, checkboxes are reachable via arrow keys and toggled with Space.
  6. Understand the Table structure and ARIA roles

    master

    The component renders div elements with explicit ARIA roles to provide a full table structure. By default, it acts as a static table where only sortable headers are focusable. If you pass the cellNavigation prop, it transforms into an interactive grid (WAI-ARIA grid pattern) where every cell becomes focusable via a single roving tab stop and arrow keys.

    | Element | Role / attribute |
    | --- | --- |
    | Table wrapper | `role="table"` (or `role="grid"` with `cellNavigation` — see below), `aria-label` (from `ariaLabel` prop), `aria-busy` during load |
    | Header section | `role="rowgroup"` |
    | Body section | `role="rowgroup"` |
    | Header row | `role="row"` |
    | Data row | `role="row"`, `aria-selected` (when `selectableRows` is enabled) |
    | Data cell | `role="cell"` (or `role="gridcell"` with `cellNavigation`) |
    | Column header | `role="columnheader"` |
  7. Accessibility for Loading and empty states

    master

    The component manages state visibility for assistive technology:

    • Loading: The table wrapper carries aria-busy="true". Skeleton rows are marked aria-hidden="true" so they are not read aloud.
    • Re-fetching: When an overlay appears during a re-fetch, the overlay is aria-hidden="true" and the wrapper's aria-busy state is updated.
    • Empty State: When no data is present, the empty-state container uses role="status" to ensure the "no records" message is announced.
  8. When to choose AG Grid

    master

    AG Grid is a heavyweight, full-featured data grid designed for complex data manipulation.

    Use AG Grid if:

    • You need enterprise-grade features like pivoting, row grouping, or Excel export (available in the Enterprise license).
    • You are rendering extremely large datasets and require built-in row virtualization.
    • You have a budget for an Enterprise license to access advanced features like master/detail views.
  9. Upgrade to v8: New Filter API and FilterState

    master

    The Filter API in v8 has been replaced with a structured FilterState that supports multiple operators, two conditions per column, and explicit Apply semantics.

    DataTable Props

    filterValues and onFilterChange now use FilterState instead of string:

    • filterValues?: Record<string | number, FilterState>
    • onFilterChange?: (columnId: string | number, filter: FilterState) => void

    TableColumn Props

    Set filterType on a column to define the available operators and input widget. Supported values are 'text' (default), 'number', and 'date'. The filterFunction now receives FilterState as its second argument.

    FilterState Structure

    type FilterState = {
      condition1: { operator: FilterOperator; value?: string; value2?: string };
      condition2?: { operator: FilterOperator; value?: string; value2?: string };
      logic?: 'AND' | 'OR'; // defaults to 'AND'
    };

    Behavior

    Filters no longer apply on every keystroke. Users must click an Apply button to trigger onFilterChange. The clear action still clears immediately.

    Utilities

    • emptyFilterState(type: 'text' | 'number' | 'date'): Creates a default empty state.
    • isFilterActive(state: FilterState): Returns true if the filter has an active condition.
    import type { FilterState } from 'react-data-table-component';
    
    // TableColumn configuration
    const columns: TableColumn<Row>[] = [
      { 
        id: 'age', 
        name: 'Age', 
        selector: r => r.age, 
        filterable: true, 
        filterType: 'number' 
      },
    ];
    
    // filterFunction implementation
    { 
      filterable: true, 
      filterType: 'text', 
      filterFunction: (row, filter) => {
        const v = filter.condition1.value ?? '';
        return row.name.toLowerCase().startsWith(v.toLowerCase());
      }, 
    }