-
-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathBodyGrid.tsx
259 lines (217 loc) · 7.19 KB
/
BodyGrid.tsx
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { useContext } from '@rc-component/context';
import VirtualList, { type ListProps, type ListRef } from 'rc-virtual-list';
import * as React from 'react';
import TableContext, { responseImmutable } from '../context/TableContext';
import useFlattenRecords, { type FlattenData } from '../hooks/useFlattenRecords';
import type { ColumnType, OnCustomizeScroll, ScrollConfig } from '../interface';
import BodyLine from './BodyLine';
import { GridContext, StaticContext } from './context';
export interface GridProps<RecordType = any> {
data: RecordType[];
onScroll: OnCustomizeScroll;
}
export interface GridRef {
scrollLeft: number;
nativeElement: HTMLDivElement;
scrollTo: (scrollConfig: ScrollConfig) => void;
}
const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
const { data, onScroll } = props;
const {
flattenColumns,
onColumnResize,
getRowKey,
expandedKeys,
prefixCls,
childrenColumnName,
scrollX,
direction,
} = useContext(TableContext, [
'flattenColumns',
'onColumnResize',
'getRowKey',
'prefixCls',
'expandedKeys',
'childrenColumnName',
'scrollX',
'direction',
]);
const {
sticky,
scrollY,
listItemHeight,
getComponent,
onScroll: onTablePropScroll,
} = useContext(StaticContext);
// =========================== Ref ============================
const listRef = React.useRef<ListRef>();
// =========================== Data ===========================
const flattenData = useFlattenRecords(data, childrenColumnName, expandedKeys, getRowKey);
// ========================== Column ==========================
const columnsWidth = React.useMemo<[key: React.Key, width: number, total: number][]>(() => {
let total = 0;
return flattenColumns.map(({ width, key }) => {
total += width as number;
return [key, width as number, total];
});
}, [flattenColumns]);
const columnsOffset = React.useMemo<number[]>(
() => columnsWidth.map(colWidth => colWidth[2]),
[columnsWidth],
);
React.useEffect(() => {
columnsWidth.forEach(([key, width]) => {
onColumnResize(key, width);
});
}, [columnsWidth]);
// =========================== Ref ============================
React.useImperativeHandle(ref, () => {
const obj = {
scrollTo: (config: ScrollConfig) => {
listRef.current?.scrollTo(config);
},
nativeElement: listRef.current?.nativeElement,
} as unknown as GridRef;
Object.defineProperty(obj, 'scrollLeft', {
get: () => listRef.current?.getScrollInfo().x || 0,
set: (value: number) => {
listRef.current?.scrollTo({
left: value,
});
},
});
return obj;
});
// ======================= Col/Row Span =======================
const getRowSpan = (column: ColumnType<any>, index: number): number => {
const record = flattenData[index]?.record;
const { onCell } = column;
if (onCell) {
const cellProps = onCell(record, index) as React.TdHTMLAttributes<HTMLElement>;
return cellProps?.rowSpan ?? 1;
}
return 1;
};
const extraRender: ListProps<any>['extraRender'] = info => {
const { start, end, getSize, offsetY } = info;
// Do nothing if no data
if (end < 0) {
return null;
}
// Find first rowSpan column
let firstRowSpanColumns = flattenColumns.filter(
// rowSpan is 0
column => getRowSpan(column, start) === 0,
);
let startIndex = start;
for (let i = start; i >= 0; i -= 1) {
firstRowSpanColumns = firstRowSpanColumns.filter(column => getRowSpan(column, i) === 0);
if (!firstRowSpanColumns.length) {
startIndex = i;
break;
}
}
// Find last rowSpan column
let lastRowSpanColumns = flattenColumns.filter(
// rowSpan is not 1
column => getRowSpan(column, end) !== 1,
);
let endIndex = end;
for (let i = end; i < flattenData.length; i += 1) {
lastRowSpanColumns = lastRowSpanColumns.filter(column => getRowSpan(column, i) !== 1);
if (!lastRowSpanColumns.length) {
endIndex = Math.max(i - 1, end);
break;
}
}
// Collect the line who has rowSpan
const spanLines: number[] = [];
for (let i = startIndex; i <= endIndex; i += 1) {
const item = flattenData[i];
// This code will never reach, just incase
if (!item) {
continue;
}
if (flattenColumns.some(column => getRowSpan(column, i) > 1)) {
spanLines.push(i);
}
}
// Patch extra line on the page
const nodes: React.ReactElement[] = spanLines.map(index => {
const item = flattenData[index];
const rowKey = getRowKey(item.record, index);
const getHeight = (rowSpan: number) => {
const endItemIndex = index + rowSpan - 1;
const endItemKey = getRowKey(flattenData[endItemIndex].record, endItemIndex);
const sizeInfo = getSize(rowKey, endItemKey);
return sizeInfo.bottom - sizeInfo.top;
};
const sizeInfo = getSize(rowKey);
return (
<BodyLine
key={index}
data={item}
rowKey={rowKey}
index={index}
style={{
top: -offsetY + sizeInfo.top,
}}
extra
getHeight={getHeight}
/>
);
});
return nodes;
};
// ========================= Context ==========================
const gridContext = React.useMemo(() => ({ columnsOffset }), [columnsOffset]);
// ========================== Render ==========================
const tblPrefixCls = `${prefixCls}-tbody`;
// default 'div' in rc-virtual-list
const wrapperComponent = getComponent(['body', 'wrapper']);
// ========================== Sticky Scroll Bar ==========================
const horizontalScrollBarStyle: React.CSSProperties = {};
if (sticky) {
horizontalScrollBarStyle.position = 'sticky';
horizontalScrollBarStyle.bottom = 0;
if (typeof sticky === 'object' && sticky.offsetScroll) {
horizontalScrollBarStyle.bottom = sticky.offsetScroll;
}
}
return (
<GridContext.Provider value={gridContext}>
<VirtualList<FlattenData<any>>
fullHeight={false}
ref={listRef}
prefixCls={`${tblPrefixCls}-virtual`}
styles={{ horizontalScrollBar: horizontalScrollBarStyle }}
className={tblPrefixCls}
height={scrollY}
itemHeight={listItemHeight || 24}
data={flattenData}
itemKey={item => getRowKey(item.record)}
component={wrapperComponent}
scrollWidth={scrollX as number}
direction={direction}
onVirtualScroll={({ x }) => {
onScroll({
currentTarget: listRef.current?.nativeElement,
scrollLeft: x,
});
}}
onScroll={onTablePropScroll}
extraRender={extraRender}
>
{(item, index, itemProps) => {
const rowKey = getRowKey(item.record, index);
return <BodyLine data={item} rowKey={rowKey} index={index} style={itemProps.style} />;
}}
</VirtualList>
</GridContext.Provider>
);
});
const ResponseGrid = responseImmutable(Grid);
if (process.env.NODE_ENV !== 'production') {
ResponseGrid.displayName = 'ResponseGrid';
}
export default ResponseGrid;