11import React , {
22 FC ,
33 useContext ,
4+ useEffect ,
45 useMemo ,
56 useRef ,
6- useEffect ,
77 useState ,
88} from 'react'
99import { 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-
3027const 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