Skip to content

@xtandard/lib/filters/tanstack-table

TanStack Table bridge: convert a table’s columnFilters, globalFilter, sorting and pagination state to and from the filter model (ColumnFilter[] / FilterNode), Sort, page/offset pagination and cursors, through a per-column mapping spec. For server-side (manual*) tables synced to the URL. Zero dependencies; works with @tanstack/table-core v8 and v9.

import { … } from "@xtandard/lib/filters/tanstack-table";
Name Summary
arrayColumn A tags-style filter on an array column: the chosen values become arrayOverlaps (has any of them, like TanStack’s arrIncludesSome; the default) or arrayContains (has all of them, like arrIncludesAll).
booleanColumn A yes/no filter: a boolean becomes eq. Anything else (including undefined, the “any” state of a tri-state control) means “no filter”.
createTableBridge Bind a per-column mapping spec once and get converters between TanStack Table state and the filter model. The spec is plain data, so share it between the browser (table ⇄ URL) and the server (URL → FilterNode → adapter).
customColumn Your own mapping, for a value shape the builders don’t cover. toFilter receives the raw (unknown) table value; return null for “no filter”. fromFilter returns undefined for a filter the column can’t show. The table value type is inferred from fromFilter.
dateColumn A date preset picker: the value is a DatePresetValue (e.g. “this month”, or a between range with end) and becomes a date filter. With timeZone, the value may omit it (and fromFilter leaves it out again when it matches). The preset is resolved later by your resolveDate.
filterColumn An operator-picker column: the value is the whole filter minus kind ({ operator: "gte", value: 3 }, { operator: "isNull" }, …), so any operator of kind round-trips. The value is checked like the compact URL codec checks a leaf: an unknown operator or a wrong-typed argument means “no filter”.
fromPagination A page or offset PaginationInput (e.g. from parsePaginationParams) → TanStack pagination. An offset that isn’t a multiple of limit lands on the page that contains it.
fromSort The model’s Sort → TanStack sorting (the inverse of toSort).
multiSelectColumn A multi-select (TanStack’s arrIncludesSome on a scalar column): the chosen values become an inArray filter (or notInArray with operator) of kind (default enum). Values of the wrong type are skipped; an empty selection means “no filter”.
numberColumn A single number input: a number (or numeric string, as an <input> gives it) becomes a number filter with operator (default eq).
numberRangeColumn A min/max range (TanStack’s inNumberRange): [min, max] becomes between, [min, ] becomes gte, [, max] becomes lte. Bounds may be numbers or numeric strings; blank bounds are open, and swapped bounds are put in order (as inNumberRange does). [ , ] means “no filter”.
rememberCursor Record the endCursor of the page just fetched at pageIndex, so the next page can be requested. Cursors past it are dropped (they belonged to an older walk); on the last page (hasNextPage: false) nothing is added.
selectColumn A single-choice select (TanStack’s equals): the chosen value becomes an eq filter of kind (default enum; text and number work too).
tablePaginationOptions The pageCount / rowCount table options for a server-paginated table (manualPagination: true), from the response’s PageInfo. With a total (totalItems), both are exact; without one (cursor or no COUNT(*)), pageCount is “this page, plus one more while hasNextPage”, so getCanNextPage() follows the server.
textColumn A text input (TanStack’s includesString): a non-empty string becomes a text filter with operator (default contains). "" and non-strings mean “no filter”.
toCursorPagination Turn the table’s index-based pagination into a cursor request, using the cursors recorded so far (rememberCursor). The request always walks forward from the cursor of the page before. A pageIndex the stack can’t reach yet (a deep link, a jump) is clamped to the furthest known page and returned as pageIndex, so you can correct the table state.
toPagination TanStack pagination (0-based pageIndex) → a page (page is 1-based) or offset PaginationInput, for an API that takes ?page= / ?offset=.
toSort TanStack sorting → the model’s Sort (desc: true → dir: "desc"). Column ids pass through unchanged; use createTableBridge’s toSort to map ids to model fields.
Name Summary
AnyTableColumnSpec Any TableColumnSpec, whatever its value type and kind.
ArrayColumnOperator The array-column operators (arrayOverlaps = has any, arrayContains = has all).
ColumnKinds
DatePresetValue A date preset as a table value: the model’s DatePreset without kind; timeZone may be left out when the column has a default.
FieldOf
FieldOption
FilterOfKind The filter variant of one field kind (FilterOfKind<"number"> = NumberFilter).
InferColumnValue The table filter value type of one column spec.
InferTableFilterValues Column id → table filter value type, inferred from a columns map (what column.getFilterValue() holds for each column).
InferTableKinds Model field → kind, inferred from the columns map (a column’s field, or its id) plus the global-filter fields (text). This is the allow-list for compileFilters, createFilterUrlCodec and the adapters’ specs.
NumberRangeValue A [min, max] range value; either bound may be absent (open-ended).
NumberValueOperator The number operators that take one number value.
OperatorValue A filter of kind without its kind key: { operator, value }, { operator, from, to }, …
SelectKind The kinds a select / multi-select column can filter on.
SelectValue
Simplify
TableBridge What createTableBridge returns: the spec plus bound converters.
TableColumnFilter One entry of TanStack Table’s columnFilters state (ColumnFilter in table-core v8 and v9).
TableColumnFiltersState TanStack Table’s columnFilters state (ColumnFiltersState in table-core v8 and v9).
TableColumns A column id → TableColumnSpec map (the columns of createTableBridge).
TableColumnSort One entry of TanStack Table’s sorting state (ColumnSort in table-core v8 and v9).
TableColumnSpec How one table column’s filter value maps to the filter model and back. Built by the *Column builders (textColumn, numberRangeColumn, …) or customColumn.
TableCursors The cursors a cursor-paginated table has seen: cursors[i] is the after cursor that fetches page index i + 1 (page 0 needs none). Keep it in component state and reset it to [] whenever filters, sort or page size change.
TableGlobalFilterOptions The globalFilter option of createTableBridge.
TablePaginationState TanStack Table’s pagination state (PaginationState in table-core v8 and v9). pageIndex is 0-based.
TableSortingState TanStack Table’s sorting state (SortingState in table-core v8 and v9).
TextValueOperator The text operators that take one string value.
WithoutKind filter without its kind key — the operator-shaped table value.