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: should check sticky scroll bar visible when table height changed #1076

Merged
merged 5 commits into from
Feb 5, 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
1 change: 1 addition & 0 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ function Table<RecordType extends DefaultRecordType>(
scrollBodyRef={scrollBodyRef}
onScroll={onScroll}
container={container}
data={data}
/>
)}
</>
Expand Down
16 changes: 11 additions & 5 deletions src/stickyScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ interface StickyScrollBarProps {
onScroll: (params: { scrollLeft?: number }) => void;
offsetScroll: number;
container: HTMLElement | Window;
data?: readonly any[];
}

const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarProps> = (
{ scrollBodyRef, onScroll, offsetScroll, container },
{ scrollBodyRef, onScroll, offsetScroll, container, data },
ref,
) => {
const prefixCls = useContext(TableContext, 'prefixCls');
Expand Down Expand Up @@ -80,7 +81,7 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
refState.current.x = event.pageX;
};

const onContainerScroll = () => {
const checkScrollBarVisible = () => {
if (!scrollBodyRef.current) {
return;
}
Expand Down Expand Up @@ -123,16 +124,16 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
React.useEffect(() => {
const onMouseUpListener = addEventListener(document.body, 'mouseup', onMouseUp, false);
const onMouseMoveListener = addEventListener(document.body, 'mousemove', onMouseMove, false);
onContainerScroll();
checkScrollBarVisible();
return () => {
onMouseUpListener.remove();
onMouseMoveListener.remove();
};
}, [scrollBarWidth, isActive]);

React.useEffect(() => {
const onScrollListener = addEventListener(container, 'scroll', onContainerScroll, false);
const onResizeListener = addEventListener(window, 'resize', onContainerScroll, false);
const onScrollListener = addEventListener(container, 'scroll', checkScrollBarVisible, false);
const onResizeListener = addEventListener(window, 'resize', checkScrollBarVisible, false);

return () => {
onScrollListener.remove();
Expand All @@ -155,6 +156,11 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
}
}, [scrollState.isHiddenScrollBar]);

// The best way is to use ResizeObserver to detect the body height, but this way is enough
linxianxi marked this conversation as resolved.
Show resolved Hide resolved
React.useEffect(() => {
checkScrollBarVisible();
}, [data]);
Copy link
Member

@afc163 afc163 Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data 放这里很容易触发的,如果写成字面量对象的。

改成 data.length 是不是就够了?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方式已经很保守了,你说的这个,比如切换分页时,新的文本可能很长跟原来的差很多可能就有问题了。


if (bodyScrollWidth <= bodyWidth || !scrollBarWidth || scrollState.isHiddenScrollBar) {
return null;
}
Expand Down
Loading