@tanstack/react-table 8.0.0-alpha.9 → 8.0.0-beta.2
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 +139 -0
- package/build/cjs/react-table/src/index.js.map +1 -0
- package/build/cjs/table-core/build/esm/index.js +3867 -0
- package/build/cjs/table-core/build/esm/index.js.map +1 -0
- package/build/esm/index.js +3196 -4117
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +24 -813
- package/build/types/index.d.ts +16 -9
- package/build/umd/index.development.js +3234 -4125
- 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 +10 -6
- package/src/index.tsx +111 -9
- 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 -545
- package/build/cjs/core.js.map +0 -1
- package/build/cjs/createTable.js +0 -122
- package/build/cjs/createTable.js.map +0 -1
- package/build/cjs/features/ColumnSizing.js +0 -318
- package/build/cjs/features/ColumnSizing.js.map +0 -1
- package/build/cjs/features/Expanding.js +0 -239
- package/build/cjs/features/Expanding.js.map +0 -1
- package/build/cjs/features/Filters.js +0 -414
- package/build/cjs/features/Filters.js.map +0 -1
- package/build/cjs/features/Grouping.js +0 -232
- package/build/cjs/features/Grouping.js.map +0 -1
- package/build/cjs/features/Headers.js +0 -629
- 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 -193
- 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 -526
- package/build/cjs/features/RowSelection.js.map +0 -1
- package/build/cjs/features/Sorting.js +0 -315
- package/build/cjs/features/Sorting.js.map +0 -1
- package/build/cjs/features/Visibility.js +0 -174
- 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 -44
- 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/types.js +0 -22
- package/build/cjs/types.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 -167
- package/build/cjs/utils.js.map +0 -1
- package/build/types/aggregationTypes.d.ts +0 -22
- package/build/types/core.d.ts +0 -105
- package/build/types/createTable.d.ts +0 -42
- package/build/types/features/ColumnSizing.d.ts +0 -73
- package/build/types/features/Expanding.d.ts +0 -48
- package/build/types/features/Filters.d.ts +0 -90
- package/build/types/features/Grouping.d.ts +0 -83
- 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 -39
- package/build/types/features/Pinning.d.ts +0 -39
- package/build/types/features/RowSelection.d.ts +0 -58
- package/build/types/features/Sorting.d.ts +0 -79
- 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 -138
- 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 -23
- package/src/aggregationTypes.ts +0 -115
- package/src/core.tsx +0 -763
- package/src/createTable.tsx +0 -186
- package/src/features/ColumnSizing.ts +0 -424
- package/src/features/Expanding.ts +0 -318
- package/src/features/Filters.ts +0 -560
- package/src/features/Grouping.ts +0 -361
- package/src/features/Headers.ts +0 -729
- package/src/features/Ordering.ts +0 -110
- package/src/features/Pagination.ts +0 -250
- package/src/features/Pinning.ts +0 -174
- package/src/features/RowSelection.ts +0 -651
- package/src/features/Sorting.ts +0 -453
- package/src/features/Visibility.ts +0 -238
- package/src/filterTypes.ts +0 -188
- package/src/sortTypes.ts +0 -147
- package/src/types.ts +0 -311
- package/src/utils/columnFilterRowsFn.ts +0 -113
- package/src/utils/expandRowsFn.ts +0 -30
- package/src/utils/globalFilterRowsFn.ts +0 -89
- package/src/utils/groupRowsFn.ts +0 -170
- package/src/utils/paginateRowsFn.ts +0 -28
- package/src/utils/sortRowsFn.ts +0 -95
- package/src/utils.tsx +0 -221
package/src/features/Sorting.ts
DELETED
|
@@ -1,453 +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
|
-
PartialGenerics,
|
|
11
|
-
PropGetterValue,
|
|
12
|
-
TableInstance,
|
|
13
|
-
Row,
|
|
14
|
-
Updater,
|
|
15
|
-
} from '../types'
|
|
16
|
-
|
|
17
|
-
import {
|
|
18
|
-
functionalUpdate,
|
|
19
|
-
isFunction,
|
|
20
|
-
makeStateUpdater,
|
|
21
|
-
memo,
|
|
22
|
-
Overwrite,
|
|
23
|
-
propGetter,
|
|
24
|
-
} from '../utils'
|
|
25
|
-
|
|
26
|
-
export type SortDirection = 'asc' | 'desc'
|
|
27
|
-
|
|
28
|
-
export type ColumnSort = {
|
|
29
|
-
id: string
|
|
30
|
-
desc: boolean
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type SortingState = ColumnSort[]
|
|
34
|
-
|
|
35
|
-
export type SortingFn<TGenerics extends PartialGenerics> = {
|
|
36
|
-
(rowA: Row<TGenerics>, rowB: Row<TGenerics>, columnId: string): number
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export type CustomSortingTypes<TGenerics extends PartialGenerics> = Record<
|
|
40
|
-
string,
|
|
41
|
-
SortingFn<TGenerics>
|
|
42
|
-
>
|
|
43
|
-
|
|
44
|
-
export type SortingTableState = {
|
|
45
|
-
sorting: SortingState
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export type SortType<TGenerics extends PartialGenerics> =
|
|
49
|
-
| 'auto'
|
|
50
|
-
| BuiltInSortType
|
|
51
|
-
| keyof TGenerics['SortingFns']
|
|
52
|
-
| SortingFn<TGenerics>
|
|
53
|
-
|
|
54
|
-
export type SortingColumnDef<TGenerics extends PartialGenerics> = {
|
|
55
|
-
sortType?: SortType<Overwrite<TGenerics, { Value: any }>>
|
|
56
|
-
sortDescFirst?: boolean
|
|
57
|
-
enableSorting?: boolean
|
|
58
|
-
enableMultiSort?: boolean
|
|
59
|
-
defaultCanSort?: boolean
|
|
60
|
-
invertSorting?: boolean
|
|
61
|
-
sortUndefined?: false | -1 | 1
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type SortingColumn<TGenerics extends PartialGenerics> = {
|
|
65
|
-
sortType: SortType<Overwrite<TGenerics, { Value: any }>>
|
|
66
|
-
getCanSort: () => boolean
|
|
67
|
-
getCanMultiSort: () => boolean
|
|
68
|
-
getSortIndex: () => number
|
|
69
|
-
getIsSorted: () => false | SortDirection
|
|
70
|
-
toggleSorting: (desc?: boolean, isMulti?: boolean) => void
|
|
71
|
-
getToggleSortingProps: <TGetter extends Getter<ToggleSortingProps>>(
|
|
72
|
-
userProps?: TGetter
|
|
73
|
-
) => undefined | PropGetterValue<ToggleSortingProps, TGetter>
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export type SortingOptions<TGenerics extends PartialGenerics> = {
|
|
77
|
-
sortTypes?: TGenerics['SortingFns']
|
|
78
|
-
onSortingChange?: OnChangeFn<SortingState>
|
|
79
|
-
autoResetSorting?: boolean
|
|
80
|
-
enableSorting?: boolean
|
|
81
|
-
enableSortingRemoval?: boolean
|
|
82
|
-
enableMultiRemove?: boolean
|
|
83
|
-
enableMultiSort?: boolean
|
|
84
|
-
sortDescFirst?: boolean
|
|
85
|
-
sortRowsFn?: (
|
|
86
|
-
instance: TableInstance<TGenerics>,
|
|
87
|
-
rowModel: RowModel<TGenerics>
|
|
88
|
-
) => RowModel<TGenerics>
|
|
89
|
-
maxMultiSortColCount?: number
|
|
90
|
-
isMultiSortEvent?: (e: MouseEvent | TouchEvent) => boolean
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export type ToggleSortingProps = {
|
|
94
|
-
title?: string
|
|
95
|
-
onClick?: (event: MouseEvent | TouchEvent) => void
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export type SortingInstance<TGenerics extends PartialGenerics> = {
|
|
99
|
-
_notifySortingReset: () => void
|
|
100
|
-
getColumnAutoSortingFn: (columnId: string) => SortingFn<TGenerics> | undefined
|
|
101
|
-
getColumnAutoSortDir: (columnId: string) => SortDirection
|
|
102
|
-
|
|
103
|
-
getColumnSortingFn: (columnId: string) => SortingFn<TGenerics> | undefined
|
|
104
|
-
|
|
105
|
-
setSorting: (updater: Updater<SortingState>) => void
|
|
106
|
-
toggleColumnSorting: (
|
|
107
|
-
columnId: string,
|
|
108
|
-
desc?: boolean,
|
|
109
|
-
multi?: boolean
|
|
110
|
-
) => void
|
|
111
|
-
resetSorting: () => void
|
|
112
|
-
getColumnCanSort: (columnId: string) => boolean
|
|
113
|
-
getColumnCanMultiSort: (columnId: string) => boolean
|
|
114
|
-
getColumnIsSorted: (columnId: string) => false | 'asc' | 'desc'
|
|
115
|
-
getColumnSortIndex: (columnId: string) => number
|
|
116
|
-
getToggleSortingProps: <TGetter extends Getter<ToggleSortingProps>>(
|
|
117
|
-
columnId: string,
|
|
118
|
-
userProps?: TGetter
|
|
119
|
-
) => undefined | PropGetterValue<ToggleSortingProps, TGetter>
|
|
120
|
-
getPreSortedRowModel: () => RowModel<TGenerics>
|
|
121
|
-
getSortedRowModel: () => RowModel<TGenerics>
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
export function getDefaultColumn<
|
|
127
|
-
TGenerics extends PartialGenerics
|
|
128
|
-
>(): SortingColumnDef<TGenerics> {
|
|
129
|
-
return {
|
|
130
|
-
sortType: 'auto',
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export function getInitialState(): SortingTableState {
|
|
135
|
-
return {
|
|
136
|
-
sorting: [],
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export function getDefaultOptions<TGenerics extends PartialGenerics>(
|
|
141
|
-
instance: TableInstance<TGenerics>
|
|
142
|
-
): SortingOptions<TGenerics> {
|
|
143
|
-
return {
|
|
144
|
-
onSortingChange: makeStateUpdater('sorting', instance),
|
|
145
|
-
autoResetSorting: true,
|
|
146
|
-
isMultiSortEvent: (e: MouseEvent | TouchEvent) => {
|
|
147
|
-
return e.shiftKey
|
|
148
|
-
},
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export function createColumn<TGenerics extends PartialGenerics>(
|
|
153
|
-
column: Column<TGenerics>,
|
|
154
|
-
instance: TableInstance<TGenerics>
|
|
155
|
-
): SortingColumn<TGenerics> {
|
|
156
|
-
return {
|
|
157
|
-
sortType: column.sortType,
|
|
158
|
-
getCanSort: () => instance.getColumnCanSort(column.id),
|
|
159
|
-
getCanMultiSort: () => instance.getColumnCanMultiSort(column.id),
|
|
160
|
-
getSortIndex: () => instance.getColumnSortIndex(column.id),
|
|
161
|
-
getIsSorted: () => instance.getColumnIsSorted(column.id),
|
|
162
|
-
toggleSorting: (desc, isMulti) =>
|
|
163
|
-
instance.toggleColumnSorting(column.id, desc, isMulti),
|
|
164
|
-
getToggleSortingProps: userProps =>
|
|
165
|
-
instance.getToggleSortingProps(column.id, userProps),
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export function getInstance<TGenerics extends PartialGenerics>(
|
|
170
|
-
instance: TableInstance<TGenerics>
|
|
171
|
-
): SortingInstance<TGenerics> {
|
|
172
|
-
let registered = false
|
|
173
|
-
|
|
174
|
-
return {
|
|
175
|
-
_notifySortingReset: () => {
|
|
176
|
-
if (!registered) {
|
|
177
|
-
registered = true
|
|
178
|
-
return
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (instance.options.autoResetAll === false) {
|
|
182
|
-
return
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (
|
|
186
|
-
instance.options.autoResetAll === true ||
|
|
187
|
-
instance.options.autoResetSorting
|
|
188
|
-
) {
|
|
189
|
-
instance.resetSorting()
|
|
190
|
-
}
|
|
191
|
-
},
|
|
192
|
-
getColumnAutoSortingFn: columnId => {
|
|
193
|
-
const firstRows = instance.getGlobalFilteredRowModel().flatRows.slice(100)
|
|
194
|
-
|
|
195
|
-
let isString = false
|
|
196
|
-
|
|
197
|
-
for (const row of firstRows) {
|
|
198
|
-
const value = row?.values[columnId]
|
|
199
|
-
|
|
200
|
-
if (Object.prototype.toString.call(value) === '[object Date]') {
|
|
201
|
-
return sortTypes.datetime
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (typeof value === 'string') {
|
|
205
|
-
isString = true
|
|
206
|
-
|
|
207
|
-
if (value.split(reSplitAlphaNumeric).length > 1) {
|
|
208
|
-
return sortTypes.alphanumeric
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (isString) {
|
|
214
|
-
return sortTypes.text
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return sortTypes.basic
|
|
218
|
-
},
|
|
219
|
-
getColumnAutoSortDir: columnId => {
|
|
220
|
-
const firstRow = instance.getGlobalFilteredRowModel().flatRows[0]
|
|
221
|
-
|
|
222
|
-
const value = firstRow?.values[columnId]
|
|
223
|
-
|
|
224
|
-
if (typeof value === 'string') {
|
|
225
|
-
return 'asc'
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return 'desc'
|
|
229
|
-
},
|
|
230
|
-
getColumnSortingFn: columnId => {
|
|
231
|
-
const column = instance.getColumn(columnId)
|
|
232
|
-
const userSortTypes = instance.options.sortTypes
|
|
233
|
-
|
|
234
|
-
if (!column) {
|
|
235
|
-
throw new Error()
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return isFunction(column.sortType)
|
|
239
|
-
? column.sortType
|
|
240
|
-
: column.sortType === 'auto'
|
|
241
|
-
? instance.getColumnAutoSortingFn(columnId)
|
|
242
|
-
: (userSortTypes as Record<string, any>)?.[column.sortType as string] ??
|
|
243
|
-
(sortTypes[
|
|
244
|
-
column.sortType as BuiltInSortType
|
|
245
|
-
] as SortingFn<TGenerics>)
|
|
246
|
-
},
|
|
247
|
-
|
|
248
|
-
setSorting: updater =>
|
|
249
|
-
instance.options.onSortingChange?.(
|
|
250
|
-
updater,
|
|
251
|
-
functionalUpdate(updater, instance.getState().sorting)
|
|
252
|
-
),
|
|
253
|
-
|
|
254
|
-
toggleColumnSorting: (columnId, desc, multi) => {
|
|
255
|
-
const column = instance.getColumn(columnId)
|
|
256
|
-
|
|
257
|
-
if (!column) {
|
|
258
|
-
throw new Error()
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// if (column.columns.length) {
|
|
262
|
-
// column.columns.forEach((c, i) => {
|
|
263
|
-
// if (c.id) {
|
|
264
|
-
// instance.toggleColumnSorting(c.id, undefined, multi || !!i)
|
|
265
|
-
// }
|
|
266
|
-
// })
|
|
267
|
-
// return
|
|
268
|
-
// }
|
|
269
|
-
|
|
270
|
-
instance.setSorting(old => {
|
|
271
|
-
// Find any existing sorting for this column
|
|
272
|
-
const existingSorting = old?.find(d => d.id === columnId)
|
|
273
|
-
const existingIndex = old?.findIndex(d => d.id === columnId)
|
|
274
|
-
const hasDescDefined = typeof desc !== 'undefined' && desc !== null
|
|
275
|
-
|
|
276
|
-
let newSorting: SortingState = []
|
|
277
|
-
|
|
278
|
-
// What should we do with this sort action?
|
|
279
|
-
let sortAction
|
|
280
|
-
|
|
281
|
-
if (column.getCanMultiSort() && multi) {
|
|
282
|
-
if (existingSorting) {
|
|
283
|
-
sortAction = 'toggle'
|
|
284
|
-
} else {
|
|
285
|
-
sortAction = 'add'
|
|
286
|
-
}
|
|
287
|
-
} else {
|
|
288
|
-
// Normal mode
|
|
289
|
-
if (old?.length && existingIndex !== old.length - 1) {
|
|
290
|
-
sortAction = 'replace'
|
|
291
|
-
} else if (existingSorting) {
|
|
292
|
-
sortAction = 'toggle'
|
|
293
|
-
} else {
|
|
294
|
-
sortAction = 'replace'
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
const sortDescFirst =
|
|
299
|
-
column.sortDescFirst ??
|
|
300
|
-
instance.options.sortDescFirst ??
|
|
301
|
-
instance.getColumnAutoSortDir(columnId) === 'desc'
|
|
302
|
-
|
|
303
|
-
// Handle toggle states that will remove the sorting
|
|
304
|
-
if (
|
|
305
|
-
sortAction === 'toggle' && // Must be toggling
|
|
306
|
-
(instance.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
|
|
307
|
-
!hasDescDefined && // Must not be setting desc
|
|
308
|
-
(multi ? instance.options.enableMultiRemove ?? true : true) && // If multi, don't allow if enableMultiRemove
|
|
309
|
-
(existingSorting?.desc // Finally, detect if it should indeed be removed
|
|
310
|
-
? !sortDescFirst
|
|
311
|
-
: sortDescFirst)
|
|
312
|
-
) {
|
|
313
|
-
sortAction = 'remove'
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
if (sortAction === 'replace') {
|
|
317
|
-
newSorting = [
|
|
318
|
-
{
|
|
319
|
-
id: columnId,
|
|
320
|
-
desc: hasDescDefined ? desc! : !!sortDescFirst,
|
|
321
|
-
},
|
|
322
|
-
]
|
|
323
|
-
} else if (sortAction === 'add' && old?.length) {
|
|
324
|
-
newSorting = [
|
|
325
|
-
...old,
|
|
326
|
-
{
|
|
327
|
-
id: columnId,
|
|
328
|
-
desc: hasDescDefined ? desc! : !!sortDescFirst,
|
|
329
|
-
},
|
|
330
|
-
]
|
|
331
|
-
// Take latest n columns
|
|
332
|
-
newSorting.splice(
|
|
333
|
-
0,
|
|
334
|
-
newSorting.length -
|
|
335
|
-
(instance.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
|
|
336
|
-
)
|
|
337
|
-
} else if (sortAction === 'toggle' && old?.length) {
|
|
338
|
-
// This flips (or sets) the
|
|
339
|
-
newSorting = old.map(d => {
|
|
340
|
-
if (d.id === columnId) {
|
|
341
|
-
return {
|
|
342
|
-
...d,
|
|
343
|
-
desc: hasDescDefined ? desc! : !existingSorting?.desc,
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
return d
|
|
347
|
-
})
|
|
348
|
-
} else if (sortAction === 'remove' && old?.length) {
|
|
349
|
-
newSorting = old.filter(d => d.id !== columnId)
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
return newSorting
|
|
353
|
-
})
|
|
354
|
-
},
|
|
355
|
-
|
|
356
|
-
getColumnCanSort: columnId => {
|
|
357
|
-
const column = instance.getColumn(columnId)
|
|
358
|
-
|
|
359
|
-
if (!column) {
|
|
360
|
-
throw new Error()
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
return (
|
|
364
|
-
column.enableSorting ??
|
|
365
|
-
instance.options.enableSorting ??
|
|
366
|
-
column.defaultCanSort ??
|
|
367
|
-
!!column.accessorFn
|
|
368
|
-
// (!!column.accessorFn ||
|
|
369
|
-
// column.columns?.some(c => c.id && instance.getColumnCanSort(c.id))) ??
|
|
370
|
-
// false
|
|
371
|
-
)
|
|
372
|
-
},
|
|
373
|
-
|
|
374
|
-
getColumnCanMultiSort: columnId => {
|
|
375
|
-
const column = instance.getColumn(columnId)
|
|
376
|
-
|
|
377
|
-
if (!column) {
|
|
378
|
-
throw new Error()
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
return (
|
|
382
|
-
column.enableMultiSort ??
|
|
383
|
-
instance.options.enableMultiSort ??
|
|
384
|
-
!!column.accessorFn
|
|
385
|
-
)
|
|
386
|
-
},
|
|
387
|
-
|
|
388
|
-
getColumnIsSorted: columnId => {
|
|
389
|
-
const columnSort = instance
|
|
390
|
-
.getState()
|
|
391
|
-
.sorting?.find(d => d.id === columnId)
|
|
392
|
-
|
|
393
|
-
return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'
|
|
394
|
-
},
|
|
395
|
-
|
|
396
|
-
getColumnSortIndex: columnId =>
|
|
397
|
-
instance.getState().sorting?.findIndex(d => d.id === columnId) ?? -1,
|
|
398
|
-
|
|
399
|
-
resetSorting: () => {
|
|
400
|
-
instance.setSorting(instance.initialState?.sorting ?? [])
|
|
401
|
-
},
|
|
402
|
-
|
|
403
|
-
getToggleSortingProps: (columnId, userProps) => {
|
|
404
|
-
const column = instance.getColumn(columnId)
|
|
405
|
-
|
|
406
|
-
if (!column) {
|
|
407
|
-
throw new Error()
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
const canSort = column.getCanSort()
|
|
411
|
-
|
|
412
|
-
const initialProps: ToggleSortingProps = {
|
|
413
|
-
title: canSort ? 'Toggle Sorting' : undefined,
|
|
414
|
-
onClick: canSort
|
|
415
|
-
? (e: MouseEvent | TouchEvent) => {
|
|
416
|
-
e.persist()
|
|
417
|
-
column.toggleSorting?.(
|
|
418
|
-
undefined,
|
|
419
|
-
column.getCanMultiSort()
|
|
420
|
-
? instance.options.isMultiSortEvent?.(e)
|
|
421
|
-
: false
|
|
422
|
-
)
|
|
423
|
-
}
|
|
424
|
-
: undefined,
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
return propGetter(initialProps, userProps)
|
|
428
|
-
},
|
|
429
|
-
|
|
430
|
-
getPreSortedRowModel: () => instance.getGlobalFilteredRowModel(),
|
|
431
|
-
getSortedRowModel: memo(
|
|
432
|
-
() => [
|
|
433
|
-
instance.getState().sorting,
|
|
434
|
-
instance.getGlobalFilteredRowModel(),
|
|
435
|
-
instance.options.sortRowsFn,
|
|
436
|
-
],
|
|
437
|
-
(sorting, rowModel, sortingFn) => {
|
|
438
|
-
if (!sortingFn || !sorting?.length) {
|
|
439
|
-
return rowModel
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
return sortingFn(instance, rowModel)
|
|
443
|
-
},
|
|
444
|
-
{
|
|
445
|
-
key: 'getSortedRowModel',
|
|
446
|
-
debug: () => instance.options.debugAll ?? instance.options.debugTable,
|
|
447
|
-
onChange: () => {
|
|
448
|
-
instance._notifyGroupingReset()
|
|
449
|
-
},
|
|
450
|
-
}
|
|
451
|
-
),
|
|
452
|
-
}
|
|
453
|
-
}
|
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Cell,
|
|
3
|
-
Column,
|
|
4
|
-
Getter,
|
|
5
|
-
OnChangeFn,
|
|
6
|
-
PartialGenerics,
|
|
7
|
-
PropGetterValue,
|
|
8
|
-
TableInstance,
|
|
9
|
-
Updater,
|
|
10
|
-
} from '../types'
|
|
11
|
-
import { functionalUpdate, makeStateUpdater, memo, propGetter } from '../utils'
|
|
12
|
-
|
|
13
|
-
export type VisibilityOptions = {
|
|
14
|
-
onColumnVisibilityChange?: OnChangeFn<VisibilityState>
|
|
15
|
-
enableHiding?: boolean
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type VisibilityDefaultOptions = {
|
|
19
|
-
onColumnVisibilityChange: OnChangeFn<VisibilityState>
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type VisibilityState = Record<string, boolean>
|
|
23
|
-
|
|
24
|
-
export type VisibilityTableState = {
|
|
25
|
-
columnVisibility: VisibilityState
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type VisibilityInstance<TGenerics extends PartialGenerics> = {
|
|
29
|
-
getVisibleFlatColumns: () => Column<TGenerics>[]
|
|
30
|
-
getVisibleLeafColumns: () => Column<TGenerics>[]
|
|
31
|
-
setColumnVisibility: (updater: Updater<VisibilityState>) => void
|
|
32
|
-
toggleColumnVisibility: (columnId: string, value?: boolean) => void
|
|
33
|
-
toggleAllColumnsVisible: (value?: boolean) => void
|
|
34
|
-
getColumnIsVisible: (columId: string) => boolean
|
|
35
|
-
getColumnCanHide: (columnId: string) => boolean
|
|
36
|
-
getIsAllColumnsVisible: () => boolean
|
|
37
|
-
getIsSomeColumnsVisible: () => boolean
|
|
38
|
-
getToggleAllColumnsVisibilityProps: <
|
|
39
|
-
TGetter extends Getter<ToggleAllColumnsVisibilityProps>
|
|
40
|
-
>(
|
|
41
|
-
userProps?: TGetter
|
|
42
|
-
) => undefined | PropGetterValue<ToggleAllColumnsVisibilityProps, TGetter>
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
type ToggleVisibilityProps = {}
|
|
46
|
-
type ToggleAllColumnsVisibilityProps = {}
|
|
47
|
-
|
|
48
|
-
export type VisibilityColumnDef = {
|
|
49
|
-
enableHiding?: boolean
|
|
50
|
-
defaultCanHide?: boolean
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export type VisibilityRow<TGenerics extends PartialGenerics> = {
|
|
54
|
-
getVisibleCells: () => Cell<TGenerics>[]
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type VisibilityColumn = {
|
|
58
|
-
getCanHide: () => boolean
|
|
59
|
-
getIsVisible: () => boolean
|
|
60
|
-
toggleVisibility: (value?: boolean) => void
|
|
61
|
-
getToggleVisibilityProps: <TGetter extends Getter<ToggleVisibilityProps>>(
|
|
62
|
-
userProps?: TGetter
|
|
63
|
-
) => PropGetterValue<ToggleVisibilityProps, TGetter>
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
export function getInitialState(): VisibilityTableState {
|
|
69
|
-
return {
|
|
70
|
-
columnVisibility: {},
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function getDefaultOptions<TGenerics extends PartialGenerics>(
|
|
75
|
-
instance: TableInstance<TGenerics>
|
|
76
|
-
): VisibilityDefaultOptions {
|
|
77
|
-
return {
|
|
78
|
-
onColumnVisibilityChange: makeStateUpdater('columnVisibility', instance),
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function getDefaultColumn() {
|
|
83
|
-
return {
|
|
84
|
-
defaultIsVisible: true,
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function createColumn<TGenerics extends PartialGenerics>(
|
|
89
|
-
column: Column<TGenerics>,
|
|
90
|
-
instance: TableInstance<TGenerics>
|
|
91
|
-
): VisibilityColumn {
|
|
92
|
-
return {
|
|
93
|
-
getCanHide: () => instance.getColumnCanHide(column.id),
|
|
94
|
-
getIsVisible: () => instance.getColumnIsVisible(column.id),
|
|
95
|
-
toggleVisibility: value =>
|
|
96
|
-
instance.toggleColumnVisibility(column.id, value),
|
|
97
|
-
getToggleVisibilityProps: userProps => {
|
|
98
|
-
const props: ToggleVisibilityProps = {
|
|
99
|
-
type: 'checkbox',
|
|
100
|
-
checked: column.getIsVisible?.(),
|
|
101
|
-
title: 'Toggle Column Visibility',
|
|
102
|
-
onChange: (e: MouseEvent | TouchEvent) => {
|
|
103
|
-
column.toggleVisibility?.((e.target as HTMLInputElement).checked)
|
|
104
|
-
},
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return propGetter(props, userProps)
|
|
108
|
-
},
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function getInstance<TGenerics extends PartialGenerics>(
|
|
113
|
-
instance: TableInstance<TGenerics>
|
|
114
|
-
): VisibilityInstance<TGenerics> {
|
|
115
|
-
return {
|
|
116
|
-
getVisibleFlatColumns: memo(
|
|
117
|
-
() => [
|
|
118
|
-
instance.getAllFlatColumns(),
|
|
119
|
-
instance
|
|
120
|
-
.getAllFlatColumns()
|
|
121
|
-
.filter(d => d.getIsVisible?.())
|
|
122
|
-
.map(d => d.id)
|
|
123
|
-
.join('_'),
|
|
124
|
-
],
|
|
125
|
-
allFlatColumns => {
|
|
126
|
-
return allFlatColumns.filter(d => d.getIsVisible?.())
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
key: 'getVisibleFlatColumns',
|
|
130
|
-
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
131
|
-
}
|
|
132
|
-
),
|
|
133
|
-
|
|
134
|
-
getVisibleLeafColumns: memo(
|
|
135
|
-
() => [
|
|
136
|
-
instance.getAllLeafColumns(),
|
|
137
|
-
instance
|
|
138
|
-
.getAllLeafColumns()
|
|
139
|
-
.filter(d => d.getIsVisible?.())
|
|
140
|
-
.map(d => d.id)
|
|
141
|
-
.join('_'),
|
|
142
|
-
],
|
|
143
|
-
allFlatColumns => {
|
|
144
|
-
return allFlatColumns.filter(d => d.getIsVisible?.())
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
key: 'getVisibleLeafColumns',
|
|
148
|
-
debug: () => instance.options.debugAll ?? instance.options.debugColumns,
|
|
149
|
-
}
|
|
150
|
-
),
|
|
151
|
-
|
|
152
|
-
setColumnVisibility: updater =>
|
|
153
|
-
instance.options.onColumnVisibilityChange?.(
|
|
154
|
-
updater,
|
|
155
|
-
functionalUpdate(updater, instance.getState().columnVisibility)
|
|
156
|
-
),
|
|
157
|
-
|
|
158
|
-
toggleColumnVisibility: (columnId, value) => {
|
|
159
|
-
if (!columnId) return
|
|
160
|
-
|
|
161
|
-
if (instance.getColumnCanHide(columnId)) {
|
|
162
|
-
instance.setColumnVisibility(old => ({
|
|
163
|
-
...old,
|
|
164
|
-
[columnId]: value ?? !instance.getColumnIsVisible(columnId),
|
|
165
|
-
}))
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
|
|
169
|
-
toggleAllColumnsVisible: value => {
|
|
170
|
-
value = value ?? !instance.getIsAllColumnsVisible()
|
|
171
|
-
|
|
172
|
-
instance.setColumnVisibility(
|
|
173
|
-
instance.getAllLeafColumns().reduce(
|
|
174
|
-
(obj, column) => ({
|
|
175
|
-
...obj,
|
|
176
|
-
[column.id]: !value ? !column.getCanHide?.() : value,
|
|
177
|
-
}),
|
|
178
|
-
{}
|
|
179
|
-
)
|
|
180
|
-
)
|
|
181
|
-
},
|
|
182
|
-
|
|
183
|
-
getColumnIsVisible: columnId => {
|
|
184
|
-
const column = instance.getColumn(columnId)
|
|
185
|
-
|
|
186
|
-
if (!column) {
|
|
187
|
-
throw new Error()
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return (
|
|
191
|
-
instance.getState().columnVisibility?.[columnId] ??
|
|
192
|
-
column.defaultIsVisible ??
|
|
193
|
-
true
|
|
194
|
-
)
|
|
195
|
-
},
|
|
196
|
-
|
|
197
|
-
getColumnCanHide: columnId => {
|
|
198
|
-
const column = instance.getColumn(columnId)
|
|
199
|
-
|
|
200
|
-
if (!column) {
|
|
201
|
-
throw new Error()
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return (
|
|
205
|
-
instance.options.enableHiding ??
|
|
206
|
-
column.enableHiding ??
|
|
207
|
-
column.defaultCanHide ??
|
|
208
|
-
true
|
|
209
|
-
)
|
|
210
|
-
},
|
|
211
|
-
|
|
212
|
-
getIsAllColumnsVisible: () =>
|
|
213
|
-
!instance.getAllLeafColumns().some(column => !column.getIsVisible?.()),
|
|
214
|
-
|
|
215
|
-
getIsSomeColumnsVisible: () =>
|
|
216
|
-
instance.getAllLeafColumns().some(column => column.getIsVisible?.()),
|
|
217
|
-
|
|
218
|
-
getToggleAllColumnsVisibilityProps: userProps => {
|
|
219
|
-
const props: ToggleAllColumnsVisibilityProps = {
|
|
220
|
-
onChange: (e: MouseEvent) => {
|
|
221
|
-
instance.toggleAllColumnsVisible(
|
|
222
|
-
(e.target as HTMLInputElement)?.checked
|
|
223
|
-
)
|
|
224
|
-
},
|
|
225
|
-
type: 'checkbox',
|
|
226
|
-
title: 'Toggle visibility for all columns',
|
|
227
|
-
checked: instance.getIsAllColumnsVisible(),
|
|
228
|
-
indeterminate:
|
|
229
|
-
!instance.getIsAllColumnsVisible() &&
|
|
230
|
-
instance.getIsSomeColumnsVisible()
|
|
231
|
-
? 'indeterminate'
|
|
232
|
-
: undefined,
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return propGetter(props, userProps)
|
|
236
|
-
},
|
|
237
|
-
}
|
|
238
|
-
}
|