11import React from 'react' ;
2- import { Table as GravityTable , useTable , useWindowRowVirtualizer } from '@gravity-ui/table' ;
2+ import {
3+ Table as GravityTable ,
4+ useRowVirtualizer ,
5+ useTable ,
6+ useWindowRowVirtualizer ,
7+ } from '@gravity-ui/table' ;
38import type { ColumnDef , Row } from '@gravity-ui/table/tanstack' ;
49import { UnipikaFlattenTreeItem , SearchInfo } from '../utils/flattenUnipika' ;
510import { CollapseIconType , UnipikaSettings } from '../StructuredYson/types' ;
@@ -19,6 +24,8 @@ export interface TableProps {
1924 onToggleCollapse : ( path : string ) => void ;
2025 onShowFullText : ( index : number ) => void ;
2126 scrollToRef : React . RefObject < null | { scrollToIndex ( index : number ) : void } > ;
27+ scrollContainerRef ?: React . RefObject < Element | null > ;
28+ withScrollElement ?: boolean ;
2229 collapseIconType ?: CollapseIconType ;
2330 showContainerSize ?: boolean ;
2431}
@@ -32,6 +39,8 @@ export const Table: React.FC<TableProps> = ({
3239 onToggleCollapse,
3340 onShowFullText,
3441 scrollToRef,
42+ scrollContainerRef,
43+ withScrollElement,
3544 collapseIconType,
3645 showContainerSize,
3746} ) => {
@@ -62,32 +71,41 @@ export const Table: React.FC<TableProps> = ({
6271 data,
6372 } ) ;
6473
65- const bodyRef = React . useRef < HTMLTableSectionElement > ( null ) ;
74+ const { rowVirtualizer, windowRowVirtualizer, bodyRef} = useRowVirtualization (
75+ table . getRowModel ( ) . rows . length ,
76+ scrollContainerRef ,
77+ ) ;
6678
67- const rowVirtulization = useWindowRowVirtualizer ( {
68- count : table . getRowModel ( ) . rows . length ,
69- estimateSize : ( ) => 20 ,
70- overscan : 5 ,
71- scrollMargin : bodyRef . current ?. offsetTop ?? 0 ,
72- } ) ;
79+ const activeVirtualizer = withScrollElement ? rowVirtualizer : windowRowVirtualizer ;
7380
7481 React . useEffect ( ( ) => {
7582 scrollToRef . current = {
7683 scrollToIndex : ( index : number ) =>
77- rowVirtulization . scrollToIndex ( index , { align : 'center' } ) ,
84+ activeVirtualizer . scrollToIndex ( index , { align : 'center' } ) ,
7885 } ;
79- } , [ scrollToRef , rowVirtulization ] ) ;
86+ } , [ scrollToRef , activeVirtualizer ] ) ;
8087
8188 return (
8289 < div className = { block ( ) } >
83- < GravityTable
84- table = { table }
85- rowVirtualizer = { rowVirtulization }
86- rowClassName = { rowClassName }
87- cellClassName = { block ( 'cell' ) }
88- headerCellClassName = { block ( 'header-cell' ) }
89- bodyRef = { bodyRef }
90- />
90+ { withScrollElement ? (
91+ < GravityTable
92+ table = { table }
93+ rowVirtualizer = { rowVirtualizer }
94+ rowClassName = { rowClassName }
95+ cellClassName = { block ( 'cell' ) }
96+ headerCellClassName = { block ( 'header-cell' ) }
97+ bodyRef = { bodyRef }
98+ />
99+ ) : (
100+ < GravityTable
101+ table = { table }
102+ rowVirtualizer = { windowRowVirtualizer }
103+ rowClassName = { rowClassName }
104+ cellClassName = { block ( 'cell' ) }
105+ headerCellClassName = { block ( 'header-cell' ) }
106+ bodyRef = { bodyRef }
107+ />
108+ ) }
91109 </ div >
92110 ) ;
93111} ;
@@ -97,3 +115,22 @@ function rowClassName(row?: Row<UnipikaFlattenTreeItem>) {
97115 const k = key ?. $decoded_value ?? '' ;
98116 return block ( 'row' , { key : asModifier ( k ) } ) ;
99117}
118+
119+ function useRowVirtualization ( count : number , scrollContainerRef ?: React . RefObject < Element | null > ) {
120+ const bodyRef = React . useRef < HTMLTableSectionElement > ( null ) ;
121+ const windowRowVirtualizer = useWindowRowVirtualizer ( {
122+ count,
123+ estimateSize : ( ) => 20 ,
124+ overscan : 5 ,
125+ scrollMargin : bodyRef ?. current ?. offsetTop ?? 0 ,
126+ } ) ;
127+
128+ const rowVirtualizer = useRowVirtualizer ( {
129+ count,
130+ estimateSize : ( ) => 20 ,
131+ overscan : 5 ,
132+ getScrollElement : ( ) => scrollContainerRef ?. current || null ,
133+ } ) ;
134+
135+ return { rowVirtualizer, windowRowVirtualizer, bodyRef} ;
136+ }
0 commit comments