Skip to content

Commit

Permalink
fix(BaseTable): take offset into account when virtualizing (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
beliarh authored Oct 4, 2024
1 parent 0f4eaef commit af52206
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,16 @@ const WindowVirtualizationExample = () => {
getRowId: (item) => item.id,
});

const bodyRef = React.useRef<HTMLTableSectionElement>(null);

const rowVirtualizer = useWindowRowVirtualizer({
count: table.getRowModel().rows.length,
estimateSize: () => 20,
overscan: 5,
scrollMargin: bodyRef.current?.offsetTop ?? 0,
});

return <Table table={table} rowVirtualizer={rowVirtualizer} />;
return <Table table={table} rowVirtualizer={rowVirtualizer} bodyRef={bodyRef} />;
};
```

Expand Down
5 changes: 4 additions & 1 deletion src/components/BaseRow/BaseRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ export const BaseRow = React.forwardRef(
{...restProps}
{...attributes}
style={{
top: virtualItem?.start,
top:
rowVirtualizer && virtualItem
? virtualItem.start - rowVirtualizer.options.scrollMargin
: undefined,
...style,
...attributes?.style,
}}
Expand Down
4 changes: 4 additions & 0 deletions src/components/BaseTable/BaseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface BaseTableProps<TData, TScrollElement extends Element | Window =
bodyAttributes?: React.HTMLAttributes<HTMLTableSectionElement>;
/** CSS classes for the `<tbody>` element */
bodyClassName?: string;
/** Ref for the `<tbody>` element */
bodyRef?: React.Ref<HTMLTableSectionElement>;
/** HTML attributes for the `<td>` elements inside `<tbody>` */
cellAttributes?: BaseRowProps<TData>['cellAttributes'];
/** CSS classes for the `<td>` elements inside `<tbody>` */
Expand Down Expand Up @@ -110,6 +112,7 @@ export const BaseTable = React.forwardRef(
attributes,
bodyAttributes,
bodyClassName,
bodyRef,
cellAttributes,
cellClassName,
className,
Expand Down Expand Up @@ -252,6 +255,7 @@ export const BaseTable = React.forwardRef(
</thead>
)}
<tbody
ref={bodyRef}
className={b('body', bodyClassName)}
{...bodyAttributes}
style={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ export const GroupingWithVirtualizationStory = () => {
},
});

const bodyRef = React.useRef<HTMLTableSectionElement>(null);

const rowVirtualizer = useWindowRowVirtualizer({
count: table.getRowModel().rows.length,
estimateSize: () => 20,
overscan: 5,
scrollMargin: bodyRef.current?.offsetTop ?? 0,
});

return (
Expand All @@ -49,6 +52,7 @@ export const GroupingWithVirtualizationStory = () => {
className={cnVirtualizationStory()}
headerClassName={cnVirtualizationStory('header')}
stickyHeader
bodyRef={bodyRef}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ export const ReorderingWithVirtualizationStory = () => {
getRowId: (item) => item.id,
});

const bodyRef = React.useRef<HTMLTableSectionElement>(null);

const rowVirtualizer = useWindowRowVirtualizer({
count: table.getRowModel().rows.length,
estimateSize: () => 20,
overscan: 5,
rangeExtractor: getVirtualRowRangeExtractor(tableRef.current),
scrollMargin: bodyRef.current?.offsetTop ?? 0,
});

const handleReorder = React.useCallback<
Expand Down Expand Up @@ -62,6 +65,7 @@ export const ReorderingWithVirtualizationStory = () => {
<ReorderingProvider table={table} onReorder={handleReorder}>
<BaseTable
ref={tableRef}
bodyRef={bodyRef}
table={table}
rowVirtualizer={rowVirtualizer}
stickyHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ export const WindowVirtualizationStory = () => {
getRowId: (item) => item.id,
});

const bodyRef = React.useRef<HTMLTableSectionElement>(null);

const rowVirtualizer = useWindowRowVirtualizer({
count: table.getRowModel().rows.length,
estimateSize: () => 20,
overscan: 5,
scrollMargin: bodyRef.current?.offsetTop ?? 0,
});

return (
Expand All @@ -31,6 +34,7 @@ export const WindowVirtualizationStory = () => {
className={cnVirtualizationStory()}
headerClassName={cnVirtualizationStory('header')}
stickyHeader
bodyRef={bodyRef}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ export const ReorderingWithVirtualizationStory = () => {
getRowId: (item) => item.id,
});

const bodyRef = React.useRef<HTMLTableSectionElement>(null);

const rowVirtualizer = useWindowRowVirtualizer({
count: table.getRowModel().rows.length,
estimateSize: () => 20,
overscan: 5,
rangeExtractor: getVirtualRowRangeExtractor(tableRef.current),
scrollMargin: bodyRef.current?.offsetTop ?? 0,
});

const handleReorder = React.useCallback<
Expand Down Expand Up @@ -56,7 +59,13 @@ export const ReorderingWithVirtualizationStory = () => {

return (
<ReorderingProvider table={table} onReorder={handleReorder}>
<Table ref={tableRef} table={table} rowVirtualizer={rowVirtualizer} stickyHeader />
<Table
ref={tableRef}
bodyRef={bodyRef}
table={table}
rowVirtualizer={rowVirtualizer}
stickyHeader
/>
</ReorderingProvider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export const WindowVirtualizationStory = () => {
getRowId: (item) => item.id,
});

const bodyRef = React.useRef<HTMLTableSectionElement>(null);

const rowVirtualizer = useWindowRowVirtualizer({
count: table.getRowModel().rows.length,
estimateSize: () => 40,
overscan: 5,
scrollMargin: bodyRef.current?.offsetTop ?? 0,
});

return <Table table={table} rowVirtualizer={rowVirtualizer} stickyHeader />;
return <Table table={table} rowVirtualizer={rowVirtualizer} stickyHeader bodyRef={bodyRef} />;
};

0 comments on commit af52206

Please sign in to comment.