Skip to content

Commit 4f4ba8e

Browse files
authored
fix: calculate the correct heights for the header and rows in SimpleTable (#5240)
1 parent bae7305 commit 4f4ba8e

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

src/visualization/types/SimpleTable/InnerTable.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import React, {FC} from 'react'
1+
import React, {FC, Ref} from 'react'
22
import {ComponentSize, Table, VerticalAlignment} from '@influxdata/clockface'
33
import {SubsetTable} from 'src/visualization/types/SimpleTable'
44

55
interface InnerProps {
66
table: SubsetTable
7+
pagedTableRefs: {
8+
pagedTableHeaderRef: Ref<HTMLTableSectionElement>
9+
pagedTableBodyRef: Ref<HTMLTableSectionElement>
10+
}
711
}
812

9-
const InnerTable: FC<InnerProps> = ({table}) => {
13+
const InnerTable: FC<InnerProps> = ({
14+
pagedTableRefs: {pagedTableHeaderRef, pagedTableBodyRef},
15+
table,
16+
}) => {
1017
const headers = Object.values(table.cols).map(c => {
1118
if (c.name === 'table') {
1219
return (
@@ -61,10 +68,10 @@ const InnerTable: FC<InnerProps> = ({table}) => {
6168
highlight
6269
testID="simple-table"
6370
>
64-
<Table.Header>
71+
<Table.Header ref={pagedTableHeaderRef}>
6572
<Table.Row>{headers}</Table.Row>
6673
</Table.Header>
67-
<Table.Body>{rows}</Table.Body>
74+
<Table.Body ref={pagedTableBodyRef}>{rows}</Table.Body>
6875
</Table>
6976
)
7077
}

src/visualization/types/SimpleTable/PagedTable.tsx

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, {
22
FC,
33
useContext,
4+
useEffect,
45
useMemo,
56
useRef,
6-
useEffect,
77
useState,
88
} from 'react'
99
import {DapperScrollbars} from '@influxdata/clockface'
@@ -24,13 +24,12 @@ interface ExtendedColumn {
2424
data: any[]
2525
}
2626

27-
const HEADER_HEIGHT = 51
28-
const ROW_HEIGHT = 25
29-
3027
const measurePage = (
3128
result: FluxResult['parsed'],
3229
offset: number,
33-
height: number
30+
height: number,
31+
headerHeight: number,
32+
rowHeight: number
3433
): number => {
3534
if (height === 0) {
3635
return 0
@@ -54,7 +53,7 @@ const measurePage = (
5453
.join('|')
5554

5655
if (signature !== lastSignature) {
57-
runningHeight += HEADER_HEIGHT
56+
runningHeight += headerHeight
5857

5958
if (currentTable !== undefined) {
6059
runningHeight += 10
@@ -72,9 +71,9 @@ const measurePage = (
7271
continue
7372
}
7473

75-
runningHeight += ROW_HEIGHT
74+
runningHeight += rowHeight
7675

77-
if (runningHeight >= height) {
76+
if (runningHeight + 0.25 * rowHeight >= height) {
7877
break
7978
}
8079

@@ -231,8 +230,36 @@ const PagedTable: FC<Props> = ({result, properties}) => {
231230
setPage,
232231
setTotalPages,
233232
} = useContext(PaginationContext)
234-
const [height, setHeight] = useState(0)
235-
const ref = useRef()
233+
const [height, setHeight] = useState<number>(0)
234+
const [headerHeight, setHeaderHeight] = useState<number>(0)
235+
const [rowHeight, setRowHeight] = useState<number>(0)
236+
const ref = useRef<HTMLDivElement>()
237+
const pagedTableHeaderRef = useRef<HTMLTableSectionElement>()
238+
const pagedTableBodyRef = useRef<HTMLTableSectionElement>()
239+
240+
// eslint-disable-next-line react-hooks/exhaustive-deps
241+
useEffect(() => {
242+
if (headerHeight === 0 && pagedTableHeaderRef?.current) {
243+
const calculatedHeaderHeight =
244+
pagedTableHeaderRef.current.clientHeight ?? 0
245+
246+
if (calculatedHeaderHeight !== headerHeight) {
247+
setHeaderHeight(calculatedHeaderHeight)
248+
}
249+
}
250+
})
251+
252+
// eslint-disable-next-line react-hooks/exhaustive-deps
253+
useEffect(() => {
254+
if (rowHeight === 0 && pagedTableBodyRef?.current) {
255+
const calculatedRowHeight =
256+
pagedTableBodyRef.current?.children?.[0].clientHeight ?? 0
257+
258+
if (calculatedRowHeight !== rowHeight) {
259+
setRowHeight(calculatedRowHeight)
260+
}
261+
}
262+
})
236263

237264
// this makes sure that the table is always filling it's parent container
238265
useEffect(() => {
@@ -274,8 +301,8 @@ const PagedTable: FC<Props> = ({result, properties}) => {
274301
}, []) // eslint-disable-line react-hooks/exhaustive-deps
275302

276303
const size = useMemo(() => {
277-
return measurePage(result, offset, height)
278-
}, [result, offset, height])
304+
return measurePage(result, offset, height, headerHeight, rowHeight)
305+
}, [result, offset, height, headerHeight, rowHeight])
279306
const tables = useMemo(() => {
280307
return subsetResult(result, offset, size, properties.showAll)
281308
}, [result, offset, size]) // eslint-disable-line react-hooks/exhaustive-deps
@@ -285,8 +312,8 @@ const PagedTable: FC<Props> = ({result, properties}) => {
285312
}, [size]) // eslint-disable-line react-hooks/exhaustive-deps
286313

287314
useEffect(() => {
288-
setMaxSize(measurePage(result, 0, height))
289-
}, [height, result]) // eslint-disable-line react-hooks/exhaustive-deps
315+
setMaxSize(measurePage(result, 0, height, headerHeight, rowHeight))
316+
}, [result, height, headerHeight, rowHeight]) // eslint-disable-line react-hooks/exhaustive-deps
290317

291318
useEffect(() => {
292319
setPage(1)
@@ -300,7 +327,13 @@ const PagedTable: FC<Props> = ({result, properties}) => {
300327

301328
const inner =
302329
!!size &&
303-
tables.map((t, tIdx) => <InnerTable table={t} key={`table${tIdx}`} />)
330+
tables.map((t, tIdx) => (
331+
<InnerTable
332+
table={t}
333+
key={`table${tIdx}`}
334+
pagedTableRefs={{pagedTableHeaderRef, pagedTableBodyRef}}
335+
/>
336+
))
304337

305338
return (
306339
<div className="visualization--simple-table--results" ref={ref}>

0 commit comments

Comments
 (0)