-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathuseGridKeyboardNavigation.ts
267 lines (236 loc) · 8.88 KB
/
useGridKeyboardNavigation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import * as React from 'react';
import { GridEvents, GridEventListener } from '../../../models/events';
import { GridApiCommunity } from '../../../models/api/gridApiCommunity';
import { GridCellParams } from '../../../models/params/gridCellParams';
import { gridVisibleColumnDefinitionsSelector } from '../columns/gridColumnsSelector';
import { useGridLogger } from '../../utils/useGridLogger';
import { useGridApiEventHandler } from '../../utils/useGridApiEventHandler';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { gridVisibleSortedRowEntriesSelector } from '../filter/gridFilterSelector';
import { useGridVisibleRows } from '../../utils/useGridVisibleRows';
import { GRID_CHECKBOX_SELECTION_COL_DEF } from '../../../colDef/gridCheckboxSelectionColDef';
import { gridClasses } from '../../../constants/gridClasses';
/**
* @requires useGridPage (state)
* @requires useGridPageSize (state)
* @requires useGridFilter (state)
* @requires useGridColumns (state, method)
* @requires useGridRows (state, method)
* @requires useGridSorting (method) - can be after
* @requires useGridDimensions (method) - can be after
* @requires useGridFocus (method) - can be after
* @requires useGridScroll (method) - can be after
*/
export const useGridKeyboardNavigation = (
apiRef: React.MutableRefObject<GridApiCommunity>,
props: Pick<DataGridProcessedProps, 'pagination' | 'paginationMode'>,
): void => {
const logger = useGridLogger(apiRef, 'useGridKeyboardNavigation');
const currentPage = useGridVisibleRows(apiRef, props);
const goToCell = React.useCallback(
(colIndex: number, rowIndex: number) => {
logger.debug(`Navigating to cell row ${rowIndex}, col ${colIndex}`);
apiRef.current.scrollToIndexes({ colIndex, rowIndex });
const field = apiRef.current.getVisibleColumns()[colIndex].field;
const visibleSortedRows = gridVisibleSortedRowEntriesSelector(apiRef);
const node = visibleSortedRows[rowIndex];
apiRef.current.setCellFocus(node.id, field);
},
[apiRef, logger],
);
const goToHeader = React.useCallback(
(colIndex: number, event: React.SyntheticEvent<Element>) => {
logger.debug(`Navigating to header col ${colIndex}`);
apiRef.current.scrollToIndexes({ colIndex });
const field = apiRef.current.getVisibleColumns()[colIndex].field;
apiRef.current.setColumnHeaderFocus(field, event);
},
[apiRef, logger],
);
const handleCellNavigationKeyDown = React.useCallback<
GridEventListener<GridEvents.cellNavigationKeyDown>
>(
(params, event) => {
const dimensions = apiRef.current.getRootDimensions();
if (!currentPage.range || !dimensions) {
return;
}
const viewportPageSize = apiRef.current.unstable_getViewportPageSize();
const visibleSortedRows = gridVisibleSortedRowEntriesSelector(apiRef);
const colIndexBefore = (params as GridCellParams).field
? apiRef.current.getColumnIndex((params as GridCellParams).field)
: 0;
const rowIndexBefore = visibleSortedRows.findIndex((row) => row.id === params.id);
const firstRowIndexInPage = currentPage.range.firstRowIndex;
const lastRowIndexInPage = currentPage.range.lastRowIndex;
const firstColIndex = 0;
const lastColIndex = gridVisibleColumnDefinitionsSelector(apiRef).length - 1;
let shouldPreventDefault = true;
switch (event.key) {
case 'ArrowDown':
case 'Enter': {
// "Enter" is only triggered by the row / cell editing feature
if (rowIndexBefore < lastRowIndexInPage) {
goToCell(colIndexBefore, rowIndexBefore + 1);
}
break;
}
case 'ArrowUp': {
if (rowIndexBefore > firstRowIndexInPage) {
goToCell(colIndexBefore, rowIndexBefore - 1);
} else {
goToHeader(colIndexBefore, event);
}
break;
}
case 'ArrowRight': {
if (colIndexBefore < lastColIndex) {
goToCell(colIndexBefore + 1, rowIndexBefore);
}
break;
}
case 'ArrowLeft': {
if (colIndexBefore > firstColIndex) {
goToCell(colIndexBefore - 1, rowIndexBefore);
}
break;
}
case 'Tab': {
// "Tab" is only triggered by the row / cell editing feature
if (event.shiftKey && colIndexBefore > firstColIndex) {
goToCell(colIndexBefore - 1, rowIndexBefore);
} else if (!event.shiftKey && colIndexBefore < lastColIndex) {
goToCell(colIndexBefore + 1, rowIndexBefore);
}
break;
}
case 'PageDown':
case ' ': {
if (rowIndexBefore < lastRowIndexInPage) {
goToCell(
colIndexBefore,
Math.min(rowIndexBefore + viewportPageSize, lastRowIndexInPage),
);
}
break;
}
case 'PageUp': {
// Go to the first row before going to header
const nextRowIndex = Math.max(rowIndexBefore - viewportPageSize, firstRowIndexInPage);
if (nextRowIndex !== rowIndexBefore && nextRowIndex >= firstRowIndexInPage) {
goToCell(colIndexBefore, nextRowIndex);
} else {
goToHeader(colIndexBefore, event);
}
break;
}
case 'Home': {
if (event.ctrlKey || event.metaKey || event.shiftKey) {
goToCell(firstColIndex, firstRowIndexInPage);
} else {
goToCell(firstColIndex, rowIndexBefore);
}
break;
}
case 'End': {
if (event.ctrlKey || event.metaKey || event.shiftKey) {
goToCell(lastColIndex, lastRowIndexInPage);
} else {
goToCell(lastColIndex, rowIndexBefore);
}
break;
}
default: {
shouldPreventDefault = false;
}
}
if (shouldPreventDefault) {
event.preventDefault();
}
},
[apiRef, currentPage, goToCell, goToHeader],
);
const handleColumnHeaderKeyDown = React.useCallback<
GridEventListener<GridEvents.columnHeaderKeyDown>
>(
(params, event) => {
const headerTitleNode = event.currentTarget.querySelector(
`.${gridClasses.columnHeaderTitleContainerContent}`,
);
const isFromInsideContent =
!!headerTitleNode && headerTitleNode.contains(event.target as Node | null);
if (isFromInsideContent && params.field !== GRID_CHECKBOX_SELECTION_COL_DEF.field) {
// When focus is on a nested input, keyboard events have no effect to avoid conflicts with native events.
// There is one exception for the checkBoxHeader
return;
}
const dimensions = apiRef.current.getRootDimensions();
if (!dimensions) {
return;
}
const viewportPageSize = apiRef.current.unstable_getViewportPageSize();
const colIndexBefore = params.field ? apiRef.current.getColumnIndex(params.field) : 0;
const firstRowIndexInPage = currentPage.range?.firstRowIndex ?? null;
const lastRowIndexInPage = currentPage.range?.lastRowIndex ?? null;
const firstColIndex = 0;
const lastColIndex = gridVisibleColumnDefinitionsSelector(apiRef).length - 1;
let shouldPreventDefault = true;
switch (event.key) {
case 'ArrowDown': {
if (firstRowIndexInPage !== null) {
goToCell(colIndexBefore, firstRowIndexInPage);
}
break;
}
case 'ArrowRight': {
if (colIndexBefore < lastColIndex) {
goToHeader(colIndexBefore + 1, event);
}
break;
}
case 'ArrowLeft': {
if (colIndexBefore > firstColIndex) {
goToHeader(colIndexBefore - 1, event);
}
break;
}
case 'PageDown': {
if (firstRowIndexInPage !== null && lastRowIndexInPage !== null) {
goToCell(
colIndexBefore,
Math.min(firstRowIndexInPage + viewportPageSize, lastRowIndexInPage),
);
}
break;
}
case 'Home': {
goToHeader(firstColIndex, event);
break;
}
case 'End': {
goToHeader(lastColIndex, event);
break;
}
case 'Enter': {
if (event.ctrlKey || event.metaKey) {
apiRef.current.toggleColumnMenu(params.field);
}
break;
}
case ' ': {
// prevent Space event from scrolling
break;
}
default: {
shouldPreventDefault = false;
}
}
if (shouldPreventDefault) {
event.preventDefault();
}
},
[apiRef, currentPage, goToCell, goToHeader],
);
useGridApiEventHandler(apiRef, GridEvents.cellNavigationKeyDown, handleCellNavigationKeyDown);
useGridApiEventHandler(apiRef, GridEvents.columnHeaderKeyDown, handleColumnHeaderKeyDown);
};