@@ -296,6 +296,8 @@ function DataGrid<R, SR, K extends Key>(
296
296
const [ isDragging , setDragging ] = useState ( false ) ;
297
297
const [ draggedOverRowIdx , setOverRowIdx ] = useState < number | undefined > ( undefined ) ;
298
298
const [ scrollToPosition , setScrollToPosition ] = useState < PartialPosition | null > ( null ) ;
299
+ const [ shouldFocusCell , setShouldFocusCell ] = useState ( false ) ;
300
+ const [ previousRowIdx , setPreviousRowIdx ] = useState ( - 1 ) ;
299
301
300
302
const getColumnWidth = useCallback (
301
303
( column : CalculatedColumn < R , SR > ) => {
@@ -338,15 +340,13 @@ function DataGrid<R, SR, K extends Key>(
338
340
const [ selectedPosition , setSelectedPosition ] = useState (
339
341
( ) : SelectCellState | EditCellState < R > => ( { idx : - 1 , rowIdx : minRowIdx - 1 , mode : 'SELECT' } )
340
342
) ;
343
+ const [ prevSelectedPosition , setPrevSelectedPosition ] = useState ( selectedPosition ) ;
341
344
342
345
/**
343
346
* refs
344
347
*/
345
- const prevSelectedPosition = useRef ( selectedPosition ) ;
346
348
const latestDraggedOverRowIdx = useRef ( draggedOverRowIdx ) ;
347
- const lastSelectedRowIdx = useRef ( - 1 ) ;
348
349
const focusSinkRef = useRef < HTMLDivElement > ( null ) ;
349
- const shouldFocusCellRef = useRef ( false ) ;
350
350
351
351
/**
352
352
* computed values
@@ -458,31 +458,50 @@ function DataGrid<R, SR, K extends Key>(
458
458
selectCell ( { rowIdx : minRowIdx + rowIdx - 1 , idx } ) ;
459
459
} ) ;
460
460
461
+ /**
462
+ * callbacks
463
+ */
464
+ const setDraggedOverRowIdx = useCallback ( ( rowIdx ?: number ) => {
465
+ setOverRowIdx ( rowIdx ) ;
466
+ latestDraggedOverRowIdx . current = rowIdx ;
467
+ } , [ ] ) ;
468
+
469
+ const focusCellOrCellContent = useCallback ( ( ) => {
470
+ const cell = getCellToScroll ( gridRef . current ! ) ;
471
+ if ( cell === null ) return ;
472
+
473
+ scrollIntoView ( cell ) ;
474
+ // Focus cell content when available instead of the cell itself
475
+ const elementToFocus = cell . querySelector < Element & HTMLOrSVGElement > ( '[tabindex="0"]' ) ?? cell ;
476
+ elementToFocus . focus ( { preventScroll : true } ) ;
477
+ } , [ gridRef ] ) ;
478
+
461
479
/**
462
480
* effects
463
481
*/
464
482
useLayoutEffect ( ( ) => {
465
483
if (
466
484
! selectedCellIsWithinSelectionBounds ||
467
- isSamePosition ( selectedPosition , prevSelectedPosition . current )
485
+ isSamePosition ( selectedPosition , prevSelectedPosition )
468
486
) {
469
- prevSelectedPosition . current = selectedPosition ;
487
+ setPrevSelectedPosition ( selectedPosition ) ;
470
488
return ;
471
489
}
472
490
473
- prevSelectedPosition . current = selectedPosition ;
491
+ setPrevSelectedPosition ( selectedPosition ) ;
474
492
475
- if ( selectedPosition . idx === - 1 ) {
476
- focusSinkRef . current ! . focus ( { preventScroll : true } ) ;
493
+ if ( focusSinkRef . current !== null && selectedPosition . idx === - 1 ) {
494
+ focusSinkRef . current . focus ( { preventScroll : true } ) ;
477
495
scrollIntoView ( focusSinkRef . current ) ;
478
496
}
479
- } ) ;
497
+ } , [ selectedCellIsWithinSelectionBounds , selectedPosition , prevSelectedPosition ] ) ;
480
498
481
499
useLayoutEffect ( ( ) => {
482
- if ( ! shouldFocusCellRef . current ) return ;
483
- shouldFocusCellRef . current = false ;
484
- focusCellOrCellContent ( ) ;
485
- } ) ;
500
+ if ( shouldFocusCell ) {
501
+ setShouldFocusCell ( false ) ;
502
+ focusCellOrCellContent ( ) ;
503
+ }
504
+ } , [ shouldFocusCell , focusCellOrCellContent ] ) ;
486
505
487
506
useImperativeHandle ( ref , ( ) => ( {
488
507
element : gridRef . current ,
@@ -499,14 +518,6 @@ function DataGrid<R, SR, K extends Key>(
499
518
selectCell
500
519
} ) ) ;
501
520
502
- /**
503
- * callbacks
504
- */
505
- const setDraggedOverRowIdx = useCallback ( ( rowIdx ?: number ) => {
506
- setOverRowIdx ( rowIdx ) ;
507
- latestDraggedOverRowIdx . current = rowIdx ;
508
- } , [ ] ) ;
509
-
510
521
/**
511
522
* event handlers
512
523
*/
@@ -536,9 +547,8 @@ function DataGrid<R, SR, K extends Key>(
536
547
if ( isRowSelectionDisabled ?.( row ) === true ) return ;
537
548
const newSelectedRows = new Set ( selectedRows ) ;
538
549
const rowKey = rowKeyGetter ( row ) ;
539
- const previousRowIdx = lastSelectedRowIdx . current ;
540
550
const rowIdx = rows . indexOf ( row ) ;
541
- lastSelectedRowIdx . current = rowIdx ;
551
+ setPreviousRowIdx ( rowIdx ) ;
542
552
543
553
if ( checked ) {
544
554
newSelectedRows . add ( rowKey ) ;
@@ -758,7 +768,7 @@ function DataGrid<R, SR, K extends Key>(
758
768
// Avoid re-renders if the selected cell state is the same
759
769
scrollIntoView ( getCellToScroll ( gridRef . current ! ) ) ;
760
770
} else {
761
- shouldFocusCellRef . current = true ;
771
+ setShouldFocusCell ( true ) ;
762
772
setSelectedPosition ( { ...position , mode : 'SELECT' } ) ;
763
773
}
764
774
@@ -870,16 +880,6 @@ function DataGrid<R, SR, K extends Key>(
870
880
return isDraggedOver ? selectedPosition . idx : undefined ;
871
881
}
872
882
873
- function focusCellOrCellContent ( ) {
874
- const cell = getCellToScroll ( gridRef . current ! ) ;
875
- if ( cell === null ) return ;
876
-
877
- scrollIntoView ( cell ) ;
878
- // Focus cell content when available instead of the cell itself
879
- const elementToFocus = cell . querySelector < Element & HTMLOrSVGElement > ( '[tabindex="0"]' ) ?? cell ;
880
- elementToFocus . focus ( { preventScroll : true } ) ;
881
- }
882
-
883
883
function renderDragHandle ( ) {
884
884
if (
885
885
onFill == null ||
@@ -925,7 +925,7 @@ function DataGrid<R, SR, K extends Key>(
925
925
const colSpan = getColSpan ( column , lastFrozenColumnIndex , { type : 'ROW' , row } ) ;
926
926
927
927
const closeEditor = ( shouldFocusCell : boolean ) => {
928
- shouldFocusCellRef . current = shouldFocusCell ;
928
+ setShouldFocusCell ( shouldFocusCell ) ;
929
929
setSelectedPosition ( ( { idx, rowIdx } ) => ( { idx, rowIdx, mode : 'SELECT' } ) ) ;
930
930
} ;
931
931
@@ -1061,6 +1061,7 @@ function DataGrid<R, SR, K extends Key>(
1061
1061
// Reset the positions if the current values are no longer valid. This can happen if a column or row is removed
1062
1062
if ( selectedPosition . idx > maxColIdx || selectedPosition . rowIdx > maxRowIdx ) {
1063
1063
setSelectedPosition ( { idx : - 1 , rowIdx : minRowIdx - 1 , mode : 'SELECT' } ) ;
1064
+ // eslint-disable-next-line react-compiler/react-compiler
1064
1065
setDraggedOverRowIdx ( undefined ) ;
1065
1066
}
1066
1067
@@ -1182,6 +1183,7 @@ function DataGrid<R, SR, K extends Key>(
1182
1183
) ;
1183
1184
} ) }
1184
1185
< RowSelectionChangeProvider value = { selectRowLatest } >
1186
+ { /* eslint-disable-next-line react-compiler/react-compiler */ }
1185
1187
{ getViewportRows ( ) }
1186
1188
</ RowSelectionChangeProvider >
1187
1189
{ bottomSummaryRows ?. map ( ( row , rowIdx ) => {
@@ -1245,7 +1247,7 @@ function DataGrid<R, SR, K extends Key>(
1245
1247
< ScrollToCell
1246
1248
scrollToPosition = { scrollToPosition }
1247
1249
setScrollToCellPosition = { setScrollToPosition }
1248
- gridElement = { gridRef . current ! }
1250
+ gridRef = { gridRef }
1249
1251
/>
1250
1252
) }
1251
1253
</ div >
0 commit comments