Skip to content

Commit b08600c

Browse files
authored
fix: virtual table should be able to scroll when empty (#1135)
* fix: virtual table should be able to scroll when empty * chore: remove console * chore: update logic * chore: update logic * chore: space code * test: update snap * test: add case * chore: simplify code
1 parent 6891684 commit b08600c

File tree

6 files changed

+30
-35
lines changed

6 files changed

+30
-35
lines changed

src/Body/ExpandedRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function ExpandedRow(props: ExpandedRowProps) {
4444
contentNode = (
4545
<div
4646
style={{
47-
width: componentWidth - (fixHeader ? scrollbarSize : 0),
47+
width: componentWidth - (fixHeader && !isEmpty ? scrollbarSize : 0),
4848
position: 'sticky',
4949
left: 0,
5050
overflow: 'hidden',

src/Table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ function Table<RecordType extends DefaultRecordType>(
385385

386386
if (fixHeader) {
387387
scrollYStyle = {
388-
overflowY: 'scroll',
388+
overflowY: hasData ? 'scroll' : 'auto',
389389
maxHeight: scroll.y,
390390
};
391391
}

src/VirtualTable/BodyGrid.tsx

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { useContext } from '@rc-component/context';
2-
import classNames from 'classnames';
32
import VirtualList, { type ListProps, type ListRef } from 'rc-virtual-list';
43
import * as React from 'react';
5-
import Cell from '../Cell';
64
import TableContext, { responseImmutable } from '../context/TableContext';
75
import useFlattenRecords, { type FlattenData } from '../hooks/useFlattenRecords';
86
import type { ColumnType, OnCustomizeScroll, ScrollConfig } from '../interface';
@@ -29,7 +27,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
2927
expandedKeys,
3028
prefixCls,
3129
childrenColumnName,
32-
emptyNode,
3330
scrollX,
3431
} = useContext(TableContext, [
3532
'flattenColumns',
@@ -38,7 +35,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
3835
'prefixCls',
3936
'expandedKeys',
4037
'childrenColumnName',
41-
'emptyNode',
4238
'scrollX',
4339
]);
4440
const {
@@ -206,22 +202,19 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
206202

207203
// default 'div' in rc-virtual-list
208204
const wrapperComponent = getComponent(['body', 'wrapper']);
209-
const RowComponent = getComponent(['body', 'row'], 'div');
210-
const cellComponent = getComponent(['body', 'cell'], 'div');
211-
212-
let bodyContent: React.ReactNode;
213-
if (flattenData.length) {
214-
// ========================== Sticky Scroll Bar ==========================
215-
const horizontalScrollBarStyle: React.CSSProperties = {};
216-
if (sticky) {
217-
horizontalScrollBarStyle.position = 'sticky';
218-
horizontalScrollBarStyle.bottom = 0;
219-
if (typeof sticky === 'object' && sticky.offsetScroll) {
220-
horizontalScrollBarStyle.bottom = sticky.offsetScroll;
221-
}
205+
206+
// ========================== Sticky Scroll Bar ==========================
207+
const horizontalScrollBarStyle: React.CSSProperties = {};
208+
if (sticky) {
209+
horizontalScrollBarStyle.position = 'sticky';
210+
horizontalScrollBarStyle.bottom = 0;
211+
if (typeof sticky === 'object' && sticky.offsetScroll) {
212+
horizontalScrollBarStyle.bottom = sticky.offsetScroll;
222213
}
214+
}
223215

224-
bodyContent = (
216+
return (
217+
<GridContext.Provider value={gridContext}>
225218
<VirtualList<FlattenData<any>>
226219
fullHeight={false}
227220
ref={listRef}
@@ -247,18 +240,8 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
247240
return <BodyLine data={item} rowKey={rowKey} index={index} style={itemProps.style} />;
248241
}}
249242
</VirtualList>
250-
);
251-
} else {
252-
bodyContent = (
253-
<RowComponent className={classNames(`${prefixCls}-placeholder`)}>
254-
<Cell component={cellComponent} prefixCls={prefixCls}>
255-
{emptyNode}
256-
</Cell>
257-
</RowComponent>
258-
);
259-
}
260-
261-
return <GridContext.Provider value={gridContext}>{bodyContent}</GridContext.Provider>;
243+
</GridContext.Provider>
244+
);
262245
});
263246

264247
const ResponseGrid = responseImmutable(Grid);

src/VirtualTable/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordTyp
2626

2727
function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: React.Ref<Reference>) {
2828
const {
29+
data,
2930
columns,
3031
scroll,
3132
sticky,
@@ -81,7 +82,8 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: Rea
8182
}}
8283
components={{
8384
...components,
84-
body: renderBody,
85+
// fix https://github.com/ant-design/ant-design/issues/48991
86+
body: data?.length ? renderBody : undefined,
8587
}}
8688
columns={columns}
8789
internalHooks={INTERNAL_HOOKS}

tests/Virtual.spec.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,14 @@ describe('Table.Virtual', () => {
443443
fireEvent.scroll(container.querySelector('.rc-table-tbody-virtual-holder')!);
444444
expect(onScroll).toHaveBeenCalled();
445445
});
446+
447+
it('scrollable when empty', async () => {
448+
const onScroll = vi.fn();
449+
const { container } = getTable({ data: [], onScroll });
450+
451+
await waitFakeTimer();
452+
453+
fireEvent.scroll(container.querySelector('.rc-table-body'));
454+
expect(onScroll).toHaveBeenCalled();
455+
});
446456
});

tests/__snapshots__/FixedColumn.spec.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,7 +2951,7 @@ LoadedCheerio {
29512951
</div>
29522952
<div
29532953
class="rc-table-body"
2954-
style="overflow-x: auto; overflow-y: scroll; max-height: 100px;"
2954+
style="overflow-x: auto; overflow-y: auto; max-height: 100px;"
29552955
>
29562956
<table
29572957
style="width: 1200px; min-width: 100%; table-layout: fixed;"
@@ -3102,7 +3102,7 @@ LoadedCheerio {
31023102
>
31033103
<div
31043104
class="rc-table-expanded-row-fixed"
3105-
style="width: 985px; position: sticky; left: 0px; overflow: hidden;"
3105+
style="width: 1000px; position: sticky; left: 0px; overflow: hidden;"
31063106
>
31073107
No Data
31083108
</div>

0 commit comments

Comments
 (0)