-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataCell.tsx
218 lines (208 loc) · 7.2 KB
/
DataCell.tsx
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
import { InputBase, makeStyles, TableCell, Theme } from "@material-ui/core";
import React, { CSSProperties, useContext } from "react";
import {
DragSelectionContext,
EditingContext,
EditorValueContext,
IsDraggingContext,
SelectedContext,
SetDragSelectionContext,
SetEditorValueContext,
SetIsDraggingContext,
SetSelectedContext,
useEnterEditing,
useExitEditing,
useGetCellId
} from "./state";
import {
cellStyles,
ZERO_WIDTH_SPACE,
CELL_UNIT_WIDTH,
CELL_UNIT_HEIGHT
} from "./Cell";
const useStyles = makeStyles((theme) => ({
cell: cellStyles(theme),
selectedCell: {
// Hopefully no one is using this with 9999 columns
// For some reason TypeScript hates this being a string so I have to do this
"z-index": "9999 !important"
},
eventCatcher: {
"z-index": "10000 !important",
maxWidth: "inherit !important",
width: CELL_UNIT_WIDTH + "px",
height: CELL_UNIT_HEIGHT + "px",
backgroundColor: "rgba(0,0,0,0)",
cursor: "cell"
},
selectedEventCatcher: {
outline: "2px solid " + theme.palette.primary.main,
outlineOffset: "-2px"
},
editingEventCatcher: {
"z-index": "10002 !important",
pointerEvents: "none"
},
eventCatcherCell: {
pointerEvents: "none"
},
cellEditor: {
"z-index": "10001 !important"
}
}));
const shiftEC = (style: CSSProperties) => ({
...style,
left: Number(style.left) + 0.5
});
const useEditorStyles = makeStyles<Theme, { editingWidth: number }>(
(theme) => ({
cellEditorInput: (props) => ({
backgroundColor: theme.palette.background.default,
padding: "3px 0",
height: "0px",
width: props.editingWidth - 9 + "px",
"& > *": {
paddingTop: 0,
paddingBottom: 0,
fontSize: "0.875rem"
}
})
})
);
// Separate function to only call useContext when necessary
function CellEditor(props: { style: CSSProperties }) {
const styles = useStyles();
const editorValue = useContext(EditorValueContext);
const setEditorValue = useContext(SetEditorValueContext);
const editing = useContext(EditingContext);
const editorStyles = useEditorStyles({ editingWidth: editing?.w ?? 0 });
return (
<td
className={
styles.cellEditor +
" " +
styles.cell +
" " +
styles.selectedCell
}
style={props.style}
>
<InputBase
className={editorStyles.cellEditorInput}
value={editorValue}
onChange={(e) => {
setEditorValue(e.target.value);
}}
autoFocus
/>
</td>
);
}
export default function DataCellRenderer(props: {
style: CSSProperties;
item: string;
row: number;
col: number;
isScrolling: boolean;
}) {
const { style, item, row, col, isScrolling } = props;
const styles = useStyles();
const enterEditing = useEnterEditing();
const exitEditing = useExitEditing();
const editing = useContext(EditingContext);
const selected = useContext(SelectedContext);
const setSelected = useContext(SetSelectedContext);
const getCellId = useGetCellId();
const dragSelection = useContext(DragSelectionContext);
const setDragSelection = useContext(SetDragSelectionContext);
const isDragging = useContext(IsDraggingContext);
const setIsDragging = useContext(SetIsDraggingContext);
const thisIs = (a: number[] | undefined) =>
a && a[0] === row && a[1] === col;
const isSelected = thisIs(selected);
const isEditing = thisIs(editing?.c);
const cellClickHandler = isScrolling
? undefined
: (
e: React.MouseEvent<
| HTMLDivElement
| HTMLTableHeaderCellElement
| HTMLTableDataCellElement,
globalThis.MouseEvent
>
) => {
// e.detail is the number of clicks (2 is double click)
setDragSelection(undefined);
switch (e.detail) {
case 1: {
exitEditing();
setSelected([row, col]);
break;
}
case 2: {
enterEditing();
}
}
};
const useEventCatcher = !isScrolling || isSelected;
return (
<React.Fragment key={row + "," + col}>
{
// Event catcher so that you can click on cells underneath the current cell if it is overflowing
// Also provides the outline around the cell
useEventCatcher && (
<div
className={
styles.eventCatcher +
(isSelected
? " " + styles.selectedEventCatcher
: "") +
(isEditing ? " " + styles.editingEventCatcher : "")
}
style={shiftEC(style)}
onClick={isEditing ? undefined : cellClickHandler}
onMouseMove={(e) => {
if(e.buttons === 1 && (!isDragging || !dragSelection || (dragSelection[1][0] !== row || dragSelection[1][1] !== col))) {
if(!isDragging) {
setIsDragging(true);
setSelected([row, col]);
}
setDragSelection([(isDragging ? dragSelection?.[0] : undefined) ?? [row, col], [row, col]]);
}
}}
onMouseUp={() => {
if(isDragging) {
setDragSelection([dragSelection?.[0] ?? [row, col], [row, col]]);
setIsDragging(false);
}
}}
/>
)
}
{!isScrolling && isEditing ? (
<CellEditor style={style} />
) : (
<>
<TableCell
className={
styles.cell +
" " +
(isSelected ? " " + styles.selectedCell : "") +
(useEventCatcher
? " " + styles.eventCatcherCell
: "")
}
style={style}
id={getCellId(row, col)}
onClick={isSelected ? undefined : cellClickHandler}
>
{item === ""
? // Zero-width space to make sure the cell height is correct
ZERO_WIDTH_SPACE
: item}
</TableCell>
</>
)}
</React.Fragment>
);
}