@tanstack/react-table 8.0.0-alpha.8 → 8.0.0-alpha.80
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/build/cjs/react-table/src/index.js +133 -0
- package/build/cjs/react-table/src/index.js.map +1 -0
- package/build/cjs/table-core/build/esm/index.js +3951 -0
- package/build/cjs/table-core/build/esm/index.js.map +1 -0
- package/build/esm/index.js +3253 -4059
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +24 -797
- package/build/types/index.d.ts +16 -8
- package/build/umd/index.development.js +3349 -4124
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +11 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +7 -9
- package/src/index.tsx +113 -8
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -112
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
- package/build/cjs/aggregationTypes.js +0 -130
- package/build/cjs/aggregationTypes.js.map +0 -1
- package/build/cjs/core.js +0 -560
- package/build/cjs/core.js.map +0 -1
- package/build/cjs/createTable.js +0 -108
- package/build/cjs/createTable.js.map +0 -1
- package/build/cjs/features/ColumnSizing.js +0 -317
- package/build/cjs/features/ColumnSizing.js.map +0 -1
- package/build/cjs/features/Expanding.js +0 -251
- package/build/cjs/features/Expanding.js.map +0 -1
- package/build/cjs/features/Filters.js +0 -445
- package/build/cjs/features/Filters.js.map +0 -1
- package/build/cjs/features/Grouping.js +0 -249
- package/build/cjs/features/Grouping.js.map +0 -1
- package/build/cjs/features/Headers.js +0 -549
- package/build/cjs/features/Headers.js.map +0 -1
- package/build/cjs/features/Ordering.js +0 -86
- package/build/cjs/features/Ordering.js.map +0 -1
- package/build/cjs/features/Pagination.js +0 -198
- package/build/cjs/features/Pagination.js.map +0 -1
- package/build/cjs/features/Pinning.js +0 -149
- package/build/cjs/features/Pinning.js.map +0 -1
- package/build/cjs/features/RowSelection.js +0 -541
- package/build/cjs/features/RowSelection.js.map +0 -1
- package/build/cjs/features/Sorting.js +0 -327
- package/build/cjs/features/Sorting.js.map +0 -1
- package/build/cjs/features/Visibility.js +0 -166
- package/build/cjs/features/Visibility.js.map +0 -1
- package/build/cjs/filterTypes.js +0 -172
- package/build/cjs/filterTypes.js.map +0 -1
- package/build/cjs/index.js +0 -32
- package/build/cjs/index.js.map +0 -1
- package/build/cjs/sortTypes.js +0 -122
- package/build/cjs/sortTypes.js.map +0 -1
- package/build/cjs/utils/columnFilterRowsFn.js +0 -131
- package/build/cjs/utils/columnFilterRowsFn.js.map +0 -1
- package/build/cjs/utils/expandRowsFn.js +0 -38
- package/build/cjs/utils/expandRowsFn.js.map +0 -1
- package/build/cjs/utils/globalFilterRowsFn.js +0 -101
- package/build/cjs/utils/globalFilterRowsFn.js.map +0 -1
- package/build/cjs/utils/groupRowsFn.js +0 -155
- package/build/cjs/utils/groupRowsFn.js.map +0 -1
- package/build/cjs/utils/paginateRowsFn.js +0 -44
- package/build/cjs/utils/paginateRowsFn.js.map +0 -1
- package/build/cjs/utils/sortRowsFn.js +0 -94
- package/build/cjs/utils/sortRowsFn.js.map +0 -1
- package/build/cjs/utils.js +0 -141
- package/build/cjs/utils.js.map +0 -1
- package/build/types/aggregationTypes.d.ts +0 -22
- package/build/types/core.d.ts +0 -110
- package/build/types/createTable.d.ts +0 -52
- package/build/types/features/ColumnSizing.d.ts +0 -67
- package/build/types/features/Expanding.d.ts +0 -53
- package/build/types/features/Filters.d.ts +0 -98
- package/build/types/features/Grouping.d.ts +0 -82
- package/build/types/features/Headers.d.ts +0 -41
- package/build/types/features/Ordering.d.ts +0 -19
- package/build/types/features/Pagination.d.ts +0 -44
- package/build/types/features/Pinning.d.ts +0 -39
- package/build/types/features/RowSelection.d.ts +0 -66
- package/build/types/features/Sorting.d.ts +0 -78
- package/build/types/features/Visibility.d.ts +0 -47
- package/build/types/filterTypes.d.ts +0 -50
- package/build/types/sortTypes.d.ts +0 -18
- package/build/types/types.d.ts +0 -127
- package/build/types/utils/columnFilterRowsFn.d.ts +0 -2
- package/build/types/utils/expandRowsFn.d.ts +0 -2
- package/build/types/utils/globalFilterRowsFn.d.ts +0 -2
- package/build/types/utils/groupRowsFn.d.ts +0 -2
- package/build/types/utils/paginateRowsFn.d.ts +0 -2
- package/build/types/utils/sortRowsFn.d.ts +0 -2
- package/build/types/utils.d.ts +0 -24
- package/src/aggregationTypes.ts +0 -115
- package/src/core.tsx +0 -1096
- package/src/createTable.tsx +0 -244
- package/src/features/ColumnSizing.ts +0 -453
- package/src/features/Expanding.ts +0 -406
- package/src/features/Filters.ts +0 -762
- package/src/features/Grouping.ts +0 -466
- package/src/features/Headers.ts +0 -912
- package/src/features/Ordering.ts +0 -133
- package/src/features/Pagination.ts +0 -325
- package/src/features/Pinning.ts +0 -198
- package/src/features/RowSelection.ts +0 -831
- package/src/features/Sorting.ts +0 -547
- package/src/features/Visibility.ts +0 -279
- package/src/filterTypes.ts +0 -251
- package/src/sortTypes.ts +0 -159
- package/src/types.ts +0 -331
- package/src/utils/columnFilterRowsFn.ts +0 -155
- package/src/utils/expandRowsFn.ts +0 -50
- package/src/utils/globalFilterRowsFn.ts +0 -122
- package/src/utils/groupRowsFn.ts +0 -194
- package/src/utils/paginateRowsFn.ts +0 -34
- package/src/utils/sortRowsFn.ts +0 -115
- package/src/utils.tsx +0 -195
package/src/features/Sorting.ts
DELETED
|
@@ -1,547 +0,0 @@
|
|
|
1
|
-
import { MouseEvent, TouchEvent } from 'react'
|
|
2
|
-
import { RowModel } from '..'
|
|
3
|
-
import { BuiltInSortType, reSplitAlphaNumeric, sortTypes } from '../sortTypes'
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
Column,
|
|
7
|
-
Getter,
|
|
8
|
-
Header,
|
|
9
|
-
OnChangeFn,
|
|
10
|
-
PropGetterValue,
|
|
11
|
-
ReactTable,
|
|
12
|
-
Row,
|
|
13
|
-
Updater,
|
|
14
|
-
} from '../types'
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
functionalUpdate,
|
|
18
|
-
isFunction,
|
|
19
|
-
makeStateUpdater,
|
|
20
|
-
memo,
|
|
21
|
-
propGetter,
|
|
22
|
-
} from '../utils'
|
|
23
|
-
|
|
24
|
-
export type SortDirection = 'asc' | 'desc'
|
|
25
|
-
|
|
26
|
-
export type ColumnSort = {
|
|
27
|
-
id: string
|
|
28
|
-
desc: boolean
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export type SortingState = ColumnSort[]
|
|
32
|
-
|
|
33
|
-
export type SortingFn<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> =
|
|
34
|
-
{
|
|
35
|
-
(
|
|
36
|
-
rowA: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
|
|
37
|
-
rowB: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
|
|
38
|
-
columnId: string
|
|
39
|
-
): number
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type SortingTableState = {
|
|
43
|
-
sorting: SortingState
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type SortType<TSortingFns> =
|
|
47
|
-
| 'auto'
|
|
48
|
-
| BuiltInSortType
|
|
49
|
-
| keyof TSortingFns
|
|
50
|
-
| SortingFn<any, any, any, TSortingFns, any>
|
|
51
|
-
|
|
52
|
-
export type SortingColumnDef<TFilterFns> = {
|
|
53
|
-
sortType?: SortType<TFilterFns>
|
|
54
|
-
sortDescFirst?: boolean
|
|
55
|
-
enableSorting?: boolean
|
|
56
|
-
enableMultiSort?: boolean
|
|
57
|
-
defaultCanSort?: boolean
|
|
58
|
-
invertSorting?: boolean
|
|
59
|
-
sortUndefined?: false | -1 | 1
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type SortingColumn<
|
|
63
|
-
_TData,
|
|
64
|
-
_TValue,
|
|
65
|
-
TFilterFns,
|
|
66
|
-
_TSortingFns,
|
|
67
|
-
_TAggregationFns
|
|
68
|
-
> = {
|
|
69
|
-
sortType: SortType<TFilterFns>
|
|
70
|
-
getCanSort: () => boolean
|
|
71
|
-
getCanMultiSort: () => boolean
|
|
72
|
-
getSortIndex: () => number
|
|
73
|
-
getIsSorted: () => false | SortDirection
|
|
74
|
-
toggleSorting: (desc?: boolean, isMulti?: boolean) => void
|
|
75
|
-
getToggleSortingProps: <TGetter extends Getter<ToggleSortingProps>>(
|
|
76
|
-
userProps?: TGetter
|
|
77
|
-
) => undefined | PropGetterValue<ToggleSortingProps, TGetter>
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export type SortingOptions<
|
|
81
|
-
TData,
|
|
82
|
-
TValue,
|
|
83
|
-
TFilterFns,
|
|
84
|
-
TSortingFns,
|
|
85
|
-
TAggregationFns
|
|
86
|
-
> = {
|
|
87
|
-
sortTypes?: TSortingFns
|
|
88
|
-
onSortingChange?: OnChangeFn<SortingState>
|
|
89
|
-
autoResetSorting?: boolean
|
|
90
|
-
enableSorting?: boolean
|
|
91
|
-
enableSortingRemoval?: boolean
|
|
92
|
-
enableMultiRemove?: boolean
|
|
93
|
-
enableMultiSort?: boolean
|
|
94
|
-
sortDescFirst?: boolean
|
|
95
|
-
sortRowsFn?: (
|
|
96
|
-
instance: ReactTable<
|
|
97
|
-
TData,
|
|
98
|
-
TValue,
|
|
99
|
-
TFilterFns,
|
|
100
|
-
TSortingFns,
|
|
101
|
-
TAggregationFns
|
|
102
|
-
>,
|
|
103
|
-
rowModel: RowModel<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
|
|
104
|
-
) => RowModel<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
|
|
105
|
-
maxMultiSortColCount?: number
|
|
106
|
-
isMultiSortEvent?: (e: MouseEvent | TouchEvent) => boolean
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export type ToggleSortingProps = {
|
|
110
|
-
title?: string
|
|
111
|
-
onClick?: (event: MouseEvent | TouchEvent) => void
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export type SortingInstance<
|
|
115
|
-
TData,
|
|
116
|
-
TValue,
|
|
117
|
-
TFilterFns,
|
|
118
|
-
TSortingFns,
|
|
119
|
-
TAggregationFns
|
|
120
|
-
> = {
|
|
121
|
-
_notifySortingReset: () => void
|
|
122
|
-
getColumnAutoSortingFn: (
|
|
123
|
-
columnId: string
|
|
124
|
-
) => SortingFn<any, any, any, any, any> | undefined
|
|
125
|
-
getColumnAutoSortDir: (columnId: string) => SortDirection
|
|
126
|
-
|
|
127
|
-
getColumnSortingFn: (
|
|
128
|
-
columnId: string
|
|
129
|
-
) => SortingFn<any, any, any, any, any> | undefined
|
|
130
|
-
|
|
131
|
-
setSorting: (updater: Updater<SortingState>) => void
|
|
132
|
-
toggleColumnSorting: (
|
|
133
|
-
columnId: string,
|
|
134
|
-
desc?: boolean,
|
|
135
|
-
multi?: boolean
|
|
136
|
-
) => void
|
|
137
|
-
resetSorting: () => void
|
|
138
|
-
getColumnCanSort: (columnId: string) => boolean
|
|
139
|
-
getColumnCanMultiSort: (columnId: string) => boolean
|
|
140
|
-
getColumnIsSorted: (columnId: string) => false | 'asc' | 'desc'
|
|
141
|
-
getColumnSortIndex: (columnId: string) => number
|
|
142
|
-
getToggleSortingProps: <TGetter extends Getter<ToggleSortingProps>>(
|
|
143
|
-
columnId: string,
|
|
144
|
-
userProps?: TGetter
|
|
145
|
-
) => undefined | PropGetterValue<ToggleSortingProps, TGetter>
|
|
146
|
-
getSortedRowModel: () => RowModel<
|
|
147
|
-
TData,
|
|
148
|
-
TValue,
|
|
149
|
-
TFilterFns,
|
|
150
|
-
TSortingFns,
|
|
151
|
-
TAggregationFns
|
|
152
|
-
>
|
|
153
|
-
getPreSortedRows: () => Row<
|
|
154
|
-
TData,
|
|
155
|
-
TValue,
|
|
156
|
-
TFilterFns,
|
|
157
|
-
TSortingFns,
|
|
158
|
-
TAggregationFns
|
|
159
|
-
>[]
|
|
160
|
-
getPreSortedFlatRows: () => Row<
|
|
161
|
-
TData,
|
|
162
|
-
TValue,
|
|
163
|
-
TFilterFns,
|
|
164
|
-
TSortingFns,
|
|
165
|
-
TAggregationFns
|
|
166
|
-
>[]
|
|
167
|
-
getPreSortedRowsById: () => Record<
|
|
168
|
-
string,
|
|
169
|
-
Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
|
|
170
|
-
>
|
|
171
|
-
getSortedRows: () => Row<
|
|
172
|
-
TData,
|
|
173
|
-
TValue,
|
|
174
|
-
TFilterFns,
|
|
175
|
-
TSortingFns,
|
|
176
|
-
TAggregationFns
|
|
177
|
-
>[]
|
|
178
|
-
getSortedFlatRows: () => Row<
|
|
179
|
-
TData,
|
|
180
|
-
TValue,
|
|
181
|
-
TFilterFns,
|
|
182
|
-
TSortingFns,
|
|
183
|
-
TAggregationFns
|
|
184
|
-
>[]
|
|
185
|
-
getSortedRowsById: () => Record<
|
|
186
|
-
string,
|
|
187
|
-
Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
|
|
188
|
-
>
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
//
|
|
192
|
-
|
|
193
|
-
export function getDefaultColumn<TFilterFns>(): SortingColumnDef<TFilterFns> {
|
|
194
|
-
return {
|
|
195
|
-
sortType: 'auto',
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export function getInitialState(): SortingTableState {
|
|
200
|
-
return {
|
|
201
|
-
sorting: [],
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export function getDefaultOptions<
|
|
206
|
-
TData,
|
|
207
|
-
TValue,
|
|
208
|
-
TFilterFns,
|
|
209
|
-
TSortingFns,
|
|
210
|
-
TAggregationFns
|
|
211
|
-
>(
|
|
212
|
-
instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
|
|
213
|
-
): SortingOptions<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
|
|
214
|
-
return {
|
|
215
|
-
onSortingChange: makeStateUpdater('sorting', instance),
|
|
216
|
-
autoResetSorting: true,
|
|
217
|
-
isMultiSortEvent: (e: MouseEvent | TouchEvent) => {
|
|
218
|
-
return e.shiftKey
|
|
219
|
-
},
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export function createColumn<
|
|
224
|
-
TData,
|
|
225
|
-
TValue,
|
|
226
|
-
TFilterFns,
|
|
227
|
-
TSortingFns,
|
|
228
|
-
TAggregationFns
|
|
229
|
-
>(
|
|
230
|
-
column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
|
|
231
|
-
instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
|
|
232
|
-
): SortingColumn<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
|
|
233
|
-
return {
|
|
234
|
-
sortType: column.sortType,
|
|
235
|
-
getCanSort: () => instance.getColumnCanSort(column.id),
|
|
236
|
-
getCanMultiSort: () => instance.getColumnCanMultiSort(column.id),
|
|
237
|
-
getSortIndex: () => instance.getColumnSortIndex(column.id),
|
|
238
|
-
getIsSorted: () => instance.getColumnIsSorted(column.id),
|
|
239
|
-
toggleSorting: (desc, isMulti) =>
|
|
240
|
-
instance.toggleColumnSorting(column.id, desc, isMulti),
|
|
241
|
-
getToggleSortingProps: userProps =>
|
|
242
|
-
instance.getToggleSortingProps(column.id, userProps),
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export function getInstance<
|
|
247
|
-
TData,
|
|
248
|
-
TValue,
|
|
249
|
-
TFilterFns,
|
|
250
|
-
TSortingFns,
|
|
251
|
-
TAggregationFns
|
|
252
|
-
>(
|
|
253
|
-
instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
|
|
254
|
-
): SortingInstance<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
|
|
255
|
-
let registered = false
|
|
256
|
-
|
|
257
|
-
return {
|
|
258
|
-
_notifySortingReset: () => {
|
|
259
|
-
if (!registered) {
|
|
260
|
-
registered = true
|
|
261
|
-
return
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (instance.options.autoResetAll === false) {
|
|
265
|
-
return
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if (
|
|
269
|
-
instance.options.autoResetAll === true ||
|
|
270
|
-
instance.options.autoResetSorting
|
|
271
|
-
) {
|
|
272
|
-
instance.resetSorting()
|
|
273
|
-
}
|
|
274
|
-
},
|
|
275
|
-
getColumnAutoSortingFn: columnId => {
|
|
276
|
-
const firstRows = instance.getGlobalFilteredRowModel().flatRows.slice(100)
|
|
277
|
-
|
|
278
|
-
let isString = false
|
|
279
|
-
|
|
280
|
-
for (const row of firstRows) {
|
|
281
|
-
const value = row?.values[columnId]
|
|
282
|
-
|
|
283
|
-
if (Object.prototype.toString.call(value) === '[object Date]') {
|
|
284
|
-
return sortTypes.datetime
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
if (typeof value === 'string') {
|
|
288
|
-
isString = true
|
|
289
|
-
|
|
290
|
-
if (value.split(reSplitAlphaNumeric).length > 1) {
|
|
291
|
-
return sortTypes.alphanumeric
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (isString) {
|
|
297
|
-
return sortTypes.text
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
return sortTypes.basic
|
|
301
|
-
},
|
|
302
|
-
getColumnAutoSortDir: columnId => {
|
|
303
|
-
const firstRow = instance.getGlobalFilteredRowModel().flatRows[0]
|
|
304
|
-
|
|
305
|
-
const value = firstRow?.values[columnId]
|
|
306
|
-
|
|
307
|
-
if (typeof value === 'string') {
|
|
308
|
-
return 'asc'
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
return 'desc'
|
|
312
|
-
},
|
|
313
|
-
getColumnSortingFn: columnId => {
|
|
314
|
-
const column = instance.getColumn(columnId)
|
|
315
|
-
const userSortTypes = instance.options.sortTypes
|
|
316
|
-
|
|
317
|
-
if (!column) {
|
|
318
|
-
throw new Error()
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
return isFunction(column.sortType)
|
|
322
|
-
? column.sortType
|
|
323
|
-
: column.sortType === 'auto'
|
|
324
|
-
? instance.getColumnAutoSortingFn(columnId)
|
|
325
|
-
: (userSortTypes as Record<string, any>)?.[column.sortType as string] ??
|
|
326
|
-
(sortTypes[column.sortType as BuiltInSortType] as SortingFn<
|
|
327
|
-
TData,
|
|
328
|
-
TValue,
|
|
329
|
-
TFilterFns,
|
|
330
|
-
TSortingFns,
|
|
331
|
-
TAggregationFns
|
|
332
|
-
>)
|
|
333
|
-
},
|
|
334
|
-
|
|
335
|
-
setSorting: updater =>
|
|
336
|
-
instance.options.onSortingChange?.(
|
|
337
|
-
updater,
|
|
338
|
-
functionalUpdate(updater, instance.getState().sorting)
|
|
339
|
-
),
|
|
340
|
-
|
|
341
|
-
toggleColumnSorting: (columnId, desc, multi) => {
|
|
342
|
-
const column = instance.getColumn(columnId)
|
|
343
|
-
|
|
344
|
-
if (!column) {
|
|
345
|
-
throw new Error()
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// if (column.columns.length) {
|
|
349
|
-
// column.columns.forEach((c, i) => {
|
|
350
|
-
// if (c.id) {
|
|
351
|
-
// instance.toggleColumnSorting(c.id, undefined, multi || !!i)
|
|
352
|
-
// }
|
|
353
|
-
// })
|
|
354
|
-
// return
|
|
355
|
-
// }
|
|
356
|
-
|
|
357
|
-
instance.setSorting(old => {
|
|
358
|
-
// Find any existing sorting for this column
|
|
359
|
-
const existingSorting = old?.find(d => d.id === columnId)
|
|
360
|
-
const existingIndex = old?.findIndex(d => d.id === columnId)
|
|
361
|
-
const hasDescDefined = typeof desc !== 'undefined' && desc !== null
|
|
362
|
-
|
|
363
|
-
let newSorting: SortingState = []
|
|
364
|
-
|
|
365
|
-
// What should we do with this sort action?
|
|
366
|
-
let sortAction
|
|
367
|
-
|
|
368
|
-
if (column.getCanMultiSort() && multi) {
|
|
369
|
-
if (existingSorting) {
|
|
370
|
-
sortAction = 'toggle'
|
|
371
|
-
} else {
|
|
372
|
-
sortAction = 'add'
|
|
373
|
-
}
|
|
374
|
-
} else {
|
|
375
|
-
// Normal mode
|
|
376
|
-
if (old?.length && existingIndex !== old.length - 1) {
|
|
377
|
-
sortAction = 'replace'
|
|
378
|
-
} else if (existingSorting) {
|
|
379
|
-
sortAction = 'toggle'
|
|
380
|
-
} else {
|
|
381
|
-
sortAction = 'replace'
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
const sortDescFirst =
|
|
386
|
-
column.sortDescFirst ??
|
|
387
|
-
instance.options.sortDescFirst ??
|
|
388
|
-
instance.getColumnAutoSortDir(columnId) === 'desc'
|
|
389
|
-
|
|
390
|
-
// Handle toggle states that will remove the sorting
|
|
391
|
-
if (
|
|
392
|
-
sortAction === 'toggle' && // Must be toggling
|
|
393
|
-
(instance.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
|
|
394
|
-
!hasDescDefined && // Must not be setting desc
|
|
395
|
-
(multi ? instance.options.enableMultiRemove ?? true : true) && // If multi, don't allow if enableMultiRemove
|
|
396
|
-
(existingSorting?.desc // Finally, detect if it should indeed be removed
|
|
397
|
-
? !sortDescFirst
|
|
398
|
-
: sortDescFirst)
|
|
399
|
-
) {
|
|
400
|
-
sortAction = 'remove'
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
if (sortAction === 'replace') {
|
|
404
|
-
newSorting = [
|
|
405
|
-
{
|
|
406
|
-
id: columnId,
|
|
407
|
-
desc: hasDescDefined ? desc! : !!sortDescFirst,
|
|
408
|
-
},
|
|
409
|
-
]
|
|
410
|
-
} else if (sortAction === 'add' && old?.length) {
|
|
411
|
-
newSorting = [
|
|
412
|
-
...old,
|
|
413
|
-
{
|
|
414
|
-
id: columnId,
|
|
415
|
-
desc: hasDescDefined ? desc! : !!sortDescFirst,
|
|
416
|
-
},
|
|
417
|
-
]
|
|
418
|
-
// Take latest n columns
|
|
419
|
-
newSorting.splice(
|
|
420
|
-
0,
|
|
421
|
-
newSorting.length -
|
|
422
|
-
(instance.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
|
|
423
|
-
)
|
|
424
|
-
} else if (sortAction === 'toggle' && old?.length) {
|
|
425
|
-
// This flips (or sets) the
|
|
426
|
-
newSorting = old.map(d => {
|
|
427
|
-
if (d.id === columnId) {
|
|
428
|
-
return {
|
|
429
|
-
...d,
|
|
430
|
-
desc: hasDescDefined ? desc! : !existingSorting?.desc,
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
return d
|
|
434
|
-
})
|
|
435
|
-
} else if (sortAction === 'remove' && old?.length) {
|
|
436
|
-
newSorting = old.filter(d => d.id !== columnId)
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
return newSorting
|
|
440
|
-
})
|
|
441
|
-
},
|
|
442
|
-
|
|
443
|
-
getColumnCanSort: columnId => {
|
|
444
|
-
const column = instance.getColumn(columnId)
|
|
445
|
-
|
|
446
|
-
if (!column) {
|
|
447
|
-
throw new Error()
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
return (
|
|
451
|
-
column.enableSorting ??
|
|
452
|
-
instance.options.enableSorting ??
|
|
453
|
-
column.defaultCanSort ??
|
|
454
|
-
!!column.accessorFn
|
|
455
|
-
// (!!column.accessorFn ||
|
|
456
|
-
// column.columns?.some(c => c.id && instance.getColumnCanSort(c.id))) ??
|
|
457
|
-
// false
|
|
458
|
-
)
|
|
459
|
-
},
|
|
460
|
-
|
|
461
|
-
getColumnCanMultiSort: columnId => {
|
|
462
|
-
const column = instance.getColumn(columnId)
|
|
463
|
-
|
|
464
|
-
if (!column) {
|
|
465
|
-
throw new Error()
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
return (
|
|
469
|
-
column.enableMultiSort ??
|
|
470
|
-
instance.options.enableMultiSort ??
|
|
471
|
-
!!column.accessorFn
|
|
472
|
-
)
|
|
473
|
-
},
|
|
474
|
-
|
|
475
|
-
getColumnIsSorted: columnId => {
|
|
476
|
-
const columnSort = instance
|
|
477
|
-
.getState()
|
|
478
|
-
.sorting?.find(d => d.id === columnId)
|
|
479
|
-
|
|
480
|
-
return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'
|
|
481
|
-
},
|
|
482
|
-
|
|
483
|
-
getColumnSortIndex: columnId =>
|
|
484
|
-
instance.getState().sorting?.findIndex(d => d.id === columnId) ?? -1,
|
|
485
|
-
|
|
486
|
-
resetSorting: () => {
|
|
487
|
-
instance.setSorting(instance.initialState?.sorting ?? [])
|
|
488
|
-
},
|
|
489
|
-
|
|
490
|
-
getToggleSortingProps: (columnId, userProps) => {
|
|
491
|
-
const column = instance.getColumn(columnId)
|
|
492
|
-
|
|
493
|
-
if (!column) {
|
|
494
|
-
throw new Error()
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
const canSort = column.getCanSort()
|
|
498
|
-
|
|
499
|
-
const initialProps: ToggleSortingProps = {
|
|
500
|
-
title: canSort ? 'Toggle Sorting' : undefined,
|
|
501
|
-
onClick: canSort
|
|
502
|
-
? (e: MouseEvent | TouchEvent) => {
|
|
503
|
-
e.persist()
|
|
504
|
-
column.toggleSorting?.(
|
|
505
|
-
undefined,
|
|
506
|
-
column.getCanMultiSort()
|
|
507
|
-
? instance.options.isMultiSortEvent?.(e)
|
|
508
|
-
: false
|
|
509
|
-
)
|
|
510
|
-
}
|
|
511
|
-
: undefined,
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
return propGetter(initialProps, userProps)
|
|
515
|
-
},
|
|
516
|
-
|
|
517
|
-
getSortedRowModel: memo(
|
|
518
|
-
() => [
|
|
519
|
-
instance.getState().sorting,
|
|
520
|
-
instance.getGlobalFilteredRowModel(),
|
|
521
|
-
instance.options.sortRowsFn,
|
|
522
|
-
],
|
|
523
|
-
(sorting, rowModel, sortingFn) => {
|
|
524
|
-
if (!sortingFn || !sorting?.length) {
|
|
525
|
-
return rowModel
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
if (process.env.NODE_ENV !== 'production' && instance.options.debug)
|
|
529
|
-
console.info('Sorting...')
|
|
530
|
-
|
|
531
|
-
return sortingFn(instance, rowModel)
|
|
532
|
-
},
|
|
533
|
-
{
|
|
534
|
-
key: 'getSortedRowModel',
|
|
535
|
-
debug: instance.options.debug,
|
|
536
|
-
onChange: () => instance._notifyGroupingReset(),
|
|
537
|
-
}
|
|
538
|
-
),
|
|
539
|
-
|
|
540
|
-
getPreSortedRows: () => instance.getGlobalFilteredRowModel().rows,
|
|
541
|
-
getPreSortedFlatRows: () => instance.getGlobalFilteredRowModel().flatRows,
|
|
542
|
-
getPreSortedRowsById: () => instance.getGlobalFilteredRowModel().rowsById,
|
|
543
|
-
getSortedRows: () => instance.getSortedRowModel().rows,
|
|
544
|
-
getSortedFlatRows: () => instance.getSortedRowModel().flatRows,
|
|
545
|
-
getSortedRowsById: () => instance.getSortedRowModel().rowsById,
|
|
546
|
-
}
|
|
547
|
-
}
|