@tanstack/table-core 8.9.7 → 8.9.8
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/lib/core/cell.js +1 -1
- package/build/lib/core/cell.js.map +1 -1
- package/build/lib/core/column.js +3 -3
- package/build/lib/core/column.js.map +1 -1
- package/build/lib/core/headers.js +182 -181
- package/build/lib/core/headers.js.map +1 -1
- package/build/lib/core/row.js +1 -1
- package/build/lib/core/row.js.map +1 -1
- package/build/lib/core/table.js +4 -3
- package/build/lib/core/table.js.map +1 -1
- package/build/lib/features/ColumnSizing.js +173 -179
- package/build/lib/features/ColumnSizing.js.map +1 -1
- package/build/lib/features/Expanding.js +119 -123
- package/build/lib/features/Expanding.js.map +1 -1
- package/build/lib/features/Filters.js +157 -165
- package/build/lib/features/Filters.js.map +1 -1
- package/build/lib/features/Grouping.js +71 -79
- package/build/lib/features/Grouping.js.map +1 -1
- package/build/lib/features/Ordering.js +32 -34
- package/build/lib/features/Ordering.js.map +1 -1
- package/build/lib/features/Pagination.js +112 -114
- package/build/lib/features/Pagination.js.map +1 -1
- package/build/lib/features/Pinning.js +120 -126
- package/build/lib/features/Pinning.js.map +1 -1
- package/build/lib/features/RowSelection.js +245 -247
- package/build/lib/features/RowSelection.js.map +1 -1
- package/build/lib/features/Sorting.js +163 -167
- package/build/lib/features/Sorting.js.map +1 -1
- package/build/lib/features/Visibility.js +60 -66
- package/build/lib/features/Visibility.js.map +1 -1
- package/build/lib/index.esm.js +1469 -1515
- package/build/lib/index.esm.js.map +1 -1
- package/build/lib/index.mjs +1469 -1515
- package/build/lib/index.mjs.map +1 -1
- package/build/umd/index.development.js +1469 -1515
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/core/cell.ts +5 -8
- package/src/core/column.ts +3 -3
- package/src/core/headers.ts +264 -280
- package/src/core/row.ts +1 -1
- package/src/core/table.ts +4 -3
- package/src/features/ColumnSizing.ts +220 -231
- package/src/features/Expanding.ts +132 -140
- package/src/features/Filters.ts +193 -206
- package/src/features/Grouping.ts +94 -110
- package/src/features/Ordering.ts +48 -51
- package/src/features/Pagination.ts +150 -154
- package/src/features/Pinning.ts +158 -178
- package/src/features/RowSelection.ts +280 -286
- package/src/features/Sorting.ts +196 -206
- package/src/features/Visibility.ts +98 -107
package/src/features/Sorting.ts
CHANGED
|
@@ -134,249 +134,239 @@ export const Sorting: TableFeature = {
|
|
|
134
134
|
createColumn: <TData extends RowData, TValue>(
|
|
135
135
|
column: Column<TData, TValue>,
|
|
136
136
|
table: Table<TData>
|
|
137
|
-
):
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const firstRows = table.getFilteredRowModel().flatRows.slice(10)
|
|
137
|
+
): void => {
|
|
138
|
+
column.getAutoSortingFn = () => {
|
|
139
|
+
const firstRows = table.getFilteredRowModel().flatRows.slice(10)
|
|
141
140
|
|
|
142
|
-
|
|
141
|
+
let isString = false
|
|
143
142
|
|
|
144
|
-
|
|
145
|
-
|
|
143
|
+
for (const row of firstRows) {
|
|
144
|
+
const value = row?.getValue(column.id)
|
|
146
145
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
146
|
+
if (Object.prototype.toString.call(value) === '[object Date]') {
|
|
147
|
+
return sortingFns.datetime
|
|
148
|
+
}
|
|
150
149
|
|
|
151
|
-
|
|
152
|
-
|
|
150
|
+
if (typeof value === 'string') {
|
|
151
|
+
isString = true
|
|
153
152
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
153
|
+
if (value.split(reSplitAlphaNumeric).length > 1) {
|
|
154
|
+
return sortingFns.alphanumeric
|
|
157
155
|
}
|
|
158
156
|
}
|
|
157
|
+
}
|
|
159
158
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return sortingFns.basic
|
|
165
|
-
},
|
|
166
|
-
getAutoSortDir: () => {
|
|
167
|
-
const firstRow = table.getFilteredRowModel().flatRows[0]
|
|
159
|
+
if (isString) {
|
|
160
|
+
return sortingFns.text
|
|
161
|
+
}
|
|
168
162
|
|
|
169
|
-
|
|
163
|
+
return sortingFns.basic
|
|
164
|
+
}
|
|
165
|
+
column.getAutoSortDir = () => {
|
|
166
|
+
const firstRow = table.getFilteredRowModel().flatRows[0]
|
|
170
167
|
|
|
171
|
-
|
|
172
|
-
return 'asc'
|
|
173
|
-
}
|
|
168
|
+
const value = firstRow?.getValue(column.id)
|
|
174
169
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (!column) {
|
|
179
|
-
throw new Error()
|
|
180
|
-
}
|
|
170
|
+
if (typeof value === 'string') {
|
|
171
|
+
return 'asc'
|
|
172
|
+
}
|
|
181
173
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
174
|
+
return 'desc'
|
|
175
|
+
}
|
|
176
|
+
column.getSortingFn = () => {
|
|
177
|
+
if (!column) {
|
|
178
|
+
throw new Error()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return isFunction(column.columnDef.sortingFn)
|
|
182
|
+
? column.columnDef.sortingFn
|
|
183
|
+
: column.columnDef.sortingFn === 'auto'
|
|
184
|
+
? column.getAutoSortingFn()
|
|
185
|
+
: table.options.sortingFns?.[column.columnDef.sortingFn as string] ??
|
|
186
|
+
sortingFns[column.columnDef.sortingFn as BuiltInSortingFn]
|
|
187
|
+
}
|
|
188
|
+
column.toggleSorting = (desc, multi) => {
|
|
189
|
+
// if (column.columns.length) {
|
|
190
|
+
// column.columns.forEach((c, i) => {
|
|
191
|
+
// if (c.id) {
|
|
192
|
+
// table.toggleColumnSorting(c.id, undefined, multi || !!i)
|
|
193
|
+
// }
|
|
194
|
+
// })
|
|
195
|
+
// return
|
|
196
|
+
// }
|
|
197
|
+
|
|
198
|
+
// this needs to be outside of table.setSorting to be in sync with rerender
|
|
199
|
+
const nextSortingOrder = column.getNextSortingOrder()
|
|
200
|
+
const hasManualValue = typeof desc !== 'undefined' && desc !== null
|
|
201
|
+
|
|
202
|
+
table.setSorting(old => {
|
|
203
|
+
// Find any existing sorting for this column
|
|
204
|
+
const existingSorting = old?.find(d => d.id === column.id)
|
|
205
|
+
const existingIndex = old?.findIndex(d => d.id === column.id)
|
|
206
|
+
|
|
207
|
+
let newSorting: SortingState = []
|
|
208
|
+
|
|
209
|
+
// What should we do with this sort action?
|
|
210
|
+
let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
|
|
211
|
+
let nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'
|
|
212
|
+
|
|
213
|
+
// Multi-mode
|
|
214
|
+
if (old?.length && column.getCanMultiSort() && multi) {
|
|
215
|
+
if (existingSorting) {
|
|
216
|
+
sortAction = 'toggle'
|
|
221
217
|
} else {
|
|
222
|
-
|
|
223
|
-
if (old?.length && existingIndex !== old.length - 1) {
|
|
224
|
-
sortAction = 'replace'
|
|
225
|
-
} else if (existingSorting) {
|
|
226
|
-
sortAction = 'toggle'
|
|
227
|
-
} else {
|
|
228
|
-
sortAction = 'replace'
|
|
229
|
-
}
|
|
218
|
+
sortAction = 'add'
|
|
230
219
|
}
|
|
220
|
+
} else {
|
|
221
|
+
// Normal mode
|
|
222
|
+
if (old?.length && existingIndex !== old.length - 1) {
|
|
223
|
+
sortAction = 'replace'
|
|
224
|
+
} else if (existingSorting) {
|
|
225
|
+
sortAction = 'toggle'
|
|
226
|
+
} else {
|
|
227
|
+
sortAction = 'replace'
|
|
228
|
+
}
|
|
229
|
+
}
|
|
231
230
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
231
|
+
// Handle toggle states that will remove the sorting
|
|
232
|
+
if (sortAction === 'toggle') {
|
|
233
|
+
// If we are "actually" toggling (not a manual set value), should we remove the sorting?
|
|
234
|
+
if (!hasManualValue) {
|
|
235
|
+
// Is our intention to remove?
|
|
236
|
+
if (!nextSortingOrder) {
|
|
237
|
+
sortAction = 'remove'
|
|
240
238
|
}
|
|
241
239
|
}
|
|
240
|
+
}
|
|
242
241
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
242
|
+
if (sortAction === 'add') {
|
|
243
|
+
newSorting = [
|
|
244
|
+
...old,
|
|
245
|
+
{
|
|
246
|
+
id: column.id,
|
|
247
|
+
desc: nextDesc,
|
|
248
|
+
},
|
|
249
|
+
]
|
|
250
|
+
// Take latest n columns
|
|
251
|
+
newSorting.splice(
|
|
252
|
+
0,
|
|
253
|
+
newSorting.length -
|
|
254
|
+
(table.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
|
|
255
|
+
)
|
|
256
|
+
} else if (sortAction === 'toggle') {
|
|
257
|
+
// This flips (or sets) the
|
|
258
|
+
newSorting = old.map(d => {
|
|
259
|
+
if (d.id === column.id) {
|
|
260
|
+
return {
|
|
261
|
+
...d,
|
|
248
262
|
desc: nextDesc,
|
|
249
|
-
},
|
|
250
|
-
]
|
|
251
|
-
// Take latest n columns
|
|
252
|
-
newSorting.splice(
|
|
253
|
-
0,
|
|
254
|
-
newSorting.length -
|
|
255
|
-
(table.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
|
|
256
|
-
)
|
|
257
|
-
} else if (sortAction === 'toggle') {
|
|
258
|
-
// This flips (or sets) the
|
|
259
|
-
newSorting = old.map(d => {
|
|
260
|
-
if (d.id === column.id) {
|
|
261
|
-
return {
|
|
262
|
-
...d,
|
|
263
|
-
desc: nextDesc,
|
|
264
|
-
}
|
|
265
263
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
264
|
+
}
|
|
265
|
+
return d
|
|
266
|
+
})
|
|
267
|
+
} else if (sortAction === 'remove') {
|
|
268
|
+
newSorting = old.filter(d => d.id !== column.id)
|
|
269
|
+
} else {
|
|
270
|
+
newSorting = [
|
|
271
|
+
{
|
|
272
|
+
id: column.id,
|
|
273
|
+
desc: nextDesc,
|
|
274
|
+
},
|
|
275
|
+
]
|
|
276
|
+
}
|
|
278
277
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
278
|
+
return newSorting
|
|
279
|
+
})
|
|
280
|
+
}
|
|
282
281
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
282
|
+
column.getFirstSortDir = () => {
|
|
283
|
+
const sortDescFirst =
|
|
284
|
+
column.columnDef.sortDescFirst ??
|
|
285
|
+
table.options.sortDescFirst ??
|
|
286
|
+
column.getAutoSortDir() === 'desc'
|
|
287
|
+
return sortDescFirst ? 'desc' : 'asc'
|
|
288
|
+
}
|
|
290
289
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
290
|
+
column.getNextSortingOrder = (multi?: boolean) => {
|
|
291
|
+
const firstSortDirection = column.getFirstSortDir()
|
|
292
|
+
const isSorted = column.getIsSorted()
|
|
293
|
+
|
|
294
|
+
if (!isSorted) {
|
|
295
|
+
return firstSortDirection
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (
|
|
299
|
+
isSorted !== firstSortDirection &&
|
|
300
|
+
(table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
|
|
301
|
+
(multi ? table.options.enableMultiRemove ?? true : true) // If multi, don't allow if enableMultiRemove))
|
|
302
|
+
) {
|
|
303
|
+
return false
|
|
304
|
+
}
|
|
305
|
+
return isSorted === 'desc' ? 'asc' : 'desc'
|
|
306
|
+
}
|
|
294
307
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
308
|
+
column.getCanSort = () => {
|
|
309
|
+
return (
|
|
310
|
+
(column.columnDef.enableSorting ?? true) &&
|
|
311
|
+
(table.options.enableSorting ?? true) &&
|
|
312
|
+
!!column.accessorFn
|
|
313
|
+
)
|
|
314
|
+
}
|
|
298
315
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
return isSorted === 'desc' ? 'asc' : 'desc'
|
|
307
|
-
},
|
|
316
|
+
column.getCanMultiSort = () => {
|
|
317
|
+
return (
|
|
318
|
+
column.columnDef.enableMultiSort ??
|
|
319
|
+
table.options.enableMultiSort ??
|
|
320
|
+
!!column.accessorFn
|
|
321
|
+
)
|
|
322
|
+
}
|
|
308
323
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
(column.columnDef.enableSorting ?? true) &&
|
|
312
|
-
(table.options.enableSorting ?? true) &&
|
|
313
|
-
!!column.accessorFn
|
|
314
|
-
)
|
|
315
|
-
},
|
|
324
|
+
column.getIsSorted = () => {
|
|
325
|
+
const columnSort = table.getState().sorting?.find(d => d.id === column.id)
|
|
316
326
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
column.columnDef.enableMultiSort ??
|
|
320
|
-
table.options.enableMultiSort ??
|
|
321
|
-
!!column.accessorFn
|
|
322
|
-
)
|
|
323
|
-
},
|
|
327
|
+
return !columnSort ? false : columnSort.desc ? 'desc' : 'asc'
|
|
328
|
+
}
|
|
324
329
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
.getState()
|
|
328
|
-
.sorting?.find(d => d.id === column.id)
|
|
330
|
+
column.getSortIndex = () =>
|
|
331
|
+
table.getState().sorting?.findIndex(d => d.id === column.id) ?? -1
|
|
329
332
|
|
|
330
|
-
|
|
331
|
-
|
|
333
|
+
column.clearSorting = () => {
|
|
334
|
+
//clear sorting for just 1 column
|
|
335
|
+
table.setSorting(old =>
|
|
336
|
+
old?.length ? old.filter(d => d.id !== column.id) : []
|
|
337
|
+
)
|
|
338
|
+
}
|
|
332
339
|
|
|
333
|
-
|
|
334
|
-
|
|
340
|
+
column.getToggleSortingHandler = () => {
|
|
341
|
+
const canSort = column.getCanSort()
|
|
335
342
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
343
|
+
return (e: unknown) => {
|
|
344
|
+
if (!canSort) return
|
|
345
|
+
;(e as any).persist?.()
|
|
346
|
+
column.toggleSorting?.(
|
|
347
|
+
undefined,
|
|
348
|
+
column.getCanMultiSort() ? table.options.isMultiSortEvent?.(e) : false
|
|
340
349
|
)
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
getToggleSortingHandler: () => {
|
|
344
|
-
const canSort = column.getCanSort()
|
|
345
|
-
|
|
346
|
-
return (e: unknown) => {
|
|
347
|
-
if (!canSort) return
|
|
348
|
-
;(e as any).persist?.()
|
|
349
|
-
column.toggleSorting?.(
|
|
350
|
-
undefined,
|
|
351
|
-
column.getCanMultiSort()
|
|
352
|
-
? table.options.isMultiSortEvent?.(e)
|
|
353
|
-
: false
|
|
354
|
-
)
|
|
355
|
-
}
|
|
356
|
-
},
|
|
350
|
+
}
|
|
357
351
|
}
|
|
358
352
|
},
|
|
359
353
|
|
|
360
|
-
createTable: <TData extends RowData>(
|
|
361
|
-
table
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
if (!table._getSortedRowModel && table.options.getSortedRowModel) {
|
|
371
|
-
table._getSortedRowModel = table.options.getSortedRowModel(table)
|
|
372
|
-
}
|
|
354
|
+
createTable: <TData extends RowData>(table: Table<TData>): void => {
|
|
355
|
+
table.setSorting = updater => table.options.onSortingChange?.(updater)
|
|
356
|
+
table.resetSorting = defaultState => {
|
|
357
|
+
table.setSorting(defaultState ? [] : table.initialState?.sorting ?? [])
|
|
358
|
+
}
|
|
359
|
+
table.getPreSortedRowModel = () => table.getGroupedRowModel()
|
|
360
|
+
table.getSortedRowModel = () => {
|
|
361
|
+
if (!table._getSortedRowModel && table.options.getSortedRowModel) {
|
|
362
|
+
table._getSortedRowModel = table.options.getSortedRowModel(table)
|
|
363
|
+
}
|
|
373
364
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
365
|
+
if (table.options.manualSorting || !table._getSortedRowModel) {
|
|
366
|
+
return table.getPreSortedRowModel()
|
|
367
|
+
}
|
|
377
368
|
|
|
378
|
-
|
|
379
|
-
},
|
|
369
|
+
return table._getSortedRowModel()
|
|
380
370
|
}
|
|
381
371
|
},
|
|
382
372
|
}
|