From 58cc2588e6a12d96004053a035e33d931a21fce5 Mon Sep 17 00:00:00 2001 From: Matheus Wichman Date: Thu, 29 Jul 2021 23:02:11 -0300 Subject: [PATCH] [DataGrid] Drop autoFocus --- .../grid/components/cell/GridCell.tsx | 18 +++-- .../components/cell/GridEditBooleanCell.tsx | 10 ++- .../grid/components/cell/GridEditDateCell.tsx | 10 ++- .../components/cell/GridEditInputCell.tsx | 10 ++- .../cell/GridEditSingleSelectCell.tsx | 13 ++- .../_modules_/grid/components/cell/index.ts | 1 + .../grid/hooks/features/focus/useGridFocus.ts | 78 +++++++++++------- .../hooks/features/rows/useGridEditRows.ts | 1 + .../hooks/features/rows/useGridParamsApi.ts | 14 ++-- .../src/tests/keyboard.DataGrid.test.tsx | 1 + .../x-grid/src/tests/editRows.XGrid.test.tsx | 80 ++++++++++++++++++- .../grid/x-grid/src/tests/rows.XGrid.test.tsx | 57 ++++++++++++- .../src/stories/grid-rows.stories.tsx | 15 +++- 13 files changed, 256 insertions(+), 52 deletions(-) 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 (