@tanstack/table-core 9.0.0-beta.46 → 9.0.0-beta.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/features/column-filtering/columnFilteringFeature.types.d.cts +43 -3
- package/dist/features/column-filtering/columnFilteringFeature.types.d.ts +43 -3
- package/dist/features/column-filtering/columnFilteringFeature.utils.cjs +18 -9
- package/dist/features/column-filtering/columnFilteringFeature.utils.cjs.map +1 -1
- package/dist/features/column-filtering/columnFilteringFeature.utils.d.cts +6 -2
- package/dist/features/column-filtering/columnFilteringFeature.utils.d.ts +6 -2
- package/dist/features/column-filtering/columnFilteringFeature.utils.js +18 -9
- package/dist/features/column-filtering/columnFilteringFeature.utils.js.map +1 -1
- package/dist/features/column-filtering/createFilteredRowModel.cjs +7 -2
- package/dist/features/column-filtering/createFilteredRowModel.cjs.map +1 -1
- package/dist/features/column-filtering/createFilteredRowModel.d.cts +6 -2
- package/dist/features/column-filtering/createFilteredRowModel.d.ts +6 -2
- package/dist/features/column-filtering/createFilteredRowModel.js +7 -2
- package/dist/features/column-filtering/createFilteredRowModel.js.map +1 -1
- package/dist/features/column-grouping/columnGroupingFeature.types.d.cts +41 -3
- package/dist/features/column-grouping/columnGroupingFeature.types.d.ts +41 -3
- package/dist/features/column-grouping/columnGroupingFeature.utils.cjs +17 -3
- package/dist/features/column-grouping/columnGroupingFeature.utils.cjs.map +1 -1
- package/dist/features/column-grouping/columnGroupingFeature.utils.d.cts +5 -0
- package/dist/features/column-grouping/columnGroupingFeature.utils.d.ts +5 -0
- package/dist/features/column-grouping/columnGroupingFeature.utils.js +17 -3
- package/dist/features/column-grouping/columnGroupingFeature.utils.js.map +1 -1
- package/dist/features/column-grouping/createGroupedRowModel.cjs +6 -3
- package/dist/features/column-grouping/createGroupedRowModel.cjs.map +1 -1
- package/dist/features/column-grouping/createGroupedRowModel.d.cts +6 -3
- package/dist/features/column-grouping/createGroupedRowModel.d.ts +6 -3
- package/dist/features/column-grouping/createGroupedRowModel.js +6 -3
- package/dist/features/column-grouping/createGroupedRowModel.js.map +1 -1
- package/dist/features/global-filtering/globalFilteringFeature.utils.cjs +3 -1
- package/dist/features/global-filtering/globalFilteringFeature.utils.cjs.map +1 -1
- package/dist/features/global-filtering/globalFilteringFeature.utils.d.cts +2 -6
- package/dist/features/global-filtering/globalFilteringFeature.utils.d.ts +2 -6
- package/dist/features/global-filtering/globalFilteringFeature.utils.js +3 -1
- package/dist/features/global-filtering/globalFilteringFeature.utils.js.map +1 -1
- package/dist/features/row-sorting/createSortedRowModel.cjs +6 -2
- package/dist/features/row-sorting/createSortedRowModel.cjs.map +1 -1
- package/dist/features/row-sorting/createSortedRowModel.d.cts +6 -2
- package/dist/features/row-sorting/createSortedRowModel.d.ts +6 -2
- package/dist/features/row-sorting/createSortedRowModel.js +6 -2
- package/dist/features/row-sorting/createSortedRowModel.js.map +1 -1
- package/dist/features/row-sorting/rowSortingFeature.types.d.cts +31 -2
- package/dist/features/row-sorting/rowSortingFeature.types.d.ts +31 -2
- package/dist/features/row-sorting/rowSortingFeature.utils.cjs +19 -4
- package/dist/features/row-sorting/rowSortingFeature.utils.cjs.map +1 -1
- package/dist/features/row-sorting/rowSortingFeature.utils.js +19 -4
- package/dist/features/row-sorting/rowSortingFeature.utils.js.map +1 -1
- package/dist/fns/aggregationFns.cjs +128 -57
- package/dist/fns/aggregationFns.cjs.map +1 -1
- package/dist/fns/aggregationFns.d.cts +72 -18
- package/dist/fns/aggregationFns.d.ts +72 -18
- package/dist/fns/aggregationFns.js +126 -58
- package/dist/fns/aggregationFns.js.map +1 -1
- package/dist/fns/filterFns.cjs +253 -96
- package/dist/fns/filterFns.cjs.map +1 -1
- package/dist/fns/filterFns.d.cts +126 -94
- package/dist/fns/filterFns.d.ts +126 -94
- package/dist/fns/filterFns.js +246 -97
- package/dist/fns/filterFns.js.map +1 -1
- package/dist/fns/sortFns.cjs +68 -21
- package/dist/fns/sortFns.cjs.map +1 -1
- package/dist/fns/sortFns.d.cts +48 -15
- package/dist/fns/sortFns.d.ts +48 -15
- package/dist/fns/sortFns.js +68 -22
- package/dist/fns/sortFns.js.map +1 -1
- package/dist/helpers/tableFeatures.cjs +5 -4
- package/dist/helpers/tableFeatures.cjs.map +1 -1
- package/dist/helpers/tableFeatures.d.cts +5 -4
- package/dist/helpers/tableFeatures.d.ts +5 -4
- package/dist/helpers/tableFeatures.js +5 -4
- package/dist/helpers/tableFeatures.js.map +1 -1
- package/dist/index.cjs +12 -0
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +4 -4
- package/dist/types/TableFeatures.d.cts +15 -7
- package/dist/types/TableFeatures.d.ts +15 -7
- package/dist/types/type-utils.d.cts +8 -1
- package/dist/types/type-utils.d.ts +8 -1
- package/package.json +1 -1
- package/skills/api-not-found/SKILL.md +1 -1
- package/skills/client-vs-server/SKILL.md +1 -1
- package/skills/column-faceting/SKILL.md +11 -5
- package/skills/column-filtering/SKILL.md +8 -3
- package/skills/column-ordering/SKILL.md +1 -1
- package/skills/column-pinning/SKILL.md +1 -1
- package/skills/column-resizing/SKILL.md +1 -1
- package/skills/column-sizing/SKILL.md +1 -1
- package/skills/column-visibility/SKILL.md +1 -1
- package/skills/core/SKILL.md +1 -1
- package/skills/custom-features/SKILL.md +1 -1
- package/skills/expanding/SKILL.md +1 -1
- package/skills/global-filtering/SKILL.md +3 -3
- package/skills/grouping/SKILL.md +8 -3
- package/skills/migrate-v8-to-v9/SKILL.md +7 -7
- package/skills/pagination/SKILL.md +1 -1
- package/skills/row-pinning/SKILL.md +1 -1
- package/skills/row-selection/SKILL.md +1 -1
- package/skills/sorting/SKILL.md +9 -3
- package/skills/table-features/SKILL.md +16 -13
- package/skills/typescript/SKILL.md +1 -1
- package/src/features/column-filtering/columnFilteringFeature.types.ts +60 -0
- package/src/features/column-filtering/columnFilteringFeature.utils.ts +33 -19
- package/src/features/column-filtering/createFilteredRowModel.ts +13 -3
- package/src/features/column-grouping/columnGroupingFeature.types.ts +63 -8
- package/src/features/column-grouping/columnGroupingFeature.utils.ts +43 -9
- package/src/features/column-grouping/createGroupedRowModel.ts +6 -3
- package/src/features/global-filtering/globalFilteringFeature.utils.ts +8 -0
- package/src/features/row-sorting/createSortedRowModel.ts +6 -2
- package/src/features/row-sorting/rowSortingFeature.types.ts +53 -1
- package/src/features/row-sorting/rowSortingFeature.utils.ts +45 -13
- package/src/fns/aggregationFns.ts +204 -146
- package/src/fns/filterFns.ts +361 -283
- package/src/fns/sortFns.ts +95 -78
- package/src/helpers/tableFeatures.ts +5 -4
- package/src/types/TableFeatures.ts +15 -7
- package/src/types/type-utils.ts +8 -0
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CellData, RowData } from "../../types/type-utils.js";
|
|
2
|
-
import {
|
|
3
|
-
import { FilterFn } from "../column-filtering/columnFilteringFeature.types.js";
|
|
2
|
+
import { CreatedFilterFn, FilterFn } from "../column-filtering/columnFilteringFeature.types.js";
|
|
4
3
|
import { Table } from "../../types/Table.js";
|
|
5
4
|
import { Column } from "../../types/Column.js";
|
|
6
5
|
import { TableFeatures } from "../../types/TableFeatures.js";
|
|
@@ -28,10 +27,7 @@ declare function column_getCanGlobalFilter<TFeatures extends TableFeatures, TDat
|
|
|
28
27
|
* const filterFn = table_getGlobalAutoFilterFn()
|
|
29
28
|
* ```
|
|
30
29
|
*/
|
|
31
|
-
declare function table_getGlobalAutoFilterFn():
|
|
32
|
-
autoRemove: (val: any) => boolean;
|
|
33
|
-
resolveFilterValue: (val: any) => string;
|
|
34
|
-
};
|
|
30
|
+
declare function table_getGlobalAutoFilterFn(): CreatedFilterFn<any, any>;
|
|
35
31
|
/**
|
|
36
32
|
* Resolves the filter function used for global filtering.
|
|
37
33
|
*
|
|
@@ -45,7 +45,9 @@ function table_getGlobalAutoFilterFn() {
|
|
|
45
45
|
function table_getGlobalFilterFn(table) {
|
|
46
46
|
const { globalFilterFn } = table.options;
|
|
47
47
|
const filterFns = table._rowModelFns.filterFns;
|
|
48
|
-
|
|
48
|
+
const filterFn = isFunction(globalFilterFn) ? globalFilterFn : globalFilterFn === "auto" ? table_getGlobalAutoFilterFn() : filterFns?.[globalFilterFn];
|
|
49
|
+
if (process.env.NODE_ENV === "development" && !filterFn && globalFilterFn != null) console.warn(`globalFilterFn '${String(globalFilterFn)}' is not registered`);
|
|
50
|
+
return filterFn;
|
|
49
51
|
}
|
|
50
52
|
/**
|
|
51
53
|
* Routes a global filter updater through the table's global filter handler.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"globalFilteringFeature.utils.js","names":[],"sources":["../../../src/features/global-filtering/globalFilteringFeature.utils.ts"],"sourcesContent":["import { filterFn_includesString } from '../../fns/filterFns'\nimport { cloneState, isFunction } from '../../utils'\nimport type { Column_Internal } from '../../types/Column'\nimport type { FilterFn } from '../column-filtering/columnFilteringFeature.types'\nimport type { CellData, RowData } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Table_Internal } from '../../types/Table'\n\n/**\n * Checks whether this accessor column participates in global filtering.\n *\n * The column must have an accessor and pass column-level, table-level, and\n * optional `getColumnCanGlobalFilter` checks.\n *\n * @example\n * ```ts\n * const canGlobalFilter = column_getCanGlobalFilter(column)\n * ```\n */\nexport function column_getCanGlobalFilter<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): boolean {\n return (\n (column.columnDef.enableGlobalFilter ?? true) &&\n (column.table.options.enableGlobalFilter ?? true) &&\n (column.table.options.enableFilters ?? true) &&\n (column.table.options.getColumnCanGlobalFilter?.(column as any) ?? true) &&\n !!column.accessorFn\n )\n}\n\n/**\n * Provides the built-in automatic global filter function.\n *\n * Global filtering defaults to `includesString`, which gives search-box style\n * matching across globally filterable columns.\n *\n * @example\n * ```ts\n * const filterFn = table_getGlobalAutoFilterFn()\n * ```\n */\nexport function table_getGlobalAutoFilterFn() {\n return filterFn_includesString\n}\n\n/**\n * Resolves the filter function used for global filtering.\n *\n * Function-valued `options.globalFilterFn` is returned directly, `'auto'`\n * delegates to `table_getGlobalAutoFilterFn`, and string values are looked up in\n * the table's filter function registry.\n *\n * @example\n * ```ts\n * const filterFn = table_getGlobalFilterFn(table)\n * ```\n */\nexport function table_getGlobalFilterFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): FilterFn<TFeatures, TData> | undefined {\n const { globalFilterFn: globalFilterFn } = table.options\n const filterFns: Record<string, FilterFn<TFeatures, TData>> | undefined =\n table._rowModelFns.filterFns\n\n const filterFn = isFunction(globalFilterFn)\n ? globalFilterFn\n : globalFilterFn === 'auto'\n ? table_getGlobalAutoFilterFn()\n : filterFns?.[globalFilterFn as string]\n\n return filterFn\n}\n\n/**\n * Routes a global filter updater through the table's global filter handler.\n *\n * The updater may be a next value or a function of the previous value, matching\n * the instance `table.setGlobalFilter` behavior.\n *\n * @example\n * ```ts\n * table_setGlobalFilter(table, 'search text')\n * ```\n */\nexport function table_setGlobalFilter<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, updater: any) {\n table.options.onGlobalFilterChange?.(updater)\n}\n\n/**\n * Resets `globalFilter` to the configured initial state or feature default.\n *\n * With no argument, the reset clones `table.initialState.globalFilter`. Passing\n * `true` ignores initial state and resets to `undefined`.\n *\n * @example\n * ```ts\n * table_resetGlobalFilter(table)\n * table_resetGlobalFilter(table, true)\n * ```\n */\nexport function table_resetGlobalFilter<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, defaultState?: boolean) {\n table_setGlobalFilter(\n table,\n defaultState ? undefined : cloneState(table.initialState.globalFilter),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;AAmBA,SAAgB,0BAId,QAA4D;CAC5D,QACG,OAAO,UAAU,sBAAsB,UACvC,OAAO,MAAM,QAAQ,sBAAsB,UAC3C,OAAO,MAAM,QAAQ,iBAAiB,UACtC,OAAO,MAAM,QAAQ,2BAA2B,MAAa,KAAK,SACnE,CAAC,CAAC,OAAO;AAEb;;;;;;;;;;;;AAaA,SAAgB,8BAA8B;CAC5C,OAAO;AACT;;;;;;;;;;;;;AAcA,SAAgB,wBAId,OACwC;CACxC,MAAM,EAAkB,mBAAmB,MAAM;CACjD,MAAM,YACJ,MAAM,aAAa;
|
|
1
|
+
{"version":3,"file":"globalFilteringFeature.utils.js","names":[],"sources":["../../../src/features/global-filtering/globalFilteringFeature.utils.ts"],"sourcesContent":["import { filterFn_includesString } from '../../fns/filterFns'\nimport { cloneState, isFunction } from '../../utils'\nimport type { Column_Internal } from '../../types/Column'\nimport type { FilterFn } from '../column-filtering/columnFilteringFeature.types'\nimport type { CellData, RowData } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Table_Internal } from '../../types/Table'\n\n/**\n * Checks whether this accessor column participates in global filtering.\n *\n * The column must have an accessor and pass column-level, table-level, and\n * optional `getColumnCanGlobalFilter` checks.\n *\n * @example\n * ```ts\n * const canGlobalFilter = column_getCanGlobalFilter(column)\n * ```\n */\nexport function column_getCanGlobalFilter<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): boolean {\n return (\n (column.columnDef.enableGlobalFilter ?? true) &&\n (column.table.options.enableGlobalFilter ?? true) &&\n (column.table.options.enableFilters ?? true) &&\n (column.table.options.getColumnCanGlobalFilter?.(column as any) ?? true) &&\n !!column.accessorFn\n )\n}\n\n/**\n * Provides the built-in automatic global filter function.\n *\n * Global filtering defaults to `includesString`, which gives search-box style\n * matching across globally filterable columns.\n *\n * @example\n * ```ts\n * const filterFn = table_getGlobalAutoFilterFn()\n * ```\n */\nexport function table_getGlobalAutoFilterFn() {\n return filterFn_includesString\n}\n\n/**\n * Resolves the filter function used for global filtering.\n *\n * Function-valued `options.globalFilterFn` is returned directly, `'auto'`\n * delegates to `table_getGlobalAutoFilterFn`, and string values are looked up in\n * the table's filter function registry.\n *\n * @example\n * ```ts\n * const filterFn = table_getGlobalFilterFn(table)\n * ```\n */\nexport function table_getGlobalFilterFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(\n table: Table_Internal<TFeatures, TData>,\n): FilterFn<TFeatures, TData> | undefined {\n const { globalFilterFn: globalFilterFn } = table.options\n const filterFns: Record<string, FilterFn<TFeatures, TData>> | undefined =\n table._rowModelFns.filterFns\n\n const filterFn = isFunction(globalFilterFn)\n ? globalFilterFn\n : globalFilterFn === 'auto'\n ? table_getGlobalAutoFilterFn()\n : filterFns?.[globalFilterFn as string]\n\n if (\n process.env.NODE_ENV === 'development' &&\n !filterFn &&\n globalFilterFn != null\n ) {\n console.warn(`globalFilterFn '${String(globalFilterFn)}' is not registered`)\n }\n\n return filterFn\n}\n\n/**\n * Routes a global filter updater through the table's global filter handler.\n *\n * The updater may be a next value or a function of the previous value, matching\n * the instance `table.setGlobalFilter` behavior.\n *\n * @example\n * ```ts\n * table_setGlobalFilter(table, 'search text')\n * ```\n */\nexport function table_setGlobalFilter<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, updater: any) {\n table.options.onGlobalFilterChange?.(updater)\n}\n\n/**\n * Resets `globalFilter` to the configured initial state or feature default.\n *\n * With no argument, the reset clones `table.initialState.globalFilter`. Passing\n * `true` ignores initial state and resets to `undefined`.\n *\n * @example\n * ```ts\n * table_resetGlobalFilter(table)\n * table_resetGlobalFilter(table, true)\n * ```\n */\nexport function table_resetGlobalFilter<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, defaultState?: boolean) {\n table_setGlobalFilter(\n table,\n defaultState ? undefined : cloneState(table.initialState.globalFilter),\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;AAmBA,SAAgB,0BAId,QAA4D;CAC5D,QACG,OAAO,UAAU,sBAAsB,UACvC,OAAO,MAAM,QAAQ,sBAAsB,UAC3C,OAAO,MAAM,QAAQ,iBAAiB,UACtC,OAAO,MAAM,QAAQ,2BAA2B,MAAa,KAAK,SACnE,CAAC,CAAC,OAAO;AAEb;;;;;;;;;;;;AAaA,SAAgB,8BAA8B;CAC5C,OAAO;AACT;;;;;;;;;;;;;AAcA,SAAgB,wBAId,OACwC;CACxC,MAAM,EAAkB,mBAAmB,MAAM;CACjD,MAAM,YACJ,MAAM,aAAa;CAErB,MAAM,WAAW,WAAW,cAAc,IACtC,iBACA,mBAAmB,SACjB,4BAA4B,IAC5B,YAAY;CAElB,IACE,QAAQ,IAAI,aAAa,iBACzB,CAAC,YACD,kBAAkB,MAElB,QAAQ,KAAK,mBAAmB,OAAO,cAAc,EAAE,oBAAoB;CAG7E,OAAO;AACT;;;;;;;;;;;;AAaA,SAAgB,sBAGd,OAAyC,SAAc;CACvD,MAAM,QAAQ,uBAAuB,OAAO;AAC9C;;;;;;;;;;;;;AAcA,SAAgB,wBAGd,OAAyC,cAAwB;CACjE,sBACE,OACA,eAAe,SAAY,WAAW,MAAM,aAAa,YAAY,CACvE;AACF"}
|
|
@@ -8,8 +8,12 @@ const require_rowSortingFeature_utils = require('./rowSortingFeature.utils.cjs')
|
|
|
8
8
|
*
|
|
9
9
|
* The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.
|
|
10
10
|
*
|
|
11
|
-
* Register sorting functions with the `sortFns` slot on the
|
|
12
|
-
* `
|
|
11
|
+
* Register the sorting functions you use with the `sortFns` slot on the
|
|
12
|
+
* `features` option:
|
|
13
|
+
* `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric } })`.
|
|
14
|
+
* Importing individual `sortFn_*` functions keeps unused built-ins out of
|
|
15
|
+
* your bundle; sorting functions passed directly to the `sortFn` column
|
|
16
|
+
* option need no registration at all.
|
|
13
17
|
*/
|
|
14
18
|
function createSortedRowModel() {
|
|
15
19
|
return (_table) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSortedRowModel.cjs","names":["tableMemo","table_autoResetPageIndex","column_getCanSort","column_getSortFn"],"sources":["../../../src/features/row-sorting/createSortedRowModel.ts"],"sourcesContent":["import { copyInstancePropertiesWithoutMemos, tableMemo } from '../../utils'\nimport { table_autoResetPageIndex } from '../row-pagination/rowPaginationFeature.utils'\nimport { column_getCanSort, column_getSortFn } from './rowSortingFeature.utils'\nimport type { Column_Internal } from '../../types/Column'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'\nimport type { Table, Table_Internal } from '../../types/Table'\nimport type { Row } from '../../types/Row'\nimport type { SortFn } from './rowSortingFeature.types'\nimport type { RowData } from '../../types/type-utils'\n\n/**\n * Creates a memoized sorted row model factory.\n *\n * The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.\n *\n * Register sorting functions with the `sortFns` slot on the `features` option:\n * `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns })`.\n */\nexport function createSortedRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(): (table: Table<TFeatures, TData>) => () => RowModel<TFeatures, TData> {\n return (_table) => {\n const table = _table as unknown as Table_Internal<TFeatures, TData>\n return tableMemo({\n feature: 'rowSortingFeature',\n table,\n fnName: 'table.getSortedRowModel',\n memoDeps: () => [\n table.atoms.sorting?.get(),\n table.getPreSortedRowModel(),\n ],\n fn: () => _createSortedRowModel(table),\n onAfterUpdate: () => table_autoResetPageIndex(table),\n })\n }\n}\n\nfunction _createSortedRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData = any,\n>(table: Table_Internal<TFeatures, TData>): RowModel<TFeatures, TData> {\n const preSortedRowModel = table.getPreSortedRowModel()\n const sorting = table.atoms.sorting?.get()\n\n if (!preSortedRowModel.rows.length || !sorting?.length) {\n return preSortedRowModel\n }\n\n const sortedFlatRows: Array<Row<TFeatures, TData>> = []\n\n // Filter out sortings that correspond to non existing columns\n const availableSorting = sorting.filter((sort) => {\n const column = table.getColumn(sort.id)\n return column ? column_getCanSort(column) : false\n })\n\n if (!availableSorting.length) {\n return preSortedRowModel\n }\n\n const resolvedSorting: Array<{\n id: string\n desc?: boolean\n sortUndefined?: false | -1 | 1 | 'first' | 'last'\n invertSorting?: boolean\n sortFn: SortFn<TFeatures, TData>\n }> = []\n\n for (let i = 0; i < availableSorting.length; i++) {\n const sortEntry = availableSorting[i]!\n const column: Column_Internal<TFeatures, TData> | undefined =\n table.getColumn(sortEntry.id)\n if (!column) {\n continue\n }\n\n resolvedSorting.push({\n id: sortEntry.id,\n desc: sortEntry.desc,\n sortUndefined: column.columnDef.sortUndefined,\n invertSorting: column.columnDef.invertSorting,\n sortFn: column_getSortFn(column),\n })\n }\n\n const compareRows = (\n rowA: Row<TFeatures, TData>,\n rowB: Row<TFeatures, TData>,\n ) => {\n for (let i = 0; i < resolvedSorting.length; i++) {\n const sortEntry = resolvedSorting[i]!\n const sortUndefined = sortEntry.sortUndefined\n const isDesc = sortEntry.desc\n\n let sortInt = 0\n\n // All sorting ints should always return in ascending order\n if (sortUndefined) {\n const aValue = rowA.getValue(sortEntry.id)\n const bValue = rowB.getValue(sortEntry.id)\n\n const aUndefined = aValue === undefined\n const bUndefined = bValue === undefined\n\n if (aUndefined && bUndefined) {\n continue\n }\n\n if (aUndefined || bUndefined) {\n if (sortUndefined === 'first') return aUndefined ? -1 : 1\n if (sortUndefined === 'last') return aUndefined ? 1 : -1\n sortInt = aUndefined ? sortUndefined : -sortUndefined\n }\n }\n\n if (sortInt === 0) {\n sortInt = sortEntry.sortFn(rowA, rowB, sortEntry.id)\n }\n\n // If sorting is non-zero, take care of desc and inversion\n if (sortInt !== 0) {\n if (isDesc) {\n sortInt *= -1\n }\n\n if (sortEntry.invertSorting) {\n sortInt *= -1\n }\n\n return sortInt\n }\n }\n\n return rowA.index - rowB.index\n }\n\n const sortData = (\n rows: Array<Row<TFeatures, TData>>,\n ): {\n rows: Array<Row<TFeatures, TData>>\n changed: boolean\n } => {\n const sortedData = rows.slice()\n\n sortedData.sort(compareRows)\n let changed = false\n\n // If there are sub-rows, sort them. Clone only rows that need mutation\n // (i.e. have subRows) so we don't corrupt the source row model.\n for (let i = 0; i < sortedData.length; i++) {\n const row = sortedData[i]!\n if (row !== rows[i]) {\n changed = true\n }\n\n if (row.subRows.length) {\n const sortedSubRows = sortData(row.subRows)\n\n if (sortedSubRows.changed) {\n // Preserve prototype chain so methods like getValue() remain accessible\n const cloned = Object.create(Object.getPrototypeOf(row))\n copyInstancePropertiesWithoutMemos(cloned, row)\n cloned.subRows = sortedSubRows.rows\n sortedData[i] = cloned\n sortedFlatRows.push(cloned)\n changed = true\n } else {\n sortedFlatRows.push(row)\n }\n } else {\n sortedFlatRows.push(row)\n }\n }\n\n return { rows: sortedData, changed }\n }\n\n return {\n rows: sortData(preSortedRowModel.rows).rows,\n flatRows: sortedFlatRows,\n rowsById: preSortedRowModel.rowsById,\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"createSortedRowModel.cjs","names":["tableMemo","table_autoResetPageIndex","column_getCanSort","column_getSortFn"],"sources":["../../../src/features/row-sorting/createSortedRowModel.ts"],"sourcesContent":["import { copyInstancePropertiesWithoutMemos, tableMemo } from '../../utils'\nimport { table_autoResetPageIndex } from '../row-pagination/rowPaginationFeature.utils'\nimport { column_getCanSort, column_getSortFn } from './rowSortingFeature.utils'\nimport type { Column_Internal } from '../../types/Column'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'\nimport type { Table, Table_Internal } from '../../types/Table'\nimport type { Row } from '../../types/Row'\nimport type { SortFn } from './rowSortingFeature.types'\nimport type { RowData } from '../../types/type-utils'\n\n/**\n * Creates a memoized sorted row model factory.\n *\n * The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.\n *\n * Register the sorting functions you use with the `sortFns` slot on the\n * `features` option:\n * `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric } })`.\n * Importing individual `sortFn_*` functions keeps unused built-ins out of\n * your bundle; sorting functions passed directly to the `sortFn` column\n * option need no registration at all.\n */\nexport function createSortedRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(): (table: Table<TFeatures, TData>) => () => RowModel<TFeatures, TData> {\n return (_table) => {\n const table = _table as unknown as Table_Internal<TFeatures, TData>\n return tableMemo({\n feature: 'rowSortingFeature',\n table,\n fnName: 'table.getSortedRowModel',\n memoDeps: () => [\n table.atoms.sorting?.get(),\n table.getPreSortedRowModel(),\n ],\n fn: () => _createSortedRowModel(table),\n onAfterUpdate: () => table_autoResetPageIndex(table),\n })\n }\n}\n\nfunction _createSortedRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData = any,\n>(table: Table_Internal<TFeatures, TData>): RowModel<TFeatures, TData> {\n const preSortedRowModel = table.getPreSortedRowModel()\n const sorting = table.atoms.sorting?.get()\n\n if (!preSortedRowModel.rows.length || !sorting?.length) {\n return preSortedRowModel\n }\n\n const sortedFlatRows: Array<Row<TFeatures, TData>> = []\n\n // Filter out sortings that correspond to non existing columns\n const availableSorting = sorting.filter((sort) => {\n const column = table.getColumn(sort.id)\n return column ? column_getCanSort(column) : false\n })\n\n if (!availableSorting.length) {\n return preSortedRowModel\n }\n\n const resolvedSorting: Array<{\n id: string\n desc?: boolean\n sortUndefined?: false | -1 | 1 | 'first' | 'last'\n invertSorting?: boolean\n sortFn: SortFn<TFeatures, TData>\n }> = []\n\n for (let i = 0; i < availableSorting.length; i++) {\n const sortEntry = availableSorting[i]!\n const column: Column_Internal<TFeatures, TData> | undefined =\n table.getColumn(sortEntry.id)\n if (!column) {\n continue\n }\n\n resolvedSorting.push({\n id: sortEntry.id,\n desc: sortEntry.desc,\n sortUndefined: column.columnDef.sortUndefined,\n invertSorting: column.columnDef.invertSorting,\n sortFn: column_getSortFn(column),\n })\n }\n\n const compareRows = (\n rowA: Row<TFeatures, TData>,\n rowB: Row<TFeatures, TData>,\n ) => {\n for (let i = 0; i < resolvedSorting.length; i++) {\n const sortEntry = resolvedSorting[i]!\n const sortUndefined = sortEntry.sortUndefined\n const isDesc = sortEntry.desc\n\n let sortInt = 0\n\n // All sorting ints should always return in ascending order\n if (sortUndefined) {\n const aValue = rowA.getValue(sortEntry.id)\n const bValue = rowB.getValue(sortEntry.id)\n\n const aUndefined = aValue === undefined\n const bUndefined = bValue === undefined\n\n if (aUndefined && bUndefined) {\n continue\n }\n\n if (aUndefined || bUndefined) {\n if (sortUndefined === 'first') return aUndefined ? -1 : 1\n if (sortUndefined === 'last') return aUndefined ? 1 : -1\n sortInt = aUndefined ? sortUndefined : -sortUndefined\n }\n }\n\n if (sortInt === 0) {\n sortInt = sortEntry.sortFn(rowA, rowB, sortEntry.id)\n }\n\n // If sorting is non-zero, take care of desc and inversion\n if (sortInt !== 0) {\n if (isDesc) {\n sortInt *= -1\n }\n\n if (sortEntry.invertSorting) {\n sortInt *= -1\n }\n\n return sortInt\n }\n }\n\n return rowA.index - rowB.index\n }\n\n const sortData = (\n rows: Array<Row<TFeatures, TData>>,\n ): {\n rows: Array<Row<TFeatures, TData>>\n changed: boolean\n } => {\n const sortedData = rows.slice()\n\n sortedData.sort(compareRows)\n let changed = false\n\n // If there are sub-rows, sort them. Clone only rows that need mutation\n // (i.e. have subRows) so we don't corrupt the source row model.\n for (let i = 0; i < sortedData.length; i++) {\n const row = sortedData[i]!\n if (row !== rows[i]) {\n changed = true\n }\n\n if (row.subRows.length) {\n const sortedSubRows = sortData(row.subRows)\n\n if (sortedSubRows.changed) {\n // Preserve prototype chain so methods like getValue() remain accessible\n const cloned = Object.create(Object.getPrototypeOf(row))\n copyInstancePropertiesWithoutMemos(cloned, row)\n cloned.subRows = sortedSubRows.rows\n sortedData[i] = cloned\n sortedFlatRows.push(cloned)\n changed = true\n } else {\n sortedFlatRows.push(row)\n }\n } else {\n sortedFlatRows.push(row)\n }\n }\n\n return { rows: sortedData, changed }\n }\n\n return {\n rows: sortData(preSortedRowModel.rows).rows,\n flatRows: sortedFlatRows,\n rowsById: preSortedRowModel.rowsById,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAuBA,SAAgB,uBAG0D;CACxE,QAAQ,WAAW;EACjB,MAAM,QAAQ;EACd,OAAOA,wBAAU;GACf,SAAS;GACT;GACA,QAAQ;GACR,gBAAgB,CACd,MAAM,MAAM,SAAS,IAAI,GACzB,MAAM,qBAAqB,CAC7B;GACA,UAAU,sBAAsB,KAAK;GACrC,qBAAqBC,4DAAyB,KAAK;EACrD,CAAC;CACH;AACF;AAEA,SAAS,sBAGP,OAAqE;CACrE,MAAM,oBAAoB,MAAM,qBAAqB;CACrD,MAAM,UAAU,MAAM,MAAM,SAAS,IAAI;CAEzC,IAAI,CAAC,kBAAkB,KAAK,UAAU,CAAC,SAAS,QAC9C,OAAO;CAGT,MAAM,iBAA+C,CAAC;CAGtD,MAAM,mBAAmB,QAAQ,QAAQ,SAAS;EAChD,MAAM,SAAS,MAAM,UAAU,KAAK,EAAE;EACtC,OAAO,SAASC,kDAAkB,MAAM,IAAI;CAC9C,CAAC;CAED,IAAI,CAAC,iBAAiB,QACpB,OAAO;CAGT,MAAM,kBAMD,CAAC;CAEN,KAAK,IAAI,IAAI,GAAG,IAAI,iBAAiB,QAAQ,KAAK;EAChD,MAAM,YAAY,iBAAiB;EACnC,MAAM,SACJ,MAAM,UAAU,UAAU,EAAE;EAC9B,IAAI,CAAC,QACH;EAGF,gBAAgB,KAAK;GACnB,IAAI,UAAU;GACd,MAAM,UAAU;GAChB,eAAe,OAAO,UAAU;GAChC,eAAe,OAAO,UAAU;GAChC,QAAQC,iDAAiB,MAAM;EACjC,CAAC;CACH;CAEA,MAAM,eACJ,MACA,SACG;EACH,KAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,QAAQ,KAAK;GAC/C,MAAM,YAAY,gBAAgB;GAClC,MAAM,gBAA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C;EACvC,UAAU;EACV,UAAU,kBAAkB;CAC9B;AACF"}
|
|
@@ -9,8 +9,12 @@ import { TableFeatures } from "../../types/TableFeatures.cjs";
|
|
|
9
9
|
*
|
|
10
10
|
* The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.
|
|
11
11
|
*
|
|
12
|
-
* Register sorting functions with the `sortFns` slot on the
|
|
13
|
-
* `
|
|
12
|
+
* Register the sorting functions you use with the `sortFns` slot on the
|
|
13
|
+
* `features` option:
|
|
14
|
+
* `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric } })`.
|
|
15
|
+
* Importing individual `sortFn_*` functions keeps unused built-ins out of
|
|
16
|
+
* your bundle; sorting functions passed directly to the `sortFn` column
|
|
17
|
+
* option need no registration at all.
|
|
14
18
|
*/
|
|
15
19
|
declare function createSortedRowModel<TFeatures extends TableFeatures, TData extends RowData>(): (table: Table<TFeatures, TData>) => () => RowModel<TFeatures, TData>;
|
|
16
20
|
//#endregion
|
|
@@ -9,8 +9,12 @@ import { TableFeatures } from "../../types/TableFeatures.js";
|
|
|
9
9
|
*
|
|
10
10
|
* The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.
|
|
11
11
|
*
|
|
12
|
-
* Register sorting functions with the `sortFns` slot on the
|
|
13
|
-
* `
|
|
12
|
+
* Register the sorting functions you use with the `sortFns` slot on the
|
|
13
|
+
* `features` option:
|
|
14
|
+
* `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric } })`.
|
|
15
|
+
* Importing individual `sortFn_*` functions keeps unused built-ins out of
|
|
16
|
+
* your bundle; sorting functions passed directly to the `sortFn` column
|
|
17
|
+
* option need no registration at all.
|
|
14
18
|
*/
|
|
15
19
|
declare function createSortedRowModel<TFeatures extends TableFeatures, TData extends RowData>(): (table: Table<TFeatures, TData>) => () => RowModel<TFeatures, TData>;
|
|
16
20
|
//#endregion
|
|
@@ -8,8 +8,12 @@ import { column_getCanSort, column_getSortFn } from "./rowSortingFeature.utils.j
|
|
|
8
8
|
*
|
|
9
9
|
* The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.
|
|
10
10
|
*
|
|
11
|
-
* Register sorting functions with the `sortFns` slot on the
|
|
12
|
-
* `
|
|
11
|
+
* Register the sorting functions you use with the `sortFns` slot on the
|
|
12
|
+
* `features` option:
|
|
13
|
+
* `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric } })`.
|
|
14
|
+
* Importing individual `sortFn_*` functions keeps unused built-ins out of
|
|
15
|
+
* your bundle; sorting functions passed directly to the `sortFn` column
|
|
16
|
+
* option need no registration at all.
|
|
13
17
|
*/
|
|
14
18
|
function createSortedRowModel() {
|
|
15
19
|
return (_table) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSortedRowModel.js","names":[],"sources":["../../../src/features/row-sorting/createSortedRowModel.ts"],"sourcesContent":["import { copyInstancePropertiesWithoutMemos, tableMemo } from '../../utils'\nimport { table_autoResetPageIndex } from '../row-pagination/rowPaginationFeature.utils'\nimport { column_getCanSort, column_getSortFn } from './rowSortingFeature.utils'\nimport type { Column_Internal } from '../../types/Column'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'\nimport type { Table, Table_Internal } from '../../types/Table'\nimport type { Row } from '../../types/Row'\nimport type { SortFn } from './rowSortingFeature.types'\nimport type { RowData } from '../../types/type-utils'\n\n/**\n * Creates a memoized sorted row model factory.\n *\n * The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.\n *\n * Register sorting functions with the `sortFns` slot on the `features` option:\n * `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns })`.\n */\nexport function createSortedRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(): (table: Table<TFeatures, TData>) => () => RowModel<TFeatures, TData> {\n return (_table) => {\n const table = _table as unknown as Table_Internal<TFeatures, TData>\n return tableMemo({\n feature: 'rowSortingFeature',\n table,\n fnName: 'table.getSortedRowModel',\n memoDeps: () => [\n table.atoms.sorting?.get(),\n table.getPreSortedRowModel(),\n ],\n fn: () => _createSortedRowModel(table),\n onAfterUpdate: () => table_autoResetPageIndex(table),\n })\n }\n}\n\nfunction _createSortedRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData = any,\n>(table: Table_Internal<TFeatures, TData>): RowModel<TFeatures, TData> {\n const preSortedRowModel = table.getPreSortedRowModel()\n const sorting = table.atoms.sorting?.get()\n\n if (!preSortedRowModel.rows.length || !sorting?.length) {\n return preSortedRowModel\n }\n\n const sortedFlatRows: Array<Row<TFeatures, TData>> = []\n\n // Filter out sortings that correspond to non existing columns\n const availableSorting = sorting.filter((sort) => {\n const column = table.getColumn(sort.id)\n return column ? column_getCanSort(column) : false\n })\n\n if (!availableSorting.length) {\n return preSortedRowModel\n }\n\n const resolvedSorting: Array<{\n id: string\n desc?: boolean\n sortUndefined?: false | -1 | 1 | 'first' | 'last'\n invertSorting?: boolean\n sortFn: SortFn<TFeatures, TData>\n }> = []\n\n for (let i = 0; i < availableSorting.length; i++) {\n const sortEntry = availableSorting[i]!\n const column: Column_Internal<TFeatures, TData> | undefined =\n table.getColumn(sortEntry.id)\n if (!column) {\n continue\n }\n\n resolvedSorting.push({\n id: sortEntry.id,\n desc: sortEntry.desc,\n sortUndefined: column.columnDef.sortUndefined,\n invertSorting: column.columnDef.invertSorting,\n sortFn: column_getSortFn(column),\n })\n }\n\n const compareRows = (\n rowA: Row<TFeatures, TData>,\n rowB: Row<TFeatures, TData>,\n ) => {\n for (let i = 0; i < resolvedSorting.length; i++) {\n const sortEntry = resolvedSorting[i]!\n const sortUndefined = sortEntry.sortUndefined\n const isDesc = sortEntry.desc\n\n let sortInt = 0\n\n // All sorting ints should always return in ascending order\n if (sortUndefined) {\n const aValue = rowA.getValue(sortEntry.id)\n const bValue = rowB.getValue(sortEntry.id)\n\n const aUndefined = aValue === undefined\n const bUndefined = bValue === undefined\n\n if (aUndefined && bUndefined) {\n continue\n }\n\n if (aUndefined || bUndefined) {\n if (sortUndefined === 'first') return aUndefined ? -1 : 1\n if (sortUndefined === 'last') return aUndefined ? 1 : -1\n sortInt = aUndefined ? sortUndefined : -sortUndefined\n }\n }\n\n if (sortInt === 0) {\n sortInt = sortEntry.sortFn(rowA, rowB, sortEntry.id)\n }\n\n // If sorting is non-zero, take care of desc and inversion\n if (sortInt !== 0) {\n if (isDesc) {\n sortInt *= -1\n }\n\n if (sortEntry.invertSorting) {\n sortInt *= -1\n }\n\n return sortInt\n }\n }\n\n return rowA.index - rowB.index\n }\n\n const sortData = (\n rows: Array<Row<TFeatures, TData>>,\n ): {\n rows: Array<Row<TFeatures, TData>>\n changed: boolean\n } => {\n const sortedData = rows.slice()\n\n sortedData.sort(compareRows)\n let changed = false\n\n // If there are sub-rows, sort them. Clone only rows that need mutation\n // (i.e. have subRows) so we don't corrupt the source row model.\n for (let i = 0; i < sortedData.length; i++) {\n const row = sortedData[i]!\n if (row !== rows[i]) {\n changed = true\n }\n\n if (row.subRows.length) {\n const sortedSubRows = sortData(row.subRows)\n\n if (sortedSubRows.changed) {\n // Preserve prototype chain so methods like getValue() remain accessible\n const cloned = Object.create(Object.getPrototypeOf(row))\n copyInstancePropertiesWithoutMemos(cloned, row)\n cloned.subRows = sortedSubRows.rows\n sortedData[i] = cloned\n sortedFlatRows.push(cloned)\n changed = true\n } else {\n sortedFlatRows.push(row)\n }\n } else {\n sortedFlatRows.push(row)\n }\n }\n\n return { rows: sortedData, changed }\n }\n\n return {\n rows: sortData(preSortedRowModel.rows).rows,\n flatRows: sortedFlatRows,\n rowsById: preSortedRowModel.rowsById,\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"createSortedRowModel.js","names":[],"sources":["../../../src/features/row-sorting/createSortedRowModel.ts"],"sourcesContent":["import { copyInstancePropertiesWithoutMemos, tableMemo } from '../../utils'\nimport { table_autoResetPageIndex } from '../row-pagination/rowPaginationFeature.utils'\nimport { column_getCanSort, column_getSortFn } from './rowSortingFeature.utils'\nimport type { Column_Internal } from '../../types/Column'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'\nimport type { Table, Table_Internal } from '../../types/Table'\nimport type { Row } from '../../types/Row'\nimport type { SortFn } from './rowSortingFeature.types'\nimport type { RowData } from '../../types/type-utils'\n\n/**\n * Creates a memoized sorted row model factory.\n *\n * The factory reads the relevant table state atoms and options, then returns a row model function used by the table row-model pipeline.\n *\n * Register the sorting functions you use with the `sortFns` slot on the\n * `features` option:\n * `tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric } })`.\n * Importing individual `sortFn_*` functions keeps unused built-ins out of\n * your bundle; sorting functions passed directly to the `sortFn` column\n * option need no registration at all.\n */\nexport function createSortedRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(): (table: Table<TFeatures, TData>) => () => RowModel<TFeatures, TData> {\n return (_table) => {\n const table = _table as unknown as Table_Internal<TFeatures, TData>\n return tableMemo({\n feature: 'rowSortingFeature',\n table,\n fnName: 'table.getSortedRowModel',\n memoDeps: () => [\n table.atoms.sorting?.get(),\n table.getPreSortedRowModel(),\n ],\n fn: () => _createSortedRowModel(table),\n onAfterUpdate: () => table_autoResetPageIndex(table),\n })\n }\n}\n\nfunction _createSortedRowModel<\n TFeatures extends TableFeatures,\n TData extends RowData = any,\n>(table: Table_Internal<TFeatures, TData>): RowModel<TFeatures, TData> {\n const preSortedRowModel = table.getPreSortedRowModel()\n const sorting = table.atoms.sorting?.get()\n\n if (!preSortedRowModel.rows.length || !sorting?.length) {\n return preSortedRowModel\n }\n\n const sortedFlatRows: Array<Row<TFeatures, TData>> = []\n\n // Filter out sortings that correspond to non existing columns\n const availableSorting = sorting.filter((sort) => {\n const column = table.getColumn(sort.id)\n return column ? column_getCanSort(column) : false\n })\n\n if (!availableSorting.length) {\n return preSortedRowModel\n }\n\n const resolvedSorting: Array<{\n id: string\n desc?: boolean\n sortUndefined?: false | -1 | 1 | 'first' | 'last'\n invertSorting?: boolean\n sortFn: SortFn<TFeatures, TData>\n }> = []\n\n for (let i = 0; i < availableSorting.length; i++) {\n const sortEntry = availableSorting[i]!\n const column: Column_Internal<TFeatures, TData> | undefined =\n table.getColumn(sortEntry.id)\n if (!column) {\n continue\n }\n\n resolvedSorting.push({\n id: sortEntry.id,\n desc: sortEntry.desc,\n sortUndefined: column.columnDef.sortUndefined,\n invertSorting: column.columnDef.invertSorting,\n sortFn: column_getSortFn(column),\n })\n }\n\n const compareRows = (\n rowA: Row<TFeatures, TData>,\n rowB: Row<TFeatures, TData>,\n ) => {\n for (let i = 0; i < resolvedSorting.length; i++) {\n const sortEntry = resolvedSorting[i]!\n const sortUndefined = sortEntry.sortUndefined\n const isDesc = sortEntry.desc\n\n let sortInt = 0\n\n // All sorting ints should always return in ascending order\n if (sortUndefined) {\n const aValue = rowA.getValue(sortEntry.id)\n const bValue = rowB.getValue(sortEntry.id)\n\n const aUndefined = aValue === undefined\n const bUndefined = bValue === undefined\n\n if (aUndefined && bUndefined) {\n continue\n }\n\n if (aUndefined || bUndefined) {\n if (sortUndefined === 'first') return aUndefined ? -1 : 1\n if (sortUndefined === 'last') return aUndefined ? 1 : -1\n sortInt = aUndefined ? sortUndefined : -sortUndefined\n }\n }\n\n if (sortInt === 0) {\n sortInt = sortEntry.sortFn(rowA, rowB, sortEntry.id)\n }\n\n // If sorting is non-zero, take care of desc and inversion\n if (sortInt !== 0) {\n if (isDesc) {\n sortInt *= -1\n }\n\n if (sortEntry.invertSorting) {\n sortInt *= -1\n }\n\n return sortInt\n }\n }\n\n return rowA.index - rowB.index\n }\n\n const sortData = (\n rows: Array<Row<TFeatures, TData>>,\n ): {\n rows: Array<Row<TFeatures, TData>>\n changed: boolean\n } => {\n const sortedData = rows.slice()\n\n sortedData.sort(compareRows)\n let changed = false\n\n // If there are sub-rows, sort them. Clone only rows that need mutation\n // (i.e. have subRows) so we don't corrupt the source row model.\n for (let i = 0; i < sortedData.length; i++) {\n const row = sortedData[i]!\n if (row !== rows[i]) {\n changed = true\n }\n\n if (row.subRows.length) {\n const sortedSubRows = sortData(row.subRows)\n\n if (sortedSubRows.changed) {\n // Preserve prototype chain so methods like getValue() remain accessible\n const cloned = Object.create(Object.getPrototypeOf(row))\n copyInstancePropertiesWithoutMemos(cloned, row)\n cloned.subRows = sortedSubRows.rows\n sortedData[i] = cloned\n sortedFlatRows.push(cloned)\n changed = true\n } else {\n sortedFlatRows.push(row)\n }\n } else {\n sortedFlatRows.push(row)\n }\n }\n\n return { rows: sortedData, changed }\n }\n\n return {\n rows: sortData(preSortedRowModel.rows).rows,\n flatRows: sortedFlatRows,\n rowsById: preSortedRowModel.rowsById,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAuBA,SAAgB,uBAG0D;CACxE,QAAQ,WAAW;EACjB,MAAM,QAAQ;EACd,OAAO,UAAU;GACf,SAAS;GACT;GACA,QAAQ;GACR,gBAAgB,CACd,MAAM,MAAM,SAAS,IAAI,GACzB,MAAM,qBAAqB,CAC7B;GACA,UAAU,sBAAsB,KAAK;GACrC,qBAAqB,yBAAyB,KAAK;EACrD,CAAC;CACH;AACF;AAEA,SAAS,sBAGP,OAAqE;CACrE,MAAM,oBAAoB,MAAM,qBAAqB;CACrD,MAAM,UAAU,MAAM,MAAM,SAAS,IAAI;CAEzC,IAAI,CAAC,kBAAkB,KAAK,UAAU,CAAC,SAAS,QAC9C,OAAO;CAGT,MAAM,iBAA+C,CAAC;CAGtD,MAAM,mBAAmB,QAAQ,QAAQ,SAAS;EAChD,MAAM,SAAS,MAAM,UAAU,KAAK,EAAE;EACtC,OAAO,SAAS,kBAAkB,MAAM,IAAI;CAC9C,CAAC;CAED,IAAI,CAAC,iBAAiB,QACpB,OAAO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AAI,QAAQ,KAAK,IACf,UAAU;GAGZ,IAAI,IAAI,QAAQ,QAAQ;IACtB,MAAM,gBAAgB,SAAS,IAAI,OAAO;IAE1C,IAAI,cAAc,SAAS;KAEzB,MAAM,SAAS,OAAO,OAAO,OAAO,eAAe,GAAG,CAAC;KACvD,mCAAmC,QAAQ,GAAG;KAC9C,OAAO,UAAU,cAAc;KAC/B,WAAW,KAAK;KAChB,eAAe,KAAK,MAAM;KAC1B,UAAU;IACZ,OACE,eAAe,KAAK,GAAG;GAE3B,OACE,eAAe,KAAK,GAAG;EAE3B;EAEA,OAAO;GAAE,MAAM;GAAY;EAAQ;CACrC;CAEA,OAAO;EACL,MAAM,SAAS,kBAAkB,IAAI,CAAC,CAAC;EACvC,UAAU;EACV,UAAU,kBAAkB;CAC9B;AACF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OnChangeFn, RowData, Updater } from "../../types/type-utils.cjs";
|
|
1
|
+
import { OnChangeFn, RowData, TransformDataValueFn, Updater } from "../../types/type-utils.cjs";
|
|
2
2
|
import { BuiltInSortFn } from "../../fns/sortFns.cjs";
|
|
3
3
|
import { Row } from "../../types/Row.cjs";
|
|
4
4
|
import { RowModel } from "../../core/row-models/coreRowModelsFeature.types.cjs";
|
|
@@ -20,6 +20,35 @@ interface RowModelFns_RowSorting<in out TFeatures extends TableFeatures, in out
|
|
|
20
20
|
interface SortFns {}
|
|
21
21
|
interface SortFn<in out TFeatures extends TableFeatures, in out TData extends RowData> {
|
|
22
22
|
(rowA: Row<TFeatures, TData>, rowB: Row<TFeatures, TData>, columnId: string): number;
|
|
23
|
+
/**
|
|
24
|
+
* Normalizes each row's value before the two sides are compared. Only
|
|
25
|
+
* honored by sorting functions built with `constructSortFn` (including all
|
|
26
|
+
* built-in sorting functions).
|
|
27
|
+
*/
|
|
28
|
+
resolveDataValue?: TransformDataValueFn;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The definition object accepted by `constructSortFn`.
|
|
32
|
+
*
|
|
33
|
+
* `sort` is a value-level comparator: it receives both rows' (resolved) data
|
|
34
|
+
* values instead of the whole rows, so normalization concerns stay in
|
|
35
|
+
* `resolveDataValue`.
|
|
36
|
+
*/
|
|
37
|
+
interface SortFnDef<in out TFeatures extends TableFeatures, in out TData extends RowData> {
|
|
38
|
+
sort: (dataValueA: any, dataValueB: any, rowA: Row<TFeatures, TData>, rowB: Row<TFeatures, TData>, columnId: string) => number;
|
|
39
|
+
resolveDataValue?: TransformDataValueFn;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The shape returned by `constructSortFn`: a `SortFn` with its definition
|
|
43
|
+
* attached, so variants can be built by spreading it into another
|
|
44
|
+
* `constructSortFn` call.
|
|
45
|
+
*
|
|
46
|
+
* The call signature is generic so a created sorting function can be used
|
|
47
|
+
* with any table's rows, regardless of the feature set it was defined
|
|
48
|
+
* against.
|
|
49
|
+
*/
|
|
50
|
+
interface CreatedSortFn<in out TFeatures extends TableFeatures, in out TData extends RowData> extends SortFnDef<TFeatures, TData> {
|
|
51
|
+
<TRowFeatures extends TableFeatures, TRowData extends RowData>(rowA: Row<TRowFeatures, TRowData>, rowB: Row<TRowFeatures, TRowData>, columnId: string): number;
|
|
23
52
|
}
|
|
24
53
|
type CustomSortFns<TFeatures extends TableFeatures, TData extends RowData> = Record<string, SortFn<TFeatures, TData>>;
|
|
25
54
|
/**
|
|
@@ -187,5 +216,5 @@ interface CachedRowModel_Sorted<in out TFeatures extends TableFeatures, in out T
|
|
|
187
216
|
sortedRowModel: () => RowModel<TFeatures, TData>;
|
|
188
217
|
}
|
|
189
218
|
//#endregion
|
|
190
|
-
export { CachedRowModel_Sorted, ColumnDef_RowSorting, ColumnSort, Column_RowSorting, CustomSortFns, ExtractSortFnKeys, RowModelFns_RowSorting, SortDirection, SortFn, SortFnOption, SortFns, SortingState, TableOptions_RowSorting, TableState_RowSorting, Table_RowModels_Sorted, Table_RowSorting };
|
|
219
|
+
export { CachedRowModel_Sorted, ColumnDef_RowSorting, ColumnSort, Column_RowSorting, CreatedSortFn, CustomSortFns, ExtractSortFnKeys, RowModelFns_RowSorting, SortDirection, SortFn, SortFnDef, SortFnOption, SortFns, SortingState, TableOptions_RowSorting, TableState_RowSorting, Table_RowModels_Sorted, Table_RowSorting };
|
|
191
220
|
//# sourceMappingURL=rowSortingFeature.types.d.cts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OnChangeFn, RowData, Updater } from "../../types/type-utils.js";
|
|
1
|
+
import { OnChangeFn, RowData, TransformDataValueFn, Updater } from "../../types/type-utils.js";
|
|
2
2
|
import { BuiltInSortFn } from "../../fns/sortFns.js";
|
|
3
3
|
import { Row } from "../../types/Row.js";
|
|
4
4
|
import { RowModel } from "../../core/row-models/coreRowModelsFeature.types.js";
|
|
@@ -20,6 +20,35 @@ interface RowModelFns_RowSorting<in out TFeatures extends TableFeatures, in out
|
|
|
20
20
|
interface SortFns {}
|
|
21
21
|
interface SortFn<in out TFeatures extends TableFeatures, in out TData extends RowData> {
|
|
22
22
|
(rowA: Row<TFeatures, TData>, rowB: Row<TFeatures, TData>, columnId: string): number;
|
|
23
|
+
/**
|
|
24
|
+
* Normalizes each row's value before the two sides are compared. Only
|
|
25
|
+
* honored by sorting functions built with `constructSortFn` (including all
|
|
26
|
+
* built-in sorting functions).
|
|
27
|
+
*/
|
|
28
|
+
resolveDataValue?: TransformDataValueFn;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The definition object accepted by `constructSortFn`.
|
|
32
|
+
*
|
|
33
|
+
* `sort` is a value-level comparator: it receives both rows' (resolved) data
|
|
34
|
+
* values instead of the whole rows, so normalization concerns stay in
|
|
35
|
+
* `resolveDataValue`.
|
|
36
|
+
*/
|
|
37
|
+
interface SortFnDef<in out TFeatures extends TableFeatures, in out TData extends RowData> {
|
|
38
|
+
sort: (dataValueA: any, dataValueB: any, rowA: Row<TFeatures, TData>, rowB: Row<TFeatures, TData>, columnId: string) => number;
|
|
39
|
+
resolveDataValue?: TransformDataValueFn;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The shape returned by `constructSortFn`: a `SortFn` with its definition
|
|
43
|
+
* attached, so variants can be built by spreading it into another
|
|
44
|
+
* `constructSortFn` call.
|
|
45
|
+
*
|
|
46
|
+
* The call signature is generic so a created sorting function can be used
|
|
47
|
+
* with any table's rows, regardless of the feature set it was defined
|
|
48
|
+
* against.
|
|
49
|
+
*/
|
|
50
|
+
interface CreatedSortFn<in out TFeatures extends TableFeatures, in out TData extends RowData> extends SortFnDef<TFeatures, TData> {
|
|
51
|
+
<TRowFeatures extends TableFeatures, TRowData extends RowData>(rowA: Row<TRowFeatures, TRowData>, rowB: Row<TRowFeatures, TRowData>, columnId: string): number;
|
|
23
52
|
}
|
|
24
53
|
type CustomSortFns<TFeatures extends TableFeatures, TData extends RowData> = Record<string, SortFn<TFeatures, TData>>;
|
|
25
54
|
/**
|
|
@@ -187,5 +216,5 @@ interface CachedRowModel_Sorted<in out TFeatures extends TableFeatures, in out T
|
|
|
187
216
|
sortedRowModel: () => RowModel<TFeatures, TData>;
|
|
188
217
|
}
|
|
189
218
|
//#endregion
|
|
190
|
-
export { CachedRowModel_Sorted, ColumnDef_RowSorting, ColumnSort, Column_RowSorting, CustomSortFns, ExtractSortFnKeys, RowModelFns_RowSorting, SortDirection, SortFn, SortFnOption, SortFns, SortingState, TableOptions_RowSorting, TableState_RowSorting, Table_RowModels_Sorted, Table_RowSorting };
|
|
219
|
+
export { CachedRowModel_Sorted, ColumnDef_RowSorting, ColumnSort, Column_RowSorting, CreatedSortFn, CustomSortFns, ExtractSortFnKeys, RowModelFns_RowSorting, SortDirection, SortFn, SortFnDef, SortFnOption, SortFns, SortingState, TableOptions_RowSorting, TableState_RowSorting, Table_RowModels_Sorted, Table_RowSorting };
|
|
191
220
|
//# sourceMappingURL=rowSortingFeature.types.d.ts.map
|
|
@@ -60,20 +60,31 @@ function table_resetSorting(table, defaultState) {
|
|
|
60
60
|
function column_getAutoSortFn(column) {
|
|
61
61
|
const sortFns = column.table._rowModelFns.sortFns;
|
|
62
62
|
const firstRows = column.table.getFilteredRowModel().flatRows.slice(0, 10);
|
|
63
|
+
let sortFnName;
|
|
63
64
|
let isString = false;
|
|
64
65
|
for (let i = 0; i < firstRows.length; i++) {
|
|
65
66
|
const value = firstRows[i].getValue(column.id);
|
|
66
67
|
if (Object.prototype.toString.call(value) === "[object Date]") {
|
|
67
|
-
|
|
68
|
+
sortFnName = "datetime";
|
|
69
|
+
break;
|
|
68
70
|
}
|
|
69
71
|
if (typeof value === "string") {
|
|
70
72
|
isString = true;
|
|
71
73
|
if (value.split(require_sortFns.reSplitAlphaNumeric).length > 1) {
|
|
72
|
-
|
|
74
|
+
sortFnName = "alphanumeric";
|
|
75
|
+
break;
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
78
|
}
|
|
76
|
-
if (isString)
|
|
79
|
+
if (!sortFnName && isString) sortFnName = "text";
|
|
80
|
+
if (sortFnName) {
|
|
81
|
+
let sortFn = sortFns?.[sortFnName];
|
|
82
|
+
if (!sortFn) {
|
|
83
|
+
if (process.env.NODE_ENV === "development") console.warn(`sortFn '${sortFnName}' (auto) for column '${column.id}' is not registered`);
|
|
84
|
+
if (sortFnName === "alphanumeric") sortFn = sortFns?.text;
|
|
85
|
+
}
|
|
86
|
+
if (sortFn) return sortFn;
|
|
87
|
+
}
|
|
77
88
|
return require_sortFns.sortFn_basic;
|
|
78
89
|
}
|
|
79
90
|
/**
|
|
@@ -106,7 +117,11 @@ function column_getAutoSortDir(column) {
|
|
|
106
117
|
*/
|
|
107
118
|
function column_getSortFn(column) {
|
|
108
119
|
const sortFns = column.table._rowModelFns.sortFns;
|
|
109
|
-
|
|
120
|
+
if (require_utils.isFunction(column.columnDef.sortFn)) return column.columnDef.sortFn;
|
|
121
|
+
if (column.columnDef.sortFn === "auto") return column_getAutoSortFn(column);
|
|
122
|
+
const sortFn = sortFns?.[column.columnDef.sortFn];
|
|
123
|
+
if (process.env.NODE_ENV === "development" && !sortFn) console.warn(`sortFn '${String(column.columnDef.sortFn)}' for column '${column.id}' is not registered`);
|
|
124
|
+
return sortFn ?? require_sortFns.sortFn_basic;
|
|
110
125
|
}
|
|
111
126
|
/**
|
|
112
127
|
* Applies the next sorting state for this column.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rowSortingFeature.utils.cjs","names":["cloneState","reSplitAlphaNumeric","sortFn_basic","isFunction"],"sources":["../../../src/features/row-sorting/rowSortingFeature.utils.ts"],"sourcesContent":["import { reSplitAlphaNumeric, sortFn_basic } from '../../fns/sortFns'\nimport { cloneState, isFunction } from '../../utils'\nimport type { CellData, RowData, Updater } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Table_Internal } from '../../types/Table'\nimport type { Column_Internal } from '../../types/Column'\nimport type {\n SortDirection,\n SortFn,\n SortingState,\n} from './rowSortingFeature.types'\n\n// State Utils\n\n/**\n * Creates the default sorting state.\n *\n * The feature default is an empty array, meaning no columns are sorted. Reset\n * APIs use this value when `defaultState` is `true`.\n *\n * @example\n * ```ts\n * const sorting = getDefaultSortingState()\n * ```\n */\nexport function getDefaultSortingState(): SortingState {\n return []\n}\n\n/**\n * Routes a sorting updater through the table's sorting change handler.\n *\n * The updater may be a next `SortingState` array or a function of the previous\n * sorting state, matching the instance `table.setSorting` behavior.\n *\n * @example\n * ```ts\n * table_setSorting(table, (old) => [...old, { id: 'age', desc: true }])\n * ```\n */\nexport function table_setSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, updater: Updater<SortingState>) {\n table.options.onSortingChange?.(updater)\n}\n\n/**\n * Resets `sorting` to the configured initial state or feature default.\n *\n * With no argument, the reset clones `table.initialState.sorting` when it\n * exists. Passing `true` ignores initial state and resets to `[]`.\n *\n * @example\n * ```ts\n * table_resetSorting(table)\n * table_resetSorting(table, true)\n * ```\n */\nexport function table_resetSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, defaultState?: boolean) {\n table_setSorting(\n table,\n defaultState ? [] : cloneState(table.initialState.sorting ?? []),\n )\n}\n\n// Column Utils\n\n/**\n * Chooses a built-in sorting function from sampled filtered row values.\n *\n * Date-like values use `datetime`, mixed text/numeric strings use\n * `alphanumeric`, plain strings use `text`, and unknown values fall back to\n * `basic`.\n *\n * @example\n * ```ts\n * const sortFn = column_getAutoSortFn(column)\n * ```\n */\nexport function column_getAutoSortFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): SortFn<TFeatures, TData> {\n const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =\n column.table._rowModelFns.sortFns\n\n const firstRows = column.table.getFilteredRowModel().flatRows.slice(0, 10)\n\n let isString = false\n\n for (let i = 0; i < firstRows.length; i++) {\n const value = firstRows[i]!.getValue(column.id)\n\n if (Object.prototype.toString.call(value) === '[object Date]') {\n if (sortFns?.datetime) {\n return sortFns.datetime\n }\n }\n\n if (typeof value === 'string') {\n isString = true\n\n if (value.split(reSplitAlphaNumeric).length > 1) {\n if (sortFns?.alphanumeric) {\n return sortFns.alphanumeric\n }\n }\n }\n }\n\n if (isString) {\n return sortFns?.text ?? sortFn_basic\n }\n\n return sortFn_basic\n}\n\n/**\n * Chooses the default first sort direction from the first filtered row value.\n *\n * String columns start ascending so alphabetical order is natural; other value\n * types start descending.\n *\n * @example\n * ```ts\n * const direction = column_getAutoSortDir(column)\n * ```\n */\nexport function column_getAutoSortDir<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const firstRow = column.table.getFilteredRowModel().flatRows[0]\n\n const value = firstRow ? firstRow.getValue(column.id) : undefined\n\n if (typeof value === 'string') {\n return 'asc'\n }\n\n return 'desc'\n}\n\n/**\n * Resolves the sorting function configured for a column.\n *\n * Function-valued `columnDef.sortFn` is returned directly, `'auto'` delegates\n * to `column_getAutoSortFn`, and string values are looked up in the table's\n * sorting function registry before falling back to `basic`.\n *\n * @example\n * ```ts\n * const sortFn = column_getSortFn(column)\n * ```\n */\nexport function column_getSortFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): SortFn<TFeatures, TData> {\n const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =\n column.table._rowModelFns.sortFns\n\n return isFunction(column.columnDef.sortFn)\n ? column.columnDef.sortFn\n : column.columnDef.sortFn === 'auto'\n ? column_getAutoSortFn(column)\n : (sortFns?.[column.columnDef.sortFn as string] ?? sortFn_basic)\n}\n\n/**\n * Applies the next sorting state for this column.\n *\n * The toggle can add, replace, flip, or remove this column's sort entry. Multi\n * sorting respects `enableMultiSort`, `maxMultiSortColCount`, and the `multi`\n * argument.\n *\n * @example\n * ```ts\n * column_toggleSorting(column, undefined, true)\n * ```\n */\nexport function column_toggleSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(\n column: Column_Internal<TFeatures, TData, TValue>,\n desc?: boolean,\n multi?: boolean,\n) {\n // this needs to be outside of table.setSorting to be in sync with rerender\n const nextSortingOrder = column_getNextSortingOrder(column)\n const hasManualValue = typeof desc !== 'undefined'\n\n table_setSorting(column.table, (old) => {\n // Find any existing sorting for this column\n const existingIndex = old.findIndex((d) => d.id === column.id)\n const existingSorting =\n existingIndex === -1 ? undefined : old[existingIndex]\n\n let newSorting: SortingState = []\n\n // What should we do with this sort action?\n let sortAction: 'add' | 'remove' | 'toggle' | 'replace'\n const nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'\n\n // Multi-mode\n if (old.length && column_getCanMultiSort(column) && multi) {\n if (existingSorting) {\n sortAction = 'toggle'\n } else {\n sortAction = 'add'\n }\n } else {\n // Normal mode\n if (old.length && existingIndex !== old.length - 1) {\n sortAction = 'replace'\n } else if (existingSorting) {\n sortAction = 'toggle'\n } else {\n sortAction = 'replace'\n }\n }\n\n // Handle toggle states that will remove the sorting\n if (sortAction === 'toggle') {\n // If we are \"actually\" toggling (not a manual set value), should we remove the sorting?\n if (!hasManualValue) {\n // Is our intention to remove?\n if (!nextSortingOrder) {\n sortAction = 'remove'\n }\n }\n }\n\n if (sortAction === 'add') {\n newSorting = [\n ...old,\n {\n id: column.id,\n desc: nextDesc,\n },\n ]\n // Take latest n columns\n newSorting.splice(\n 0,\n newSorting.length -\n (column.table.options.maxMultiSortColCount ??\n Number.MAX_SAFE_INTEGER),\n )\n } else if (sortAction === 'toggle') {\n // This flips (or sets) the\n newSorting = old.map((d) => {\n if (d.id === column.id) {\n return {\n ...d,\n desc: nextDesc,\n }\n }\n return d\n })\n } else if (sortAction === 'remove') {\n newSorting = old.filter((d) => d.id !== column.id)\n } else {\n newSorting = [\n {\n id: column.id,\n desc: nextDesc,\n },\n ]\n }\n\n return newSorting\n })\n}\n\n/**\n * Resolves the first direction used when this column begins sorting.\n *\n * Column-level `sortDescFirst` wins, then table-level `sortDescFirst`, then the\n * auto direction inferred from sampled values.\n *\n * @example\n * ```ts\n * const firstDirection = column_getFirstSortDir(column)\n * ```\n */\nexport function column_getFirstSortDir<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const sortDescFirst =\n column.columnDef.sortDescFirst ??\n column.table.options.sortDescFirst ??\n column_getAutoSortDir(column) === 'desc'\n return sortDescFirst ? 'desc' : 'asc'\n}\n\n/**\n * Resolves the next sort order for this column's toggle cycle.\n *\n * The cycle starts with the first sort direction, flips between `asc` and\n * `desc`, and can return `false` when sorting removal is enabled.\n *\n * @example\n * ```ts\n * const nextOrder = column_getNextSortingOrder(column)\n * ```\n */\nexport function column_getNextSortingOrder<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>, multi?: boolean) {\n const firstSortDirection = column_getFirstSortDir(column)\n const isSorted = column_getIsSorted(column)\n\n if (!isSorted) {\n return firstSortDirection\n }\n\n if (\n isSorted !== firstSortDirection &&\n (column.table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general\n (multi ? (column.table.options.enableMultiRemove ?? true) : true) // If multi, don't allow if enableMultiRemove))\n ) {\n return false\n }\n return isSorted === 'desc' ? 'asc' : 'desc'\n}\n\n/**\n * Checks whether this accessor column can participate in sorting.\n *\n * The column must have an accessor and sorting must be enabled by both the\n * column definition and table options.\n *\n * @example\n * ```ts\n * const canSort = column_getCanSort(column)\n * ```\n */\nexport function column_getCanSort<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n return (\n (column.columnDef.enableSorting ?? true) &&\n (column.table.options.enableSorting ?? true) &&\n !!column.accessorFn\n )\n}\n\n/**\n * Checks whether this column can be added to a multi-sort state.\n *\n * Column-level `enableMultiSort` wins over table-level `enableMultiSort`; if\n * neither is set, accessor columns can multi-sort by default.\n *\n * @example\n * ```ts\n * const canMultiSort = column_getCanMultiSort(column)\n * ```\n */\nexport function column_getCanMultiSort<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): boolean {\n return (\n column.columnDef.enableMultiSort ??\n column.table.options.enableMultiSort ??\n !!column.accessorFn\n )\n}\n\n/**\n * Reads this column's current sort direction.\n *\n * The result is `false` when the column is not sorted, otherwise `'asc'` or\n * `'desc'` based on the column's entry in `state.sorting`.\n *\n * @example\n * ```ts\n * const direction = column_getIsSorted(column)\n * ```\n */\nexport function column_getIsSorted<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): false | SortDirection {\n const columnSort = column.table.atoms.sorting\n ?.get()\n ?.find((d) => d.id === column.id)\n return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'\n}\n\n/**\n * Finds this column's position in the ordered `state.sorting` array.\n *\n * The result is `-1` when the column is not sorted.\n *\n * @example\n * ```ts\n * const index = column_getSortIndex(column)\n * ```\n */\nexport function column_getSortIndex<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): number {\n return (\n column.table.atoms.sorting?.get()?.findIndex((d) => d.id === column.id) ??\n -1\n )\n}\n\n/**\n * Removes this column from the sorting state.\n *\n * Other sorted columns are preserved, including their relative order.\n *\n * @example\n * ```ts\n * column_clearSorting(column)\n * ```\n */\nexport function column_clearSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n // clear sorting for just 1 column\n table_setSorting(column.table, (old) =>\n old.length ? old.filter((d) => d.id !== column.id) : [],\n )\n}\n\n/**\n * Creates a header event handler that toggles this column's sorting.\n *\n * The handler ignores events when the column cannot sort, persists React-style\n * synthetic events when present, and asks `options.isMultiSortEvent` whether\n * the event should add to a multi-sort.\n *\n * @example\n * ```ts\n * const onClick = column_getToggleSortingHandler(column)\n * ```\n */\nexport function column_getToggleSortingHandler<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const canSort = column_getCanSort(column)\n\n return (e: unknown) => {\n if (!canSort) return\n ;(e as any).persist?.()\n column_toggleSorting(\n column,\n\n undefined,\n column_getCanMultiSort(column)\n ? column.table.options.isMultiSortEvent?.(e)\n : false,\n )\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAyBA,SAAgB,yBAAuC;CACrD,OAAO,CAAC;AACV;;;;;;;;;;;;AAaA,SAAgB,iBAGd,OAAyC,SAAgC;CACzE,MAAM,QAAQ,kBAAkB,OAAO;AACzC;;;;;;;;;;;;;AAcA,SAAgB,mBAGd,OAAyC,cAAwB;CACjE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,UAC7C,QAAS,OAAO,MAAM,QAAQ,qBAAqB,OAAQ,OAE5D,OAAO;CAET,OAAO,aAAa,SAAS,QAAQ;AACvC;;;;;;;;;;;;AAaA,SAAgB,kBAId,QAAmD;CACnD,QACG,OAAO,UAAU,iBAAiB,UAClC,OAAO,MAAM,QAAQ,iBAAiB,SACvC,CAAC,CAAC,OAAO;AAEb;;;;;;;;;;;;AAaA,SAAgB,uBAId,QAA4D;CAC5D,OACE,OAAO,UAAU,mBACjB,OAAO,MAAM,QAAQ,mBACrB,CAAC,CAAC,OAAO;AAEb;;;;;;;;;;;;AAaA,SAAgB,mBAId,QAA0E;CAC1E,MAAM,aAAa,OAAO,MAAM,MAAM,SAClC,IAAI,CAAC,EACL,MAAM,MAAM,EAAE,OAAO,OAAO,EAAE;CAClC,OAAO,CAAC,aAAa,QAAQ,WAAW,OAAO,SAAS;AAC1D;;;;;;;;;;;AAYA,SAAgB,oBAId,QAA2D;CAC3D,OACE,OAAO,MAAM,MAAM,SAAS,IAAI,CAAC,EAAE,WAAW,MAAM,EAAE,OAAO,OAAO,EAAE,KACtE;AAEJ;;;;;;;;;;;AAYA,SAAgB,oBAId,QAAmD;CAEnD,iBAAiB,OAAO,QAAQ,QAC9B,IAAI,SAAS,IAAI,QAAQ,MAAM,EAAE,OAAO,OAAO,EAAE,IAAI,CAAC,CACxD;AACF;;;;;;;;;;;;;AAcA,SAAgB,+BAId,QAAmD;CACnD,MAAM,UAAU,kBAAkB,MAAM;CAExC,QAAQ,MAAe;EACrB,IAAI,CAAC,SAAS;EACb,AAAC,EAAU,UAAU;EACtB,qBACE,QAEA,QACA,uBAAuB,MAAM,IACzB,OAAO,MAAM,QAAQ,mBAAmB,CAAC,IACzC,KACN;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"rowSortingFeature.utils.cjs","names":["cloneState","reSplitAlphaNumeric","sortFn_basic","isFunction"],"sources":["../../../src/features/row-sorting/rowSortingFeature.utils.ts"],"sourcesContent":["import { reSplitAlphaNumeric, sortFn_basic } from '../../fns/sortFns'\nimport { cloneState, isFunction } from '../../utils'\nimport type { CellData, RowData, Updater } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Table_Internal } from '../../types/Table'\nimport type { Column_Internal } from '../../types/Column'\nimport type {\n SortDirection,\n SortFn,\n SortingState,\n} from './rowSortingFeature.types'\n\n// State Utils\n\n/**\n * Creates the default sorting state.\n *\n * The feature default is an empty array, meaning no columns are sorted. Reset\n * APIs use this value when `defaultState` is `true`.\n *\n * @example\n * ```ts\n * const sorting = getDefaultSortingState()\n * ```\n */\nexport function getDefaultSortingState(): SortingState {\n return []\n}\n\n/**\n * Routes a sorting updater through the table's sorting change handler.\n *\n * The updater may be a next `SortingState` array or a function of the previous\n * sorting state, matching the instance `table.setSorting` behavior.\n *\n * @example\n * ```ts\n * table_setSorting(table, (old) => [...old, { id: 'age', desc: true }])\n * ```\n */\nexport function table_setSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, updater: Updater<SortingState>) {\n table.options.onSortingChange?.(updater)\n}\n\n/**\n * Resets `sorting` to the configured initial state or feature default.\n *\n * With no argument, the reset clones `table.initialState.sorting` when it\n * exists. Passing `true` ignores initial state and resets to `[]`.\n *\n * @example\n * ```ts\n * table_resetSorting(table)\n * table_resetSorting(table, true)\n * ```\n */\nexport function table_resetSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, defaultState?: boolean) {\n table_setSorting(\n table,\n defaultState ? [] : cloneState(table.initialState.sorting ?? []),\n )\n}\n\n// Column Utils\n\n/**\n * Chooses a built-in sorting function from sampled filtered row values.\n *\n * Date-like values use `datetime`, mixed text/numeric strings use\n * `alphanumeric`, plain strings use `text`, and unknown values fall back to\n * `basic`.\n *\n * @example\n * ```ts\n * const sortFn = column_getAutoSortFn(column)\n * ```\n */\nexport function column_getAutoSortFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): SortFn<TFeatures, TData> {\n const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =\n column.table._rowModelFns.sortFns\n\n const firstRows = column.table.getFilteredRowModel().flatRows.slice(0, 10)\n\n let sortFnName: string | undefined\n let isString = false\n\n for (let i = 0; i < firstRows.length; i++) {\n const value = firstRows[i]!.getValue(column.id)\n\n if (Object.prototype.toString.call(value) === '[object Date]') {\n sortFnName = 'datetime'\n break\n }\n\n if (typeof value === 'string') {\n isString = true\n\n if (value.split(reSplitAlphaNumeric).length > 1) {\n sortFnName = 'alphanumeric'\n break\n }\n }\n }\n\n if (!sortFnName && isString) {\n sortFnName = 'text'\n }\n\n if (sortFnName) {\n let sortFn = sortFns?.[sortFnName]\n\n if (!sortFn) {\n if (process.env.NODE_ENV === 'development') {\n console.warn(\n `sortFn '${sortFnName}' (auto) for column '${column.id}' is not registered`,\n )\n }\n\n // String columns degrade to a registered text sort before basic\n if (sortFnName === 'alphanumeric') {\n sortFn = sortFns?.text\n }\n }\n\n if (sortFn) {\n return sortFn\n }\n }\n\n return sortFn_basic\n}\n\n/**\n * Chooses the default first sort direction from the first filtered row value.\n *\n * String columns start ascending so alphabetical order is natural; other value\n * types start descending.\n *\n * @example\n * ```ts\n * const direction = column_getAutoSortDir(column)\n * ```\n */\nexport function column_getAutoSortDir<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const firstRow = column.table.getFilteredRowModel().flatRows[0]\n\n const value = firstRow ? firstRow.getValue(column.id) : undefined\n\n if (typeof value === 'string') {\n return 'asc'\n }\n\n return 'desc'\n}\n\n/**\n * Resolves the sorting function configured for a column.\n *\n * Function-valued `columnDef.sortFn` is returned directly, `'auto'` delegates\n * to `column_getAutoSortFn`, and string values are looked up in the table's\n * sorting function registry before falling back to `basic`.\n *\n * @example\n * ```ts\n * const sortFn = column_getSortFn(column)\n * ```\n */\nexport function column_getSortFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): SortFn<TFeatures, TData> {\n const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =\n column.table._rowModelFns.sortFns\n\n if (isFunction(column.columnDef.sortFn)) {\n return column.columnDef.sortFn\n }\n\n if (column.columnDef.sortFn === 'auto') {\n return column_getAutoSortFn(column)\n }\n\n const sortFn = sortFns?.[column.columnDef.sortFn as string]\n\n if (process.env.NODE_ENV === 'development' && !sortFn) {\n console.warn(\n `sortFn '${String(column.columnDef.sortFn)}' for column '${column.id}' is not registered`,\n )\n }\n\n return sortFn ?? sortFn_basic\n}\n\n/**\n * Applies the next sorting state for this column.\n *\n * The toggle can add, replace, flip, or remove this column's sort entry. Multi\n * sorting respects `enableMultiSort`, `maxMultiSortColCount`, and the `multi`\n * argument.\n *\n * @example\n * ```ts\n * column_toggleSorting(column, undefined, true)\n * ```\n */\nexport function column_toggleSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(\n column: Column_Internal<TFeatures, TData, TValue>,\n desc?: boolean,\n multi?: boolean,\n) {\n // this needs to be outside of table.setSorting to be in sync with rerender\n const nextSortingOrder = column_getNextSortingOrder(column)\n const hasManualValue = typeof desc !== 'undefined'\n\n table_setSorting(column.table, (old) => {\n // Find any existing sorting for this column\n const existingIndex = old.findIndex((d) => d.id === column.id)\n const existingSorting =\n existingIndex === -1 ? undefined : old[existingIndex]\n\n let newSorting: SortingState = []\n\n // What should we do with this sort action?\n let sortAction: 'add' | 'remove' | 'toggle' | 'replace'\n const nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'\n\n // Multi-mode\n if (old.length && column_getCanMultiSort(column) && multi) {\n if (existingSorting) {\n sortAction = 'toggle'\n } else {\n sortAction = 'add'\n }\n } else {\n // Normal mode\n if (old.length && existingIndex !== old.length - 1) {\n sortAction = 'replace'\n } else if (existingSorting) {\n sortAction = 'toggle'\n } else {\n sortAction = 'replace'\n }\n }\n\n // Handle toggle states that will remove the sorting\n if (sortAction === 'toggle') {\n // If we are \"actually\" toggling (not a manual set value), should we remove the sorting?\n if (!hasManualValue) {\n // Is our intention to remove?\n if (!nextSortingOrder) {\n sortAction = 'remove'\n }\n }\n }\n\n if (sortAction === 'add') {\n newSorting = [\n ...old,\n {\n id: column.id,\n desc: nextDesc,\n },\n ]\n // Take latest n columns\n newSorting.splice(\n 0,\n newSorting.length -\n (column.table.options.maxMultiSortColCount ??\n Number.MAX_SAFE_INTEGER),\n )\n } else if (sortAction === 'toggle') {\n // This flips (or sets) the\n newSorting = old.map((d) => {\n if (d.id === column.id) {\n return {\n ...d,\n desc: nextDesc,\n }\n }\n return d\n })\n } else if (sortAction === 'remove') {\n newSorting = old.filter((d) => d.id !== column.id)\n } else {\n newSorting = [\n {\n id: column.id,\n desc: nextDesc,\n },\n ]\n }\n\n return newSorting\n })\n}\n\n/**\n * Resolves the first direction used when this column begins sorting.\n *\n * Column-level `sortDescFirst` wins, then table-level `sortDescFirst`, then the\n * auto direction inferred from sampled values.\n *\n * @example\n * ```ts\n * const firstDirection = column_getFirstSortDir(column)\n * ```\n */\nexport function column_getFirstSortDir<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const sortDescFirst =\n column.columnDef.sortDescFirst ??\n column.table.options.sortDescFirst ??\n column_getAutoSortDir(column) === 'desc'\n return sortDescFirst ? 'desc' : 'asc'\n}\n\n/**\n * Resolves the next sort order for this column's toggle cycle.\n *\n * The cycle starts with the first sort direction, flips between `asc` and\n * `desc`, and can return `false` when sorting removal is enabled.\n *\n * @example\n * ```ts\n * const nextOrder = column_getNextSortingOrder(column)\n * ```\n */\nexport function column_getNextSortingOrder<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>, multi?: boolean) {\n const firstSortDirection = column_getFirstSortDir(column)\n const isSorted = column_getIsSorted(column)\n\n if (!isSorted) {\n return firstSortDirection\n }\n\n if (\n isSorted !== firstSortDirection &&\n (column.table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general\n (multi ? (column.table.options.enableMultiRemove ?? true) : true) // If multi, don't allow if enableMultiRemove))\n ) {\n return false\n }\n return isSorted === 'desc' ? 'asc' : 'desc'\n}\n\n/**\n * Checks whether this accessor column can participate in sorting.\n *\n * The column must have an accessor and sorting must be enabled by both the\n * column definition and table options.\n *\n * @example\n * ```ts\n * const canSort = column_getCanSort(column)\n * ```\n */\nexport function column_getCanSort<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n return (\n (column.columnDef.enableSorting ?? true) &&\n (column.table.options.enableSorting ?? true) &&\n !!column.accessorFn\n )\n}\n\n/**\n * Checks whether this column can be added to a multi-sort state.\n *\n * Column-level `enableMultiSort` wins over table-level `enableMultiSort`; if\n * neither is set, accessor columns can multi-sort by default.\n *\n * @example\n * ```ts\n * const canMultiSort = column_getCanMultiSort(column)\n * ```\n */\nexport function column_getCanMultiSort<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): boolean {\n return (\n column.columnDef.enableMultiSort ??\n column.table.options.enableMultiSort ??\n !!column.accessorFn\n )\n}\n\n/**\n * Reads this column's current sort direction.\n *\n * The result is `false` when the column is not sorted, otherwise `'asc'` or\n * `'desc'` based on the column's entry in `state.sorting`.\n *\n * @example\n * ```ts\n * const direction = column_getIsSorted(column)\n * ```\n */\nexport function column_getIsSorted<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): false | SortDirection {\n const columnSort = column.table.atoms.sorting\n ?.get()\n ?.find((d) => d.id === column.id)\n return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'\n}\n\n/**\n * Finds this column's position in the ordered `state.sorting` array.\n *\n * The result is `-1` when the column is not sorted.\n *\n * @example\n * ```ts\n * const index = column_getSortIndex(column)\n * ```\n */\nexport function column_getSortIndex<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): number {\n return (\n column.table.atoms.sorting?.get()?.findIndex((d) => d.id === column.id) ??\n -1\n )\n}\n\n/**\n * Removes this column from the sorting state.\n *\n * Other sorted columns are preserved, including their relative order.\n *\n * @example\n * ```ts\n * column_clearSorting(column)\n * ```\n */\nexport function column_clearSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n // clear sorting for just 1 column\n table_setSorting(column.table, (old) =>\n old.length ? old.filter((d) => d.id !== column.id) : [],\n )\n}\n\n/**\n * Creates a header event handler that toggles this column's sorting.\n *\n * The handler ignores events when the column cannot sort, persists React-style\n * synthetic events when present, and asks `options.isMultiSortEvent` whether\n * the event should add to a multi-sort.\n *\n * @example\n * ```ts\n * const onClick = column_getToggleSortingHandler(column)\n * ```\n */\nexport function column_getToggleSortingHandler<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const canSort = column_getCanSort(column)\n\n return (e: unknown) => {\n if (!canSort) return\n ;(e as any).persist?.()\n column_toggleSorting(\n column,\n\n undefined,\n column_getCanMultiSort(column)\n ? column.table.options.isMultiSortEvent?.(e)\n : false,\n )\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAyBA,SAAgB,yBAAuC;CACrD,OAAO,CAAC;AACV;;;;;;;;;;;;AAaA,SAAgB,iBAGd,OAAyC,SAAgC;CACzE,MAAM,QAAQ,kBAAkB,OAAO;AACzC;;;;;;;;;;;;;AAcA,SAAgB,mBAGd,OAAyC,cAAwB;CACjE,iBACE,OACA,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,SAAgB,+BAId,QAAmD;CACnD,MAAM,UAAU,kBAAkB,MAAM;CAExC,QAAQ,MAAe;EACrB,IAAI,CAAC,SAAS;EACb,AAAC,EAAU,UAAU;EACtB,qBACE,QAEA,QACA,uBAAuB,MAAM,IACzB,OAAO,MAAM,QAAQ,mBAAmB,CAAC,IACzC,KACN;CACF;AACF"}
|
|
@@ -60,20 +60,31 @@ function table_resetSorting(table, defaultState) {
|
|
|
60
60
|
function column_getAutoSortFn(column) {
|
|
61
61
|
const sortFns = column.table._rowModelFns.sortFns;
|
|
62
62
|
const firstRows = column.table.getFilteredRowModel().flatRows.slice(0, 10);
|
|
63
|
+
let sortFnName;
|
|
63
64
|
let isString = false;
|
|
64
65
|
for (let i = 0; i < firstRows.length; i++) {
|
|
65
66
|
const value = firstRows[i].getValue(column.id);
|
|
66
67
|
if (Object.prototype.toString.call(value) === "[object Date]") {
|
|
67
|
-
|
|
68
|
+
sortFnName = "datetime";
|
|
69
|
+
break;
|
|
68
70
|
}
|
|
69
71
|
if (typeof value === "string") {
|
|
70
72
|
isString = true;
|
|
71
73
|
if (value.split(reSplitAlphaNumeric).length > 1) {
|
|
72
|
-
|
|
74
|
+
sortFnName = "alphanumeric";
|
|
75
|
+
break;
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
78
|
}
|
|
76
|
-
if (isString)
|
|
79
|
+
if (!sortFnName && isString) sortFnName = "text";
|
|
80
|
+
if (sortFnName) {
|
|
81
|
+
let sortFn = sortFns?.[sortFnName];
|
|
82
|
+
if (!sortFn) {
|
|
83
|
+
if (process.env.NODE_ENV === "development") console.warn(`sortFn '${sortFnName}' (auto) for column '${column.id}' is not registered`);
|
|
84
|
+
if (sortFnName === "alphanumeric") sortFn = sortFns?.text;
|
|
85
|
+
}
|
|
86
|
+
if (sortFn) return sortFn;
|
|
87
|
+
}
|
|
77
88
|
return sortFn_basic;
|
|
78
89
|
}
|
|
79
90
|
/**
|
|
@@ -106,7 +117,11 @@ function column_getAutoSortDir(column) {
|
|
|
106
117
|
*/
|
|
107
118
|
function column_getSortFn(column) {
|
|
108
119
|
const sortFns = column.table._rowModelFns.sortFns;
|
|
109
|
-
|
|
120
|
+
if (isFunction(column.columnDef.sortFn)) return column.columnDef.sortFn;
|
|
121
|
+
if (column.columnDef.sortFn === "auto") return column_getAutoSortFn(column);
|
|
122
|
+
const sortFn = sortFns?.[column.columnDef.sortFn];
|
|
123
|
+
if (process.env.NODE_ENV === "development" && !sortFn) console.warn(`sortFn '${String(column.columnDef.sortFn)}' for column '${column.id}' is not registered`);
|
|
124
|
+
return sortFn ?? sortFn_basic;
|
|
110
125
|
}
|
|
111
126
|
/**
|
|
112
127
|
* Applies the next sorting state for this column.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rowSortingFeature.utils.js","names":[],"sources":["../../../src/features/row-sorting/rowSortingFeature.utils.ts"],"sourcesContent":["import { reSplitAlphaNumeric, sortFn_basic } from '../../fns/sortFns'\nimport { cloneState, isFunction } from '../../utils'\nimport type { CellData, RowData, Updater } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Table_Internal } from '../../types/Table'\nimport type { Column_Internal } from '../../types/Column'\nimport type {\n SortDirection,\n SortFn,\n SortingState,\n} from './rowSortingFeature.types'\n\n// State Utils\n\n/**\n * Creates the default sorting state.\n *\n * The feature default is an empty array, meaning no columns are sorted. Reset\n * APIs use this value when `defaultState` is `true`.\n *\n * @example\n * ```ts\n * const sorting = getDefaultSortingState()\n * ```\n */\nexport function getDefaultSortingState(): SortingState {\n return []\n}\n\n/**\n * Routes a sorting updater through the table's sorting change handler.\n *\n * The updater may be a next `SortingState` array or a function of the previous\n * sorting state, matching the instance `table.setSorting` behavior.\n *\n * @example\n * ```ts\n * table_setSorting(table, (old) => [...old, { id: 'age', desc: true }])\n * ```\n */\nexport function table_setSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, updater: Updater<SortingState>) {\n table.options.onSortingChange?.(updater)\n}\n\n/**\n * Resets `sorting` to the configured initial state or feature default.\n *\n * With no argument, the reset clones `table.initialState.sorting` when it\n * exists. Passing `true` ignores initial state and resets to `[]`.\n *\n * @example\n * ```ts\n * table_resetSorting(table)\n * table_resetSorting(table, true)\n * ```\n */\nexport function table_resetSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, defaultState?: boolean) {\n table_setSorting(\n table,\n defaultState ? [] : cloneState(table.initialState.sorting ?? []),\n )\n}\n\n// Column Utils\n\n/**\n * Chooses a built-in sorting function from sampled filtered row values.\n *\n * Date-like values use `datetime`, mixed text/numeric strings use\n * `alphanumeric`, plain strings use `text`, and unknown values fall back to\n * `basic`.\n *\n * @example\n * ```ts\n * const sortFn = column_getAutoSortFn(column)\n * ```\n */\nexport function column_getAutoSortFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): SortFn<TFeatures, TData> {\n const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =\n column.table._rowModelFns.sortFns\n\n const firstRows = column.table.getFilteredRowModel().flatRows.slice(0, 10)\n\n let isString = false\n\n for (let i = 0; i < firstRows.length; i++) {\n const value = firstRows[i]!.getValue(column.id)\n\n if (Object.prototype.toString.call(value) === '[object Date]') {\n if (sortFns?.datetime) {\n return sortFns.datetime\n }\n }\n\n if (typeof value === 'string') {\n isString = true\n\n if (value.split(reSplitAlphaNumeric).length > 1) {\n if (sortFns?.alphanumeric) {\n return sortFns.alphanumeric\n }\n }\n }\n }\n\n if (isString) {\n return sortFns?.text ?? sortFn_basic\n }\n\n return sortFn_basic\n}\n\n/**\n * Chooses the default first sort direction from the first filtered row value.\n *\n * String columns start ascending so alphabetical order is natural; other value\n * types start descending.\n *\n * @example\n * ```ts\n * const direction = column_getAutoSortDir(column)\n * ```\n */\nexport function column_getAutoSortDir<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const firstRow = column.table.getFilteredRowModel().flatRows[0]\n\n const value = firstRow ? firstRow.getValue(column.id) : undefined\n\n if (typeof value === 'string') {\n return 'asc'\n }\n\n return 'desc'\n}\n\n/**\n * Resolves the sorting function configured for a column.\n *\n * Function-valued `columnDef.sortFn` is returned directly, `'auto'` delegates\n * to `column_getAutoSortFn`, and string values are looked up in the table's\n * sorting function registry before falling back to `basic`.\n *\n * @example\n * ```ts\n * const sortFn = column_getSortFn(column)\n * ```\n */\nexport function column_getSortFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): SortFn<TFeatures, TData> {\n const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =\n column.table._rowModelFns.sortFns\n\n return isFunction(column.columnDef.sortFn)\n ? column.columnDef.sortFn\n : column.columnDef.sortFn === 'auto'\n ? column_getAutoSortFn(column)\n : (sortFns?.[column.columnDef.sortFn as string] ?? sortFn_basic)\n}\n\n/**\n * Applies the next sorting state for this column.\n *\n * The toggle can add, replace, flip, or remove this column's sort entry. Multi\n * sorting respects `enableMultiSort`, `maxMultiSortColCount`, and the `multi`\n * argument.\n *\n * @example\n * ```ts\n * column_toggleSorting(column, undefined, true)\n * ```\n */\nexport function column_toggleSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(\n column: Column_Internal<TFeatures, TData, TValue>,\n desc?: boolean,\n multi?: boolean,\n) {\n // this needs to be outside of table.setSorting to be in sync with rerender\n const nextSortingOrder = column_getNextSortingOrder(column)\n const hasManualValue = typeof desc !== 'undefined'\n\n table_setSorting(column.table, (old) => {\n // Find any existing sorting for this column\n const existingIndex = old.findIndex((d) => d.id === column.id)\n const existingSorting =\n existingIndex === -1 ? undefined : old[existingIndex]\n\n let newSorting: SortingState = []\n\n // What should we do with this sort action?\n let sortAction: 'add' | 'remove' | 'toggle' | 'replace'\n const nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'\n\n // Multi-mode\n if (old.length && column_getCanMultiSort(column) && multi) {\n if (existingSorting) {\n sortAction = 'toggle'\n } else {\n sortAction = 'add'\n }\n } else {\n // Normal mode\n if (old.length && existingIndex !== old.length - 1) {\n sortAction = 'replace'\n } else if (existingSorting) {\n sortAction = 'toggle'\n } else {\n sortAction = 'replace'\n }\n }\n\n // Handle toggle states that will remove the sorting\n if (sortAction === 'toggle') {\n // If we are \"actually\" toggling (not a manual set value), should we remove the sorting?\n if (!hasManualValue) {\n // Is our intention to remove?\n if (!nextSortingOrder) {\n sortAction = 'remove'\n }\n }\n }\n\n if (sortAction === 'add') {\n newSorting = [\n ...old,\n {\n id: column.id,\n desc: nextDesc,\n },\n ]\n // Take latest n columns\n newSorting.splice(\n 0,\n newSorting.length -\n (column.table.options.maxMultiSortColCount ??\n Number.MAX_SAFE_INTEGER),\n )\n } else if (sortAction === 'toggle') {\n // This flips (or sets) the\n newSorting = old.map((d) => {\n if (d.id === column.id) {\n return {\n ...d,\n desc: nextDesc,\n }\n }\n return d\n })\n } else if (sortAction === 'remove') {\n newSorting = old.filter((d) => d.id !== column.id)\n } else {\n newSorting = [\n {\n id: column.id,\n desc: nextDesc,\n },\n ]\n }\n\n return newSorting\n })\n}\n\n/**\n * Resolves the first direction used when this column begins sorting.\n *\n * Column-level `sortDescFirst` wins, then table-level `sortDescFirst`, then the\n * auto direction inferred from sampled values.\n *\n * @example\n * ```ts\n * const firstDirection = column_getFirstSortDir(column)\n * ```\n */\nexport function column_getFirstSortDir<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const sortDescFirst =\n column.columnDef.sortDescFirst ??\n column.table.options.sortDescFirst ??\n column_getAutoSortDir(column) === 'desc'\n return sortDescFirst ? 'desc' : 'asc'\n}\n\n/**\n * Resolves the next sort order for this column's toggle cycle.\n *\n * The cycle starts with the first sort direction, flips between `asc` and\n * `desc`, and can return `false` when sorting removal is enabled.\n *\n * @example\n * ```ts\n * const nextOrder = column_getNextSortingOrder(column)\n * ```\n */\nexport function column_getNextSortingOrder<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>, multi?: boolean) {\n const firstSortDirection = column_getFirstSortDir(column)\n const isSorted = column_getIsSorted(column)\n\n if (!isSorted) {\n return firstSortDirection\n }\n\n if (\n isSorted !== firstSortDirection &&\n (column.table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general\n (multi ? (column.table.options.enableMultiRemove ?? true) : true) // If multi, don't allow if enableMultiRemove))\n ) {\n return false\n }\n return isSorted === 'desc' ? 'asc' : 'desc'\n}\n\n/**\n * Checks whether this accessor column can participate in sorting.\n *\n * The column must have an accessor and sorting must be enabled by both the\n * column definition and table options.\n *\n * @example\n * ```ts\n * const canSort = column_getCanSort(column)\n * ```\n */\nexport function column_getCanSort<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n return (\n (column.columnDef.enableSorting ?? true) &&\n (column.table.options.enableSorting ?? true) &&\n !!column.accessorFn\n )\n}\n\n/**\n * Checks whether this column can be added to a multi-sort state.\n *\n * Column-level `enableMultiSort` wins over table-level `enableMultiSort`; if\n * neither is set, accessor columns can multi-sort by default.\n *\n * @example\n * ```ts\n * const canMultiSort = column_getCanMultiSort(column)\n * ```\n */\nexport function column_getCanMultiSort<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): boolean {\n return (\n column.columnDef.enableMultiSort ??\n column.table.options.enableMultiSort ??\n !!column.accessorFn\n )\n}\n\n/**\n * Reads this column's current sort direction.\n *\n * The result is `false` when the column is not sorted, otherwise `'asc'` or\n * `'desc'` based on the column's entry in `state.sorting`.\n *\n * @example\n * ```ts\n * const direction = column_getIsSorted(column)\n * ```\n */\nexport function column_getIsSorted<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): false | SortDirection {\n const columnSort = column.table.atoms.sorting\n ?.get()\n ?.find((d) => d.id === column.id)\n return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'\n}\n\n/**\n * Finds this column's position in the ordered `state.sorting` array.\n *\n * The result is `-1` when the column is not sorted.\n *\n * @example\n * ```ts\n * const index = column_getSortIndex(column)\n * ```\n */\nexport function column_getSortIndex<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): number {\n return (\n column.table.atoms.sorting?.get()?.findIndex((d) => d.id === column.id) ??\n -1\n )\n}\n\n/**\n * Removes this column from the sorting state.\n *\n * Other sorted columns are preserved, including their relative order.\n *\n * @example\n * ```ts\n * column_clearSorting(column)\n * ```\n */\nexport function column_clearSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n // clear sorting for just 1 column\n table_setSorting(column.table, (old) =>\n old.length ? old.filter((d) => d.id !== column.id) : [],\n )\n}\n\n/**\n * Creates a header event handler that toggles this column's sorting.\n *\n * The handler ignores events when the column cannot sort, persists React-style\n * synthetic events when present, and asks `options.isMultiSortEvent` whether\n * the event should add to a multi-sort.\n *\n * @example\n * ```ts\n * const onClick = column_getToggleSortingHandler(column)\n * ```\n */\nexport function column_getToggleSortingHandler<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const canSort = column_getCanSort(column)\n\n return (e: unknown) => {\n if (!canSort) return\n ;(e as any).persist?.()\n column_toggleSorting(\n column,\n\n undefined,\n column_getCanMultiSort(column)\n ? column.table.options.isMultiSortEvent?.(e)\n : false,\n )\n }\n}\n"],"mappings":";;;;;;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,mBAAmB,CAAC,IACzC,KACN;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"rowSortingFeature.utils.js","names":[],"sources":["../../../src/features/row-sorting/rowSortingFeature.utils.ts"],"sourcesContent":["import { reSplitAlphaNumeric, sortFn_basic } from '../../fns/sortFns'\nimport { cloneState, isFunction } from '../../utils'\nimport type { CellData, RowData, Updater } from '../../types/type-utils'\nimport type { TableFeatures } from '../../types/TableFeatures'\nimport type { Table_Internal } from '../../types/Table'\nimport type { Column_Internal } from '../../types/Column'\nimport type {\n SortDirection,\n SortFn,\n SortingState,\n} from './rowSortingFeature.types'\n\n// State Utils\n\n/**\n * Creates the default sorting state.\n *\n * The feature default is an empty array, meaning no columns are sorted. Reset\n * APIs use this value when `defaultState` is `true`.\n *\n * @example\n * ```ts\n * const sorting = getDefaultSortingState()\n * ```\n */\nexport function getDefaultSortingState(): SortingState {\n return []\n}\n\n/**\n * Routes a sorting updater through the table's sorting change handler.\n *\n * The updater may be a next `SortingState` array or a function of the previous\n * sorting state, matching the instance `table.setSorting` behavior.\n *\n * @example\n * ```ts\n * table_setSorting(table, (old) => [...old, { id: 'age', desc: true }])\n * ```\n */\nexport function table_setSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, updater: Updater<SortingState>) {\n table.options.onSortingChange?.(updater)\n}\n\n/**\n * Resets `sorting` to the configured initial state or feature default.\n *\n * With no argument, the reset clones `table.initialState.sorting` when it\n * exists. Passing `true` ignores initial state and resets to `[]`.\n *\n * @example\n * ```ts\n * table_resetSorting(table)\n * table_resetSorting(table, true)\n * ```\n */\nexport function table_resetSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n>(table: Table_Internal<TFeatures, TData>, defaultState?: boolean) {\n table_setSorting(\n table,\n defaultState ? [] : cloneState(table.initialState.sorting ?? []),\n )\n}\n\n// Column Utils\n\n/**\n * Chooses a built-in sorting function from sampled filtered row values.\n *\n * Date-like values use `datetime`, mixed text/numeric strings use\n * `alphanumeric`, plain strings use `text`, and unknown values fall back to\n * `basic`.\n *\n * @example\n * ```ts\n * const sortFn = column_getAutoSortFn(column)\n * ```\n */\nexport function column_getAutoSortFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): SortFn<TFeatures, TData> {\n const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =\n column.table._rowModelFns.sortFns\n\n const firstRows = column.table.getFilteredRowModel().flatRows.slice(0, 10)\n\n let sortFnName: string | undefined\n let isString = false\n\n for (let i = 0; i < firstRows.length; i++) {\n const value = firstRows[i]!.getValue(column.id)\n\n if (Object.prototype.toString.call(value) === '[object Date]') {\n sortFnName = 'datetime'\n break\n }\n\n if (typeof value === 'string') {\n isString = true\n\n if (value.split(reSplitAlphaNumeric).length > 1) {\n sortFnName = 'alphanumeric'\n break\n }\n }\n }\n\n if (!sortFnName && isString) {\n sortFnName = 'text'\n }\n\n if (sortFnName) {\n let sortFn = sortFns?.[sortFnName]\n\n if (!sortFn) {\n if (process.env.NODE_ENV === 'development') {\n console.warn(\n `sortFn '${sortFnName}' (auto) for column '${column.id}' is not registered`,\n )\n }\n\n // String columns degrade to a registered text sort before basic\n if (sortFnName === 'alphanumeric') {\n sortFn = sortFns?.text\n }\n }\n\n if (sortFn) {\n return sortFn\n }\n }\n\n return sortFn_basic\n}\n\n/**\n * Chooses the default first sort direction from the first filtered row value.\n *\n * String columns start ascending so alphabetical order is natural; other value\n * types start descending.\n *\n * @example\n * ```ts\n * const direction = column_getAutoSortDir(column)\n * ```\n */\nexport function column_getAutoSortDir<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const firstRow = column.table.getFilteredRowModel().flatRows[0]\n\n const value = firstRow ? firstRow.getValue(column.id) : undefined\n\n if (typeof value === 'string') {\n return 'asc'\n }\n\n return 'desc'\n}\n\n/**\n * Resolves the sorting function configured for a column.\n *\n * Function-valued `columnDef.sortFn` is returned directly, `'auto'` delegates\n * to `column_getAutoSortFn`, and string values are looked up in the table's\n * sorting function registry before falling back to `basic`.\n *\n * @example\n * ```ts\n * const sortFn = column_getSortFn(column)\n * ```\n */\nexport function column_getSortFn<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): SortFn<TFeatures, TData> {\n const sortFns: Record<string, SortFn<TFeatures, TData>> | undefined =\n column.table._rowModelFns.sortFns\n\n if (isFunction(column.columnDef.sortFn)) {\n return column.columnDef.sortFn\n }\n\n if (column.columnDef.sortFn === 'auto') {\n return column_getAutoSortFn(column)\n }\n\n const sortFn = sortFns?.[column.columnDef.sortFn as string]\n\n if (process.env.NODE_ENV === 'development' && !sortFn) {\n console.warn(\n `sortFn '${String(column.columnDef.sortFn)}' for column '${column.id}' is not registered`,\n )\n }\n\n return sortFn ?? sortFn_basic\n}\n\n/**\n * Applies the next sorting state for this column.\n *\n * The toggle can add, replace, flip, or remove this column's sort entry. Multi\n * sorting respects `enableMultiSort`, `maxMultiSortColCount`, and the `multi`\n * argument.\n *\n * @example\n * ```ts\n * column_toggleSorting(column, undefined, true)\n * ```\n */\nexport function column_toggleSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(\n column: Column_Internal<TFeatures, TData, TValue>,\n desc?: boolean,\n multi?: boolean,\n) {\n // this needs to be outside of table.setSorting to be in sync with rerender\n const nextSortingOrder = column_getNextSortingOrder(column)\n const hasManualValue = typeof desc !== 'undefined'\n\n table_setSorting(column.table, (old) => {\n // Find any existing sorting for this column\n const existingIndex = old.findIndex((d) => d.id === column.id)\n const existingSorting =\n existingIndex === -1 ? undefined : old[existingIndex]\n\n let newSorting: SortingState = []\n\n // What should we do with this sort action?\n let sortAction: 'add' | 'remove' | 'toggle' | 'replace'\n const nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'\n\n // Multi-mode\n if (old.length && column_getCanMultiSort(column) && multi) {\n if (existingSorting) {\n sortAction = 'toggle'\n } else {\n sortAction = 'add'\n }\n } else {\n // Normal mode\n if (old.length && existingIndex !== old.length - 1) {\n sortAction = 'replace'\n } else if (existingSorting) {\n sortAction = 'toggle'\n } else {\n sortAction = 'replace'\n }\n }\n\n // Handle toggle states that will remove the sorting\n if (sortAction === 'toggle') {\n // If we are \"actually\" toggling (not a manual set value), should we remove the sorting?\n if (!hasManualValue) {\n // Is our intention to remove?\n if (!nextSortingOrder) {\n sortAction = 'remove'\n }\n }\n }\n\n if (sortAction === 'add') {\n newSorting = [\n ...old,\n {\n id: column.id,\n desc: nextDesc,\n },\n ]\n // Take latest n columns\n newSorting.splice(\n 0,\n newSorting.length -\n (column.table.options.maxMultiSortColCount ??\n Number.MAX_SAFE_INTEGER),\n )\n } else if (sortAction === 'toggle') {\n // This flips (or sets) the\n newSorting = old.map((d) => {\n if (d.id === column.id) {\n return {\n ...d,\n desc: nextDesc,\n }\n }\n return d\n })\n } else if (sortAction === 'remove') {\n newSorting = old.filter((d) => d.id !== column.id)\n } else {\n newSorting = [\n {\n id: column.id,\n desc: nextDesc,\n },\n ]\n }\n\n return newSorting\n })\n}\n\n/**\n * Resolves the first direction used when this column begins sorting.\n *\n * Column-level `sortDescFirst` wins, then table-level `sortDescFirst`, then the\n * auto direction inferred from sampled values.\n *\n * @example\n * ```ts\n * const firstDirection = column_getFirstSortDir(column)\n * ```\n */\nexport function column_getFirstSortDir<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const sortDescFirst =\n column.columnDef.sortDescFirst ??\n column.table.options.sortDescFirst ??\n column_getAutoSortDir(column) === 'desc'\n return sortDescFirst ? 'desc' : 'asc'\n}\n\n/**\n * Resolves the next sort order for this column's toggle cycle.\n *\n * The cycle starts with the first sort direction, flips between `asc` and\n * `desc`, and can return `false` when sorting removal is enabled.\n *\n * @example\n * ```ts\n * const nextOrder = column_getNextSortingOrder(column)\n * ```\n */\nexport function column_getNextSortingOrder<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>, multi?: boolean) {\n const firstSortDirection = column_getFirstSortDir(column)\n const isSorted = column_getIsSorted(column)\n\n if (!isSorted) {\n return firstSortDirection\n }\n\n if (\n isSorted !== firstSortDirection &&\n (column.table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general\n (multi ? (column.table.options.enableMultiRemove ?? true) : true) // If multi, don't allow if enableMultiRemove))\n ) {\n return false\n }\n return isSorted === 'desc' ? 'asc' : 'desc'\n}\n\n/**\n * Checks whether this accessor column can participate in sorting.\n *\n * The column must have an accessor and sorting must be enabled by both the\n * column definition and table options.\n *\n * @example\n * ```ts\n * const canSort = column_getCanSort(column)\n * ```\n */\nexport function column_getCanSort<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n return (\n (column.columnDef.enableSorting ?? true) &&\n (column.table.options.enableSorting ?? true) &&\n !!column.accessorFn\n )\n}\n\n/**\n * Checks whether this column can be added to a multi-sort state.\n *\n * Column-level `enableMultiSort` wins over table-level `enableMultiSort`; if\n * neither is set, accessor columns can multi-sort by default.\n *\n * @example\n * ```ts\n * const canMultiSort = column_getCanMultiSort(column)\n * ```\n */\nexport function column_getCanMultiSort<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): boolean {\n return (\n column.columnDef.enableMultiSort ??\n column.table.options.enableMultiSort ??\n !!column.accessorFn\n )\n}\n\n/**\n * Reads this column's current sort direction.\n *\n * The result is `false` when the column is not sorted, otherwise `'asc'` or\n * `'desc'` based on the column's entry in `state.sorting`.\n *\n * @example\n * ```ts\n * const direction = column_getIsSorted(column)\n * ```\n */\nexport function column_getIsSorted<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): false | SortDirection {\n const columnSort = column.table.atoms.sorting\n ?.get()\n ?.find((d) => d.id === column.id)\n return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'\n}\n\n/**\n * Finds this column's position in the ordered `state.sorting` array.\n *\n * The result is `-1` when the column is not sorted.\n *\n * @example\n * ```ts\n * const index = column_getSortIndex(column)\n * ```\n */\nexport function column_getSortIndex<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>): number {\n return (\n column.table.atoms.sorting?.get()?.findIndex((d) => d.id === column.id) ??\n -1\n )\n}\n\n/**\n * Removes this column from the sorting state.\n *\n * Other sorted columns are preserved, including their relative order.\n *\n * @example\n * ```ts\n * column_clearSorting(column)\n * ```\n */\nexport function column_clearSorting<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n // clear sorting for just 1 column\n table_setSorting(column.table, (old) =>\n old.length ? old.filter((d) => d.id !== column.id) : [],\n )\n}\n\n/**\n * Creates a header event handler that toggles this column's sorting.\n *\n * The handler ignores events when the column cannot sort, persists React-style\n * synthetic events when present, and asks `options.isMultiSortEvent` whether\n * the event should add to a multi-sort.\n *\n * @example\n * ```ts\n * const onClick = column_getToggleSortingHandler(column)\n * ```\n */\nexport function column_getToggleSortingHandler<\n TFeatures extends TableFeatures,\n TData extends RowData,\n TValue extends CellData = CellData,\n>(column: Column_Internal<TFeatures, TData, TValue>) {\n const canSort = column_getCanSort(column)\n\n return (e: unknown) => {\n if (!canSort) return\n ;(e as any).persist?.()\n column_toggleSorting(\n column,\n\n undefined,\n column_getCanMultiSort(column)\n ? column.table.options.isMultiSortEvent?.(e)\n : false,\n )\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAyBA,SAAgB,yBAAuC;CACrD,OAAO,CAAC;AACV;;;;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