Skip to content

Commit

Permalink
[Tables] fix scroll in table with virtualizer
Browse files Browse the repository at this point in the history
  • Loading branch information
claire2212 committed Dec 20, 2024
1 parent 4464bfb commit 245684a
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 100 deletions.
50 changes: 35 additions & 15 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"test:unit:watch": "npm run test:unit -- --watch"
},
"dependencies": {
"@mtes-mct/monitor-ui": "24.11.0",
"@mtes-mct/monitor-ui": "24.11.3",
"@react-pdf/renderer": "4.1.5",
"@reduxjs/toolkit": "2.4.0",
"@sentry/browser": "8.41.0",
Expand All @@ -35,7 +35,7 @@
"@sentry/vite-plugin": "2.22.6",
"@svgr/webpack": "8.1.0",
"@tanstack/react-table": "8.20.5",
"@tanstack/react-virtual": "3.10.9",
"@tanstack/react-virtual": "3.11.2",
"classnames": "2.5.1",
"compressorjs": "1.2.1",
"dayjs": "1.11.13",
Expand Down
42 changes: 30 additions & 12 deletions frontend/src/components/Table/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { ChevronIcon } from '@features/commonStyles/icons/ChevronIcon.style'
import { Icon, SimpleTable } from '@mtes-mct/monitor-ui'
import { flexRender } from '@tanstack/react-table'
import { notUndefined } from '@tanstack/react-virtual'
import { forwardRef } from 'react'
import styled from 'styled-components'

export function TableWithRef({ rows, table, virtualRows }, ref) {
import { TableContainer } from './style'

export function TableWithRef({ columnsLength, rows, rowVirtualizer, table, virtualRows }, ref) {
const [before, after] =
virtualRows.length > 0
? [
notUndefined(virtualRows[0]).start - rowVirtualizer.options.scrollMargin,
rowVirtualizer.getTotalSize() - notUndefined(virtualRows[virtualRows.length - 1]).end
]
: [0, 0]

return (
<StyledDashboardsContainer ref={ref}>
<TableContainer ref={ref}>
<SimpleTable.Table>
<SimpleTable.Head>
{table.getHeaderGroups().map(headerGroup => (
<tr key={headerGroup.id}>
{headerGroup.headers.map(header => (
<SimpleTable.Th key={header.id} $width={header.column.getSize()}>
<SimpleTable.Th key={header.id} $width={header.getSize()}>
{header.isPlaceholder ? undefined : (
<SimpleTable.SortContainer
className={header.column.getCanSort() ? 'cursor-pointer select-none' : ''}
Expand All @@ -32,20 +43,28 @@ export function TableWithRef({ rows, table, virtualRows }, ref) {
</tr>
))}
</SimpleTable.Head>
{before > 0 && (
<tr>
<td aria-label="padding before" colSpan={columnsLength} style={{ height: before }} />
</tr>
)}
<tbody>
{virtualRows?.map(virtualRow => {
const row = rows[virtualRow?.index]

return (
<SimpleTable.BodyTr key={virtualRow.key}>
<SimpleTable.BodyTr
key={row?.id}
ref={rowVirtualizer.measureElement} // measure dynamic row height
data-index={virtualRow.index} // needed for dynamic row height measurement
>
{row?.getVisibleCells().map(cell => (
<SimpleTable.Td
key={cell.id}
$isCenter={cell.column.id === 'geom' || cell.column.id === 'edit'}
style={{
maxWidth: cell.column.getSize(),
minWidth: cell.column.getSize(),
padding: cell.column.id === 'geom' || cell.column.id === 'edit' ? '0px' : '10px 12px',
width: cell.column.getSize()
}}
>
Expand All @@ -56,19 +75,18 @@ export function TableWithRef({ rows, table, virtualRows }, ref) {
)
})}
</tbody>
{after > 0 && (
<tr>
<td aria-label="padding after" colSpan={columnsLength} style={{ height: after }} />
</tr>
)}
</SimpleTable.Table>
</StyledDashboardsContainer>
</TableContainer>
)
}

export const Table = forwardRef(TableWithRef)

const StyledDashboardsContainer = styled.div`
overflow: auto;
width: fit-content;
// scroll width (~15px) + 4px
padding-right: 19px;
`
const StyledChevronIcon = styled(ChevronIcon)`
margin-top: 0px;
margin-right: 0px;
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/components/Table/style.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import styled from 'styled-components'

export const TableContainer = styled.div`
overflow: auto;
width: fit-content;
// scroll width (~15px) + 4px
padding-right: 19px;
`
export const TotalResults = styled.h2`
font-size: 13px;
margin-top: 32px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,14 @@ export function DashboardsTable({ dashboards, isFetching, isLoading }: Dashboard

const virtualRows = rowVirtualizer.getVirtualItems()

return <Table ref={tableContainerRef} rows={rows} table={table} virtualRows={virtualRows} />
return (
<Table
ref={tableContainerRef}
columnsLength={columns.length}
rows={rows}
rowVirtualizer={rowVirtualizer}
table={table}
virtualRows={virtualRows}
/>
)
}
Loading

0 comments on commit 245684a

Please sign in to comment.