@tanstack/table-core 8.0.0-alpha.88 → 8.0.0-alpha.89
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 +27 -9
- package/build/cjs/features/Sorting.js.map +1 -1
- package/build/esm/index.js +27 -9
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +308 -308
- package/build/types/features/Sorting.d.ts +1 -0
- package/build/umd/index.development.js +27 -9
- 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 +27 -12
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/table-core",
|
|
3
3
|
"author": "Tanner Linsley",
|
|
4
|
-
"version": "8.0.0-alpha.
|
|
4
|
+
"version": "8.0.0-alpha.89",
|
|
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<TGenerics extends TableGenerics> = {
|
|
|
58
58
|
getAutoSortingFn: () => SortingFn<TGenerics>
|
|
59
59
|
getAutoSortDir: () => SortDirection
|
|
60
60
|
getSortingFn: () => SortingFn<TGenerics>
|
|
61
|
+
getNextSortingOrder: () => SortDirection | false
|
|
61
62
|
getCanSort: () => boolean
|
|
62
63
|
getCanMultiSort: () => boolean
|
|
63
64
|
getSortIndex: () => number
|
|
@@ -191,6 +192,9 @@ export const Sorting: TableFeature = {
|
|
|
191
192
|
// return
|
|
192
193
|
// }
|
|
193
194
|
|
|
195
|
+
// this needs to be outside of instance.setSorting to be in sync with rerender
|
|
196
|
+
const nextSortingOrder = column.getNextSortingOrder()
|
|
197
|
+
|
|
194
198
|
instance.setSorting(old => {
|
|
195
199
|
// Find any existing sorting for this column
|
|
196
200
|
const existingSorting = old?.find(d => d.id === column.id)
|
|
@@ -200,7 +204,7 @@ export const Sorting: TableFeature = {
|
|
|
200
204
|
let newSorting: SortingState = []
|
|
201
205
|
|
|
202
206
|
// What should we do with this sort action?
|
|
203
|
-
let sortAction
|
|
207
|
+
let sortAction: 'add' | 'remove' | 'toggle' | 'replace'
|
|
204
208
|
|
|
205
209
|
if (column.getCanMultiSort() && multi) {
|
|
206
210
|
if (existingSorting) {
|
|
@@ -219,20 +223,13 @@ export const Sorting: TableFeature = {
|
|
|
219
223
|
}
|
|
220
224
|
}
|
|
221
225
|
|
|
222
|
-
const sortDescFirst =
|
|
223
|
-
column.columnDef.sortDescFirst ??
|
|
224
|
-
instance.options.sortDescFirst ??
|
|
225
|
-
column.getAutoSortDir() === 'desc'
|
|
226
|
-
|
|
227
226
|
// Handle toggle states that will remove the sorting
|
|
228
227
|
if (
|
|
229
228
|
sortAction === 'toggle' && // Must be toggling
|
|
230
229
|
(instance.options.enableSortingRemoval ?? true) && // If enableSortRemove, enable in general
|
|
231
230
|
!hasDescDefined && // Must not be setting desc
|
|
232
231
|
(multi ? instance.options.enableMultiRemove ?? true : true) && // If multi, don't allow if enableMultiRemove
|
|
233
|
-
|
|
234
|
-
? !sortDescFirst
|
|
235
|
-
: sortDescFirst)
|
|
232
|
+
!nextSortingOrder // Finally, detect if it should indeed be removed
|
|
236
233
|
) {
|
|
237
234
|
sortAction = 'remove'
|
|
238
235
|
}
|
|
@@ -241,7 +238,7 @@ export const Sorting: TableFeature = {
|
|
|
241
238
|
newSorting = [
|
|
242
239
|
{
|
|
243
240
|
id: column.id,
|
|
244
|
-
desc: hasDescDefined ? desc! :
|
|
241
|
+
desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
|
|
245
242
|
},
|
|
246
243
|
]
|
|
247
244
|
} else if (sortAction === 'add' && old?.length) {
|
|
@@ -249,7 +246,7 @@ export const Sorting: TableFeature = {
|
|
|
249
246
|
...old,
|
|
250
247
|
{
|
|
251
248
|
id: column.id,
|
|
252
|
-
desc: hasDescDefined ? desc! :
|
|
249
|
+
desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
|
|
253
250
|
},
|
|
254
251
|
]
|
|
255
252
|
// Take latest n columns
|
|
@@ -265,7 +262,7 @@ export const Sorting: TableFeature = {
|
|
|
265
262
|
if (d.id === column.id) {
|
|
266
263
|
return {
|
|
267
264
|
...d,
|
|
268
|
-
desc: hasDescDefined ? desc! : !
|
|
265
|
+
desc: hasDescDefined ? desc! : nextSortingOrder! === 'desc',
|
|
269
266
|
}
|
|
270
267
|
}
|
|
271
268
|
return d
|
|
@@ -278,6 +275,24 @@ export const Sorting: TableFeature = {
|
|
|
278
275
|
})
|
|
279
276
|
},
|
|
280
277
|
|
|
278
|
+
getNextSortingOrder: () => {
|
|
279
|
+
const sortDescFirst =
|
|
280
|
+
column.columnDef.sortDescFirst ??
|
|
281
|
+
instance.options.sortDescFirst ??
|
|
282
|
+
column.getAutoSortDir() === 'desc'
|
|
283
|
+
const firstSortDirection = sortDescFirst ? 'desc' : 'asc'
|
|
284
|
+
|
|
285
|
+
const isSorted = column.getIsSorted()
|
|
286
|
+
if (!isSorted) {
|
|
287
|
+
return firstSortDirection
|
|
288
|
+
}
|
|
289
|
+
if (isSorted === firstSortDirection) {
|
|
290
|
+
return isSorted === 'desc' ? 'asc' : 'desc'
|
|
291
|
+
} else {
|
|
292
|
+
return false
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
|
|
281
296
|
getCanSort: () => {
|
|
282
297
|
return (
|
|
283
298
|
(column.columnDef.enableSorting ?? true) &&
|