Skip to content

Commit 2c2ac79

Browse files
committed
chore: bump rc-virtual-list and modify logic
1 parent af76c56 commit 2c2ac79

File tree

5 files changed

+17
-40
lines changed

5 files changed

+17
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"classnames": "^2.2.5",
5959
"rc-resize-observer": "^1.1.0",
6060
"rc-util": "^5.37.0",
61-
"rc-virtual-list": "^3.12.0"
61+
"rc-virtual-list": "^3.13.0"
6262
},
6363
"devDependencies": {
6464
"@rc-component/father-plugin": "^1.0.2",

src/VirtualTable/BodyGrid.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
3131
childrenColumnName,
3232
emptyNode,
3333
scrollX,
34-
componentWidth,
3534
} = useContext(TableContext, [
3635
'flattenColumns',
3736
'onColumnResize',
@@ -41,13 +40,11 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
4140
'childrenColumnName',
4241
'emptyNode',
4342
'scrollX',
44-
'componentWidth',
4543
]);
4644
const {
4745
sticky,
4846
scrollY,
4947
listItemHeight,
50-
horizontalVirtual,
5148
getComponent,
5249
onScroll: onTablePropScroll,
5350
} = useContext(StaticContext);
@@ -72,8 +69,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
7269
[columnsWidth],
7370
);
7471

75-
const [scrollLeft, setScrollLeft] = React.useState(0);
76-
7772
React.useEffect(() => {
7873
columnsWidth.forEach(([key, width]) => {
7974
onColumnResize(key, width);
@@ -92,12 +87,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
9287
get: () => listRef.current?.getScrollInfo().x || 0,
9388

9489
set: (value: number) => {
95-
if (horizontalVirtual) {
96-
const max = (scrollX as number) - componentWidth;
97-
let left = Math.max(value, 0);
98-
left = Math.min(left, max);
99-
setScrollLeft(left);
100-
}
10190
listRef.current?.scrollTo({
10291
left: value,
10392
});
@@ -120,7 +109,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
120109
};
121110

122111
const extraRender: ListProps<any>['extraRender'] = info => {
123-
const { start, end, getSize, offsetY } = info;
112+
const { start, end, getSize, offsetX, offsetY } = info;
124113

125114
// Do nothing if no data
126115
if (end < 0) {
@@ -200,9 +189,9 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
200189
style={{
201190
top: -offsetY + sizeInfo.top,
202191
}}
192+
offsetX={offsetX}
203193
extra
204194
getHeight={getHeight}
205-
scrollLeft={scrollLeft}
206195
/>
207196
);
208197
});
@@ -247,9 +236,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
247236
component={wrapperComponent}
248237
scrollWidth={scrollX as number}
249238
onVirtualScroll={({ x }) => {
250-
if (horizontalVirtual) {
251-
setScrollLeft(x);
252-
}
253239
onScroll({
254240
scrollLeft: x,
255241
});
@@ -259,15 +245,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
259245
>
260246
{(item, index, itemProps) => {
261247
const rowKey = getRowKey(item.record, index);
262-
return (
263-
<BodyLine
264-
data={item}
265-
rowKey={rowKey}
266-
index={index}
267-
scrollLeft={scrollLeft}
268-
{...itemProps}
269-
/>
270-
);
248+
return <BodyLine data={item} rowKey={rowKey} index={index} {...itemProps} />;
271249
}}
272250
</VirtualList>
273251
);

src/VirtualTable/BodyLine.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ export interface BodyLineProps<RecordType = any> {
1616
className?: string;
1717
style?: React.CSSProperties;
1818
rowKey: React.Key;
19-
scrollLeft: number;
19+
offsetX: number;
2020

2121
/** Render cell only when it has `rowSpan > 1` */
2222
extra?: boolean;
2323
getHeight?: (rowSpan: number) => number;
2424
}
2525

2626
const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) => {
27-
const { data, index, className, rowKey, style, extra, getHeight, scrollLeft, ...restProps } =
28-
props;
27+
const { data, index, className, rowKey, style, extra, getHeight, offsetX, ...restProps } = props;
2928
const { record, indent, index: renderIndex } = data;
3029

3130
const { scrollX, flattenColumns, prefixCls, fixColumn, componentWidth } = useContext(
@@ -122,7 +121,7 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
122121
{horizontalVirtual ? (
123122
<VirtualRow
124123
cellPropsCollections={cellPropsCollections}
125-
scrollLeft={scrollLeft}
124+
offsetX={offsetX}
126125
{...shareCellProps}
127126
/>
128127
) : (

src/VirtualTable/VirtualRow.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { CustomizeComponent } from '../interface';
99

1010
export interface VirtualRowProps {
1111
cellPropsCollections: ReturnType<typeof getCellProps>[];
12-
scrollLeft: number;
12+
offsetX: number;
1313
index: number;
1414
renderIndex: number;
1515
inverse: boolean;
@@ -21,14 +21,14 @@ export interface VirtualRowProps {
2121

2222
export default function VirtualRow({
2323
cellPropsCollections,
24-
scrollLeft,
24+
offsetX,
2525
...restProps
2626
}: VirtualRowProps) {
2727
const { flattenColumns } = useContext(TableContext, ['flattenColumns']);
2828

2929
const [startIndex, virtualOffset, showColumnIndexes] = useHorizontalVirtual(
3030
cellPropsCollections,
31-
scrollLeft,
31+
offsetX,
3232
);
3333

3434
return showColumnIndexes.map(colIndex => (

src/VirtualTable/useHorizontalVirtual.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function generateRange(start: number, end: number) {
99

1010
export default function useHorizontalVirtual(
1111
cellPropsCollections: ReturnType<typeof getCellProps>[],
12-
scrollLeft: number,
12+
offsetX: number,
1313
) {
1414
const { flattenColumns, componentWidth } = useContext(TableContext, [
1515
'flattenColumns',
@@ -26,20 +26,20 @@ export default function useHorizontalVirtual(
2626

2727
const startIndex = useMemo(() => {
2828
let virtualStartIndex = flattenColumns.findIndex(col => col.fixed !== 'left');
29-
let tempScrollLeft = scrollLeft;
29+
let tempOffsetX = offsetX;
3030
for (let i = virtualStartIndex; i < flattenColumns.length; i++) {
3131
const colSpan = cellColSpanCollections[i];
3232
const width = flattenColumns
3333
.slice(i, i + colSpan)
3434
.reduce((total, col) => total + (col.width as number), 0);
35-
tempScrollLeft = tempScrollLeft - width;
35+
tempOffsetX = tempOffsetX - width;
3636
virtualStartIndex = i;
37-
if (tempScrollLeft < 0) {
37+
if (tempOffsetX < 0) {
3838
break;
3939
}
4040
}
4141
return virtualStartIndex;
42-
}, [flattenColumns, scrollLeft, cellColSpanCollections]);
42+
}, [flattenColumns, offsetX, cellColSpanCollections]);
4343

4444
const virtualOffset = useMemo(
4545
() =>
@@ -65,7 +65,7 @@ export default function useHorizontalVirtual(
6565

6666
// 计算当前这个右边部分的距离
6767
const firstRightWidth =
68-
(flattenColumns[startIndex].width as number) - (scrollLeft - virtualOffset);
68+
(flattenColumns[startIndex].width as number) - (offsetX - virtualOffset);
6969

7070
for (let i = startIndex; i < flattenColumns.length; i++) {
7171
if (i === startIndex) {
@@ -79,7 +79,7 @@ export default function useHorizontalVirtual(
7979
}
8080
}
8181
return virtualEndIndex;
82-
}, [componentWidth, flattenColumns, scrollLeft, virtualOffset, startIndex]);
82+
}, [componentWidth, flattenColumns, offsetX, virtualOffset, startIndex]);
8383

8484
const showColumnIndexes = useMemo(() => {
8585
const fixedLeftIndexes = [];

0 commit comments

Comments
 (0)