diff --git a/packages/grid/_modules_/grid/components/cell/GridCell.tsx b/packages/grid/_modules_/grid/components/cell/GridCell.tsx index 2fba02dd00e7..520350f69b38 100644 --- a/packages/grid/_modules_/grid/components/cell/GridCell.tsx +++ b/packages/grid/_modules_/grid/components/cell/GridCell.tsx @@ -145,13 +145,17 @@ export const GridCell = React.memo(function GridCell(props: GridCellProps) { }; React.useLayoutEffect(() => { - const doc = ownerDocument(apiRef!.current.rootElementRef!.current as HTMLElement); + if (!hasFocus) { + return; + } + + if (cellMode === 'edit' && isEditable) { + return; + } + + const doc = ownerDocument(apiRef!.current.rootElementRef!.current as HTMLElement)!; - if ( - hasFocus && - cellRef.current && - (!doc.activeElement || !cellRef.current!.contains(doc.activeElement)) - ) { + if (cellRef.current && (doc.activeElement || !cellRef.current!.contains(doc.activeElement))) { const focusableElement = cellRef.current!.querySelector('[tabindex="0"]') as HTMLElement; if (focusableElement) { focusableElement!.focus(); @@ -175,7 +179,7 @@ export const GridCell = React.memo(function GridCell(props: GridCellProps) { data-mode={cellMode} aria-colindex={colIndex + 1} style={style} - tabIndex={tabIndex} + tabIndex={cellMode === 'view' || !isEditable ? tabIndex : -1} {...eventsHandlers} > {children || valueToRender?.toString()} diff --git a/packages/grid/_modules_/grid/components/cell/GridEditBooleanCell.tsx b/packages/grid/_modules_/grid/components/cell/GridEditBooleanCell.tsx index 8ca3887b9fd1..48b0141ab94a 100644 --- a/packages/grid/_modules_/grid/components/cell/GridEditBooleanCell.tsx +++ b/packages/grid/_modules_/grid/components/cell/GridEditBooleanCell.tsx @@ -19,12 +19,14 @@ export function GridEditBooleanCell( colDef, cellMode, isEditable, + tabIndex, className, getValue, hasFocus, ...other } = props; + const inputRef = React.useRef(null); const id = useId(); const [valueState, setValueState] = React.useState(value); @@ -41,11 +43,17 @@ export function GridEditBooleanCell( setValueState(value); }, [value]); + React.useLayoutEffect(() => { + if (hasFocus && inputRef.current) { + inputRef.current.focus(); + } + }, [hasFocus]); + return (