Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: virtual table should be able to scroll when empty #1135

Merged
merged 8 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Body/ExpandedRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function ExpandedRow(props: ExpandedRowProps) {
contentNode = (
<div
style={{
width: componentWidth - (fixHeader ? scrollbarSize : 0),
linxianxi marked this conversation as resolved.
Show resolved Hide resolved
width: componentWidth - (fixHeader && !isEmpty ? scrollbarSize : 0),
position: 'sticky',
left: 0,
overflow: 'hidden',
Expand Down
2 changes: 1 addition & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ function Table<RecordType extends DefaultRecordType>(

if (fixHeader) {
scrollYStyle = {
overflowY: 'scroll',
overflowY: hasData ? 'scroll' : 'auto',
maxHeight: scroll.y,
};
}
Expand Down
43 changes: 13 additions & 30 deletions src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useContext } from '@rc-component/context';
import classNames from 'classnames';
import VirtualList, { type ListProps, type ListRef } from 'rc-virtual-list';
import * as React from 'react';
import Cell from '../Cell';
import TableContext, { responseImmutable } from '../context/TableContext';
import useFlattenRecords, { type FlattenData } from '../hooks/useFlattenRecords';
import type { ColumnType, OnCustomizeScroll, ScrollConfig } from '../interface';
Expand All @@ -29,7 +27,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
expandedKeys,
prefixCls,
childrenColumnName,
emptyNode,
scrollX,
} = useContext(TableContext, [
'flattenColumns',
Expand All @@ -38,7 +35,6 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
'prefixCls',
'expandedKeys',
'childrenColumnName',
'emptyNode',
'scrollX',
]);
const {
Expand Down Expand Up @@ -206,22 +202,19 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {

// default 'div' in rc-virtual-list
const wrapperComponent = getComponent(['body', 'wrapper']);
const RowComponent = getComponent(['body', 'row'], 'div');
const cellComponent = getComponent(['body', 'cell'], 'div');

let bodyContent: React.ReactNode;
if (flattenData.length) {
// ========================== 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;
}

// ========================== 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;
}
}

bodyContent = (
return (
<GridContext.Provider value={gridContext}>
<VirtualList<FlattenData<any>>
fullHeight={false}
ref={listRef}
Expand All @@ -247,18 +240,8 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
return <BodyLine data={item} rowKey={rowKey} index={index} {...itemProps} />;
}}
</VirtualList>
);
} else {
bodyContent = (
linxianxi marked this conversation as resolved.
Show resolved Hide resolved
<RowComponent className={classNames(`${prefixCls}-placeholder`)}>
<Cell component={cellComponent} prefixCls={prefixCls}>
{emptyNode}
</Cell>
</RowComponent>
);
}

return <GridContext.Provider value={gridContext}>{bodyContent}</GridContext.Provider>;
</GridContext.Provider>
);
});

const ResponseGrid = responseImmutable(Grid);
Expand Down
4 changes: 3 additions & 1 deletion src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordTyp

function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: React.Ref<Reference>) {
const {
data,
columns,
scroll,
sticky,
Expand Down Expand Up @@ -81,7 +82,8 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: Rea
}}
components={{
...components,
body: renderBody,
// fix https://github.com/ant-design/ant-design/issues/48991
body: data?.length ? renderBody : undefined,
}}
columns={columns}
internalHooks={INTERNAL_HOOKS}
Expand Down
10 changes: 10 additions & 0 deletions tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,14 @@ describe('Table.Virtual', () => {
fireEvent.scroll(container.querySelector('.rc-table-tbody-virtual-holder')!);
expect(onScroll).toHaveBeenCalled();
});

it('scrollable when empty', async () => {
const onScroll = vi.fn();
const { container } = getTable({ data: [], onScroll });

await waitFakeTimer();

fireEvent.scroll(container.querySelector('.rc-table-body'));
expect(onScroll).toHaveBeenCalled();
});
});
4 changes: 2 additions & 2 deletions tests/__snapshots__/FixedColumn.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2951,7 +2951,7 @@ LoadedCheerio {
</div>
<div
class="rc-table-body"
style="overflow-x: auto; overflow-y: scroll; max-height: 100px;"
style="overflow-x: auto; overflow-y: auto; max-height: 100px;"
>
<table
style="width: 1200px; min-width: 100%; table-layout: fixed;"
Expand Down Expand Up @@ -3102,7 +3102,7 @@ LoadedCheerio {
>
<div
class="rc-table-expanded-row-fixed"
style="width: 985px; position: sticky; left: 0px; overflow: hidden;"
style="width: 1000px; position: sticky; left: 0px; overflow: hidden;"
>
No Data
</div>
Expand Down
Loading