-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathBodyLine.tsx
More file actions
159 lines (138 loc) · 4.4 KB
/
BodyLine.tsx
File metadata and controls
159 lines (138 loc) · 4.4 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
148
149
150
151
152
153
154
155
156
157
158
159
import { useContext } from '@rc-component/context';
import classNames from 'classnames';
import * as React from 'react';
import Cell from '../Cell';
import TableContext, { responseImmutable } from '../context/TableContext';
import type { FlattenData } from '../hooks/useFlattenRecords';
import useRowInfo from '../hooks/useRowInfo';
import VirtualCell from './VirtualCell';
import { StaticContext } from './context';
import { getCellProps } from '../Body/BodyRow';
import VirtualRow from './VirtualRow';
export interface BodyLineProps<RecordType = any> {
data: FlattenData<RecordType>;
index: number;
className?: string;
style?: React.CSSProperties;
rowKey: React.Key;
offsetX: number;
/** Render cell only when it has `rowSpan > 1` */
extra?: boolean;
getHeight?: (rowSpan: number) => number;
}
const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) => {
const { data, index, className, rowKey, style, extra, getHeight, offsetX, ...restProps } = props;
const { record, indent, index: renderIndex } = data;
const { scrollX, flattenColumns, prefixCls, fixColumn, componentWidth } = useContext(
TableContext,
['prefixCls', 'flattenColumns', 'fixColumn', 'componentWidth', 'scrollX'],
);
const { getComponent, horizontalVirtual } = useContext(StaticContext, [
'getComponent',
'horizontalVirtual',
]);
const rowInfo = useRowInfo(record, rowKey, index, indent);
const cellPropsCollections = flattenColumns.map((column, colIndex) =>
getCellProps(rowInfo, column, colIndex, indent, index),
);
const RowComponent = getComponent(['body', 'row'], 'div');
const cellComponent = getComponent(['body', 'cell'], 'div');
// ========================== Expand ==========================
const { rowSupportExpand, expanded, rowProps, expandedRowRender, expandedRowClassName } = rowInfo;
let expandRowNode: React.ReactElement;
if (rowSupportExpand && expanded) {
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
const computedExpandedRowClassName = expandedRowClassName?.(record, index, indent);
let additionalProps: React.TdHTMLAttributes<HTMLElement> = {};
if (fixColumn) {
additionalProps = {
style: {
['--virtual-width' as any]: `${componentWidth}px`,
},
};
}
const rowCellCls = `${prefixCls}-expanded-row-cell`;
expandRowNode = (
<RowComponent
className={classNames(
`${prefixCls}-expanded-row`,
`${prefixCls}-expanded-row-level-${indent + 1}`,
computedExpandedRowClassName,
)}
>
<Cell
component={cellComponent}
prefixCls={prefixCls}
className={classNames(rowCellCls, {
[`${rowCellCls}-fixed`]: fixColumn,
})}
additionalProps={additionalProps}
>
{expandContent}
</Cell>
</RowComponent>
);
}
// ========================== Render ==========================
const rowStyle: React.CSSProperties = {
...style,
width: scrollX as number,
};
if (extra) {
rowStyle.position = 'absolute';
rowStyle.pointerEvents = 'none';
}
const shareCellProps = {
index,
renderIndex,
inverse: extra,
record,
rowInfo,
component: cellComponent,
getHeight,
};
const rowNode = (
<RowComponent
{...rowProps}
{...restProps}
data-row-key={rowKey}
ref={rowSupportExpand ? null : ref}
className={classNames(className, `${prefixCls}-row`, rowProps?.className, {
[`${prefixCls}-row-extra`]: extra,
})}
style={{ ...rowStyle, ...rowProps?.style }}
>
{horizontalVirtual ? (
<VirtualRow
cellPropsCollections={cellPropsCollections}
offsetX={offsetX}
{...shareCellProps}
/>
) : (
flattenColumns.map((column, colIndex) => (
<VirtualCell
key={colIndex}
column={column}
colIndex={colIndex}
cellProps={cellPropsCollections[colIndex]}
{...shareCellProps}
/>
))
)}
</RowComponent>
);
if (rowSupportExpand) {
return (
<div ref={ref}>
{rowNode}
{expandRowNode}
</div>
);
}
return rowNode;
});
const ResponseBodyLine = responseImmutable(BodyLine);
if (process.env.NODE_ENV !== 'production') {
ResponseBodyLine.displayName = 'BodyLine';
}
export default ResponseBodyLine;