-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathindex.tsx
More file actions
120 lines (101 loc) · 3.28 KB
/
index.tsx
File metadata and controls
120 lines (101 loc) · 3.28 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
import type { CompareProps } from '@rc-component/context/lib/Immutable';
import classNames from 'classnames';
import { useEvent, warning } from 'rc-util';
import * as React from 'react';
import { INTERNAL_HOOKS } from '../constant';
import { makeImmutable } from '../context/TableContext';
import type { CustomizeScrollBody, GetComponent, Reference } from '../interface';
import Table, { DEFAULT_PREFIX, type TableProps } from '../Table';
import Grid from './BodyGrid';
import { StaticContext } from './context';
import getValue from 'rc-util/lib/utils/get';
const renderBody: CustomizeScrollBody<any> = (rawData, props) => {
const { ref, onScroll } = props;
return <Grid ref={ref} data={rawData as any} onScroll={onScroll} />;
};
export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordType>, 'scroll'> {
scroll: {
x?: number;
y: number;
};
virtual?: { x?: boolean };
listItemHeight?: number;
}
function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: React.Ref<Reference>) {
const {
columns,
scroll,
sticky,
prefixCls = DEFAULT_PREFIX,
className,
listItemHeight,
components,
virtual,
onScroll,
} = props;
let { x: scrollX, y: scrollY } = scroll || {};
// Fill scrollX
if (typeof scrollX !== 'number') {
if (process.env.NODE_ENV !== 'production') {
warning(!scrollX, '`scroll.x` in virtual table must be number.');
}
scrollX = 1;
}
// Fill scrollY
if (typeof scrollY !== 'number') {
scrollY = 500;
if (process.env.NODE_ENV !== 'production') {
warning(false, '`scroll.y` in virtual table must be number.');
}
}
const getComponent = useEvent<GetComponent>(
(path, defaultComponent) => getValue(components, path) || defaultComponent,
);
// Memo this
const onInternalScroll = useEvent(onScroll);
const horizontalVirtual = !!virtual?.x;
// ========================= Context ==========================
const context = React.useMemo(
() => ({
sticky,
scrollY,
listItemHeight,
horizontalVirtual,
getComponent,
onScroll: onInternalScroll,
}),
[sticky, scrollY, listItemHeight, horizontalVirtual, getComponent, onInternalScroll],
);
// ========================== Render ==========================
return (
<StaticContext.Provider value={context}>
<Table
{...props}
className={classNames(className, `${prefixCls}-virtual`)}
scroll={{
...scroll,
x: scrollX,
}}
components={{
...components,
body: renderBody,
}}
columns={columns}
internalHooks={INTERNAL_HOOKS}
tailor
ref={ref}
/>
</StaticContext.Provider>
);
}
export type ForwardGenericVirtualTable = (<RecordType>(
props: VirtualTableProps<RecordType> & React.RefAttributes<Reference>,
) => React.ReactElement) & { displayName?: string };
const RefVirtualTable = React.forwardRef(VirtualTable) as ForwardGenericVirtualTable;
if (process.env.NODE_ENV !== 'production') {
RefVirtualTable.displayName = 'VirtualTable';
}
export function genVirtualTable(shouldTriggerRender?: CompareProps<typeof Table>) {
return makeImmutable(RefVirtualTable, shouldTriggerRender) as ForwardGenericVirtualTable;
}
export default genVirtualTable();