-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
useKeyboard.ts
224 lines (191 loc) · 7.52 KB
/
useKeyboard.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
import { useEffect, useRef } from 'react';
import { useLogger } from '../utils/useLogger';
import { KEYDOWN_EVENT, KEYUP_EVENT, MULTIPLE_KEY_PRESS_CHANGED } from '../../constants/eventsConstants';
import { GridApiRef } from '../../grid';
import {
findGridRootFromCurrent,
findParentElementFromClassName,
getCellElementFromIndexes,
getDataFromElem,
getIdFromRowElem,
isArrowKeys,
isCell,
isHomeOrEndKeys,
isMultipleKey,
isNavigationKey,
isPageKeys,
isSpaceKey,
isTabKey,
} from '../../utils';
import { CELL_CSS_CLASS, ROW_CSS_CLASS } from '../../constants/cssClassesConstants';
import { CellIndexCoordinates } from '../../models';
const getNextCellIndexes = (code: string, indexes: CellIndexCoordinates) => {
if (!isArrowKeys(code)) {
throw new Error('first argument code should be an Arrow Key code');
}
if (code === 'ArrowLeft') {
return { ...indexes, colIndex: indexes.colIndex - 1 };
}
if (code === 'ArrowRight') {
return { ...indexes, colIndex: indexes.colIndex + 1 };
}
if (code === 'ArrowUp') {
return { ...indexes, rowIndex: indexes.rowIndex - 1 };
}
// Last option code === 'ArrowDown'
return { ...indexes, rowIndex: indexes.rowIndex + 1 };
};
export const useKeyboard = (initialised: boolean, apiRef: GridApiRef): void => {
const logger = useLogger('useKeyboard');
const isMultipleKeyPressed = useRef(false);
const onMultipleKeyChange = (isPressed: boolean) => {
isMultipleKeyPressed.current = isPressed;
if (apiRef.current) {
apiRef.current.emit(MULTIPLE_KEY_PRESS_CHANGED, isPressed);
}
};
const navigateCells = (code: string, isCtrlPressed: boolean) => {
const cellEl = findParentElementFromClassName(
document.activeElement as HTMLDivElement,
CELL_CSS_CLASS,
)! as HTMLElement;
cellEl.tabIndex = -1;
const root = findGridRootFromCurrent(cellEl)!;
const currentColIndex = Number(getDataFromElem(cellEl, 'colindex'));
const currentRowIndex = Number(getDataFromElem(cellEl, 'rowindex'));
const rowCount = apiRef.current!.getRowsCount();
const colCount = apiRef.current!.getVisibleColumns().length;
let nextCellIndexes: CellIndexCoordinates;
if (isArrowKeys(code)) {
nextCellIndexes = getNextCellIndexes(code, { colIndex: currentColIndex, rowIndex: currentRowIndex });
} else if (isHomeOrEndKeys(code)) {
const colIdx = code === 'Home' ? 0 : colCount - 1;
if (!isCtrlPressed) {
//we go to the current row, first col, or last col!
nextCellIndexes = { colIndex: colIdx, rowIndex: currentRowIndex };
} else {
//In that case we go to first row, first col, or last row last col!
const rowIndex = colIdx === 0 ? 0 : rowCount - 1;
nextCellIndexes = { colIndex: colIdx, rowIndex };
}
} else if (isPageKeys(code) || isSpaceKey(code)) {
const pageSize = apiRef.current!.getContainerPropsState()!.viewportPageSize;
const nextRowIndex = currentRowIndex + (code.indexOf('Down') > -1 || isSpaceKey(code) ? pageSize : -1 * pageSize);
nextCellIndexes = { colIndex: currentColIndex, rowIndex: nextRowIndex };
} else {
throw new Error('Key not mapped to navigation behaviour');
}
nextCellIndexes.rowIndex = nextCellIndexes.rowIndex <= 0 ? 0 : nextCellIndexes.rowIndex;
nextCellIndexes.rowIndex = nextCellIndexes.rowIndex >= rowCount ? rowCount - 1 : nextCellIndexes.rowIndex;
nextCellIndexes.colIndex = nextCellIndexes.colIndex <= 0 ? 0 : nextCellIndexes.colIndex;
nextCellIndexes.colIndex = nextCellIndexes.colIndex >= colCount ? colCount - 1 : nextCellIndexes.colIndex;
apiRef.current!.scrollToIndexes(nextCellIndexes);
setTimeout(() => {
const nextCell = getCellElementFromIndexes(root, nextCellIndexes);
if (nextCell) {
nextCell.tabIndex = 0;
(nextCell as HTMLDivElement).focus();
}
}, 100);
return nextCellIndexes;
};
const selectActiveRow = () => {
const rowEl = findParentElementFromClassName(
document.activeElement as HTMLDivElement,
ROW_CSS_CLASS,
)! as HTMLElement;
const rowId = getIdFromRowElem(rowEl);
if (apiRef.current) {
apiRef.current.selectRow(rowId);
}
};
const expandSelection = (code: string) => {
const rowEl = findParentElementFromClassName(
document.activeElement as HTMLDivElement,
ROW_CSS_CLASS,
)! as HTMLElement;
const currentRowIndex = Number(getDataFromElem(rowEl, 'rowindex'));
let selectionFromRowIndex = currentRowIndex;
const selectedRows = apiRef.current!.getSelectedRows();
if (selectedRows.length > 0) {
const selectedRowsIndex = selectedRows.map(row => apiRef.current!.getRowIndexFromId(row.id));
const diffWithCurrentIndex: number[] = selectedRowsIndex.map(idx => Math.abs(currentRowIndex - idx));
const minIndex = Math.max(...diffWithCurrentIndex);
selectionFromRowIndex = selectedRowsIndex[diffWithCurrentIndex.indexOf(minIndex)];
}
const nextCellIndexes = navigateCells(code, false);
//We select the rows in between
const rowIds = Array(Math.abs(nextCellIndexes.rowIndex - selectionFromRowIndex) + 1)
.fill(nextCellIndexes.rowIndex > selectionFromRowIndex ? selectionFromRowIndex : nextCellIndexes.rowIndex)
.map((cur, idx) => apiRef.current!.getRowIdFromRowIndex(cur + idx));
logger.debug('Selecting rows ', rowIds);
apiRef.current!.selectRows(rowIds, true, true);
};
const handleCopy = () => {
const rowEl = findParentElementFromClassName(
document.activeElement as HTMLDivElement,
ROW_CSS_CLASS,
)! as HTMLElement;
const rowId = getIdFromRowElem(rowEl);
const rowModel = apiRef.current!.getRowFromId(rowId);
if (rowModel.selected) {
window?.getSelection()?.selectAllChildren(rowEl);
} else {
window?.getSelection()?.selectAllChildren(document.activeElement!);
}
document.execCommand('copy');
};
const onKeyDownHandler = (e: KeyboardEvent) => {
if (isMultipleKey(e.key)) {
logger.debug('Multiple Select key pressed');
onMultipleKeyChange(true);
return;
}
if (!isCell(document.activeElement)) {
return;
}
if (!isTabKey(e.code)) {
//WE prevent default behaviour for all key shortcut except tab when the current active element is a cell
e.preventDefault();
e.stopPropagation();
}
if (isSpaceKey(e.code) && e.shiftKey) {
selectActiveRow();
return;
}
if (isNavigationKey(e.code) && !e.shiftKey) {
navigateCells(e.code, e.ctrlKey || e.metaKey);
return;
}
if (isNavigationKey(e.code) && e.shiftKey) {
expandSelection(e.code);
return;
}
if (e.key.toLowerCase() === 'c' && (e.ctrlKey || e.metaKey)) {
handleCopy();
return;
}
if (e.key.toLowerCase() === 'a' && (e.ctrlKey || e.metaKey)) {
apiRef.current!.selectRows(apiRef.current!.getAllRowIds(), true);
return;
}
};
const onKeyUpHandler = (e: KeyboardEvent) => {
if (isMultipleKey(e.key)) {
logger.debug('Multiple Select key released');
onMultipleKeyChange(false);
}
};
useEffect(() => {
if (apiRef && apiRef.current && initialised) {
logger.debug('Binding keyboard events');
apiRef.current.on(KEYDOWN_EVENT, onKeyDownHandler);
apiRef.current.on(KEYUP_EVENT, onKeyUpHandler);
return () => {
apiRef.current!.removeListener(KEYDOWN_EVENT, onKeyDownHandler);
apiRef.current!.removeListener(KEYUP_EVENT, onKeyUpHandler);
apiRef.current!.removeAllListeners(MULTIPLE_KEY_PRESS_CHANGED);
};
}
}, [initialised]);
};