import type { TableFeature } from "../core/table.js"; import { type BuiltInSortingFn } from "../sorting-fns.js"; import type { OnChangeFn, RowModel, Row, RowData, SortingFns, TableInstance, Updater } from "../types.js"; export type SortDirection = "asc" | "desc"; export interface ColumnSort { desc: boolean; id: string; } export type SortingState = ColumnSort[]; export interface SortingTableState { sorting: SortingState; } export interface SortingFn { (rowA: Row, rowB: Row, columnId: string): number; } export type CustomSortingFns = Record>; export type SortingFnOption = "auto" | BuiltInSortingFn | SortingFn; export interface SortingColumnDef { /** * Enables/Disables multi-sorting for this column. */ enableMultiSort?: boolean; /** * Enables/Disables sorting for this column. */ enableSorting?: boolean; /** * Inverts the order of the sorting for this column. This is useful for values * that have an inverted best/worst scale where lower numbers are better, e.g.,a * ranking (1st, 2nd, 3rd) or golf-like scoring */ invertSorting?: boolean; /** * Set to `true` for sorting toggles on this column to start in the descending * direction. */ sortDescFirst?: boolean; /** * The sorting function to use with this column. * - A `string` referencing a built-in sorting function * - A custom sorting function */ sortingFn?: SortingFnOption; /** * Configure how undefined values are sorted for this column. * * @option `false`: Undefined values will be considered tied and need to be sorted by the next column filter or original index (whichever applies). * @option `-1`: Undefined values will be sorted with higher priority (ascending) (if ascending, undefined will appear on the beginning of the list) * @option `1`: Undefined values will be sorted with lower priority (descending) (if ascending, undefined will appear on the end of the list) */ sortUndefined?: false | -1 | 1; } export interface SortingColumn { /** * Removes this column from the table's sorting state */ clearSorting: () => void; /** * Returns a sort direction automatically inferred based on the columns values. */ getAutoSortDir: () => SortDirection; /** * Returns a sorting function automatically inferred based on the columns values. */ getAutoSortingFn: () => SortingFn; /** * Returns whether this column can be multi-sorted. */ getCanMultiSort: () => boolean; /** * Returns whether this column can be sorted. */ getCanSort: () => boolean; /** * Returns the first direction that should be used when sorting this column. * * Useful for tooltips and aria-labels. */ getFirstSortDir: () => SortDirection; /** * Returns the current sort direction of this column. */ getIsSorted: () => false | SortDirection; /** * Returns the next sorting order. Useful for tooltips and aria-labels. */ getNextSortingOrder: () => SortDirection | false; /** * Returns the index position of this column's sorting within the sorting state */ getSortIndex: () => number; /** * Returns the resolved sorting function to be used for this column */ getSortingFn: () => SortingFn; /** * Returns a function that can be used to toggle this column's sorting state. This * is useful for attaching a click handler to the column header. */ getToggleSortingHandler: () => undefined | ((event: unknown) => void); /** * Toggles this columns sorting state. If `desc` is provided, it will force the * sort direction to that value. If `isMulti` is provided, it will additivity * multi-sort the column (or toggle it if it is already sorted). */ toggleSorting: (desc?: boolean, isMulti?: boolean) => void; } interface SortingOptionsBase { /** * Enables/disables the ability to remove multi-sorts */ enableMultiRemove?: boolean; /** * Enables/Disables multi-sorting for the table. */ enableMultiSort?: boolean; /** * Enables/Disables sorting for the table. */ enableSorting?: boolean; /** * Enables/Disables the ability to remove sorting for the table. * - If `true` then changing sort order will circle like: 'none' -> 'desc' -> * 'asc' -> 'none' -> ... - If `false` then changing sort order will circle like: * 'none' -> 'desc' -> 'asc' -> 'desc' -> 'asc' -> ... */ enableSortingRemoval?: boolean; /** * This function is used to retrieve the sorted row model. If using server-side * sorting, this function is not required. To use client-side sorting, pass the * exported `getSortedRowModel()` from your adapter to your table or implement * your own. */ getSortedRowModel?: ( /** @inheritDoc */ table: TableInstance) => () => RowModel; /** * Pass a custom function that will be used to determine if a multi-sort event * should be triggered. It is passed the event from the sort toggle handler and * should return `true` if the event should trigger a multi-sort. */ isMultiSortEvent?: (e: unknown) => boolean; /** * Enables manual sorting for the table. If this is `true`, you will be expected * to sort your data before it is passed to the table. This is useful if you are * doing server-side sorting. */ manualSorting?: boolean; /** * Set a maximum number of columns that can be multi-sorted. */ maxMultiSortColCount?: number; /** * If provided, this function will be called with an `updaterFn` when * `state.sorting` changes. This overrides the default internal state management, * so you will need to persist the state change either fully or partially outside * of the table. */ onSortingChange?: OnChangeFn; /** * If `true`, all sorts will default to descending as their first toggle state. */ sortDescFirst?: boolean; } type ResolvedSortingFns = keyof SortingFns extends never ? { sortingFns?: Record>; } : { sortingFns: Record>; }; export interface SortingOptions<_TData extends RowData> extends SortingOptionsBase, ResolvedSortingFns { } export interface SortingInstance { /** @internal */ _getSortedRowModel?: () => RowModel; /** * Returns the row model for the table before any sorting has been applied. */ getPreSortedRowModel: () => RowModel; /** * Returns the row model for the table after sorting has been applied. */ getSortedRowModel: () => RowModel; /** * Resets the `sorting` state to `initialState.sorting`, or `true` can be passed * to force a default blank state reset to `[]`. */ resetSorting: (defaultState?: boolean) => void; /** * Sets or updates the `state.sorting` state. */ setSorting: (updater: Updater) => void; } export declare const Sorting: TableFeature; export {}; //# sourceMappingURL=sorting.d.ts.map