Skip to content

Commit

Permalink
fix: support sticky when virtual (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
linxianxi authored Sep 22, 2023
1 parent cef90e5 commit 0796b06
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"classnames": "^2.2.5",
"rc-resize-observer": "^1.1.0",
"rc-util": "^5.36.0",
"rc-virtual-list": "^3.10.7"
"rc-virtual-list": "^3.11.1"
},
"devDependencies": {
"@rc-component/father-plugin": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
</FixedHolder>
)}

{isSticky && (
{isSticky && scrollBodyRef.current instanceof Element && (
<StickyScrollBar
ref={stickyRef}
offsetScroll={offsetScroll}
Expand Down
13 changes: 12 additions & 1 deletion src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
'emptyNode',
'scrollX',
]);
const { scrollY, listItemHeight } = useContext(StaticContext);
const { sticky, scrollY, listItemHeight } = useContext(StaticContext);

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

let bodyContent: React.ReactNode;
if (flattenData.length) {
// ========================== Sticky Scroll Bar ==========================
const horizontalScrollBarStyle: React.CSSProperties = {};
if (sticky) {
horizontalScrollBarStyle.position = 'sticky';
horizontalScrollBarStyle.bottom = 0;
if (typeof sticky === 'object' && sticky.offsetScroll) {
horizontalScrollBarStyle.bottom = sticky.offsetScroll;
}
}

bodyContent = (
<VirtualList<FlattenData<any>>
fullHeight={false}
ref={listRef}
styles={{ horizontalScrollBar: horizontalScrollBarStyle }}
className={classNames(tblPrefixCls, `${tblPrefixCls}-virtual`)}
height={scrollY}
itemHeight={listItemHeight || 24}
Expand Down
2 changes: 2 additions & 0 deletions src/VirtualTable/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createContext } from '@rc-component/context';
import type { TableSticky } from '../interface';

export interface StaticContextProps {
scrollY: number;
listItemHeight: number;
sticky: boolean | TableSticky;
}

export const StaticContext = createContext<StaticContextProps>(null);
Expand Down
7 changes: 5 additions & 2 deletions src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordTyp
}

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

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

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

// ========================= Context ==========================
const context = React.useMemo(() => ({ scrollY, listItemHeight }), [scrollY, listItemHeight]);
const context = React.useMemo(
() => ({ sticky, scrollY, listItemHeight }),
[sticky, scrollY, listItemHeight],
);

// ========================== Render ==========================
return (
Expand Down
27 changes: 27 additions & 0 deletions tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,31 @@ describe('Table.Virtual', () => {
'1128',
);
});

it('sticky header with virtual should work', async () => {
const { container } = getTable({ sticky: { offsetHeader: 10 } });

await waitFakeTimer();

expect(container.querySelector('.rc-table-header')).toHaveStyle({
overflow: 'hidden',
top: '10px',
});

expect(container.querySelector('.rc-table-header')).toHaveClass(
'rc-table-header',
'rc-table-sticky-holder',
);
});

it('sticky scrollbar with virtual should work', async () => {
const { container } = getTable({ sticky: { offsetScroll: 10 } });

await waitFakeTimer();

expect(container.querySelector('.rc-virtual-list-scrollbar-horizontal')).toHaveStyle({
position: 'sticky',
bottom: '10px',
});
});
});

1 comment on commit 0796b06

@vercel
Copy link

@vercel vercel bot commented on 0796b06 Sep 22, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

table – ./

table-git-master-react-component.vercel.app
table-react-component.vercel.app

Please sign in to comment.