@@ -57,6 +57,7 @@ import type {
57
57
Position ,
58
58
Renderers ,
59
59
RowsChangeData ,
60
+ SelectCellOptions ,
60
61
SelectHeaderRowEvent ,
61
62
SelectRowEvent ,
62
63
SortColumn
@@ -109,7 +110,7 @@ export type DefaultColumnOptions<R, SR> = Pick<
109
110
export interface DataGridHandle {
110
111
element : HTMLDivElement | null ;
111
112
scrollToCell : ( position : PartialPosition ) => void ;
112
- selectCell : ( position : Position , enableEditor ?: Maybe < boolean > ) => void ;
113
+ selectCell : ( position : Position , options : SelectCellOptions ) => void ;
113
114
}
114
115
115
116
type SharedDivProps = Pick <
@@ -502,7 +503,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
502
503
/**
503
504
* callbacks
504
505
*/
505
- const focusCellOrCellContent = useCallback (
506
+ const focusCell = useCallback (
506
507
( shouldScroll = true ) => {
507
508
const cell = getCellToScroll ( gridRef . current ! ) ;
508
509
if ( cell === null ) return ;
@@ -511,10 +512,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
511
512
scrollIntoView ( cell ) ;
512
513
}
513
514
514
- // Focus cell content when available instead of the cell itself
515
- const elementToFocus =
516
- cell . querySelector < Element & HTMLOrSVGElement > ( '[tabindex="0"]' ) ?? cell ;
517
- elementToFocus . focus ( { preventScroll : true } ) ;
515
+ cell . focus ( { preventScroll : true } ) ;
518
516
} ,
519
517
[ gridRef ]
520
518
) ;
@@ -528,11 +526,11 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
528
526
focusSinkRef . current . focus ( { preventScroll : true } ) ;
529
527
scrollIntoView ( focusSinkRef . current ) ;
530
528
} else {
531
- focusCellOrCellContent ( ) ;
529
+ focusCell ( ) ;
532
530
}
533
531
setShouldFocusCell ( false ) ;
534
532
}
535
- } , [ shouldFocusCell , focusCellOrCellContent , selectedPosition . idx ] ) ;
533
+ } , [ shouldFocusCell , focusCell , selectedPosition . idx ] ) ;
536
534
537
535
useImperativeHandle ( ref , ( ) => ( {
538
536
element : gridRef . current ,
@@ -654,7 +652,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
654
652
function handleFocus ( event : React . FocusEvent < HTMLDivElement > ) {
655
653
// select the first header cell if the focus event is triggered by the grid
656
654
if ( event . target === event . currentTarget ) {
657
- selectHeaderCell ( { idx : minColIdx , rowIdx : headerRowsCount } ) ;
655
+ selectHeaderCell ( { idx : minColIdx , rowIdx : headerRowsCount } , { shouldFocusCell : true } ) ;
658
656
}
659
657
}
660
658
@@ -777,7 +775,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
777
775
778
776
function handleDragHandleClick ( ) {
779
777
// keep the focus on the cell but do not scroll
780
- focusCellOrCellContent ( false ) ;
778
+ focusCell ( false ) ;
781
779
}
782
780
783
781
function handleDragHandleDoubleClick ( event : React . MouseEvent < HTMLDivElement > ) {
@@ -838,20 +836,20 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
838
836
) ;
839
837
}
840
838
841
- function selectCell ( position : Position , enableEditor ?: Maybe < boolean > ) : void {
839
+ function selectCell ( position : Position , options ?: SelectCellOptions ) : void {
842
840
if ( ! isCellWithinSelectionBounds ( position ) ) return ;
843
841
commitEditorChanges ( ) ;
844
842
845
843
const samePosition = isSamePosition ( selectedPosition , position ) ;
846
844
847
- if ( enableEditor && isCellEditable ( position ) ) {
845
+ if ( options ?. enableEditor && isCellEditable ( position ) ) {
848
846
const row = rows [ position . rowIdx ] ;
849
847
setSelectedPosition ( { ...position , mode : 'EDIT' , row, originalRow : row } ) ;
850
848
} else if ( samePosition ) {
851
849
// Avoid re-renders if the selected cell state is the same
852
850
scrollIntoView ( getCellToScroll ( gridRef . current ! ) ) ;
853
851
} else {
854
- setShouldFocusCell ( true ) ;
852
+ setShouldFocusCell ( options ?. shouldFocusCell === true ) ;
855
853
setSelectedPosition ( { ...position , mode : 'SELECT' } ) ;
856
854
}
857
855
@@ -864,8 +862,8 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
864
862
}
865
863
}
866
864
867
- function selectHeaderCell ( { idx, rowIdx } : Position ) {
868
- selectCell ( { rowIdx : minRowIdx + rowIdx - 1 , idx } ) ;
865
+ function selectHeaderCell ( { idx, rowIdx } : Position , options ?: SelectCellOptions ) : void {
866
+ selectCell ( { rowIdx : minRowIdx + rowIdx - 1 , idx } , options ) ;
869
867
}
870
868
871
869
function getNextPosition ( key : string , ctrlKey : boolean , shiftKey : boolean ) : Position {
@@ -952,7 +950,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
952
950
isCellWithinBounds : isCellWithinSelectionBounds
953
951
} ) ;
954
952
955
- selectCell ( nextSelectedCellPosition ) ;
953
+ selectCell ( nextSelectedCellPosition , { shouldFocusCell : true } ) ;
956
954
}
957
955
958
956
function getDraggedOverCellIdx ( currentRowIdx : number ) : number | undefined {
0 commit comments