Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix multiple focus behaviors #1421

Merged
merged 17 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/grid/_modules_/grid/GridComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { GridComponentProps } from './GridComponentProps';
import { useGridColumnMenu } from './hooks/features/columnMenu/useGridColumnMenu';
import { useGridColumns } from './hooks/features/columns/useGridColumns';
import { useGridState } from './hooks/features/core/useGridState';
import { useGridFocus } from './hooks/features/focus/useGridFocus';
import { useGridKeyboardNavigation } from './hooks/features/keyboard/useGridKeyboardNavigation';
import { useGridPagination } from './hooks/features/pagination/useGridPagination';
import { useGridPreferencesPanel } from './hooks/features/preferencesPanel/useGridPreferencesPanel';
Expand Down Expand Up @@ -80,6 +81,7 @@ export const GridComponent = React.forwardRef<HTMLDivElement, GridComponentProps
useGridParamsApi(apiRef);
useGridRows(apiRef, props.rows, props.getRowId);
useGridEditRows(apiRef);
useGridFocus(apiRef);
useGridKeyboard(rootContainerRef, apiRef);
useGridKeyboardNavigation(rootContainerRef, apiRef);
useGridSelection(apiRef);
Expand Down
68 changes: 0 additions & 68 deletions packages/grid/_modules_/grid/components/GridCheckboxRenderer.tsx

This file was deleted.

9 changes: 7 additions & 2 deletions packages/grid/_modules_/grid/components/GridViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { visibleGridColumnsSelector } from '../hooks/features/columns/gridColumn
import { useGridSelector } from '../hooks/features/core/useGridSelector';
import { gridDensityRowHeightSelector } from '../hooks/features/density/densitySelector';
import { visibleSortedGridRowsAsArraySelector } from '../hooks/features/filter/gridFilterSelector';
import { gridKeyboardCellSelector } from '../hooks/features/keyboard/gridKeyboardSelector';
import {
gridFocusCellSelector,
gridTabIndexCellSelector,
} from '../hooks/features/focus/gridFocusStateSelector';
import { gridSelectionStateSelector } from '../hooks/features/selection/gridSelectionSelector';
import { renderStateSelector } from '../hooks/features/virtualization/renderingStateSelector';
import { optionsSelector } from '../hooks/utils/optionsSelector';
Expand Down Expand Up @@ -32,7 +35,8 @@ export const GridViewport: ViewportType = React.forwardRef<HTMLDivElement, {}>(
const scrollBarState = useGridSelector(apiRef, gridScrollBarSizeSelector);
const visibleColumns = useGridSelector(apiRef, visibleGridColumnsSelector);
const renderState = useGridSelector(apiRef, renderStateSelector);
const cellFocus = useGridSelector(apiRef, gridKeyboardCellSelector);
const cellFocus = useGridSelector(apiRef, gridFocusCellSelector);
const cellTabIndex = useGridSelector(apiRef, gridTabIndexCellSelector);
const selectionState = useGridSelector(apiRef, gridSelectionStateSelector);
const rows = useGridSelector(apiRef, visibleSortedGridRowsAsArraySelector);
const rowHeight = useGridSelector(apiRef, gridDensityRowHeightSelector);
Expand Down Expand Up @@ -68,6 +72,7 @@ export const GridViewport: ViewportType = React.forwardRef<HTMLDivElement, {}>(
extendRowFullWidth={!options.disableExtendRowFullWidth}
rowIndex={renderState.renderContext!.firstRowIdx! + idx}
cellFocus={cellFocus}
cellTabIndex={cellTabIndex}
/>
<GridEmptyCell width={renderState.renderContext!.rightEmptyWidth} height={rowHeight} />
</GridRow>
Expand Down
16 changes: 10 additions & 6 deletions packages/grid/_modules_/grid/components/cell/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface GridCellProps {
width: number;
cellMode?: GridCellMode;
children: React.ReactElement | null;
tabIndex: 0 | -1;
}

export const GridCell = React.memo((props: GridCellProps) => {
Expand All @@ -53,16 +54,14 @@ export const GridCell = React.memo((props: GridCellProps) => {
rowIndex,
rowId,
showRightBorder,
tabIndex,
value,
width,
} = props;

const valueToRender = formattedValue == null ? value : formattedValue;
const cellRef = React.useRef<HTMLDivElement>(null);
const apiRef = React.useContext(GridApiContext);
const currentFocusedCell = apiRef!.current.getState().keyboard.cell;
const isCellFocused =
DanailH marked this conversation as resolved.
Show resolved Hide resolved
(currentFocusedCell && hasFocus) || (rowIndex === 0 && colIndex === 0 && !currentFocusedCell);

const cssClasses = classnames(
GRID_CELL_CSS_CLASS,
Expand All @@ -89,6 +88,7 @@ export const GridCell = React.memo((props: GridCellProps) => {
},
[apiRef, field, rowId],
);

const publishClick = React.useCallback(
(eventName: string) => (event: React.MouseEvent) => {
const params = apiRef!.current.getCellParams(rowId, field || '');
Expand Down Expand Up @@ -149,7 +149,12 @@ export const GridCell = React.memo((props: GridCellProps) => {
cellRef.current &&
(!document.activeElement || !cellRef.current!.contains(document.activeElement))
) {
cellRef.current!.focus();
const focusableElement = cellRef.current.querySelector('[tabindex="0"]') as HTMLElement;
if (focusableElement) {
focusableElement!.focus();
} else {
cellRef.current!.focus();
}
}
});

Expand All @@ -165,8 +170,7 @@ export const GridCell = React.memo((props: GridCellProps) => {
data-mode={cellMode}
aria-colindex={colIndex}
style={style}
/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */
tabIndex={isCellFocused ? 0 : -1}
tabIndex={tabIndex}
{...eventsHandlers}
>
{children || valueToRender?.toString()}
Expand Down
8 changes: 8 additions & 0 deletions packages/grid/_modules_/grid/components/cell/GridRowCells.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface RowCellsProps {
rowIndex: number;
showCellRightBorder: boolean;
cellFocus: GridCellIndexCoordinates | null;
cellTabIndex: GridCellIndexCoordinates | null;
}

export const GridRowCells = React.memo((props: RowCellsProps) => {
Expand All @@ -45,6 +46,7 @@ export const GridRowCells = React.memo((props: RowCellsProps) => {
lastColIdx,
rowIndex,
cellFocus,
cellTabIndex,
showCellRightBorder,
} = props;
const apiRef = React.useContext(GridApiContext);
Expand Down Expand Up @@ -107,6 +109,12 @@ export const GridRowCells = React.memo((props: RowCellsProps) => {
cellFocus !== null &&
cellFocus.rowIndex === rowIndex &&
cellFocus.colIndex === cellParams.colIndex,
tabIndex:
cellTabIndex !== null &&
cellTabIndex.rowIndex === rowIndex &&
cellTabIndex.colIndex === cellParams.colIndex
? 0
: -1,
};

return cellProps;
Expand Down
Loading