-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathindex.tsx
More file actions
107 lines (90 loc) · 3.2 KB
/
index.tsx
File metadata and controls
107 lines (90 loc) · 3.2 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
import type { CompareProps } from '@rc-component/context/lib/Immutable';
import { clsx } from 'clsx';
import { useEvent, warning } from '@rc-component/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-component/util/lib/utils/get';
const renderBody: CustomizeScrollBody<any> = (rawData, props) => {
const { ref, onScroll } = props;
return <Grid ref={ref as any} data={rawData as any} onScroll={onScroll} />;
};
export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordType>, 'scroll'> {
listItemHeight?: number;
scroll: { x?: number; y?: number };
}
const VirtualTable = <RecordType,>(
props: VirtualTableProps<RecordType>,
ref: React.Ref<Reference>,
) => {
const {
data,
columns,
scroll,
sticky,
prefixCls = DEFAULT_PREFIX,
className,
listItemHeight,
components,
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);
// ========================= Context ==========================
const context = React.useMemo(
() => ({ sticky, scrollY, listItemHeight, getComponent, onScroll: onInternalScroll }),
[sticky, scrollY, listItemHeight, getComponent, onInternalScroll],
);
// ========================== Render ==========================
return (
<StaticContext.Provider value={context}>
<Table
{...props}
className={clsx(className, `${prefixCls}-virtual`)}
scroll={{ ...scroll, x: scrollX }}
components={{
...components,
// fix https://github.com/ant-design/ant-design/issues/48991
body: data?.length ? renderBody : undefined,
}}
columns={columns}
internalHooks={INTERNAL_HOOKS}
tailor
ref={ref}
/>
</StaticContext.Provider>
);
};
export type ForwardGenericVirtualTable = (<RecordType>(
props: TableProps<RecordType> & React.RefAttributes<Reference>,
) => React.ReactElement<any>) & { displayName?: string };
const RefVirtualTable = React.forwardRef(VirtualTable) as ForwardGenericVirtualTable;
if (process.env.NODE_ENV !== 'production') {
RefVirtualTable.displayName = 'VirtualTable';
}
export const genVirtualTable = (shouldTriggerRender?: CompareProps<ForwardGenericVirtualTable>) => {
return makeImmutable(RefVirtualTable, shouldTriggerRender);
};
export default genVirtualTable();