-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
row.ts
120 lines (106 loc) 路 3.26 KB
/
row.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { RowData, Cell, Row, Table } from '../types'
import { flattenBy, memo } from '../utils'
import { createCell } from './cell'
export interface CoreRow<TData extends RowData> {
id: string
index: number
original: TData
depth: number
_valuesCache: Record<string, unknown>
_uniqueValuesCache: Record<string, unknown>
getValue: <TValue>(columnId: string) => TValue
getUniqueValues: <TValue>(columnId: string) => TValue[]
renderValue: <TValue>(columnId: string) => TValue
subRows: Row<TData>[]
getLeafRows: () => Row<TData>[]
originalSubRows?: TData[]
getAllCells: () => Cell<TData, unknown>[]
_getAllCellsByColumnId: () => Record<string, Cell<TData, unknown>>
parentRow?: Row<TData>
}
export const createRow = <TData extends RowData>(
table: Table<TData>,
id: string,
original: TData,
rowIndex: number,
depth: number,
subRows?: Row<TData>[],
parentRow?: Row<TData>
): Row<TData> => {
let row: CoreRow<TData> = {
id,
index: rowIndex,
original,
depth,
parentRow,
_valuesCache: {},
_uniqueValuesCache: {},
getValue: columnId => {
if (row._valuesCache.hasOwnProperty(columnId)) {
return row._valuesCache[columnId]
}
const column = table.getColumn(columnId)
if (!column?.accessorFn) {
return undefined
}
row._valuesCache[columnId] = column.accessorFn(
row.original as TData,
rowIndex
)
return row._valuesCache[columnId] as any
},
getUniqueValues: columnId => {
if (row._uniqueValuesCache.hasOwnProperty(columnId)) {
return row._uniqueValuesCache[columnId]
}
const column = table.getColumn(columnId)
if (!column?.accessorFn) {
return undefined
}
if (!column.columnDef.getUniqueValues) {
row._uniqueValuesCache[columnId] = [row.getValue(columnId)]
return row._uniqueValuesCache[columnId]
}
row._uniqueValuesCache[columnId] = column.columnDef.getUniqueValues(
row.original as TData,
rowIndex
)
return row._uniqueValuesCache[columnId] as any
},
renderValue: columnId =>
row.getValue(columnId) ?? table.options.renderFallbackValue,
subRows: subRows ?? [],
getLeafRows: () => flattenBy(row.subRows, d => d.subRows),
getAllCells: memo(
() => [table.getAllLeafColumns()],
leafColumns => {
return leafColumns.map(column => {
return createCell(table, row as Row<TData>, column, column.id)
})
},
{
key: process.env.NODE_ENV === 'development' && 'row.getAllCells',
debug: () => table.options.debugAll ?? table.options.debugRows,
}
),
_getAllCellsByColumnId: memo(
() => [row.getAllCells()],
allCells => {
return allCells.reduce((acc, cell) => {
acc[cell.column.id] = cell
return acc
}, {} as Record<string, Cell<TData, unknown>>)
},
{
key:
process.env.NODE_ENV === 'production' && 'row.getAllCellsByColumnId',
debug: () => table.options.debugAll ?? table.options.debugRows,
}
),
}
for (let i = 0; i < table._features.length; i++) {
const feature = table._features[i]
Object.assign(row, feature?.createRow?.(row, table))
}
return row as Row<TData>
}