@@ -12,7 +12,7 @@ import { useExportToCSV, useLongPress } from "./hooks";
12
12
import { Icons } from "./icons" ;
13
13
import { SearchInput } from "./search" ;
14
14
import { Loading } from "./loading" ;
15
- import { clone } from "lodash" ;
15
+ import { clone , values } from "lodash" ;
16
16
17
17
type IPaginationProps = {
18
18
pageCount : number ;
@@ -183,7 +183,7 @@ const TData: FC<ITDataProps> = ({ cell, onCellUpdate, disableEdit }) => {
183
183
"bg-gray-200 dark:bg-white/10 blur-[2px]" : editable || preview ,
184
184
} ) }
185
185
>
186
- < span className = "hidden" > { editedData } </ span >
186
+ < span className = "cell-data hidden" > { editedData } </ span >
187
187
< div
188
188
className = { classNames ( "w-full h-full p-2 leading-tight focus:outline-none focus:shadow-outline appearance-none transition-all duration-300 border-solid border-gray-200 dark:border-white/5 overflow-hidden whitespace-nowrap select-none text-gray-600 dark:text-neutral-300" , {
189
189
"group-even/row:bg-gray-100 hover:bg-gray-300 group-even/row:hover:bg-gray-300 dark:group-even/row:bg-white/10 dark:group-odd/row:bg-white/5 dark:group-even/row:hover:bg-white/15 dark:group-odd/row:hover:bg-white/15" : ! editable ,
@@ -305,7 +305,7 @@ const TableRow: FC<ITableRow> = ({ row, style, onRowUpdate, disableEdit }) => {
305
305
< div className = "table-row-group text-xs group/row" { ...props } key = { props . key } >
306
306
{
307
307
row . cells . map ( ( cell ) => (
308
- < TData key = { cell . getCellProps ( ) . key } cell = { cell } onCellUpdate = { handleCellUpdate } disableEdit = { disableEdit } />
308
+ < TData key = { cell . getCellProps ( ) . key } cell = { cell } onCellUpdate = { handleCellUpdate } disableEdit = { disableEdit || cell . column . id === "#" } />
309
309
) )
310
310
}
311
311
</ div >
@@ -325,6 +325,7 @@ type ITableProps = {
325
325
}
326
326
327
327
export const Table : FC < ITableProps > = ( { className, columns : actualColumns , rows : actualRows , columnTags, totalPages, currentPage, onPageChange, onRowUpdate, disableEdit } ) => {
328
+ const fixedTableRef = useRef < FixedSizeList > ( null ) ;
328
329
const containerRef = useRef < HTMLDivElement > ( null ) ;
329
330
const operationsRef = useRef < HTMLDivElement > ( null ) ;
330
331
const tableRef = useRef < HTMLTableElement > ( null ) ;
@@ -413,46 +414,46 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
413
414
}
414
415
let interval : NodeJS . Timeout ;
415
416
if ( e . key === "Enter" ) {
416
- let newSearchIndex = ( searchIndex + 1 ) % rowCount ;
417
- setSearchIndex ( newSearchIndex ) ;
418
417
const searchText = search . toLowerCase ( ) ;
419
- let index = 0 ;
420
- const tbody = tableRef . current . querySelector ( ".tbody" ) ;
421
- if ( tbody == null ) {
422
- return ;
423
- }
424
- for ( const childNode of tbody . childNodes ) {
425
- if ( childNode instanceof HTMLTableRowElement ) {
426
- const text = childNode . textContent ?. toLowerCase ( ) ;
418
+ const filteredToOriginalIndex = [ ] ;
419
+ for ( const [ index , row ] of rows . entries ( ) ) {
420
+ for ( const value of values ( row . values ) ) {
421
+ const text = value . toLowerCase ( ) ;
427
422
if ( text != null && searchText != null && text . includes ( searchText ) ) {
428
- if ( index === newSearchIndex ) {
429
- childNode . scrollIntoView ( {
430
- behavior : "smooth" ,
431
- block : "center" ,
432
- inline : "center" ,
433
- } ) ;
434
- for ( const cell of childNode . querySelectorAll ( "input" ) ) {
435
- if ( cell instanceof HTMLInputElement ) {
436
- cell . classList . add ( "!bg-yellow-100" ) ;
437
- interval = setTimeout ( ( ) => {
438
- cell . classList . remove ( "!bg-yellow-100" ) ;
439
- } , 3000 ) ;
440
- }
423
+ filteredToOriginalIndex . push ( index ) ;
424
+ }
425
+ }
426
+ }
427
+
428
+ if ( rows . length > 0 && filteredToOriginalIndex . length > 0 ) {
429
+ const newSearchIndex = ( searchIndex + 1 ) % filteredToOriginalIndex . length ;
430
+ setSearchIndex ( newSearchIndex ) ;
431
+ const originalIndex = filteredToOriginalIndex [ newSearchIndex ] + 1 ;
432
+ fixedTableRef . current ?. scrollToItem ( originalIndex , "center" ) ;
433
+ setTimeout ( ( ) => {
434
+ const currentVisibleRows = tableRef . current ?. querySelectorAll ( ".table-row-group" ) ?? [ ] ;
435
+ for ( const currentVisibleRow of currentVisibleRows ) {
436
+ const text = currentVisibleRow . querySelector ( "div > span" ) ?. textContent ?? "" ;
437
+ if ( isNumeric ( text ) ) {
438
+ const id = parseInt ( text ) ;
439
+ if ( id === originalIndex ) {
440
+ currentVisibleRow . classList . add ( "!bg-yellow-100" , "dark:!bg-yellow-800" ) ;
441
+ interval = setTimeout ( ( ) => {
442
+ currentVisibleRow . classList . remove ( "!bg-yellow-100" , "dark:!bg-yellow-800" ) ;
443
+ } , 3000 ) ;
441
444
}
442
- return ;
443
445
}
444
- index ++ ;
445
446
}
446
- }
447
- } ;
447
+ } , 100 ) ;
448
+ }
448
449
}
449
450
450
451
return ( ) => {
451
452
if ( interval != null ) {
452
453
clearInterval ( interval ) ;
453
454
}
454
455
}
455
- } , [ search , rowCount , searchIndex ] ) ;
456
+ } , [ rows , search , searchIndex ] ) ;
456
457
457
458
const handleSearchChange = useCallback ( ( newValue : string ) => {
458
459
setSearchIndex ( - 1 ) ;
@@ -541,6 +542,7 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
541
542
</ div >
542
543
< div className = "tbody" { ...getTableBodyProps ( ) } >
543
544
< FixedSizeList
545
+ ref = { fixedTableRef }
544
546
height = { height }
545
547
itemCount = { sortedRows . length }
546
548
itemSize = { 31 }
0 commit comments