Skip to content

Commit 26dd154

Browse files
authored
fix: should check sticky scroll bar visible when table height changed (#1076)
* fix: should check sticky scroll bar visible when table height changed * chore: use rc-resize-observer * chore: remove ResizeObserver * chore: update
1 parent 356eb90 commit 26dd154

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/Table.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ function Table<RecordType extends DefaultRecordType>(
706706
scrollBodyRef={scrollBodyRef}
707707
onScroll={onScroll}
708708
container={container}
709+
data={data}
709710
/>
710711
)}
711712
</>

src/stickyScrollBar.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ interface StickyScrollBarProps {
1212
onScroll: (params: { scrollLeft?: number }) => void;
1313
offsetScroll: number;
1414
container: HTMLElement | Window;
15+
data?: readonly any[];
1516
}
1617

1718
const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarProps> = (
18-
{ scrollBodyRef, onScroll, offsetScroll, container },
19+
{ scrollBodyRef, onScroll, offsetScroll, container, data },
1920
ref,
2021
) => {
2122
const prefixCls = useContext(TableContext, 'prefixCls');
@@ -80,7 +81,7 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
8081
refState.current.x = event.pageX;
8182
};
8283

83-
const onContainerScroll = () => {
84+
const checkScrollBarVisible = () => {
8485
if (!scrollBodyRef.current) {
8586
return;
8687
}
@@ -123,16 +124,16 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
123124
React.useEffect(() => {
124125
const onMouseUpListener = addEventListener(document.body, 'mouseup', onMouseUp, false);
125126
const onMouseMoveListener = addEventListener(document.body, 'mousemove', onMouseMove, false);
126-
onContainerScroll();
127+
checkScrollBarVisible();
127128
return () => {
128129
onMouseUpListener.remove();
129130
onMouseMoveListener.remove();
130131
};
131132
}, [scrollBarWidth, isActive]);
132133

133134
React.useEffect(() => {
134-
const onScrollListener = addEventListener(container, 'scroll', onContainerScroll, false);
135-
const onResizeListener = addEventListener(window, 'resize', onContainerScroll, false);
135+
const onScrollListener = addEventListener(container, 'scroll', checkScrollBarVisible, false);
136+
const onResizeListener = addEventListener(window, 'resize', checkScrollBarVisible, false);
136137

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

159+
// The best way is to use ResizeObserver to detect the body height, but this way is enough
160+
React.useEffect(() => {
161+
checkScrollBarVisible();
162+
}, [data]);
163+
158164
if (bodyScrollWidth <= bodyWidth || !scrollBarWidth || scrollState.isHiddenScrollBar) {
159165
return null;
160166
}

0 commit comments

Comments
 (0)