@tanstack/table-core 9.0.0-beta.18 → 9.0.0-beta.19
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/dist/features/row-selection/rowSelectionFeature.cjs +49 -6
- package/dist/features/row-selection/rowSelectionFeature.cjs.map +1 -1
- package/dist/features/row-selection/rowSelectionFeature.js +50 -7
- package/dist/features/row-selection/rowSelectionFeature.js.map +1 -1
- package/dist/features/row-selection/rowSelectionFeature.types.d.cts +11 -3
- package/dist/features/row-selection/rowSelectionFeature.types.d.ts +11 -3
- package/dist/features/row-selection/rowSelectionFeature.utils.cjs +58 -49
- package/dist/features/row-selection/rowSelectionFeature.utils.cjs.map +1 -1
- package/dist/features/row-selection/rowSelectionFeature.utils.d.cts +23 -10
- package/dist/features/row-selection/rowSelectionFeature.utils.d.ts +23 -10
- package/dist/features/row-selection/rowSelectionFeature.utils.js +59 -51
- package/dist/features/row-selection/rowSelectionFeature.utils.js.map +1 -1
- package/dist/static-functions.cjs +1 -0
- package/dist/static-functions.d.cts +2 -2
- package/dist/static-functions.d.ts +2 -2
- package/dist/static-functions.js +2 -2
- package/package.json +1 -1
- package/src/features/row-selection/rowSelectionFeature.ts +24 -2
- package/src/features/row-selection/rowSelectionFeature.types.ts +13 -3
- package/src/features/row-selection/rowSelectionFeature.utils.ts +119 -68
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
callMemoOrStaticFn,
|
|
3
|
+
cloneState,
|
|
4
|
+
hasOwn,
|
|
5
|
+
makeObjectMap,
|
|
6
|
+
} from '../../utils'
|
|
2
7
|
import type { RowData, Updater } from '../../types/type-utils'
|
|
3
8
|
import type { TableFeatures } from '../../types/TableFeatures'
|
|
4
9
|
import type { RowModel } from '../../core/row-models/coreRowModelsFeature.types'
|
|
@@ -65,7 +70,7 @@ export function table_resetRowSelection<
|
|
|
65
70
|
defaultState
|
|
66
71
|
? makeObjectMap()
|
|
67
72
|
: Object.assign(
|
|
68
|
-
makeObjectMap<
|
|
73
|
+
makeObjectMap<true>(),
|
|
69
74
|
cloneState(table.initialState.rowSelection ?? {}),
|
|
70
75
|
),
|
|
71
76
|
)
|
|
@@ -87,26 +92,36 @@ export function table_resetRowSelection<
|
|
|
87
92
|
export function table_toggleAllRowsSelected<
|
|
88
93
|
TFeatures extends TableFeatures,
|
|
89
94
|
TData extends RowData,
|
|
90
|
-
>(
|
|
95
|
+
>(
|
|
96
|
+
table: Table_Internal<TFeatures, TData>,
|
|
97
|
+
value?: boolean,
|
|
98
|
+
opts?: { deselectAll?: boolean },
|
|
99
|
+
) {
|
|
91
100
|
table_setRowSelection(table, (old) => {
|
|
92
101
|
value =
|
|
93
|
-
typeof value !== 'undefined'
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
102
|
+
typeof value !== 'undefined'
|
|
103
|
+
? value
|
|
104
|
+
: !callMemoOrStaticFn(
|
|
105
|
+
table,
|
|
106
|
+
'getIsAllRowsSelected',
|
|
107
|
+
table_getIsAllRowsSelected,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
if (opts?.deselectAll && !value) {
|
|
111
|
+
// deselectAll opt: clear the whole selection map instead of deleting ids one-by-one
|
|
112
|
+
return makeObjectMap<true>()
|
|
113
|
+
}
|
|
99
114
|
|
|
115
|
+
const rowSelection = Object.assign(makeObjectMap<true>(), old)
|
|
100
116
|
const preGroupedFlatRows = table.getPreGroupedRowModel().flatRows
|
|
101
117
|
|
|
102
118
|
// We don't use `mutateRowIsSelected` here for performance reasons.
|
|
103
119
|
// All of the rows are flat already, so it wouldn't be worth it
|
|
104
120
|
if (value) {
|
|
105
121
|
preGroupedFlatRows.forEach((row) => {
|
|
106
|
-
if (
|
|
107
|
-
|
|
122
|
+
if (row_getCanSelect(row)) {
|
|
123
|
+
rowSelection[row.id] = true
|
|
108
124
|
}
|
|
109
|
-
rowSelection[row.id] = true
|
|
110
125
|
})
|
|
111
126
|
} else {
|
|
112
127
|
preGroupedFlatRows.forEach((row) => {
|
|
@@ -132,15 +147,28 @@ export function table_toggleAllRowsSelected<
|
|
|
132
147
|
export function table_toggleAllPageRowsSelected<
|
|
133
148
|
TFeatures extends TableFeatures,
|
|
134
149
|
TData extends RowData,
|
|
135
|
-
>(
|
|
150
|
+
>(
|
|
151
|
+
table: Table_Internal<TFeatures, TData>,
|
|
152
|
+
value?: boolean,
|
|
153
|
+
opts?: { deselectAll?: boolean },
|
|
154
|
+
) {
|
|
136
155
|
table_setRowSelection(table, (old) => {
|
|
137
156
|
const resolvedValue =
|
|
138
157
|
typeof value !== 'undefined'
|
|
139
158
|
? value
|
|
140
|
-
: !
|
|
159
|
+
: !callMemoOrStaticFn(
|
|
160
|
+
table,
|
|
161
|
+
'getIsAllPageRowsSelected',
|
|
162
|
+
table_getIsAllPageRowsSelected,
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
if (opts?.deselectAll && !resolvedValue) {
|
|
166
|
+
// deselectAll opt: clear the whole selection map instead of deleting ids one-by-one
|
|
167
|
+
return makeObjectMap<true>()
|
|
168
|
+
}
|
|
141
169
|
|
|
142
170
|
const rowSelection: RowSelectionState = Object.assign(
|
|
143
|
-
makeObjectMap<
|
|
171
|
+
makeObjectMap<true>(),
|
|
144
172
|
old,
|
|
145
173
|
)
|
|
146
174
|
|
|
@@ -187,7 +215,13 @@ export function table_getSelectedRowModel<
|
|
|
187
215
|
>(table: Table_Internal<TFeatures, TData>) {
|
|
188
216
|
const rowModel = table.getCoreRowModel()
|
|
189
217
|
|
|
190
|
-
if (
|
|
218
|
+
if (
|
|
219
|
+
!callMemoOrStaticFn(
|
|
220
|
+
table,
|
|
221
|
+
'getIsSomeRowsSelected',
|
|
222
|
+
table_getIsSomeRowsSelected,
|
|
223
|
+
)
|
|
224
|
+
) {
|
|
191
225
|
return {
|
|
192
226
|
rows: [],
|
|
193
227
|
flatRows: [],
|
|
@@ -195,7 +229,7 @@ export function table_getSelectedRowModel<
|
|
|
195
229
|
}
|
|
196
230
|
}
|
|
197
231
|
|
|
198
|
-
return selectRowsFn(rowModel)
|
|
232
|
+
return selectRowsFn(rowModel, table)
|
|
199
233
|
}
|
|
200
234
|
|
|
201
235
|
/**
|
|
@@ -215,7 +249,13 @@ export function table_getFilteredSelectedRowModel<
|
|
|
215
249
|
>(table: Table_Internal<TFeatures, TData>) {
|
|
216
250
|
const rowModel = table.getCoreRowModel()
|
|
217
251
|
|
|
218
|
-
if (
|
|
252
|
+
if (
|
|
253
|
+
!callMemoOrStaticFn(
|
|
254
|
+
table,
|
|
255
|
+
'getIsSomeRowsSelected',
|
|
256
|
+
table_getIsSomeRowsSelected,
|
|
257
|
+
)
|
|
258
|
+
) {
|
|
219
259
|
return {
|
|
220
260
|
rows: [],
|
|
221
261
|
flatRows: [],
|
|
@@ -223,7 +263,7 @@ export function table_getFilteredSelectedRowModel<
|
|
|
223
263
|
}
|
|
224
264
|
}
|
|
225
265
|
|
|
226
|
-
return selectRowsFn(rowModel)
|
|
266
|
+
return selectRowsFn(rowModel, table)
|
|
227
267
|
}
|
|
228
268
|
|
|
229
269
|
/**
|
|
@@ -243,7 +283,13 @@ export function table_getGroupedSelectedRowModel<
|
|
|
243
283
|
>(table: Table_Internal<TFeatures, TData>) {
|
|
244
284
|
const rowModel = table.getCoreRowModel()
|
|
245
285
|
|
|
246
|
-
if (
|
|
286
|
+
if (
|
|
287
|
+
!callMemoOrStaticFn(
|
|
288
|
+
table,
|
|
289
|
+
'getIsSomeRowsSelected',
|
|
290
|
+
table_getIsSomeRowsSelected,
|
|
291
|
+
)
|
|
292
|
+
) {
|
|
247
293
|
return {
|
|
248
294
|
rows: [],
|
|
249
295
|
flatRows: [],
|
|
@@ -251,7 +297,22 @@ export function table_getGroupedSelectedRowModel<
|
|
|
251
297
|
}
|
|
252
298
|
}
|
|
253
299
|
|
|
254
|
-
return selectRowsFn(rowModel)
|
|
300
|
+
return selectRowsFn(rowModel, table)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Returns the ids of all selected rows.
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```ts
|
|
308
|
+
* const selectedRowIds = table_getSelectedRowIds(table)
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
export function table_getSelectedRowIds<
|
|
312
|
+
TFeatures extends TableFeatures,
|
|
313
|
+
TData extends RowData,
|
|
314
|
+
>(table: Table_Internal<TFeatures, TData>): Array<string> {
|
|
315
|
+
return Object.keys(table.atoms.rowSelection?.get() ?? {})
|
|
255
316
|
}
|
|
256
317
|
|
|
257
318
|
/**
|
|
@@ -270,7 +331,7 @@ export function table_getIsAllRowsSelected<
|
|
|
270
331
|
TData extends RowData,
|
|
271
332
|
>(table: Table_Internal<TFeatures, TData>) {
|
|
272
333
|
const preGroupedFlatRows = table.getFilteredRowModel().flatRows
|
|
273
|
-
const rowSelection
|
|
334
|
+
const rowSelection = table.atoms.rowSelection?.get() ?? {}
|
|
274
335
|
|
|
275
336
|
let isAllRowsSelected = Boolean(
|
|
276
337
|
preGroupedFlatRows.length && Object.keys(rowSelection).length,
|
|
@@ -279,8 +340,7 @@ export function table_getIsAllRowsSelected<
|
|
|
279
340
|
if (isAllRowsSelected) {
|
|
280
341
|
if (
|
|
281
342
|
preGroupedFlatRows.some(
|
|
282
|
-
(row) =>
|
|
283
|
-
row_getCanSelect(row) && !isRowIdSelected(rowSelection, row.id),
|
|
343
|
+
(row) => row_getCanSelect(row) && !isRowSelected(row, rowSelection),
|
|
284
344
|
)
|
|
285
345
|
) {
|
|
286
346
|
isAllRowsSelected = false
|
|
@@ -307,13 +367,13 @@ export function table_getIsAllPageRowsSelected<
|
|
|
307
367
|
const paginationFlatRows = table
|
|
308
368
|
.getPaginatedRowModel()
|
|
309
369
|
.flatRows.filter((row) => row_getCanSelect(row))
|
|
310
|
-
const rowSelection
|
|
370
|
+
const rowSelection = table.atoms.rowSelection?.get() ?? {}
|
|
311
371
|
|
|
312
372
|
let isAllPageRowsSelected = !!paginationFlatRows.length
|
|
313
373
|
|
|
314
374
|
if (
|
|
315
375
|
isAllPageRowsSelected &&
|
|
316
|
-
paginationFlatRows.some((row) => !
|
|
376
|
+
paginationFlatRows.some((row) => !isRowSelected(row, rowSelection))
|
|
317
377
|
) {
|
|
318
378
|
isAllPageRowsSelected = false
|
|
319
379
|
}
|
|
@@ -324,8 +384,7 @@ export function table_getIsAllPageRowsSelected<
|
|
|
324
384
|
/**
|
|
325
385
|
* Checks whether selection is partially applied across filtered rows.
|
|
326
386
|
*
|
|
327
|
-
* The result is true when at least one row id is selected
|
|
328
|
-
* selected than the current filtered flat row count.
|
|
387
|
+
* The result is true when at least one row id is selected
|
|
329
388
|
*
|
|
330
389
|
* @example
|
|
331
390
|
* ```ts
|
|
@@ -336,21 +395,15 @@ export function table_getIsSomeRowsSelected<
|
|
|
336
395
|
TFeatures extends TableFeatures,
|
|
337
396
|
TData extends RowData,
|
|
338
397
|
>(table: Table_Internal<TFeatures, TData>) {
|
|
339
|
-
const totalSelected = Object.keys(
|
|
340
|
-
table.atoms.rowSelection?.get() ?? {},
|
|
341
|
-
).length
|
|
342
398
|
return (
|
|
343
|
-
|
|
344
|
-
|
|
399
|
+
callMemoOrStaticFn(table, 'getSelectedRowIds', table_getSelectedRowIds)
|
|
400
|
+
.length > 0
|
|
345
401
|
)
|
|
346
402
|
}
|
|
347
403
|
|
|
348
404
|
/**
|
|
349
405
|
* Checks whether the current page has a partial selection.
|
|
350
406
|
*
|
|
351
|
-
* This is false when all selectable page rows are selected. Otherwise it is true
|
|
352
|
-
* if any selectable page row or descendant is selected.
|
|
353
|
-
*
|
|
354
407
|
* @example
|
|
355
408
|
* ```ts
|
|
356
409
|
* const somePageRowsSelected = table_getIsSomePageRowsSelected(table)
|
|
@@ -360,12 +413,10 @@ export function table_getIsSomePageRowsSelected<
|
|
|
360
413
|
TFeatures extends TableFeatures,
|
|
361
414
|
TData extends RowData,
|
|
362
415
|
>(table: Table_Internal<TFeatures, TData>) {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
.filter((row) => row_getCanSelect(row))
|
|
368
|
-
.some((row) => row_getIsSelected(row) || row_getIsSomeSelected(row))
|
|
416
|
+
return table
|
|
417
|
+
.getPaginatedRowModel()
|
|
418
|
+
.flatRows.filter((row) => row_getCanSelect(row))
|
|
419
|
+
.some((row) => row_getIsSelected(row) || row_getIsSomeSelected(row))
|
|
369
420
|
}
|
|
370
421
|
|
|
371
422
|
/**
|
|
@@ -425,6 +476,10 @@ export function table_getToggleAllPageRowsSelectedHandler<
|
|
|
425
476
|
* @example
|
|
426
477
|
* ```ts
|
|
427
478
|
* row_toggleSelected(row)
|
|
479
|
+
* row_toggleSelected(row, true)
|
|
480
|
+
* row_toggleSelected(row, false)
|
|
481
|
+
* row_toggleSelected(row, true, { selectChildren: false })
|
|
482
|
+
* row_toggleSelected(row, false, { selectChildren: false })
|
|
428
483
|
* ```
|
|
429
484
|
*/
|
|
430
485
|
export function row_toggleSelected<
|
|
@@ -446,20 +501,17 @@ export function row_toggleSelected<
|
|
|
446
501
|
return old
|
|
447
502
|
}
|
|
448
503
|
|
|
449
|
-
const
|
|
450
|
-
makeObjectMap<boolean | undefined>(),
|
|
451
|
-
old,
|
|
452
|
-
)
|
|
504
|
+
const rowSelection = Object.assign(makeObjectMap<true>(), old)
|
|
453
505
|
|
|
454
506
|
mutateRowIsSelected(
|
|
455
|
-
|
|
507
|
+
rowSelection,
|
|
456
508
|
row.id,
|
|
457
509
|
value,
|
|
458
510
|
opts?.selectChildren ?? true,
|
|
459
511
|
row.table,
|
|
460
512
|
)
|
|
461
513
|
|
|
462
|
-
return
|
|
514
|
+
return rowSelection
|
|
463
515
|
})
|
|
464
516
|
}
|
|
465
517
|
|
|
@@ -477,7 +529,8 @@ export function row_getIsSelected<
|
|
|
477
529
|
TFeatures extends TableFeatures,
|
|
478
530
|
TData extends RowData,
|
|
479
531
|
>(row: Row<TFeatures, TData>) {
|
|
480
|
-
|
|
532
|
+
const rowSelection = row.table.atoms.rowSelection?.get() ?? {}
|
|
533
|
+
return isRowSelected(row, rowSelection)
|
|
481
534
|
}
|
|
482
535
|
|
|
483
536
|
/**
|
|
@@ -613,28 +666,28 @@ const mutateRowIsSelected = <
|
|
|
613
666
|
TFeatures extends TableFeatures,
|
|
614
667
|
TData extends RowData,
|
|
615
668
|
>(
|
|
616
|
-
|
|
669
|
+
rowSelection: RowSelectionState,
|
|
617
670
|
rowId: string,
|
|
618
671
|
value: boolean,
|
|
619
672
|
includeChildren: boolean,
|
|
620
673
|
table: Table_Internal<TFeatures, TData>,
|
|
621
|
-
) => {
|
|
674
|
+
): void => {
|
|
622
675
|
const row = table.getRow(rowId, true)
|
|
623
676
|
|
|
624
677
|
if (value) {
|
|
625
678
|
if (!row_getCanMultiSelect(row)) {
|
|
626
|
-
Object.keys(
|
|
679
|
+
Object.keys(rowSelection).forEach((key) => delete rowSelection[key])
|
|
627
680
|
}
|
|
628
681
|
if (row_getCanSelect(row)) {
|
|
629
|
-
|
|
682
|
+
rowSelection[rowId] = true
|
|
630
683
|
}
|
|
631
684
|
} else {
|
|
632
|
-
delete
|
|
685
|
+
delete rowSelection[rowId]
|
|
633
686
|
}
|
|
634
687
|
|
|
635
688
|
if (includeChildren && row.subRows.length && row_getCanSelectSubRows(row)) {
|
|
636
689
|
row.subRows.forEach((r) =>
|
|
637
|
-
mutateRowIsSelected(
|
|
690
|
+
mutateRowIsSelected(rowSelection, r.id, value, includeChildren, table),
|
|
638
691
|
)
|
|
639
692
|
}
|
|
640
693
|
}
|
|
@@ -653,10 +706,13 @@ const mutateRowIsSelected = <
|
|
|
653
706
|
export function selectRowsFn<
|
|
654
707
|
TFeatures extends TableFeatures,
|
|
655
708
|
TData extends RowData,
|
|
656
|
-
>(
|
|
709
|
+
>(
|
|
710
|
+
rowModel: RowModel<TFeatures, TData>,
|
|
711
|
+
table: Table_Internal<TFeatures, TData>,
|
|
712
|
+
): RowModel<TFeatures, TData> {
|
|
657
713
|
const newSelectedFlatRows: Array<Row<TFeatures, TData>> = []
|
|
658
714
|
const newSelectedRowsById = makeObjectMap<Row<TFeatures, TData>>()
|
|
659
|
-
|
|
715
|
+
const rowSelection = table.atoms.rowSelection?.get() ?? {}
|
|
660
716
|
// Filters top level and nested rows.
|
|
661
717
|
const recurseRows = (
|
|
662
718
|
rows: Array<Row<TFeatures, TData>>,
|
|
@@ -665,7 +721,7 @@ export function selectRowsFn<
|
|
|
665
721
|
const result: Array<Row<TFeatures, TData>> = []
|
|
666
722
|
for (let i = 0; i < rows.length; i++) {
|
|
667
723
|
const row = rows[i]!
|
|
668
|
-
const isSelected = isRowSelected(row)
|
|
724
|
+
const isSelected = isRowSelected(row, rowSelection)
|
|
669
725
|
|
|
670
726
|
if (isSelected) {
|
|
671
727
|
newSelectedFlatRows.push(row)
|
|
@@ -709,15 +765,8 @@ export function selectRowsFn<
|
|
|
709
765
|
export function isRowSelected<
|
|
710
766
|
TFeatures extends TableFeatures,
|
|
711
767
|
TData extends RowData,
|
|
712
|
-
>(row: Row<TFeatures, TData
|
|
713
|
-
return
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
function isRowIdSelected(
|
|
717
|
-
rowSelection: RowSelectionState | undefined,
|
|
718
|
-
rowId: string,
|
|
719
|
-
): boolean {
|
|
720
|
-
return !!(rowSelection && hasOwn(rowSelection, rowId) && rowSelection[rowId])
|
|
768
|
+
>(row: Row<TFeatures, TData>, rowSelection: RowSelectionState): boolean {
|
|
769
|
+
return !!(hasOwn(rowSelection, row.id) && rowSelection[row.id])
|
|
721
770
|
}
|
|
722
771
|
|
|
723
772
|
/**
|
|
@@ -736,6 +785,8 @@ export function isSubRowSelected<
|
|
|
736
785
|
>(row: Row<TFeatures, TData>): boolean | 'some' | 'all' {
|
|
737
786
|
if (!row.subRows.length) return false
|
|
738
787
|
|
|
788
|
+
const rowSelection = row.table.atoms.rowSelection?.get() ?? {}
|
|
789
|
+
|
|
739
790
|
let allChildrenSelected = true
|
|
740
791
|
let someSelected = false
|
|
741
792
|
|
|
@@ -746,7 +797,7 @@ export function isSubRowSelected<
|
|
|
746
797
|
}
|
|
747
798
|
|
|
748
799
|
if (row_getCanSelect(subRow)) {
|
|
749
|
-
if (isRowSelected(subRow)) {
|
|
800
|
+
if (isRowSelected(subRow, rowSelection)) {
|
|
750
801
|
someSelected = true
|
|
751
802
|
} else {
|
|
752
803
|
allChildrenSelected = false
|