@tanstack/table-core 8.2.1 → 8.2.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/features/Sorting.js +33 -26
- package/build/cjs/features/Sorting.js.map +1 -1
- package/build/cjs/sortingFns.js +6 -1
- package/build/cjs/sortingFns.js.map +1 -1
- package/build/esm/index.js +39 -27
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +303 -303
- package/build/types/index.d.ts +1 -0
- package/build/umd/index.development.js +39 -27
- 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/features/Sorting.ts +38 -27
- package/src/sortingFns.ts +7 -4
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/table-core",
|
|
3
3
|
"author": "Tanner Linsley",
|
|
4
|
-
"version": "8.2.
|
|
4
|
+
"version": "8.2.2",
|
|
5
5
|
"description": "Headless UI for building powerful tables & datagrids for TS/JS.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/tanstack/table#readme",
|
package/src/features/Sorting.ts
CHANGED
|
@@ -58,6 +58,7 @@ export type SortingColumn<TData extends RowData> = {
|
|
|
58
58
|
getAutoSortingFn: () => SortingFn<TData>
|
|
59
59
|
getAutoSortDir: () => SortDirection
|
|
60
60
|
getSortingFn: () => SortingFn<TData>
|
|
61
|
+
getFirstSortDir: () => SortDirection
|
|
61
62
|
getNextSortingOrder: () => SortDirection | false
|
|
62
63
|
getCanSort: () => boolean
|
|
63
64
|
getCanMultiSort: () => boolean
|
|
@@ -184,19 +185,21 @@ export const Sorting: TableFeature = {
|
|
|
184
185
|
|
|
185
186
|
// this needs to be outside of table.setSorting to be in sync with rerender
|
|
186
187
|
const nextSortingOrder = column.getNextSortingOrder()
|
|
188
|
+
const hasManualValue = typeof desc !== 'undefined' && desc !== null
|
|
187
189
|
|
|
188
190
|
table.setSorting(old => {
|
|
189
191
|
// Find any existing sorting for this column
|
|
190
192
|
const existingSorting = old?.find(d => d.id === column.id)
|
|
191
193
|
const existingIndex = old?.findIndex(d => d.id === column.id)
|
|
192
|
-
const hasDescDefined = typeof desc !== 'undefined' && desc !== null
|
|
193
194
|
|
|
194
195
|
let newSorting: SortingState = []
|
|
195
196
|
|
|
196
197
|
// What should we do with this sort action?
|
|
197
198
|
let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
|
|
199
|
+
let nextDesc = hasManualValue ? desc : nextSortingOrder === 'desc'
|
|
198
200
|
|
|
199
|
-
|
|
201
|
+
// Multi-mode
|
|
202
|
+
if (old?.length && column.getCanMultiSort() && multi) {
|
|
200
203
|
if (existingSorting) {
|
|
201
204
|
sortAction = 'toggle'
|
|
202
205
|
} else {
|
|
@@ -214,29 +217,22 @@ export const Sorting: TableFeature = {
|
|
|
214
217
|
}
|
|
215
218
|
|
|
216
219
|
// Handle toggle states that will remove the sorting
|
|
217
|
-
if (
|
|
218
|
-
|
|
219
|
-
(
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
220
|
+
if (sortAction === 'toggle') {
|
|
221
|
+
// If we are "actually" toggling (not a manual set value), should we remove the sorting?
|
|
222
|
+
if (!hasManualValue) {
|
|
223
|
+
// Is our intention to remove?
|
|
224
|
+
if (!nextSortingOrder) {
|
|
225
|
+
sortAction = 'remove'
|
|
226
|
+
}
|
|
227
|
+
}
|
|
225
228
|
}
|
|
226
229
|
|
|
227
|
-
if (sortAction === '
|
|
228
|
-
newSorting = [
|
|
229
|
-
{
|
|
230
|
-
id: column.id,
|
|
231
|
-
desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
|
|
232
|
-
},
|
|
233
|
-
]
|
|
234
|
-
} else if (sortAction === 'add' && old?.length) {
|
|
230
|
+
if (sortAction === 'add') {
|
|
235
231
|
newSorting = [
|
|
236
232
|
...old,
|
|
237
233
|
{
|
|
238
234
|
id: column.id,
|
|
239
|
-
desc:
|
|
235
|
+
desc: nextDesc,
|
|
240
236
|
},
|
|
241
237
|
]
|
|
242
238
|
// Take latest n columns
|
|
@@ -245,41 +241,56 @@ export const Sorting: TableFeature = {
|
|
|
245
241
|
newSorting.length -
|
|
246
242
|
(table.options.maxMultiSortColCount ?? Number.MAX_SAFE_INTEGER)
|
|
247
243
|
)
|
|
248
|
-
} else if (sortAction === 'toggle'
|
|
244
|
+
} else if (sortAction === 'toggle') {
|
|
249
245
|
// This flips (or sets) the
|
|
250
246
|
newSorting = old.map(d => {
|
|
251
247
|
if (d.id === column.id) {
|
|
252
248
|
return {
|
|
253
249
|
...d,
|
|
254
|
-
desc:
|
|
250
|
+
desc: nextDesc,
|
|
255
251
|
}
|
|
256
252
|
}
|
|
257
253
|
return d
|
|
258
254
|
})
|
|
259
|
-
} else if (sortAction === 'remove'
|
|
255
|
+
} else if (sortAction === 'remove') {
|
|
260
256
|
newSorting = old.filter(d => d.id !== column.id)
|
|
257
|
+
} else {
|
|
258
|
+
newSorting = [
|
|
259
|
+
{
|
|
260
|
+
id: column.id,
|
|
261
|
+
desc: nextDesc,
|
|
262
|
+
},
|
|
263
|
+
]
|
|
261
264
|
}
|
|
262
265
|
|
|
263
266
|
return newSorting
|
|
264
267
|
})
|
|
265
268
|
},
|
|
266
269
|
|
|
267
|
-
|
|
270
|
+
getFirstSortDir: () => {
|
|
268
271
|
const sortDescFirst =
|
|
269
272
|
column.columnDef.sortDescFirst ??
|
|
270
273
|
table.options.sortDescFirst ??
|
|
271
274
|
column.getAutoSortDir() === 'desc'
|
|
272
|
-
|
|
275
|
+
return sortDescFirst ? 'desc' : 'asc'
|
|
276
|
+
},
|
|
273
277
|
|
|
278
|
+
getNextSortingOrder: (multi?: boolean) => {
|
|
279
|
+
const firstSortDirection = column.getFirstSortDir()
|
|
274
280
|
const isSorted = column.getIsSorted()
|
|
281
|
+
|
|
275
282
|
if (!isSorted) {
|
|
276
283
|
return firstSortDirection
|
|
277
284
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
285
|
+
|
|
286
|
+
if (
|
|
287
|
+
isSorted !== firstSortDirection &&
|
|
288
|
+
(table.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
|
|
289
|
+
(multi ? table.options.enableMultiRemove ?? true : true) // If multi, don't allow if enableMultiRemove))
|
|
290
|
+
) {
|
|
281
291
|
return false
|
|
282
292
|
}
|
|
293
|
+
return isSorted === 'desc' ? 'asc' : 'desc'
|
|
283
294
|
},
|
|
284
295
|
|
|
285
296
|
getCanSort: () => {
|
package/src/sortingFns.ts
CHANGED
|
@@ -35,10 +35,13 @@ const textCaseSensitive: SortingFn<any> = (rowA, rowB, columnId) => {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
const datetime: SortingFn<any> = (rowA, rowB, columnId) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
const a = rowA.getValue<Date>(columnId)
|
|
39
|
+
const b = rowB.getValue<Date>(columnId)
|
|
40
|
+
|
|
41
|
+
// Can handle nullish values
|
|
42
|
+
// Use > and < because == (and ===) doesn't work with
|
|
43
|
+
// Date objects (would require calling getTime()).
|
|
44
|
+
return a > b ? 1 : a < b ? -1 : 0
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
const basic: SortingFn<any> = (rowA, rowB, columnId) => {
|