Use inline sort/filter getters instead of mapping columns

Replace the satisfies-ColumnsType + .map(applyColumn) approach with inline tableState.sortOrder()/filteredValue() getters set per column, so the column arrays stay plain literals (no satisfies, no constraints; UsersTable stays any[]). Move useColumnSearch into util/ next to useTableQueryState.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 4.8 committed 2026-06-25 02:32:04 +00:00
1 parent 4e94092730
commit 8cacdc5112
12 files changed
+86 -67

No files matched your search

@@ -0,0 +1,49 @@
import React, { useRef } from "react";
import { Input, InputRef } from "antd";
import { SearchOutlined } from "@ant-design/icons";
import type { FilterDropdownProps } from "antd/es/table/interface";
/**
* Column props for a free-text search filter rendered in the column header
* dropdown (the same style as the Users table). Spread the result onto a
* column; the search value is persisted through the table's filter state by
* {@link useTableQueryState}.
*
* @param getText Returns the searchable text for a row.
* @param placeholder Placeholder shown in the search box.
*/
export default function useColumnSearch<T>(getText: (record: T) => string, placeholder = "Search") {
const searchInput = useRef<InputRef>(null);
return {
filterIcon: (filtered: boolean) => (
<SearchOutlined style={{ color: filtered ? "#1677ff" : undefined }} />
),
filterDropdown: ({ selectedKeys, setSelectedKeys, confirm, clearFilters }: FilterDropdownProps) => (
<div style={{ padding: 8, width: 240 }} onKeyDown={e => e.stopPropagation()}>
<Input.Search
ref={searchInput}
placeholder={placeholder}
value={selectedKeys[0] as string | undefined}
allowClear
onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
onSearch={() => confirm({ closeDropdown: true })}
onClear={() => {
clearFilters?.({ closeDropdown: false });
confirm({ closeDropdown: true });
}}
/>
</div>
),
filterDropdownProps: {
onOpenChange: (open: boolean) => {
if (open) {
setTimeout(() => searchInput.current?.select(), 100);
}
},
},
onFilter: (value: React.Key | boolean, record: T) => (
getText(record).toLowerCase().includes(String(value).toLowerCase())
),
};
}
+12 -18
View File
@@ -1,6 +1,6 @@
import { useSearchParams } from "react-router-dom";
import type { TablePaginationConfig } from "antd";
import type { ColumnType, FilterValue, SorterResult } from "antd/es/table/interface";
import type { FilterValue, SorterResult } from "antd/es/table/interface";
import {
applyFilters, applyPagination, applySort,
@@ -28,12 +28,12 @@ export interface TableQueryState<T> {
filters: Record<string, FilterValue | null>,
sorter: SorterResult<T> | SorterResult<T>[],
) => void;
/** Wrap each column to inject controlled `sortOrder`/`filteredValue` from the URL. */
applyColumn: (column: ColumnType<T>) => ColumnType<T>;
/** Controlled `sortOrder` for the column with this key (null unless it's the active sort). */
sortOrder: (columnKey: string) => SortOrder | null;
/** Controlled `filteredValue` for the column with this key (null when no filter is active). */
filteredValue: (columnKey: string) => string[] | null;
/** Controlled pagination ({@link current}/{@link pageSize}), or `false` when disabled. */
pagination: false | TablePaginationConfig;
/** The active column filters parsed from the URL, keyed by column key. */
filters: Record<string, string[]>;
}
/**
@@ -83,23 +83,17 @@ export default function useTableQueryState<T>(options: TableQueryStateOptions):
setParams(next);
},
applyColumn(column) {
const key = String(column.key ?? column.dataIndex ?? "");
const next: ColumnType<T> = { ...column };
if (column.sorter) {
next.sortOrder = key === sort.columnKey ? (sort.order ?? null) : null;
}
if (column.filters || column.onFilter || column.filterDropdown) {
const codec = codecs[key];
next.filteredValue = codec ? codec.decode(params, prefix) : (filters[key] ?? null);
}
return next;
sortOrder(columnKey) {
return columnKey === sort.columnKey ? (sort.order ?? null) : null;
},
filteredValue(columnKey) {
const codec = codecs[columnKey];
return codec ? codec.decode(params, prefix) : (filters[columnKey] ?? null);
},
pagination: paginationEnabled && page
? { current: page.page, pageSize: page.pageSize }
: false,
filters,
};
}