Skip to content

Commit 667439a

Browse files
committed
[frontend] add focus logic for search queries
1 parent 7ca2182 commit 667439a

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

frontend/src/Omnibox.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ export const UrlInput: Component<
2828
active: boolean;
2929
input: HTMLInputElement;
3030

31+
focusindex: number;
32+
3133
overflowItems: string[];
3234
}
3335
> = function (cx) {
36+
this.focusindex = 0;
3437
this.value = "";
3538
this.overflowItems = ["test", "test2", "test3", "test4", "test5"];
3639
const fetchSuggestions = async () => {
37-
console.log("fetched");
3840
let resp = await client.fetch(
39-
`http://suggestqueries.google.com/complete/search?client=firefox&q=${encodeURIComponent(this.input.value)}`
41+
`http://suggestqueries.google.com/complete/search?client=chrome&q=${encodeURIComponent(this.input.value)}`
4042
);
4143
let json = await resp.json();
4244
console.log(json);
@@ -46,6 +48,10 @@ export const UrlInput: Component<
4648
let ratelimiting = false;
4749
let interval = 100;
4850
use(this.value).listen(() => {
51+
if (!this.value) {
52+
this.overflowItems = [];
53+
return;
54+
}
4955
if (ratelimiting) {
5056
if (currentTimeout) return;
5157
// TODO: why is it using the node types here
@@ -78,10 +84,13 @@ export const UrlInput: Component<
7884
<div
7985
class="overflowitem"
8086
on:click={() => {
81-
this.input.value = item;
87+
this.value = item;
8288
this.active = false;
8389
this.input.blur();
8490
}}
91+
class:focused={use(this.focusindex).map(
92+
(i) => i - 1 === this.overflowItems.indexOf(item)
93+
)}
8594
>
8695
<IconButton icon={iconSearch}></IconButton>
8796
<span>{item}</span>
@@ -93,8 +102,30 @@ export const UrlInput: Component<
93102
<input
94103
this={use(this.input).bind()}
95104
value={use(this.value).bind()}
105+
on:keydown={(e: KeyboardEvent) => {
106+
if (e.key === "ArrowDown") {
107+
e.preventDefault();
108+
this.active = true;
109+
this.focusindex++;
110+
if (this.focusindex > this.overflowItems.length) {
111+
this.focusindex = 0;
112+
}
113+
}
114+
if (e.key === "ArrowUp") {
115+
e.preventDefault();
116+
this.active = true;
117+
this.focusindex--;
118+
if (this.focusindex < 0) {
119+
this.focusindex = this.overflowItems.length;
120+
}
121+
}
122+
if (e.key === "Enter") {
123+
e.preventDefault();
124+
}
125+
}}
96126
on:input={(e: InputEvent) => {
97-
this.value = (e.target as HTMLInputElement).value;
127+
this.value = this.input.value;
128+
this.focusindex = 0;
98129
}}
99130
></input>
100131

@@ -128,6 +159,9 @@ UrlInput.css = `
128159
height: 2.5em;
129160
cursor: pointer;
130161
}
162+
.overflowitem.focused {
163+
background: blue;
164+
}
131165
132166
.overflow.active {
133167
display: block;

0 commit comments

Comments
 (0)