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] Tab should update state.focus on edit mode #6668

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: useGridRowEditing.old 'Tab' key handle
  • Loading branch information
yaredtsy committed Dec 10, 2022
commit aa6b186729b22c5ded48600b6ee63bbeee370db3
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
GridCellModes,
} from '../../../models/gridEditRowModel';
import { useGridSelector } from '../../utils/useGridSelector';
import { gridColumnDefinitionsSelector } from '../columns/gridColumnsSelector';
import {
gridColumnDefinitionsSelector,
gridColumnFieldsSelector,
} from '../columns/gridColumnsSelector';
import { gridEditRowsStateSelector } from './gridEditRowsSelector';
import { GridApiCommunity } from '../../../models/api/gridApiCommunity';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
Expand All @@ -24,6 +27,7 @@ import { gridFocusCellSelector } from '../focus/gridFocusStateSelector';
import {
GridRowEditStartParams,
GridRowEditStopParams,
GridRowEditStopReasons,
} from '../../../models/params/gridRowParams';
import {
useGridApiOptionHandler,
Expand Down Expand Up @@ -237,6 +241,39 @@ export const useGridRowEditing = (
apiRef.current.publishEvent('rowEditStop', rowParams as GridRowEditStopParams, event);
} else if (event.key === 'Escape') {
apiRef.current.publishEvent('rowEditStop', rowParams as GridRowEditStopParams, event);
} else if (event.key === 'Tab') {
let reason: GridRowEditStopReasons | undefined;
const columnFields = gridColumnFieldsSelector(apiRef).filter((field) =>
apiRef.current.isCellEditable(apiRef.current.getCellParams(params.id, field)),
);

if (event.shiftKey) {
if (params.field === columnFields[0]) {
// Exit if user pressed Shift+Tab on the first field
reason = GridRowEditStopReasons.shiftTabKeyDown;
}
} else if (params.field === columnFields[columnFields.length - 1]) {
// Exit if user pressed Tab on the last field
reason = GridRowEditStopReasons.tabKeyDown;
}

if (reason) {
event.preventDefault(); // Prevent going to the next element in the tab sequence

const newParams: GridRowEditStopParams = {
...rowParams,
reason,
field: params.field,
};
apiRef.current.publishEvent('rowEditStop', newParams, event);
}

if (!reason) {
event.preventDefault();
const index = columnFields.findIndex((field) => field === params.field);
const nextFieldToFocus = columnFields[event.shiftKey ? index - 1 : index + 1];
apiRef.current.setCellFocus(params.id, nextFieldToFocus);
}
}
} else if (event.key === 'Enter') {
apiRef.current.publishEvent('rowEditStart', rowParams as GridRowEditStartParams, event);
Expand Down