Skip to content

Commit

Permalink
fix: should check sticky scroll bar visible when table height changed (
Browse files Browse the repository at this point in the history
…#1076)

* fix: should check sticky scroll bar visible when table height changed

* chore: use rc-resize-observer

* chore: remove ResizeObserver

* chore: update
  • Loading branch information
linxianxi authored Feb 5, 2024
1 parent 356eb90 commit 26dd154
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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
React.useEffect(() => {
checkScrollBarVisible();
}, [data]);

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

0 comments on commit 26dd154

Please sign in to comment.