/
dropdown-cell.tsx
230 lines (209 loc) 路 7.73 KB
/
dropdown-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
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
import * as React from "react";
import { styled } from "@linaria/react";
import Select, { type MenuProps, components } from "react-select";
import {
type CustomCell,
type ProvideEditorCallback,
type CustomRenderer,
getMiddleCenterBias,
useTheme,
GridCellKind,
TextCellEntry,
} from "@glideapps/glide-data-grid";
interface CustomMenuProps extends MenuProps<any> {}
const CustomMenu: React.FC<CustomMenuProps> = p => {
const { Menu } = components;
const { children, ...rest } = p;
return <Menu {...rest}>{children}</Menu>;
};
type DropdownOption = string | { value: string; label: string } | undefined | null;
interface DropdownCellProps {
readonly kind: "dropdown-cell";
readonly value: string | undefined | null;
readonly allowedValues: readonly DropdownOption[];
}
export type DropdownCell = CustomCell<DropdownCellProps>;
const Wrap = styled.div`
display: flex;
flex-direction: column;
align-items: stretch;
.glide-select {
font-family: var(--gdg-font-family);
font-size: var(--gdg-editor-font-size);
}
`;
const PortalWrap = styled.div`
font-family: var(--gdg-font-family);
font-size: var(--gdg-editor-font-size);
color: var(--gdg-text-dark);
> div {
border-radius: 4px;
border: 1px solid var(--gdg-border-color);
}
`;
// This is required since the padding is disabled for this cell type
// The settings are based on the "pad" settings in the data-grid-overlay-editor-style.tsx
const ReadOnlyWrap = styled.div`
display: "flex";
margin: auto 8.5px;
padding-bottom: 3px;
`;
const Editor: ReturnType<ProvideEditorCallback<DropdownCell>> = p => {
const { value: cell, onFinishedEditing, initialValue } = p;
const { allowedValues, value: valueIn } = cell.data;
const [value, setValue] = React.useState(valueIn);
const [inputValue, setInputValue] = React.useState(initialValue ?? "");
const theme = useTheme();
const values = React.useMemo(() => {
return allowedValues.map(option => {
if (typeof option === "string" || option === null || option === undefined) {
return { value: option, label: option?.toString() ?? "" };
}
return option;
});
}, [allowedValues]);
if (cell.readonly) {
return (
<ReadOnlyWrap>
<TextCellEntry
highlight={true}
autoFocus={false}
disabled={true}
value={value ?? ""}
onChange={() => undefined}
/>
</ReadOnlyWrap>
);
}
return (
<Wrap>
<Select
className="glide-select"
inputValue={inputValue}
onInputChange={setInputValue}
menuPlacement={"auto"}
value={values.find(x => x.value === value)}
styles={{
control: base => ({
...base,
border: 0,
boxShadow: "none",
}),
option: (base, { isFocused }) => ({
...base,
fontSize: theme.editorFontSize,
fontFamily: theme.fontFamily,
cursor: isFocused ? "pointer" : undefined,
paddingLeft: theme.cellHorizontalPadding,
paddingRight: theme.cellHorizontalPadding,
":active": {
...base[":active"],
color: theme.accentFg,
},
// Add some content in case the option is empty
// so that the option height can be calculated correctly
":empty::after": {
content: '" "',
visibility: "hidden",
},
}),
}}
theme={t => {
return {
...t,
colors: {
...t.colors,
neutral0: theme.bgCell, // this is both the background color AND the fg color of
// the selected item because of course it is.
neutral5: theme.bgCell,
neutral10: theme.bgCell,
neutral20: theme.bgCellMedium,
neutral30: theme.bgCellMedium,
neutral40: theme.bgCellMedium,
neutral50: theme.textLight,
neutral60: theme.textMedium,
neutral70: theme.textMedium,
neutral80: theme.textDark,
neutral90: theme.textDark,
neutral100: theme.textDark,
primary: theme.accentColor,
primary75: theme.accentColor,
primary50: theme.accentColor,
primary25: theme.accentLight, // prelight color
},
};
}}
menuPortalTarget={document.getElementById("portal")}
autoFocus={true}
openMenuOnFocus={true}
components={{
DropdownIndicator: () => null,
IndicatorSeparator: () => null,
Menu: props => (
<PortalWrap>
<CustomMenu className={"click-outside-ignore"} {...props} />
</PortalWrap>
),
}}
options={values}
onChange={async e => {
if (e === null) return;
setValue(e.value);
await new Promise(r => window.requestAnimationFrame(r));
onFinishedEditing({
...cell,
data: {
...cell.data,
value: e.value,
},
});
}}
/>
</Wrap>
);
};
const renderer: CustomRenderer<DropdownCell> = {
kind: GridCellKind.Custom,
isMatch: (c): c is DropdownCell => (c.data as any).kind === "dropdown-cell",
draw: (args, cell) => {
const { ctx, theme, rect } = args;
const { value } = cell.data;
const foundOption = cell.data.allowedValues.find(opt => {
if (typeof opt === "string" || opt === null || opt === undefined) {
return opt === value;
}
return opt.value === value;
});
const displayText = typeof foundOption === "string" ? foundOption : foundOption?.label ?? "";
if (displayText) {
ctx.fillStyle = theme.textDark;
ctx.fillText(
displayText,
rect.x + theme.cellHorizontalPadding,
rect.y + rect.height / 2 + getMiddleCenterBias(ctx, theme)
);
}
return true;
},
measure: (ctx, cell, theme) => {
const { value } = cell.data;
return (value ? ctx.measureText(value).width : 0) + theme.cellHorizontalPadding * 2;
},
provideEditor: () => ({
editor: Editor,
disablePadding: true,
deletedValue: v => ({
...v,
copyData: "",
data: {
...v.data,
value: "",
},
}),
}),
onPaste: (v, d) => ({
...d,
value: d.allowedValues.includes(v) ? v : d.value,
}),
};
export default renderer;