EXEPERTAI LAB

Research alpha

Time Machine
EXEPERTAI LAB
GALLERY / COLLECTION
← Browse Astryx gallery
Table·hook·@astryxdesign/core

useTableFilterState

Managed state hook for table filtering. Returns the current filter map and an onChange handler. Pass the result directly to useTableFiltering.

Open in Playground @astryxdesign/core/Table

Signature

Call shape
useTableFilterState()

Showcases and examples

2 documented examples

Table — Popover Filters

Table with popover filter controls triggered by icons in column headers.

Preview loads on approachPreview loads on approach
Exact source · table-filterable-table
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

import {
  Table,
  useTableFiltering,
  useTableFilterState,
  toSearchFilters,
  proportional,
} from '@astryxdesign/core/Table';
import type {TableColumn} from '@astryxdesign/core/Table';
import {usePowerSearchConfig} from '@astryxdesign/core/PowerSearch';
import type {PowerSearchFilter} from '@astryxdesign/core/PowerSearch';

interface Employee extends Record<string, unknown> {
  id: string;
  name: string;
  email: string;
  role: string;
  department: string;
}

const employees: Employee[] = [
  {
    id: '1',
    name: 'Alice',
    email: 'alice@example.com',
    role: 'Engineer',
    department: 'Platform',
  },
  {
    id: '2',
    name: 'Bob',
    email: 'bob@example.com',
    role: 'Designer',
    department: 'Product',
  },
  {
    id: '3',
    name: 'Charlie',
    email: 'charlie@example.com',
    role: 'Manager',
    department: 'Platform',
  },
  {
    id: '4',
    name: 'Diana',
    email: 'diana@example.com',
    role: 'Engineer',
    department: 'Infrastructure',
  },
  {
    id: '5',
    name: 'Eve',
    email: 'eve@example.com',
    role: 'Admin',
    department: 'Operations',
  },
];

const fieldDefs = [
  {key: 'name', type: 'string', label: 'Name'},
  {key: 'email', type: 'string', label: 'Email'},
  {
    key: 'role',
    type: 'enum',
    label: 'Role',
    enumValues: [
      {value: 'Engineer', label: 'Engineer'},
      {value: 'Designer', label: 'Designer'},
      {value: 'Manager', label: 'Manager'},
      {value: 'Admin', label: 'Admin'},
    ],
  },
] as const;

const columns: TableColumn<Employee>[] = [
  {key: 'name', header: 'Name', width: proportional(1), filter: 'name'},
  {key: 'email', header: 'Email', width: proportional(2), filter: 'email'},
  {key: 'role', header: 'Role', width: proportional(1), filter: 'role'},
  {key: 'department', header: 'Department', width: proportional(1)},
];

export default function TableFilterableTable() {
  const {config, applyFilters} = usePowerSearchConfig(fieldDefs);
  const {filters, onFilterChange} = useTableFilterState();

  const filterPlugin = useTableFiltering<Employee>({
    filters,
    onFilterChange,
    searchConfig: config,
  });

  const data = applyFilters(
    toSearchFilters(filters, columns, config) as PowerSearchFilter[],
    employees,
  );

  return (
    <Table
      data={data}
      columns={columns}
      idKey="id"
      plugins={{filter: filterPlugin}}
    />
  );
}

Usage

Managed state hook for table filtering. Returns the current filter map and an onChange handler. Pass the result directly to useTableFiltering.

Typed props

PropType and behavior
initialStateTableFilterState
Optional initial filter state map.