/
cell.ts
60 lines (54 loc) 路 1.42 KB
/
cell.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
import { RowData, Cell, Column, Row, Table } from '../types'
import { Getter } from '../utils'
export type CellContext<TData extends RowData, TValue> = {
table: Table<TData>
column: Column<TData, TValue>
row: Row<TData>
cell: Cell<TData, TValue>
getValue: Getter<TValue>
renderValue: Getter<TValue | null>
}
export type CoreCell<TData extends RowData, TValue> = {
id: string
getValue: CellContext<TData, TValue>['getValue']
renderValue: CellContext<TData, TValue>['renderValue']
row: Row<TData>
column: Column<TData, TValue>
getContext: () => CellContext<TData, TValue>
}
export function createCell<TData extends RowData, TValue>(
table: Table<TData>,
row: Row<TData>,
column: Column<TData, TValue>,
columnId: string
) {
const getRenderValue = () =>
cell.getValue() ?? table.options.renderFallbackValue
const cell: CoreCell<TData, TValue> = {
id: `${row.id}_${column.id}`,
row,
column,
getValue: () => row.getValue(columnId),
renderValue: getRenderValue,
getContext: () => ({
table,
column,
row,
cell: cell as Cell<TData, TValue>,
getValue: cell.getValue,
renderValue: cell.renderValue,
}),
}
table._features.forEach(feature => {
Object.assign(
cell,
feature.createCell?.(
cell as Cell<TData, TValue>,
column,
row as Row<TData>,
table
)
)
}, {})
return cell as Cell<TData, TValue>
}