-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
getCoreRowModel.ts
86 lines (76 loc) 路 2.4 KB
/
getCoreRowModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { createRow } from '../core/row'
import { Table, Row, RowModel, RowData } from '../types'
import { memo } from '../utils'
export function getCoreRowModel<TData extends RowData>(): (
table: Table<TData>
) => () => RowModel<TData> {
return table =>
memo(
() => [table.options.data],
(
data
): {
rows: Row<TData>[]
flatRows: Row<TData>[]
rowsById: Record<string, Row<TData>>
} => {
const rowModel: RowModel<TData> = {
rows: [],
flatRows: [],
rowsById: {},
}
const accessRows = (
originalRows: TData[],
depth = 0,
parentRow?: Row<TData>
): Row<TData>[] => {
const rows = [] as Row<TData>[]
for (let i = 0; i < originalRows.length; i++) {
// This could be an expensive check at scale, so we should move it somewhere else, but where?
// if (!id) {
// if (process.env.NODE_ENV !== 'production') {
// throw new Error(`getRowId expected an ID, but got ${id}`)
// }
// }
// Make the row
const row = createRow(
table,
table._getRowId(originalRows[i]!, i, parentRow),
originalRows[i]!,
i,
depth,
undefined,
parentRow
)
// Keep track of every row in a flat array
rowModel.flatRows.push(row)
// Also keep track of every row by its ID
rowModel.rowsById[row.id] = row
// Push table row into parent
rows.push(row)
// Get the original subrows
if (table.options.getSubRows) {
row.originalSubRows = table.options.getSubRows(
originalRows[i]!,
i
)
// Then recursively access them
if (row.originalSubRows?.length) {
row.subRows = accessRows(row.originalSubRows, depth + 1, row)
}
}
}
return rows
}
rowModel.rows = accessRows(data)
return rowModel
},
{
key: process.env.NODE_ENV === 'development' && 'getRowModel',
debug: () => table.options.debugAll ?? table.options.debugTable,
onChange: () => {
table._autoResetPageIndex()
},
}
)
}