-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.tsx
163 lines (146 loc) · 4.7 KB
/
Cell.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
import { makeStyles, TableCell, Theme } from "@material-ui/core";
import type { CSSProperties as MuiCSSProperties } from "@material-ui/styles";
import React, { CSSProperties, useContext, useMemo } from "react";
import type { GridChildComponentProps } from "react-window";
import DataCellRenderer from "./DataCell";
import RowAdder from "./RowAdder";
import { SelectedContext, useData } from "./state";
export const CELL_BORDER_COLOR = (theme: Theme) => theme.palette.type === "dark" ? "#4d5155" : "rgb(231, 231, 231)";
export const CELL_UNIT_WIDTH = 74;
export const CELL_UNIT_HEIGHT = 28;
export const cellStyles: {(theme: Theme): MuiCSSProperties} = (theme: Theme) => ({
cursor: "cell",
padding: "0.25rem",
textAlign: "right",
userSelect: "none",
boxSizing: "border-box",
minWidth: CELL_UNIT_WIDTH + "px",
height: CELL_UNIT_HEIGHT + "px",
border: "0.5px solid " + CELL_BORDER_COLOR(theme),
color: theme.palette.text.primary,
backgroundColor: theme.palette.background.default,
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
borderRight: "none",
borderLeft: "1px solid " + CELL_BORDER_COLOR(theme)
});
export const HEADER_CELL_BG = (theme: Theme) => theme.palette.type === "dark" ? "#464749" : "#dedede";
const useStyles = makeStyles((theme) => ({
cell: cellStyles(theme),
headerCell: {
textAlign: "center",
backgroundColor: HEADER_CELL_BG(theme),
cursor: "initial"
},
selectedHeaderCell: {
backgroundColor: theme.palette.type === "dark" ? "#73757c" : "#cacaca"
},
headerRow: {
height: "29px"
}
}));
export const ZERO_WIDTH_SPACE = "";
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numberToAlphabet = (i: number) =>
ALPHABET[i % ALPHABET.length].repeat(((i / 26) >> 0) + 1);
const processVirtualStyles = (style: CSSProperties) =>
Object.fromEntries(
Object.entries(style).filter(
([key]) => key !== "width" && key !== "height"
)
);
export default function CellRenderer({
columnIndex,
rowIndex,
style,
isScrolling,
data: nRows
}: GridChildComponentProps<number>) {
const selected = useContext(SelectedContext);
const styles = useStyles();
const headerCell = (selected: boolean) =>
styles.headerCell +
" " +
styles.cell +
(selected ? " " + styles.selectedHeaderCell : "");
return rowIndex === 0 ? (
columnIndex === 0 ? (
<TableCell
style={processVirtualStyles(style)}
className={headerCell(false)}
// Fix for weird positioning bug
>
{ZERO_WIDTH_SPACE}
</TableCell>
) : (
<TableCell
style={processVirtualStyles(style)}
className={headerCell(selected[1] === columnIndex - 1)}
>
{numberToAlphabet(columnIndex - 1)}
</TableCell>
)
) : rowIndex === nRows + 1 ? (
columnIndex === 0 ? (
<RowAdder style={style} />
) : null
) : columnIndex === 0 ? (
<TableCell
style={processVirtualStyles(style)}
className={headerCell(selected[0] === rowIndex - 1)}
>
{rowIndex}
</TableCell>
) : (
<DataCellWrapper
style={style}
row={rowIndex - 1}
col={columnIndex - 1}
isScrolling={isScrolling}
/>
);
}
function DataCellWrapper({
style,
row,
col,
isScrolling
}: {
style: CSSProperties;
row: number;
col: number;
isScrolling: boolean | undefined;
}) {
const data = useData();
const selected = useContext(SelectedContext);
const isSelected = selected[0] === row && selected[1] === col;
const processedStyles = useMemo(() => {
const newStyle = processVirtualStyles(style);
const indexFromEnd = data[0].length - col - 1;
if (isSelected) {
newStyle.maxWidth = (indexFromEnd + 1) * CELL_UNIT_WIDTH;
} else {
// Find the next cell in the row with data
newStyle.maxWidth = 0;
let i = col + 1;
for (; i < data[row].length; i++) {
if (data[row][i].trim().length > 0) {
break;
}
}
newStyle.maxWidth += (i - col) * CELL_UNIT_WIDTH;
}
newStyle.zIndex = indexFromEnd;
return newStyle;
}, [style, isSelected, [...data[row]]]);
return (
<DataCellRenderer
style={processedStyles}
item={data[row][col]}
row={row}
col={col}
isScrolling={isScrolling ?? false}
/>
);
}