forked from antdv-next/vue-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualCell.tsx
More file actions
147 lines (131 loc) · 3.96 KB
/
Copy pathVirtualCell.tsx
File metadata and controls
147 lines (131 loc) · 3.96 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { CSSProperties } from 'vue'
import type useRowInfo from '../hooks/useRowInfo'
import type { ColumnType, CustomizeComponent } from '../interface'
import { clsx } from '@v-c/util'
import { getStylePxValue } from '@v-c/util/dist/props-util'
import { defineComponent } from 'vue'
import { getCellProps } from '../Body/BodyRow'
import Cell from '../Cell'
import { useInjectGridContext } from './context'
export interface VirtualCellProps<RecordType> {
rowInfo: ReturnType<typeof useRowInfo<RecordType>>
column: ColumnType<RecordType>
colIndex: number
indent: number
index: number
component?: CustomizeComponent
renderIndex: number
record: RecordType
style?: CSSProperties
className?: string
inverse?: boolean
getHeight?: (rowSpan: number) => number
}
export function getColumnWidth(colIndex: number, colSpan: number, columnsOffset: number[]) {
const mergedColSpan = colSpan || 1
return columnsOffset[colIndex + mergedColSpan] - (columnsOffset[colIndex] || 0)
}
const VirtualCell = defineComponent<VirtualCellProps<any>>({
name: 'TableVirtualCell',
props: [
'rowInfo',
'column',
'colIndex',
'indent',
'index',
'component',
'renderIndex',
'record',
'style',
'className',
'inverse',
'getHeight',
] as any,
setup(props) {
const gridContext = useInjectGridContext()
return () => {
const {
rowInfo,
column,
colIndex,
indent,
index,
component,
renderIndex,
record,
style,
className,
inverse,
getHeight,
} = props
const { render, dataIndex, className: columnClassName, width: colWidth } = column
const columnsOffset = gridContext.columnsOffset || []
const { key, fixedInfo, appendCellNode, additionalCellProps, hoverRowSpan } = getCellProps(
rowInfo,
record,
column,
colIndex,
indent,
index,
)
const { style: cellStyle, colSpan = 1, rowSpan = 1 } = additionalCellProps
const startColIndex = colIndex - 1
const concatColWidth = getColumnWidth(startColIndex, colSpan, columnsOffset)
const marginOffset = colSpan > 1 ? (colWidth as number) - concatColWidth : 0
const normalizedCellStyle = cellStyle && !Array.isArray(cellStyle) && typeof cellStyle === 'object'
? cellStyle
: {}
const mergedStyle: CSSProperties = {
...normalizedCellStyle,
...(style || {}),
flex: `0 0 ${concatColWidth}px`,
width: `${concatColWidth}px`,
marginRight: typeof marginOffset === 'number' ? `${marginOffset}px` : marginOffset,
pointerEvents: 'auto',
}
const needHide = inverse
? rowSpan <= 1
: colSpan === 0 || rowSpan === 0 || rowSpan > 1
if (needHide) {
mergedStyle.visibility = 'hidden'
}
else if (inverse) {
mergedStyle.height = getStylePxValue(getHeight?.(rowSpan))
}
const mergedRender = needHide ? () => null : render
const cellSpan: Record<string, any> = {}
if (rowSpan === 0 || colSpan === 0) {
cellSpan.rowSpan = 1
cellSpan.colSpan = 1
}
return (
<Cell
className={clsx(columnClassName, className)}
ellipsis={column.ellipsis}
align={column.align}
scope={column.rowScope}
component={component}
prefixCls={rowInfo.tableContext.prefixCls}
key={key}
record={record}
index={index}
renderIndex={renderIndex}
dataIndex={dataIndex}
render={mergedRender}
column={column}
rowType="body"
shouldCellUpdate={column.shouldCellUpdate}
{...fixedInfo}
appendNode={appendCellNode}
hoverRowSpan={hoverRowSpan}
additionalProps={{
...additionalCellProps,
style: mergedStyle,
...cellSpan,
}}
/>
)
}
},
})
export default VirtualCell