Skip to content

Commit 259a352

Browse files
committed
fix arrow up and down
1 parent 2a9c204 commit 259a352

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

clients/search-component/src/TrieveModal/index.tsx

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { act, useEffect, useState } from "react";
1+
import React, { act, useEffect, useRef, useState } from "react";
22
import { SearchChunksReqPayload, TrieveSDK } from "trieve-ts-sdk";
33
import { Chunk } from "../utils/types";
44
import * as Dialog from "@radix-ui/react-dialog";
@@ -32,6 +32,7 @@ export const TrieveModalSearch = ({
3232
const [query, setQuery] = useState("");
3333
const [results, setResults] = useState<ChunkWithHighlights[]>([]);
3434
const [open, setOpen] = useState(false);
35+
const inputRef = useRef<HTMLInputElement>(null);
3536

3637
const search = async () => {
3738
const results = await trieve.search({
@@ -68,19 +69,16 @@ export const TrieveModalSearch = ({
6869
};
6970

7071
const onUpOrDownClicked = (index: number, code: string) => {
71-
console.log("clicked");
7272
if (code === "ArrowDown") {
73-
document
74-
.getElementsByClassName("trieve-elements-search")[0]
75-
.getElementsByClassName("item")
76-
[index + 1]?.focus();
73+
index < results.length - 1
74+
? document.getElementById(`trieve-search-item-${index + 1}`)?.focus()
75+
: document.getElementById(`trieve-search-item-0`)?.focus();
7776
}
7877

7978
if (code === "ArrowUp") {
80-
document
81-
.getElementsByClassName("trieve-elements-search")[0]
82-
.getElementsByClassName("item")
83-
[index - 1]?.focus();
79+
index > 0
80+
? document.getElementById(`trieve-search-item-${index - 1}`)?.focus()
81+
: inputRef.current?.focus();
8482
}
8583
};
8684

@@ -89,7 +87,7 @@ export const TrieveModalSearch = ({
8987
return () => {
9088
document.removeEventListener("keydown", checkForCMDK);
9189
};
92-
});
90+
}, []);
9391

9492
return (
9593
<Dialog.Root open={open} onOpenChange={setOpen}>
@@ -143,6 +141,7 @@ export const TrieveModalSearch = ({
143141
<path d="m21 21-4.3-4.3"></path>
144142
</svg>
145143
<input
144+
ref={inputRef}
146145
value={query}
147146
onChange={(e) => setQuery(e.target.value)}
148147
placeholder={placeholder || "sup"}

clients/search-component/src/TrieveModal/item.tsx

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChunkWithHighlights } from ".";
22
import { Chunk } from "../utils/types";
3-
import React, { useEffect, useRef } from "react";
3+
import React, { useCallback, useEffect, useRef } from "react";
44

55
type Props = {
66
item: ChunkWithHighlights;
@@ -24,26 +24,28 @@ export const Item = ({
2424
item.chunk.metadata?.page_title ||
2525
item.chunk.metadata?.name;
2626

27-
const checkForUpAndDown = (e: KeyboardEvent) => {
28-
if (
29-
(e.code === "ArrowDown" || e.code === "ArrowUp") &&
30-
itemRef.current === document.activeElement
31-
) {
32-
onUpOrDownClicked(index, e.code);
33-
}
34-
};
27+
const checkForUpAndDown = useCallback(
28+
(e: KeyboardEvent) => {
29+
if (e.code === "ArrowDown" || e.code === "ArrowUp") {
30+
e.preventDefault();
31+
onUpOrDownClicked(index, e.code);
32+
}
33+
},
34+
[item]
35+
);
3536

3637
useEffect(() => {
37-
document.addEventListener("keydown", checkForUpAndDown);
38+
itemRef.current.addEventListener("keydown", checkForUpAndDown, false);
3839
return () => {
39-
document.removeEventListener("keydown", checkForUpAndDown);
40+
itemRef.current.removeEventListener("keydown", checkForUpAndDown, false);
4041
};
41-
});
42+
}, []);
4243

4344
return (
4445
<li>
4546
<Component
4647
ref={itemRef}
48+
id={`trieve-search-item-${index}`}
4749
className="item"
4850
onClick={() => onResultClick && onResultClick(item.chunk)}
4951
{...(item.chunk.link ? { href: item.chunk.link } : {})}
@@ -75,9 +77,9 @@ export const Item = ({
7577
viewBox="0 0 24 24"
7678
fill="none"
7779
stroke="currentColor"
78-
stroke-width="2"
79-
stroke-linecap="round"
80-
stroke-linejoin="round"
80+
strokeWidth="2"
81+
strokeLinecap="round"
82+
strokeLinejoin="round"
8183
>
8284
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
8385
<path d="M9 6l6 6l-6 6" />

0 commit comments

Comments
 (0)