-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChipsSearchInput.tsx
More file actions
318 lines (296 loc) · 8.13 KB
/
Copy pathChipsSearchInput.tsx
File metadata and controls
318 lines (296 loc) · 8.13 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import React, {
useState,
useRef,
useEffect,
useMemo,
type RefObject,
} from "react";
import { type NodeTypeConfig, hexToRgba } from "./types";
type Props = {
chips: string[];
setChips: (chips: string[]) => void;
value: string;
setValue: (v: string) => void;
types: NodeTypeConfig[];
inputRef: RefObject<HTMLInputElement>;
onArrowDown: () => void;
onArrowUp: () => void;
onEnter: () => void;
onShiftEnter: () => void;
onCmdEnter: () => void;
onEscape: () => void;
};
type Ghost = {
typeId: string;
full: string; // full alias string
};
const ChipsSearchInput = ({
chips,
setChips,
value,
setValue,
types,
inputRef,
onArrowDown,
onArrowUp,
onEnter,
onShiftEnter,
onCmdEnter,
onEscape,
}: Props) => {
const [focusedChip, setFocusedChip] = useState(-1);
const chipRefs = useRef<(HTMLSpanElement | null)[]>([]);
// Build lookup map once per types change — avoids repeated find() in render
const typesById = useMemo(
() => Object.fromEntries(types.map((t) => [t.id, t])),
[types],
);
// Focus chip element when focusedChip changes
useEffect(() => {
if (focusedChip >= 0 && chipRefs.current[focusedChip]) {
chipRefs.current[focusedChip]?.focus();
}
}, [focusedChip]);
// Clamp focusedChip when chip list shrinks
useEffect(() => {
if (focusedChip >= chips.length) setFocusedChip(-1);
}, [chips.length, focusedChip]);
const focusInput = () => {
setFocusedChip(-1);
setTimeout(() => inputRef.current?.focus(), 0);
};
// Ghost autocomplete: value prefix matches exactly one unselected alias
const ghost = useMemo((): Ghost | null => {
const v = value.trim().toLowerCase();
if (!v) return null;
const candidates: Ghost[] = [];
for (const t of types) {
if (chips.includes(t.id)) continue;
for (const alias of t.aliases) {
if (alias.toLowerCase().startsWith(v) && alias.toLowerCase() !== v) {
candidates.push({ typeId: t.id, full: alias });
break;
}
}
}
return candidates.length === 1 ? candidates[0] : null;
}, [value, types, chips]);
const tryConsumeAsTrigger = (word: string): boolean => {
const lower = word.toLowerCase();
const match = types.find(
(t) =>
!chips.includes(t.id) &&
t.aliases.some((a) => a.toLowerCase() === lower),
);
if (match) {
setChips([...chips, match.id]);
return true;
}
return false;
};
const removeChip = (idx: number) => chips.filter((_, i) => i !== idx);
const onChipKeyDown = (e: React.KeyboardEvent, idx: number) => {
if (e.key === "ArrowLeft") {
e.preventDefault();
if (idx > 0) setFocusedChip(idx - 1);
return;
}
if (e.key === "ArrowRight") {
e.preventDefault();
if (idx < chips.length - 1) setFocusedChip(idx + 1);
else focusInput();
return;
}
if (e.key === "Backspace" || e.key === "Delete") {
e.preventDefault();
const next = removeChip(idx);
setChips(next);
if (next.length === 0) {
focusInput();
return;
}
if (e.key === "Backspace") {
setFocusedChip(idx > 0 ? idx - 1 : 0);
} else {
if (idx >= next.length) focusInput();
else setFocusedChip(idx);
}
return;
}
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
const next = removeChip(idx);
setChips(next);
if (idx > 0 && next.length > 0)
setFocusedChip(Math.min(idx, next.length - 1));
else focusInput();
return;
}
if (e.key === "Escape") {
focusInput();
return;
}
if (e.key === "ArrowUp") {
e.preventDefault();
onArrowUp();
return;
}
if (e.key === "ArrowDown") {
e.preventDefault();
onArrowDown();
return;
}
// Printable char → return focus to input and let the keystroke land
if (e.key.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey) {
focusInput();
}
};
const onInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Tab") {
if (ghost) {
e.preventDefault();
setChips([...chips, ghost.typeId]);
setValue("");
}
return;
}
if (e.key === " ") {
// If input is a single token (no embedded spaces), try to convert to chip
if (!/\s/.test(value) && value.length > 0) {
if (tryConsumeAsTrigger(value)) {
e.preventDefault();
setValue("");
return;
}
}
}
if (e.key === "Backspace") {
const el = inputRef.current;
if (
el &&
el.selectionStart === 0 &&
el.selectionEnd === 0 &&
value.length === 0 &&
chips.length > 0
) {
e.preventDefault();
setFocusedChip(chips.length - 1);
return;
}
}
if (e.key === "ArrowLeft") {
const el = inputRef.current;
if (
el &&
el.selectionStart === 0 &&
el.selectionEnd === 0 &&
chips.length > 0
) {
e.preventDefault();
setFocusedChip(chips.length - 1);
return;
}
}
if (e.key === "ArrowDown") {
e.preventDefault();
onArrowDown();
return;
}
if (e.key === "ArrowUp") {
e.preventDefault();
onArrowUp();
return;
}
if (e.key === "Enter") {
e.preventDefault();
if (e.metaKey || e.ctrlKey) onCmdEnter();
else if (e.shiftKey) onShiftEnter();
else onEnter();
return;
}
if (e.key === "Escape") {
onEscape();
}
};
return (
<div className="dg-as-chips-input">
{chips.map((id, idx) => {
const t = typesById[id];
if (!t) return null;
const isFocused = focusedChip === idx;
const chipStyle: React.CSSProperties = {
background: hexToRgba(t.color, isFocused ? 0.18 : 0.08),
borderColor: hexToRgba(t.color, isFocused ? 1 : 0.3),
color: t.color,
boxShadow: isFocused
? `0 0 0 2px ${hexToRgba(t.color, 0.2)}`
: undefined,
};
return (
<span
key={id}
ref={(el) => {
chipRefs.current[idx] = el;
}}
className="dg-as-chip"
style={chipStyle}
tabIndex={-1}
role="button"
aria-label={`${t.label} filter — press Backspace or Delete to remove`}
onKeyDown={(e) => onChipKeyDown(e, idx)}
onClick={() => setFocusedChip(idx)}
>
<span className="dg-as-chip-dot" style={{ background: t.color }} />
<span>{t.label}</span>
<span
className="dg-as-chip-x"
role="button"
aria-label={`Remove ${t.label} filter`}
onClick={(e) => {
e.stopPropagation();
setChips(chips.filter((x) => x !== id));
focusInput();
}}
>
<svg
width="10"
height="10"
viewBox="0 0 10 10"
fill="none"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
>
<line x1="2" y1="2" x2="8" y2="8" />
<line x1="8" y1="2" x2="2" y2="8" />
</svg>
</span>
</span>
);
})}
<span className="dg-as-input-wrap">
{ghost && (
<span className="dg-as-ghost" aria-hidden="true">
<span className="dg-as-ghost-typed">{value}</span>
<span className="dg-as-ghost-completion">
{ghost.full.slice(value.length)}
</span>
<span className="dg-as-ghost-tabkey">tab</span>
</span>
)}
<input
ref={inputRef}
className="dg-as-search-input"
value={value}
onChange={(e) => setValue(e.target.value)}
onKeyDown={onInputKeyDown}
placeholder={chips.length === 0 ? "Search nodes" : ""}
autoFocus
spellCheck={false}
autoComplete="off"
/>
</span>
</div>
);
};
export default ChipsSearchInput;