Skip to content

Commit 0796b06

Browse files
authored
fix: support sticky when virtual (#1027)
1 parent cef90e5 commit 0796b06

File tree

6 files changed

+48
-5
lines changed

6 files changed

+48
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"classnames": "^2.2.5",
5959
"rc-resize-observer": "^1.1.0",
6060
"rc-util": "^5.36.0",
61-
"rc-virtual-list": "^3.10.7"
61+
"rc-virtual-list": "^3.11.1"
6262
},
6363
"devDependencies": {
6464
"@rc-component/father-plugin": "^1.0.2",

src/Table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
669669
</FixedHolder>
670670
)}
671671

672-
{isSticky && (
672+
{isSticky && scrollBodyRef.current instanceof Element && (
673673
<StickyScrollBar
674674
ref={stickyRef}
675675
offsetScroll={offsetScroll}

src/VirtualTable/BodyGrid.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
4040
'emptyNode',
4141
'scrollX',
4242
]);
43-
const { scrollY, listItemHeight } = useContext(StaticContext);
43+
const { sticky, scrollY, listItemHeight } = useContext(StaticContext);
4444

4545
// =========================== Ref ============================
4646
const listRef = React.useRef<ListRef>();
@@ -195,10 +195,21 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
195195

196196
let bodyContent: React.ReactNode;
197197
if (flattenData.length) {
198+
// ========================== Sticky Scroll Bar ==========================
199+
const horizontalScrollBarStyle: React.CSSProperties = {};
200+
if (sticky) {
201+
horizontalScrollBarStyle.position = 'sticky';
202+
horizontalScrollBarStyle.bottom = 0;
203+
if (typeof sticky === 'object' && sticky.offsetScroll) {
204+
horizontalScrollBarStyle.bottom = sticky.offsetScroll;
205+
}
206+
}
207+
198208
bodyContent = (
199209
<VirtualList<FlattenData<any>>
200210
fullHeight={false}
201211
ref={listRef}
212+
styles={{ horizontalScrollBar: horizontalScrollBarStyle }}
202213
className={classNames(tblPrefixCls, `${tblPrefixCls}-virtual`)}
203214
height={scrollY}
204215
itemHeight={listItemHeight || 24}

src/VirtualTable/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { createContext } from '@rc-component/context';
2+
import type { TableSticky } from '../interface';
23

34
export interface StaticContextProps {
45
scrollY: number;
56
listItemHeight: number;
7+
sticky: boolean | TableSticky;
68
}
79

810
export const StaticContext = createContext<StaticContextProps>(null);

src/VirtualTable/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordTyp
2424
}
2525

2626
function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>) {
27-
const { columns, scroll, prefixCls = DEFAULT_PREFIX, className, listItemHeight } = props;
27+
const { columns, scroll, sticky, prefixCls = DEFAULT_PREFIX, className, listItemHeight } = props;
2828

2929
let { x: scrollX, y: scrollY } = scroll || {};
3030

@@ -47,7 +47,10 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>) {
4747
}
4848

4949
// ========================= Context ==========================
50-
const context = React.useMemo(() => ({ scrollY, listItemHeight }), [scrollY, listItemHeight]);
50+
const context = React.useMemo(
51+
() => ({ sticky, scrollY, listItemHeight }),
52+
[sticky, scrollY, listItemHeight],
53+
);
5154

5255
// ========================== Render ==========================
5356
return (

tests/Virtual.spec.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,31 @@ describe('Table.Virtual', () => {
268268
'1128',
269269
);
270270
});
271+
272+
it('sticky header with virtual should work', async () => {
273+
const { container } = getTable({ sticky: { offsetHeader: 10 } });
274+
275+
await waitFakeTimer();
276+
277+
expect(container.querySelector('.rc-table-header')).toHaveStyle({
278+
overflow: 'hidden',
279+
top: '10px',
280+
});
281+
282+
expect(container.querySelector('.rc-table-header')).toHaveClass(
283+
'rc-table-header',
284+
'rc-table-sticky-holder',
285+
);
286+
});
287+
288+
it('sticky scrollbar with virtual should work', async () => {
289+
const { container } = getTable({ sticky: { offsetScroll: 10 } });
290+
291+
await waitFakeTimer();
292+
293+
expect(container.querySelector('.rc-virtual-list-scrollbar-horizontal')).toHaveStyle({
294+
position: 'sticky',
295+
bottom: '10px',
296+
});
297+
});
271298
});

0 commit comments

Comments
 (0)