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,40 @@ export const Table: React.FC<TableProps> = ({
6271 data,
6372 } ) ;
6473
65- const bodyRef = React . useRef < HTMLTableSectionElement > ( null ) ;
66-
67- const rowVirtulization = useWindowRowVirtualizer ( {
68- count : table . getRowModel ( ) . rows . length ,
69- estimateSize : ( ) => 20 ,
70- overscan : 5 ,
71- scrollMargin : bodyRef . current ?. offsetTop ?? 0 ,
72- } ) ;
74+ const { rowVirtualizer, windowRowVirtualizer, bodyRef} = useRowVirtualization (
75+ table . getRowModel ( ) . rows . length ,
76+ scrollContainerRef ,
77+ ) ;
7378
7479 React . useEffect ( ( ) => {
80+ const activeVirtualizer = withScrollElement ? rowVirtualizer : windowRowVirtualizer ;
7581 scrollToRef . current = {
7682 scrollToIndex : ( index : number ) =>
77- rowVirtulization . scrollToIndex ( index , { align : 'center' } ) ,
83+ activeVirtualizer . scrollToIndex ( index , { align : 'center' } ) ,
7884 } ;
79- } , [ scrollToRef , rowVirtulization ] ) ;
85+ } , [ scrollToRef , withScrollElement , rowVirtualizer , windowRowVirtualizer ] ) ;
8086
8187 return (
8288 < 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- />
89+ { withScrollElement ? (
90+ < GravityTable
91+ table = { table }
92+ rowVirtualizer = { rowVirtualizer }
93+ rowClassName = { rowClassName }
94+ cellClassName = { block ( 'cell' ) }
95+ headerCellClassName = { block ( 'header-cell' ) }
96+ bodyRef = { bodyRef }
97+ />
98+ ) : (
99+ < GravityTable
100+ table = { table }
101+ rowVirtualizer = { windowRowVirtualizer }
102+ rowClassName = { rowClassName }
103+ cellClassName = { block ( 'cell' ) }
104+ headerCellClassName = { block ( 'header-cell' ) }
105+ bodyRef = { bodyRef }
106+ />
107+ ) }
91108 </ div >
92109 ) ;
93110} ;
@@ -97,3 +114,22 @@ function rowClassName(row?: Row<UnipikaFlattenTreeItem>) {
97114 const k = key ?. $decoded_value ?? '' ;
98115 return block ( 'row' , { key : asModifier ( k ) } ) ;
99116}
117+
118+ function useRowVirtualization ( count : number , scrollContainerRef ?: React . RefObject < Element | null > ) {
119+ const bodyRef = React . useRef < HTMLTableSectionElement > ( null ) ;
120+ const windowRowVirtualizer = useWindowRowVirtualizer ( {
121+ count,
122+ estimateSize : ( ) => 20 ,
123+ overscan : 5 ,
124+ scrollMargin : bodyRef ?. current ?. offsetTop ?? 0 ,
125+ } ) ;
126+
127+ const rowVirtualizer = useRowVirtualizer ( {
128+ count,
129+ estimateSize : ( ) => 20 ,
130+ overscan : 5 ,
131+ getScrollElement : ( ) => scrollContainerRef ?. current || null ,
132+ } ) ;
133+
134+ return { rowVirtualizer, windowRowVirtualizer, bodyRef} ;
135+ }
0 commit comments