Skip to content
Open
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
11 changes: 4 additions & 7 deletions src/stickyScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,10 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
Math.min(isRTL ? 0 : bodyWidth - scrollBarWidth, left),
);
// Calculate the scroll position and update
const shouldScroll = !isRTL || Math.abs(left) + Math.abs(scrollBarWidth) < bodyWidth;
if (shouldScroll) {
onScroll({
scrollLeft: (left / bodyWidth) * (bodyScrollWidth + 2),
});
refState.current.x = event.pageX;
}
onScroll({
scrollLeft: (left / bodyWidth) * (bodyScrollWidth + 2),
});
refState.current.x = event.pageX;
};

const checkScrollBarVisible = () => {
Expand Down
68 changes: 68 additions & 0 deletions tests/Sticky.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,74 @@ describe('Table.Sticky', () => {
domSpy.mockRestore();
});

it.each([
['rtl', -50, -101, -25, -50.5],
['rtl', -75, -101, -25, -50.5],
['ltr', 50, 101, 25, 50.5],
['ltr', 75, 101, 25, 50.5],
])('clamps %s sticky scrollbar dragging at %s and allows reversing', async (
direction,
endpoint,
expectedEndpoint,
reverse,
expectedReverse,
) => {
window.pageYOffset = 900;
document.documentElement.scrollTop = 200;
let scrollLeft = 0;
const domSpy = spyElementPrototypes(HTMLDivElement, {
scrollLeft: {
get: () => scrollLeft,
set: (_, left) => {
scrollLeft = left;
},
},
scrollTop: { get: () => 100 },
scrollWidth: { get: () => 200 },
clientWidth: { get: () => 100 },
offsetHeight: { get: () => 100 },
});
const { container, unmount } = render(
<Table
direction={direction}
columns={[{ dataIndex: 'value', width: 200 }]}
data={[{ key: 1, value: 'value' }]}
scroll={{ x: 200 }}
sticky
/>,
);
await act(async () => {
vi.runAllTimers();
await Promise.resolve();
});

const scrollBar = container.querySelector('.rc-table-sticky-scroll-bar');
expect(scrollBar).not.toBeNull();
fireEvent.mouseDown(scrollBar, { pageX: 0 });
const move = async pageX => {
const event = new MouseEvent('mousemove', { bubbles: true, buttons: 1 });
Object.defineProperty(event, 'pageX', { get: () => pageX });
await act(async () => {
document.body.dispatchEvent(event);
vi.runAllTimers();
await Promise.resolve();
});
};
await move(endpoint);
const actualEndpoint = scrollLeft;
await move(reverse);
const actualReverse = scrollLeft;

fireEvent.mouseUp(document.body);
unmount();
domSpy.mockRestore();
window.pageYOffset = 0;
document.documentElement.scrollTop = 0;
vi.useRealTimers();
expect(actualEndpoint).toBe(expectedEndpoint);
expect(actualReverse).toBe(expectedReverse);
});

it('Sticky Header with border classname', async () => {
const TableDemo = props => {
return (
Expand Down