-
-
Notifications
You must be signed in to change notification settings - Fork 620
Expand file tree
/
Copy pathVirtualCell.tsx
More file actions
146 lines (126 loc) · 3.99 KB
/
VirtualCell.tsx
File metadata and controls
146 lines (126 loc) · 3.99 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
import { useContext } from '@rc-component/context';
import { clsx } from 'clsx';
import * as React from 'react';
import { getCellProps } from '../Body/BodyRow';
import Cell from '../Cell';
import type useRowInfo from '../hooks/useRowInfo';
import type { ColumnType, CustomizeComponent } from '../interface';
import { GridContext } from './context';
export interface VirtualCellProps<RecordType> {
rowInfo: ReturnType<typeof useRowInfo<RecordType>>;
column: ColumnType<RecordType>;
colIndex: number;
indent: number;
index: number;
component?: CustomizeComponent;
/** Used for `column.render` */
renderIndex: number;
record: RecordType;
// Follow props is used for RowSpanVirtualCell only
style?: React.CSSProperties;
className?: string;
/** Render cell only when it has `rowSpan > 1` */
inverse?: boolean;
getHeight?: (rowSpan: number) => number;
}
/**
* Return the width of the column by `colSpan`.
* When `colSpan` is `0` will be trade as `1`.
*/
export function getColumnWidth(colIndex: number, colSpan: number, columnsOffset: number[]): number {
const mergedColSpan = colSpan || 1;
const startIndex = Math.max(colIndex, 0);
const endIndex = Math.min(startIndex + mergedColSpan, columnsOffset.length - 1);
const startOffset = columnsOffset[startIndex] || 0;
const endOffset = columnsOffset[endIndex] || startOffset;
return Math.max(endOffset - startOffset, 0);
}
const VirtualCell = <RecordType,>(props: VirtualCellProps<RecordType>) => {
const {
rowInfo,
column,
colIndex,
indent,
index,
component,
renderIndex,
record,
style,
className,
inverse,
getHeight,
} = props;
const { render, dataIndex, className: columnClassName, width: colWidth } = column;
const { columnsOffset } = useContext(GridContext, ['columnsOffset']);
// TODO: support `expandableRowOffset`
const { key, fixedInfo, appendCellNode, additionalCellProps } = getCellProps(
rowInfo,
column,
colIndex,
indent,
index,
);
const { style: cellStyle, colSpan = 1, rowSpan = 1 } = additionalCellProps;
// ========================= ColWidth =========================
// column width
const startColIndex = colIndex - 1;
const concatColWidth = getColumnWidth(startColIndex, colSpan, columnsOffset);
// margin offset
const marginOffset = colSpan > 1 ? (colWidth as number) - concatColWidth : 0;
// ========================== Style ===========================
const mergedStyle: React.CSSProperties = {
...cellStyle,
...style,
flex: `0 0 ${concatColWidth}px`,
width: `${concatColWidth}px`,
marginRight: marginOffset,
pointerEvents: 'auto',
};
// When `colSpan` or `rowSpan` is `0`, should skip render.
const needHide = React.useMemo(() => {
if (inverse) {
return rowSpan <= 1;
} else {
return colSpan === 0 || rowSpan === 0 || rowSpan > 1;
}
}, [rowSpan, colSpan, inverse]);
// 0 rowSpan or colSpan should not render
if (needHide) {
mergedStyle.visibility = 'hidden';
} else if (inverse) {
mergedStyle.height = getHeight?.(rowSpan);
}
const mergedRender = needHide ? () => null : render;
// ========================== Render ==========================
const cellSpan: React.TdHTMLAttributes<HTMLElement> = {};
// Virtual should reset `colSpan` & `rowSpan`
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.prefixCls}
key={key}
record={record}
index={index}
renderIndex={renderIndex}
dataIndex={dataIndex}
render={mergedRender}
shouldCellUpdate={column.shouldCellUpdate}
{...fixedInfo}
appendNode={appendCellNode}
additionalProps={{
...additionalCellProps,
style: mergedStyle,
...cellSpan,
}}
/>
);
};
export default VirtualCell;