@tanstack/table-core 8.0.0-alpha.30 → 8.0.0-alpha.36
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/aggregationTypes.js +1 -0
- package/build/cjs/aggregationTypes.js.map +1 -1
- package/build/cjs/core.js +43 -78
- package/build/cjs/core.js.map +1 -1
- package/build/cjs/createTable.js.map +1 -1
- package/build/cjs/features/ColumnSizing.js.map +1 -1
- package/build/cjs/features/Expanding.js +12 -16
- package/build/cjs/features/Expanding.js.map +1 -1
- package/build/cjs/features/Filters.js +28 -81
- package/build/cjs/features/Filters.js.map +1 -1
- package/build/cjs/features/Grouping.js +10 -16
- package/build/cjs/features/Grouping.js.map +1 -1
- package/build/cjs/features/Headers.js.map +1 -1
- package/build/cjs/features/Ordering.js.map +1 -1
- package/build/cjs/features/Pagination.js +8 -13
- package/build/cjs/features/Pagination.js.map +1 -1
- package/build/cjs/features/Pinning.js.map +1 -1
- package/build/cjs/features/RowSelection.js +9 -3
- package/build/cjs/features/RowSelection.js.map +1 -1
- package/build/cjs/features/Sorting.js +10 -16
- package/build/cjs/features/Sorting.js.map +1 -1
- package/build/cjs/features/Visibility.js.map +1 -1
- package/build/cjs/filterTypes.js.map +1 -1
- package/build/cjs/index.js +19 -12
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/sortTypes.js.map +1 -1
- package/build/cjs/utils/filterRowsUtils.js.map +1 -1
- package/build/cjs/utils/getColumnFilteredRowModelSync.js +115 -0
- package/build/cjs/utils/getColumnFilteredRowModelSync.js.map +1 -0
- package/build/cjs/utils/getCoreRowModelAsync.js +127 -0
- package/build/cjs/utils/getCoreRowModelAsync.js.map +1 -0
- package/build/cjs/utils/getCoreRowModelSync.js +104 -0
- package/build/cjs/utils/getCoreRowModelSync.js.map +1 -0
- package/build/cjs/utils/getExpandedRowModel.js +61 -0
- package/build/cjs/utils/getExpandedRowModel.js.map +1 -0
- package/build/cjs/utils/getGlobalFilteredRowModelSync.js +91 -0
- package/build/cjs/utils/getGlobalFilteredRowModelSync.js.map +1 -0
- package/build/cjs/utils/getGroupedRowModel.js +176 -0
- package/build/cjs/utils/getGroupedRowModel.js.map +1 -0
- package/build/cjs/utils/getPaginationRowModel.js +61 -0
- package/build/cjs/utils/getPaginationRowModel.js.map +1 -0
- package/build/cjs/utils/getSortedRowModelSync.js +118 -0
- package/build/cjs/utils/getSortedRowModelSync.js.map +1 -0
- package/build/cjs/utils.js +155 -16
- package/build/cjs/utils.js.map +1 -1
- package/build/esm/index.js +877 -488
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +408 -300
- package/build/types/aggregationTypes.d.ts +1 -1
- package/build/types/core.d.ts +9 -1
- package/build/types/features/Expanding.d.ts +2 -1
- package/build/types/features/Filters.d.ts +6 -2
- package/build/types/features/Grouping.d.ts +3 -2
- package/build/types/features/Pagination.d.ts +2 -1
- package/build/types/features/Sorting.d.ts +2 -1
- package/build/types/index.d.ts +8 -6
- package/build/types/types.d.ts +2 -2
- package/build/types/utils/getColumnFilteredRowModelAsync.d.ts +4 -0
- package/build/types/utils/getColumnFilteredRowModelSync.d.ts +2 -0
- package/build/types/utils/getCoreRowModelAsync.d.ts +4 -0
- package/build/types/utils/getCoreRowModelSync.d.ts +2 -0
- package/build/types/utils/getExpandedRowModel.d.ts +7 -0
- package/build/types/utils/getGlobalFilteredRowModelAsync.d.ts +4 -0
- package/build/types/utils/getGlobalFilteredRowModelSync.d.ts +2 -0
- package/build/types/utils/getGroupedRowModel.d.ts +2 -0
- package/build/types/utils/getPaginationRowModel.d.ts +4 -0
- package/build/types/utils/{sortRowsQuicksortFn.d.ts → getSortedRowModelAsync.d.ts} +3 -1
- package/build/types/utils/getSortedRowModelSync.d.ts +2 -0
- package/build/types/utils.d.ts +16 -0
- package/build/umd/index.development.js +887 -493
- 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 +3 -1
- package/src/aggregationTypes.ts +1 -1
- package/src/core.tsx +53 -97
- package/src/features/Expanding.ts +19 -27
- package/src/features/Filters.ts +52 -99
- package/src/features/Grouping.ts +19 -21
- package/src/features/Pagination.ts +17 -20
- package/src/features/RowSelection.ts +5 -3
- package/src/features/Sorting.ts +19 -23
- package/src/index.tsx +8 -6
- package/src/types.ts +3 -1
- package/src/utils/getColumnFilteredRowModelAsync.ts +119 -0
- package/src/utils/getColumnFilteredRowModelSync.ts +99 -0
- package/src/utils/getCoreRowModelAsync.ts +105 -0
- package/src/utils/getCoreRowModelSync.ts +107 -0
- package/src/utils/getExpandedRowModel.ts +58 -0
- package/src/utils/getGlobalFilteredRowModelAsync.ts +97 -0
- package/src/utils/getGlobalFilteredRowModelSync.ts +76 -0
- package/src/utils/getGroupedRowModel.ts +191 -0
- package/src/utils/getPaginationRowModel.ts +45 -0
- package/src/utils/getSortedRowModelAsync.ts +201 -0
- package/src/utils/getSortedRowModelSync.ts +118 -0
- package/src/utils.tsx +189 -16
- package/build/cjs/utils/columnFilterRowsFn.js +0 -73
- package/build/cjs/utils/columnFilterRowsFn.js.map +0 -1
- package/build/cjs/utils/expandRowsFn.js +0 -37
- package/build/cjs/utils/expandRowsFn.js.map +0 -1
- package/build/cjs/utils/globalFilterRowsFn.js +0 -49
- 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/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/src/utils/columnFilterRowsFn.ts +0 -56
- package/src/utils/expandRowsFn.ts +0 -34
- package/src/utils/globalFilterRowsFn.ts +0 -39
- package/src/utils/groupRowsFn.ts +0 -174
- package/src/utils/paginateRowsFn.ts +0 -28
- package/src/utils/sortRowsFn.ts +0 -99
- package/src/utils/sortRowsQuicksortFn.ts +0 -174
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { TableInstance, RowModel, AnyGenerics, Row } from '../types'
|
|
2
|
+
import { incrementalMemo, memo } from '../utils'
|
|
3
|
+
import {
|
|
4
|
+
filterRowModelFromLeafs,
|
|
5
|
+
filterRowModelFromRoot,
|
|
6
|
+
} from './filterRowsUtils'
|
|
7
|
+
|
|
8
|
+
export function getGlobalFilteredRowModelAsync<
|
|
9
|
+
TGenerics extends AnyGenerics
|
|
10
|
+
>(opts?: {
|
|
11
|
+
initialSync?: boolean
|
|
12
|
+
}): (instance: TableInstance<TGenerics>) => () => RowModel<TGenerics> {
|
|
13
|
+
return instance =>
|
|
14
|
+
incrementalMemo(
|
|
15
|
+
() => [instance.getState().globalFilter, instance.getCoreRowModel()],
|
|
16
|
+
(_globalFilter, rowModel): RowModel<TGenerics> => {
|
|
17
|
+
return {
|
|
18
|
+
rows: rowModel.rows.slice(),
|
|
19
|
+
flatRows: [],
|
|
20
|
+
rowsById: rowModel.rowsById,
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
(globalFilter, rowModel) => rowModelRef => scheduleTask => {
|
|
24
|
+
// TODO: Figure out how to do scheduled filtering
|
|
25
|
+
// The trick will be batching those tasks in a way
|
|
26
|
+
// that makes them fast. JS work loops don't tend to do well with
|
|
27
|
+
// a high number of tasks that do one thing. Instead, shoot for each
|
|
28
|
+
// task to have a few thousand opts (or whatever amount is generally
|
|
29
|
+
// fast for most devices).
|
|
30
|
+
throw new Error('')
|
|
31
|
+
|
|
32
|
+
// const globalFilteredRowModel: RowModel<TGenerics> = (() => {
|
|
33
|
+
// if (!rowModel.rows.length || !globalFilter) {
|
|
34
|
+
// return rowModel
|
|
35
|
+
// }
|
|
36
|
+
|
|
37
|
+
// const filterFromLeafRows = instance.options.filterFromLeafRows
|
|
38
|
+
|
|
39
|
+
// const filterFn = instance.getGlobalFilterFn()
|
|
40
|
+
|
|
41
|
+
// if (!filterFn) {
|
|
42
|
+
// if (process.env.NODE_ENV !== 'production') {
|
|
43
|
+
// console.warn(`Could not find a valid 'globalFilterType'`)
|
|
44
|
+
// }
|
|
45
|
+
// return rowModel
|
|
46
|
+
// }
|
|
47
|
+
|
|
48
|
+
// const filterableColumns = instance
|
|
49
|
+
// .getAllLeafColumns()
|
|
50
|
+
// .filter(column => column.getCanGlobalFilter())
|
|
51
|
+
|
|
52
|
+
// const filterableColumnIds = filterableColumns.map(d => d.id)
|
|
53
|
+
|
|
54
|
+
// const filterRows = (rows: Row<TGenerics>[]) => {
|
|
55
|
+
// return filterFn(rows, filterableColumnIds, globalFilter)
|
|
56
|
+
// }
|
|
57
|
+
|
|
58
|
+
// if (filterFromLeafRows) {
|
|
59
|
+
// return filterRowModelFromLeafs(rowModel.rows, filterRows, instance)
|
|
60
|
+
// }
|
|
61
|
+
|
|
62
|
+
// return filterRowModelFromRoot(rowModel.rows, filterRows, instance)
|
|
63
|
+
// })()
|
|
64
|
+
|
|
65
|
+
// // Now that each filtered column has it's partially filtered rows,
|
|
66
|
+
// // lets assign the final filtered rows to all of the other columns
|
|
67
|
+
// const nonFilteredColumns = instance
|
|
68
|
+
// .getAllLeafColumns()
|
|
69
|
+
// .filter(
|
|
70
|
+
// column =>
|
|
71
|
+
// !instance.getState().columnFilters?.find(d => d.id === column.id)
|
|
72
|
+
// )
|
|
73
|
+
|
|
74
|
+
// // This essentially enables faceted filter options to be built easily
|
|
75
|
+
// // using every column's preFilteredRows value
|
|
76
|
+
|
|
77
|
+
// nonFilteredColumns.forEach(column => {
|
|
78
|
+
// column.getPreFilteredRows = () => globalFilteredRowModel.rows
|
|
79
|
+
// })
|
|
80
|
+
|
|
81
|
+
// return globalFilteredRowModel
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
key: 'getGlobalFilteredRowModelAsync',
|
|
85
|
+
initialSync: opts?.initialSync,
|
|
86
|
+
onProgress: progress => {
|
|
87
|
+
instance.setState(old => ({ ...old, globalFilterProgress: progress }))
|
|
88
|
+
},
|
|
89
|
+
debug: () => instance.options.debugAll ?? instance.options.debugTable,
|
|
90
|
+
onChange: () => {
|
|
91
|
+
instance.queue(() => {
|
|
92
|
+
instance._notifySortingReset()
|
|
93
|
+
})
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { TableInstance, RowModel, AnyGenerics, Row } from '../types'
|
|
2
|
+
import { memo } from '../utils'
|
|
3
|
+
import {
|
|
4
|
+
filterRowModelFromLeafs,
|
|
5
|
+
filterRowModelFromRoot,
|
|
6
|
+
} from './filterRowsUtils'
|
|
7
|
+
|
|
8
|
+
export function getGlobalFilteredRowModelSync<
|
|
9
|
+
TGenerics extends AnyGenerics
|
|
10
|
+
>(): (instance: TableInstance<TGenerics>) => () => RowModel<TGenerics> {
|
|
11
|
+
return instance =>
|
|
12
|
+
memo(
|
|
13
|
+
() => [instance.getState().globalFilter, instance.getCoreRowModel()],
|
|
14
|
+
(globalFilter, rowModel) => {
|
|
15
|
+
const globalFilteredRowModel: RowModel<TGenerics> = (() => {
|
|
16
|
+
if (!rowModel.rows.length || !globalFilter) {
|
|
17
|
+
return rowModel
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const filterFromLeafRows = instance.options.filterFromLeafRows
|
|
21
|
+
|
|
22
|
+
const filterFn = instance.getGlobalFilterFn()
|
|
23
|
+
|
|
24
|
+
if (!filterFn) {
|
|
25
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
26
|
+
console.warn(`Could not find a valid 'globalFilterType'`)
|
|
27
|
+
}
|
|
28
|
+
return rowModel
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const filterableColumns = instance
|
|
32
|
+
.getAllLeafColumns()
|
|
33
|
+
.filter(column => column.getCanGlobalFilter())
|
|
34
|
+
|
|
35
|
+
const filterableColumnIds = filterableColumns.map(d => d.id)
|
|
36
|
+
|
|
37
|
+
const filterRows = (rows: Row<TGenerics>[]) => {
|
|
38
|
+
return filterFn(rows, filterableColumnIds, globalFilter)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (filterFromLeafRows) {
|
|
42
|
+
return filterRowModelFromLeafs(rowModel.rows, filterRows, instance)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return filterRowModelFromRoot(rowModel.rows, filterRows, instance)
|
|
46
|
+
})()
|
|
47
|
+
|
|
48
|
+
// Now that each filtered column has it's partially filtered rows,
|
|
49
|
+
// lets assign the final filtered rows to all of the other columns
|
|
50
|
+
const nonFilteredColumns = instance
|
|
51
|
+
.getAllLeafColumns()
|
|
52
|
+
.filter(
|
|
53
|
+
column =>
|
|
54
|
+
!instance.getState().columnFilters?.find(d => d.id === column.id)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
// This essentially enables faceted filter options to be built easily
|
|
58
|
+
// using every column's preFilteredRows value
|
|
59
|
+
|
|
60
|
+
nonFilteredColumns.forEach(column => {
|
|
61
|
+
column.getPreFilteredRows = () => globalFilteredRowModel.rows
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
return globalFilteredRowModel
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
key: 'getGlobalFilteredRowModelSync',
|
|
68
|
+
debug: () => instance.options.debugAll ?? instance.options.debugTable,
|
|
69
|
+
onChange: () => {
|
|
70
|
+
instance.queue(() => {
|
|
71
|
+
instance._notifySortingReset()
|
|
72
|
+
})
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { TableInstance, Row, RowModel, AnyGenerics } from '../types'
|
|
2
|
+
import { flattenBy, memo } from '../utils'
|
|
3
|
+
|
|
4
|
+
export function getGroupedRowModelSync<TGenerics extends AnyGenerics>(): (
|
|
5
|
+
instance: TableInstance<TGenerics>
|
|
6
|
+
) => () => RowModel<TGenerics> {
|
|
7
|
+
return instance =>
|
|
8
|
+
memo(
|
|
9
|
+
() => [instance.getState().grouping, instance.getSortedRowModel()],
|
|
10
|
+
(grouping, rowModel) => {
|
|
11
|
+
if (!rowModel.rows.length || !grouping.length) {
|
|
12
|
+
return rowModel
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Filter the grouping list down to columns that exist
|
|
16
|
+
const existingGrouping = grouping.filter(columnId =>
|
|
17
|
+
instance.getColumn(columnId)
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
// Find the columns that can or are aggregating
|
|
21
|
+
// Uses each column to aggregate rows into a single value
|
|
22
|
+
const aggregateRowsToValues = (
|
|
23
|
+
leafRows: Row<TGenerics>[],
|
|
24
|
+
groupedRows: Row<TGenerics>[],
|
|
25
|
+
depth: number
|
|
26
|
+
) => {
|
|
27
|
+
const values: Record<string, unknown> = {}
|
|
28
|
+
|
|
29
|
+
instance.getAllLeafColumns().forEach(column => {
|
|
30
|
+
// Don't aggregate columns that are in the grouping
|
|
31
|
+
if (existingGrouping.includes(column.id)) {
|
|
32
|
+
values[column.id] = groupedRows[0]
|
|
33
|
+
? groupedRows[0].values[column.id]
|
|
34
|
+
: null
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Aggregate the values
|
|
39
|
+
const aggregateFn = instance.getColumnAggregationFn(column.id)
|
|
40
|
+
|
|
41
|
+
if (aggregateFn) {
|
|
42
|
+
values[column.id] = aggregateFn(
|
|
43
|
+
() =>
|
|
44
|
+
leafRows.map(row => {
|
|
45
|
+
let columnValue = row.values[column.id]
|
|
46
|
+
|
|
47
|
+
if (!depth && column.aggregateValue) {
|
|
48
|
+
columnValue = column.aggregateValue(columnValue)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return columnValue
|
|
52
|
+
}),
|
|
53
|
+
() => groupedRows.map(row => row.values[column.id])
|
|
54
|
+
)
|
|
55
|
+
} else if (column.aggregationType) {
|
|
56
|
+
console.info({ column })
|
|
57
|
+
throw new Error(
|
|
58
|
+
process.env.NODE_ENV !== 'production'
|
|
59
|
+
? `Table: Invalid column.aggregateType option for column listed above`
|
|
60
|
+
: ''
|
|
61
|
+
)
|
|
62
|
+
} else {
|
|
63
|
+
values[column.id] = null
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
return values
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const groupedFlatRows: Row<TGenerics>[] = []
|
|
71
|
+
const groupedRowsById: Record<string, Row<TGenerics>> = {}
|
|
72
|
+
// const onlyGroupedFlatRows: Row[] = [];
|
|
73
|
+
// const onlyGroupedRowsById: Record<RowId, Row> = {};
|
|
74
|
+
// const nonGroupedFlatRows: Row[] = [];
|
|
75
|
+
// const nonGroupedRowsById: Record<RowId, Row> = {};
|
|
76
|
+
|
|
77
|
+
// Recursively group the data
|
|
78
|
+
const groupUpRecursively = (
|
|
79
|
+
rows: Row<TGenerics>[],
|
|
80
|
+
depth = 0,
|
|
81
|
+
parentId: string
|
|
82
|
+
) => {
|
|
83
|
+
// This is the last level, just return the rows
|
|
84
|
+
if (depth === existingGrouping.length) {
|
|
85
|
+
return rows
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const columnId = existingGrouping[depth]!
|
|
89
|
+
|
|
90
|
+
// Group the rows together for this level
|
|
91
|
+
const rowGroupsMap = groupBy(rows, columnId)
|
|
92
|
+
|
|
93
|
+
// Peform aggregations for each group
|
|
94
|
+
const aggregatedGroupedRows = Array.from(rowGroupsMap.entries()).map(
|
|
95
|
+
([groupingValue, groupedRows], index) => {
|
|
96
|
+
let id = `${columnId}:${groupingValue}`
|
|
97
|
+
id = parentId ? `${parentId}>${id}` : id
|
|
98
|
+
|
|
99
|
+
// First, Recurse to group sub rows before aggregation
|
|
100
|
+
const subRows = groupUpRecursively(groupedRows, depth + 1, id)
|
|
101
|
+
|
|
102
|
+
// Flatten the leaf rows of the rows in this group
|
|
103
|
+
const leafRows = depth
|
|
104
|
+
? flattenBy(groupedRows, row => row.subRows)
|
|
105
|
+
: groupedRows
|
|
106
|
+
|
|
107
|
+
const values = aggregateRowsToValues(leafRows, groupedRows, depth)
|
|
108
|
+
|
|
109
|
+
const row = instance.createRow(
|
|
110
|
+
id,
|
|
111
|
+
undefined,
|
|
112
|
+
index,
|
|
113
|
+
depth,
|
|
114
|
+
values
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
Object.assign(row, {
|
|
118
|
+
groupingColumnId: columnId,
|
|
119
|
+
groupingValue,
|
|
120
|
+
subRows,
|
|
121
|
+
leafRows,
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
subRows.forEach(subRow => {
|
|
125
|
+
groupedFlatRows.push(subRow)
|
|
126
|
+
groupedRowsById[subRow.id] = subRow
|
|
127
|
+
// if (subRow.getIsGrouped?.()) {
|
|
128
|
+
// onlyGroupedFlatRows.push(subRow);
|
|
129
|
+
// onlyGroupedRowsById[subRow.id] = subRow;
|
|
130
|
+
// } else {
|
|
131
|
+
// nonGroupedFlatRows.push(subRow);
|
|
132
|
+
// nonGroupedRowsById[subRow.id] = subRow;
|
|
133
|
+
// }
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
return row
|
|
137
|
+
}
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
return aggregatedGroupedRows
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const groupedRows = groupUpRecursively(rowModel.rows, 0, '')
|
|
144
|
+
|
|
145
|
+
groupedRows.forEach(subRow => {
|
|
146
|
+
groupedFlatRows.push(subRow)
|
|
147
|
+
groupedRowsById[subRow.id] = subRow
|
|
148
|
+
// if (subRow.getIsGrouped?.()) {
|
|
149
|
+
// onlyGroupedFlatRows.push(subRow);
|
|
150
|
+
// onlyGroupedRowsById[subRow.id] = subRow;
|
|
151
|
+
// } else {
|
|
152
|
+
// nonGroupedFlatRows.push(subRow);
|
|
153
|
+
// nonGroupedRowsById[subRow.id] = subRow;
|
|
154
|
+
// }
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
rows: groupedRows,
|
|
159
|
+
flatRows: groupedFlatRows,
|
|
160
|
+
rowsById: groupedRowsById,
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
key: 'getGroupedRowModel',
|
|
165
|
+
debug: () => instance.options.debugAll ?? instance.options.debugTable,
|
|
166
|
+
onChange: () => {
|
|
167
|
+
instance.queue(() => {
|
|
168
|
+
instance._notifyExpandedReset()
|
|
169
|
+
})
|
|
170
|
+
},
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function groupBy<TGenerics extends AnyGenerics>(
|
|
176
|
+
rows: Row<TGenerics>[],
|
|
177
|
+
columnId: string
|
|
178
|
+
) {
|
|
179
|
+
const groupMap = new Map<any, Row<TGenerics>[]>()
|
|
180
|
+
|
|
181
|
+
return rows.reduce((map, row) => {
|
|
182
|
+
const resKey = `${row.values[columnId]}`
|
|
183
|
+
const previous = map.get(resKey)
|
|
184
|
+
if (!previous) {
|
|
185
|
+
map.set(resKey, [row])
|
|
186
|
+
} else {
|
|
187
|
+
map.set(resKey, [...previous, row])
|
|
188
|
+
}
|
|
189
|
+
return map
|
|
190
|
+
}, groupMap)
|
|
191
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { TableInstance, RowModel, AnyGenerics } from '../types'
|
|
2
|
+
import { memo } from '../utils'
|
|
3
|
+
import { expandRows } from './getExpandedRowModel'
|
|
4
|
+
|
|
5
|
+
export function getPaginationRowModel<TGenerics extends AnyGenerics>(opts?: {
|
|
6
|
+
initialSync: boolean
|
|
7
|
+
}): (instance: TableInstance<TGenerics>) => () => RowModel<TGenerics> {
|
|
8
|
+
return instance =>
|
|
9
|
+
memo(
|
|
10
|
+
() => [instance.getState().pagination, instance.getExpandedRowModel()],
|
|
11
|
+
(pagination, rowModel) => {
|
|
12
|
+
if (!rowModel.rows.length) {
|
|
13
|
+
return rowModel
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const { pageSize, pageIndex } = pagination
|
|
17
|
+
let { rows, flatRows, rowsById } = rowModel
|
|
18
|
+
const pageStart = pageSize * pageIndex
|
|
19
|
+
const pageEnd = pageStart + pageSize
|
|
20
|
+
|
|
21
|
+
rows = rows.slice(pageStart, pageEnd)
|
|
22
|
+
|
|
23
|
+
if (!instance.options.paginateExpandedRows) {
|
|
24
|
+
return expandRows(
|
|
25
|
+
{
|
|
26
|
+
rows,
|
|
27
|
+
flatRows,
|
|
28
|
+
rowsById,
|
|
29
|
+
},
|
|
30
|
+
instance
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
rows,
|
|
36
|
+
flatRows,
|
|
37
|
+
rowsById,
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
key: 'getPaginationRowModel',
|
|
42
|
+
debug: () => instance.options.debugAll ?? instance.options.debugTable,
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { TableInstance, Row, RowModel, AnyGenerics } from '../types'
|
|
2
|
+
import { SortingFn } from '../features/Sorting'
|
|
3
|
+
import { incrementalMemo, memo } from '../utils'
|
|
4
|
+
|
|
5
|
+
export function getSortedRowModelSync<TGenerics extends AnyGenerics>(opts?: {
|
|
6
|
+
initialSync?: boolean
|
|
7
|
+
}): (instance: TableInstance<TGenerics>) => () => RowModel<TGenerics> {
|
|
8
|
+
return instance =>
|
|
9
|
+
incrementalMemo(
|
|
10
|
+
() => [instance.getState().sorting, instance.getGlobalFilteredRowModel()],
|
|
11
|
+
(_sorting, rowModel): RowModel<TGenerics> => {
|
|
12
|
+
return {
|
|
13
|
+
rows: rowModel.rows.slice(),
|
|
14
|
+
flatRows: [],
|
|
15
|
+
rowsById: rowModel.rowsById,
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
(sorting, rowModel) => rowModelRef => scheduleTask => {
|
|
19
|
+
// TODO: Figure out how to do async sorting
|
|
20
|
+
// We probably need to use a sorting algo that is "divide and
|
|
21
|
+
// conquer", since that will probably distribute the easiest into
|
|
22
|
+
// separate tasks. The trick will be batching those tasks in a way
|
|
23
|
+
// that makes them fast. JS work loops don't tend to do well with
|
|
24
|
+
// a high number of tasks that do one thing. Instead, shoot for each
|
|
25
|
+
// task to have a few thousand opts (or whatever amount is generally
|
|
26
|
+
// fast for most devices).
|
|
27
|
+
|
|
28
|
+
throw new Error()
|
|
29
|
+
|
|
30
|
+
// if (!rowModel.rows.length || !sorting?.length) {
|
|
31
|
+
// rowModelRef.current = rowModel
|
|
32
|
+
// return
|
|
33
|
+
// }
|
|
34
|
+
|
|
35
|
+
// const sortingState = instance.getState().sorting
|
|
36
|
+
|
|
37
|
+
// const sortedFlatRows: Row<TGenerics>[] = []
|
|
38
|
+
|
|
39
|
+
// // Filter out sortings that correspond to non existing columns
|
|
40
|
+
// const availableSorting = sortingState.filter(sort =>
|
|
41
|
+
// instance.getColumnCanSort(sort.id)
|
|
42
|
+
// )
|
|
43
|
+
|
|
44
|
+
// const sorters = availableSorting.map(columnSorting => {
|
|
45
|
+
// const column = instance.getColumn(columnSorting.id)!
|
|
46
|
+
// const sortingFn = instance.getColumnSortingFn(columnSorting.id)!
|
|
47
|
+
|
|
48
|
+
// return {
|
|
49
|
+
// id: columnSorting.id,
|
|
50
|
+
// compare: (rowA: Row<TGenerics>, rowB: Row<TGenerics>) =>
|
|
51
|
+
// sortingFn(rowA, rowB, columnSorting.id),
|
|
52
|
+
// desc: columnSorting.desc,
|
|
53
|
+
// invertSorting: column.invertSorting,
|
|
54
|
+
// sortUndefined: column.sortUndefined,
|
|
55
|
+
// }
|
|
56
|
+
// })
|
|
57
|
+
|
|
58
|
+
// const sortData = (rows: Row<TGenerics>[]) => {
|
|
59
|
+
// // This will also perform a stable sorting using the row index
|
|
60
|
+
// // if needed.
|
|
61
|
+
|
|
62
|
+
// const sortedData = quicksort(rows, sorters)
|
|
63
|
+
|
|
64
|
+
// // If there are sub-rows, sort them
|
|
65
|
+
// sortedData.forEach(row => {
|
|
66
|
+
// rowModelRef.current.flatRows.push(row)
|
|
67
|
+
// if (!row.subRows || row.subRows.length <= 1) {
|
|
68
|
+
// return
|
|
69
|
+
// }
|
|
70
|
+
// row.subRows = sortData(row.subRows)
|
|
71
|
+
// })
|
|
72
|
+
|
|
73
|
+
// return sortedData
|
|
74
|
+
// }
|
|
75
|
+
|
|
76
|
+
// sortData(rowModelRef.current.rows)
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
key: 'getSortedRowModel',
|
|
80
|
+
initialSync: opts?.initialSync,
|
|
81
|
+
onProgress: progress => {
|
|
82
|
+
instance.setState(old => ({ ...old, sortingProgress: progress }))
|
|
83
|
+
},
|
|
84
|
+
debug: () => instance.options.debugAll ?? instance.options.debugTable,
|
|
85
|
+
onChange: () => {
|
|
86
|
+
instance.queue(() => {
|
|
87
|
+
instance._notifyGroupingReset()
|
|
88
|
+
})
|
|
89
|
+
},
|
|
90
|
+
}
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type Sorter<TGenerics extends AnyGenerics> = {
|
|
95
|
+
id: string
|
|
96
|
+
compare: (a: Row<TGenerics>, b: Row<TGenerics>) => number
|
|
97
|
+
desc?: boolean
|
|
98
|
+
sortUndefined?: false | -1 | 1
|
|
99
|
+
invertSorting?: boolean
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function quicksort<TGenerics extends AnyGenerics>(
|
|
103
|
+
rows: Row<TGenerics>[],
|
|
104
|
+
sorters: Sorter<TGenerics>[]
|
|
105
|
+
): Row<TGenerics>[] {
|
|
106
|
+
if (!rows.length) {
|
|
107
|
+
return rows
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Copy the rows so we can mutate the order
|
|
111
|
+
rows = rows.slice()
|
|
112
|
+
|
|
113
|
+
// Start the quicksort recursion
|
|
114
|
+
recurse(0, rows.length - 1)
|
|
115
|
+
|
|
116
|
+
// Return the rows
|
|
117
|
+
return rows
|
|
118
|
+
|
|
119
|
+
function recurse(start: number, end: number) {
|
|
120
|
+
const pivotIndex = pivot(start, end)
|
|
121
|
+
|
|
122
|
+
if (start < pivotIndex - 1) {
|
|
123
|
+
recurse(start, pivotIndex - 1)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (pivotIndex < end) {
|
|
127
|
+
recurse(pivotIndex + 1, end)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function pivot(start: number, end: number) {
|
|
132
|
+
const pivotIndex = Math.floor((start + end) / 2)
|
|
133
|
+
let i = start
|
|
134
|
+
let j = end
|
|
135
|
+
|
|
136
|
+
const compare = (a: number, b: number) => {
|
|
137
|
+
const rowA = rows[a]
|
|
138
|
+
const rowB = rows[b]
|
|
139
|
+
|
|
140
|
+
for (let i = 0; i < sorters.length; i += 1) {
|
|
141
|
+
const sorter = sorters[i]!
|
|
142
|
+
const isDesc = sorter?.desc ?? false
|
|
143
|
+
|
|
144
|
+
if (sorter.sortUndefined) {
|
|
145
|
+
const aValue = rowA.values[sorter.id]
|
|
146
|
+
const bValue = rowB.values[sorter.id]
|
|
147
|
+
|
|
148
|
+
const aUndefined = typeof aValue === 'undefined'
|
|
149
|
+
const bUndefined = typeof bValue === 'undefined'
|
|
150
|
+
|
|
151
|
+
if (aUndefined || bUndefined) {
|
|
152
|
+
if (aUndefined !== bUndefined) {
|
|
153
|
+
return aUndefined ? sorter.sortUndefined : -sorter.sortUndefined
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// This function should always return in ascending order
|
|
159
|
+
let sortInt = sorter.compare(rowA, rowB)
|
|
160
|
+
|
|
161
|
+
if (sortInt !== 0) {
|
|
162
|
+
if (isDesc) {
|
|
163
|
+
sortInt *= -1
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (sorter.invertSorting) {
|
|
167
|
+
sortInt *= -1
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return sortInt
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return rowA.index < rowB.index ? -1 : 1
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
while (i <= j) {
|
|
178
|
+
while (compare(i, pivotIndex) < 0) {
|
|
179
|
+
i++
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
while (compare(pivotIndex, j) < 0) {
|
|
183
|
+
j--
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (i <= j) {
|
|
187
|
+
swap(rows, i, j)
|
|
188
|
+
i++
|
|
189
|
+
j--
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return i
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function swap<T>(arr: T[], a: number, b: number) {
|
|
197
|
+
const t = arr[a]
|
|
198
|
+
arr[a] = arr[b]
|
|
199
|
+
arr[b] = t
|
|
200
|
+
}
|
|
201
|
+
}
|