Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tables): fix scroll in virtualize tables #1511

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tables/SimpleTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Head = styled.thead`
top: 0;
z-index: 1;

> th:first-child {
> tr > th:first-child {
border-left: 1px solid ${p => p.theme.color.lightGray};
}
`
Expand Down
34 changes: 19 additions & 15 deletions stories/tables/SimpleTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable react-hooks/rules-of-hooks */

import { flexRender, getCoreRowModel, getSortedRowModel, type SortingState, useReactTable } from '@tanstack/react-table'
import { useVirtualizer } from '@tanstack/react-virtual'
import { notUndefined, useVirtualizer } from '@tanstack/react-virtual'
import { useCallback, useMemo, useRef, useState } from 'react'

import { generateStoryDecorator } from '../../.storybook/utils/generateStoryDecorator'
Expand Down Expand Up @@ -267,11 +267,11 @@ export function _SimpleTable() {
})

const virtualRows = rowVirtualizer.getVirtualItems()
const [paddingTop, paddingBottom] =
const [before, after] =
virtualRows.length > 0
? [
Math.max(0, virtualRows[0]?.start ?? 0),
Math.max(0, rowVirtualizer.getTotalSize() - (virtualRows[virtualRows.length - 1]?.end ?? 0))
notUndefined(virtualRows[0]).start - rowVirtualizer.options.scrollMargin,
rowVirtualizer.getTotalSize() - notUndefined(virtualRows[virtualRows.length - 1]).end
]
: [0, 0]

Expand Down Expand Up @@ -312,17 +312,21 @@ export function _SimpleTable() {
</tr>
))}
</SimpleTable.Head>
{before > 0 && (
<tr>
<td aria-label="padding before" colSpan={columns.length} style={{ height: before }} />
</tr>
)}
<tbody>
{paddingTop > 0 && (
<tr>
<td style={{ height: `${paddingTop}px` }} />
</tr>
)}
{virtualRows.map(virtualRow => {
const row = rows[virtualRow.index]

return (
<SimpleTable.BodyTr key={virtualRow.key}>
<SimpleTable.BodyTr
key={virtualRow.key}
ref={rowVirtualizer.measureElement} // measure dynamic row height
data-index={virtualRow.index} // needed for dynamic row height measurement
>
{row?.getVisibleCells().map(cell => (
<SimpleTable.Td
{...{
Expand All @@ -341,12 +345,12 @@ export function _SimpleTable() {
</SimpleTable.BodyTr>
)
})}
{paddingBottom > 0 && (
<tr>
<td style={{ height: `${paddingBottom}px` }} />
</tr>
)}
</tbody>
{after > 0 && (
<tr>
<td aria-label="padding after" colSpan={columns.length} style={{ height: after }} />
</tr>
)}
</SimpleTable.Table>
</div>
)
Expand Down
45 changes: 26 additions & 19 deletions stories/tables/TableWithSelectableRows.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable react-hooks/rules-of-hooks */

import { flexRender, getCoreRowModel, getSortedRowModel, type SortingState, useReactTable } from '@tanstack/react-table'
import { useVirtualizer } from '@tanstack/react-virtual'
import { notUndefined, useVirtualizer } from '@tanstack/react-virtual'
import { useCallback, useMemo, useRef, useState } from 'react'
import styled from 'styled-components'

Expand Down Expand Up @@ -321,19 +321,20 @@ export function _TableWithSelectableRows() {
})

const virtualRows = rowVirtualizer.getVirtualItems()
const [paddingTop, paddingBottom] =
virtualRows.length > 0
? [
Math.max(0, virtualRows[0]?.start ?? 0),
Math.max(0, rowVirtualizer.getTotalSize() - (virtualRows[virtualRows.length - 1]?.end ?? 0))
]
: [0, 0]

const archiveReportings = () => {
// eslint-disable-next-line no-console
console.log('this reportings will be archived: ', selectedIds)
}

const [before, after] =
virtualRows.length > 0
? [
notUndefined(virtualRows[0]).start - rowVirtualizer.options.scrollMargin,
rowVirtualizer.getTotalSize() - notUndefined(virtualRows[virtualRows.length - 1]).end
]
: [0, 0]

return (
<>
<div>
Expand Down Expand Up @@ -365,17 +366,23 @@ export function _TableWithSelectableRows() {
</tr>
))}
</TableWithSelectableRows.Head>
{before > 0 && (
<tr>
<td aria-label="padding before" colSpan={columns.length} style={{ height: before }} />
</tr>
)}
<tbody>
{paddingTop > 0 && (
<tr>
<td style={{ height: `${paddingTop}px` }} />
</tr>
)}
{virtualRows.map((virtualRow, index) => {
const row = rows[virtualRow.index]

return (
<TableWithSelectableRows.BodyTr key={virtualRow.key} $isHighlighted={index % 2 === 0}>
<TableWithSelectableRows.BodyTr
key={virtualRow.key}
ref={rowVirtualizer.measureElement} // measure dynamic row height
$isHighlighted={index % 2 === 0}
data-cy="reporting-row"
data-index={virtualRow.index} // needed for dynamic row height measurement
>
{row?.getVisibleCells().map(cell => (
<TableWithSelectableRows.Td
key={cell.id}
Expand All @@ -388,12 +395,12 @@ export function _TableWithSelectableRows() {
</TableWithSelectableRows.BodyTr>
)
})}
{paddingBottom > 0 && (
<tr>
<td style={{ height: `${paddingBottom}px` }} />
</tr>
)}
</tbody>
{after > 0 && (
<tr>
<td aria-label="padding after" colSpan={columns.length} style={{ height: after }} />
</tr>
)}
</TableWithSelectableRows.Table>
</div>
</>
Expand Down
Loading