-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathEditCell.tsx
More file actions
222 lines (199 loc) · 6.72 KB
/
EditCell.tsx
File metadata and controls
222 lines (199 loc) · 6.72 KB
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
import { useEffectEvent, useLayoutEffect, useRef } from 'react';
import { css } from 'ecij';
import { createCellEvent, getCellClassname, getCellStyle, onEditorNavigation } from './utils';
import type {
CellKeyboardEvent,
CellRendererProps,
EditCellKeyDownArgs,
Maybe,
Omit,
RenderEditCellProps
} from './types';
declare global {
const scheduler: Scheduler | undefined;
}
interface Scheduler {
readonly postTask?: (
callback: () => void,
options?: {
priority?: 'user-blocking' | 'user-visible' | 'background';
signal?: AbortSignal;
delay?: number;
}
) => Promise<unknown>;
}
/*
* To check for outside `mousedown` events, we listen to all `mousedown` events at their birth,
* i.e. on the window during the capture phase, and at their death, i.e. on the window during the bubble phase.
*
* We schedule a check at the birth of the event, cancel the check when the event reaches the "inside" container,
* and trigger the "outside" callback when the event bubbles back up to the window.
*
* The event can be `stopPropagation()`ed halfway through, so they may not always bubble back up to the window,
* so an alternative check must be used. The check must happen after the event can reach the "inside" container,
* and not before it run to completion. `postTask`/`requestAnimationFrame` are the best way we know to achieve this.
* Usually we want click event handlers from parent components to access the latest commited values,
* so `mousedown` is used instead of `click`.
*
* We must also rely on React's event capturing/bubbling to handle elements rendered in a portal.
*/
const canUsePostTask = typeof scheduler === 'object' && typeof scheduler.postTask === 'function';
const cellEditing = css`
@layer rdg.EditCell {
padding: 0;
}
`;
type SharedCellRendererProps<R, SR> = Pick<CellRendererProps<R, SR>, 'colSpan'>;
interface EditCellProps<R, SR>
extends
Omit<RenderEditCellProps<R, SR>, 'onRowChange' | 'onClose'>,
SharedCellRendererProps<R, SR> {
rowIdx: number;
onRowChange: (row: R, commitChanges: boolean, shouldFocusCell: boolean) => void;
closeEditor: (shouldFocusCell: boolean) => void;
navigate: (event: React.KeyboardEvent<HTMLDivElement>) => void;
onKeyDown: Maybe<(args: EditCellKeyDownArgs<R, SR>, event: CellKeyboardEvent) => void>;
}
export default function EditCell<R, SR>({
column,
colSpan,
row,
rowIdx,
onRowChange,
closeEditor,
onKeyDown,
navigate
}: EditCellProps<R, SR>) {
const captureEventRef = useRef<MouseEvent | undefined>(undefined);
const abortControllerRef = useRef<AbortController>(undefined);
const frameRequestRef = useRef<number>(undefined);
const commitOnOutsideClick = column.editorOptions?.commitOnOutsideClick ?? true;
// We need to prevent the `useLayoutEffect` from cleaning up between re-renders,
// as `onWindowCaptureMouseDown` might otherwise miss valid mousedown events.
// To that end we instead access the latest props via useEffectEvent.
const commitOnOutsideMouseDown = useEffectEvent(() => {
onClose(true, false);
});
useLayoutEffect(() => {
if (!commitOnOutsideClick) return;
function onWindowCaptureMouseDown(event: MouseEvent) {
captureEventRef.current = event;
if (canUsePostTask) {
const abortController = new AbortController();
const { signal } = abortController;
abortControllerRef.current = abortController;
// Use postTask to ensure that the event is not called in the middle of a React render
// and that it is called before the next paint.
scheduler
.postTask(commitOnOutsideMouseDown, {
priority: 'user-blocking',
signal
})
// ignore abort errors
.catch(() => {});
} else {
frameRequestRef.current = requestAnimationFrame(commitOnOutsideMouseDown);
}
}
function onWindowMouseDown(event: MouseEvent) {
if (captureEventRef.current === event) {
commitOnOutsideMouseDown();
}
}
window.addEventListener('mousedown', onWindowCaptureMouseDown, { capture: true });
window.addEventListener('mousedown', onWindowMouseDown);
return () => {
window.removeEventListener('mousedown', onWindowCaptureMouseDown, { capture: true });
window.removeEventListener('mousedown', onWindowMouseDown);
cancelTask();
};
}, [commitOnOutsideClick]);
function cancelTask() {
captureEventRef.current = undefined;
if (abortControllerRef.current !== undefined) {
abortControllerRef.current.abort();
abortControllerRef.current = undefined;
}
if (frameRequestRef.current !== undefined) {
cancelAnimationFrame(frameRequestRef.current);
frameRequestRef.current = undefined;
}
}
function handleKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
if (onKeyDown) {
const cellEvent = createCellEvent(event);
onKeyDown(
{
mode: 'EDIT',
row,
column,
rowIdx,
navigate() {
navigate(event);
},
onClose
},
cellEvent
);
if (cellEvent.isGridDefaultPrevented()) return;
}
if (event.key === 'Escape') {
// Discard changes
onClose();
} else if (event.key === 'Enter') {
onClose(true);
} else if (onEditorNavigation(event)) {
navigate(event);
}
}
function onClose(commitChanges = false, shouldFocusCell = true) {
if (commitChanges) {
onRowChange(row, true, shouldFocusCell);
} else {
closeEditor(shouldFocusCell);
}
}
function onEditorRowChange(row: R, commitChangesAndFocus = false) {
onRowChange(row, commitChangesAndFocus, commitChangesAndFocus);
}
const { cellClass } = column;
const className = getCellClassname(
column,
'rdg-editor-container',
!column.editorOptions?.displayCellContent && cellEditing,
typeof cellClass === 'function' ? cellClass(row) : cellClass
);
return (
<div
role="gridcell"
aria-colindex={column.idx + 1} // aria-colindex is 1-based
aria-colspan={colSpan}
aria-selected
className={className}
style={getCellStyle(column, colSpan)}
onKeyDown={handleKeyDown}
onMouseDownCapture={cancelTask}
>
{column.renderEditCell != null && (
<>
{column.renderEditCell({
column,
row,
rowIdx,
onRowChange: onEditorRowChange,
onClose
})}
{column.editorOptions?.displayCellContent &&
column.renderCell({
column,
row,
rowIdx,
isCellEditable: true,
tabIndex: -1,
onRowChange: onEditorRowChange
})}
</>
)}
</div>
);
}