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: right shadow should display correctly in virtual when mount #1130

Merged
merged 9 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"classnames": "^2.2.5",
"rc-resize-observer": "^1.1.0",
"rc-util": "^5.37.0",
"rc-virtual-list": "^3.11.1"
"rc-virtual-list": "^3.14.0"
},
"devDependencies": {
"@rc-component/father-plugin": "^1.0.2",
Expand Down
8 changes: 6 additions & 2 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,9 @@ function Table<RecordType extends DefaultRecordType>(

const measureTarget = currentTarget || scrollHeaderRef.current;
if (measureTarget) {
const { scrollWidth, clientWidth } = measureTarget;
const scrollWidth =
typeof mergedScrollX === 'number' ? mergedScrollX : measureTarget.scrollWidth;
zombieJ marked this conversation as resolved.
Show resolved Hide resolved
const clientWidth = measureTarget.clientWidth;
// There is no space to scroll
if (scrollWidth === clientWidth) {
setPingedLeft(false);
Expand All @@ -481,7 +483,9 @@ function Table<RecordType extends DefaultRecordType>(

const triggerOnScroll = () => {
if (horizonScroll && scrollBodyRef.current) {
onInternalScroll({ currentTarget: scrollBodyRef.current } as React.UIEvent<HTMLDivElement>);
onInternalScroll({
currentTarget: (scrollBodyRef.current as any).nativeElement || scrollBodyRef.current,
linxianxi marked this conversation as resolved.
Show resolved Hide resolved
linxianxi marked this conversation as resolved.
Show resolved Hide resolved
} as React.UIEvent<HTMLDivElement>);
} else {
setPingedLeft(false);
setPingedRight(false);
Expand Down
7 changes: 5 additions & 2 deletions src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export interface GridProps<RecordType = any> {

export interface GridRef {
scrollLeft: number;
scrollTo?: (scrollConfig: ScrollConfig) => void;
nativeElement: HTMLDivElement;
scrollTo: (scrollConfig: ScrollConfig) => void;
}

const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
Expand Down Expand Up @@ -81,6 +82,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
scrollTo: (config: ScrollConfig) => {
listRef.current?.scrollTo(config);
},
nativeElement: listRef.current?.nativeElement,
} as unknown as GridRef;

Object.defineProperty(obj, 'scrollLeft', {
Expand Down Expand Up @@ -236,6 +238,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
scrollWidth={scrollX as number}
onVirtualScroll={({ x }) => {
onScroll({
currentTarget: listRef.current?.nativeElement,
scrollLeft: x,
});
}}
Expand All @@ -244,7 +247,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
>
{(item, index, itemProps) => {
const rowKey = getRowKey(item.record, index);
return <BodyLine data={item} rowKey={rowKey} index={index} {...itemProps} />;
return <BodyLine data={item} rowKey={rowKey} index={index} style={itemProps.style} />;
}}
</VirtualList>
);
Expand Down
2 changes: 1 addition & 1 deletion src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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} />;
return <Grid ref={ref as any} data={rawData as any} onScroll={onScroll} />;
};

export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordType>, 'scroll'> {
Expand Down
75 changes: 75 additions & 0 deletions tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ describe('Table.Virtual', () => {
},
set: () => {},
},
clientWidth: {
get: () => 80,
},
scrollWidth: {
get: () => 100,
},
});
});

Expand Down Expand Up @@ -443,4 +449,73 @@ describe('Table.Virtual', () => {
fireEvent.scroll(container.querySelector('.rc-table-tbody-virtual-holder')!);
expect(onScroll).toHaveBeenCalled();
});

describe('shadow', () => {
beforeAll(() => {
spyElementPrototypes(HTMLElement, {
scrollLeft: {
get: () => 0,
},
});
});

it('right shadow should display correctly when mount', async () => {
const { container } = getTable({
columns: [
{
dataIndex: 'name',
width: 30,
},
{
dataIndex: 'age',
width: 30,
},
{
dataIndex: 'address',
width: 40,
fixed: 'right',
},
],
getContainerWidth: () => 80,
});

resize(container.querySelector('.rc-table'));

await waitFakeTimer();

expect(
container.querySelector('.rc-table').classList.contains('rc-table-ping-right'),
).toBeTruthy();
});

it('right shadow should display correctly when showHeader is false', async () => {
const { container } = getTable({
showHeader: false,
columns: [
{
dataIndex: 'name',
width: 30,
},
{
dataIndex: 'age',
width: 30,
},
{
dataIndex: 'address',
width: 40,
fixed: 'right',
},
],
getContainerWidth: () => 80,
});

resize(container.querySelector('.rc-table'));

await waitFakeTimer();

expect(
container.querySelector('.rc-table').classList.contains('rc-table-ping-right'),
).toBeTruthy();
});
});
});
2 changes: 1 addition & 1 deletion tests/__snapshots__/ExpandRow.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ LoadedCheerio {
exports[`Table.Expand > renders fixed column correctly > work 1`] = `
LoadedCheerio {
"0": <div
class="rc-table rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
class="rc-table rc-table-ping-right rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
>
<div
class="rc-table-container"
Expand Down
8 changes: 4 additions & 4 deletions tests/__snapshots__/FixedColumn.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ LoadedCheerio {
exports[`Table.FixedColumn > renders correctly > scrollX - with data 1`] = `
LoadedCheerio {
"0": <div
class="rc-table rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
class="rc-table rc-table-ping-right rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
>
<div
class="rc-table-container"
Expand Down Expand Up @@ -1642,7 +1642,7 @@ LoadedCheerio {
exports[`Table.FixedColumn > renders correctly > scrollX - without data 1`] = `
LoadedCheerio {
"0": <div
class="rc-table rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
class="rc-table rc-table-ping-right rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
>
<div
class="rc-table-container"
Expand Down Expand Up @@ -1931,7 +1931,7 @@ LoadedCheerio {
exports[`Table.FixedColumn > renders correctly > scrollXY - with data 1`] = `
LoadedCheerio {
"0": <div
class="rc-table rc-table-fixed-header rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
class="rc-table rc-table-ping-right rc-table-fixed-header rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
>
<div
class="rc-table-container"
Expand Down Expand Up @@ -2804,7 +2804,7 @@ LoadedCheerio {
exports[`Table.FixedColumn > renders correctly > scrollXY - without data 1`] = `
LoadedCheerio {
"0": <div
class="rc-table rc-table-fixed-header rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
class="rc-table rc-table-ping-right rc-table-fixed-header rc-table-fixed-column rc-table-scroll-horizontal rc-table-has-fix-left rc-table-has-fix-right"
>
<div
class="rc-table-container"
Expand Down
Loading