1
- import React , { act , useEffect , useState } from "react" ;
1
+ import React , { act , useEffect , useRef , useState } from "react" ;
2
2
import { SearchChunksReqPayload , TrieveSDK } from "trieve-ts-sdk" ;
3
3
import { Chunk } from "../utils/types" ;
4
4
import * as Dialog from "@radix-ui/react-dialog" ;
@@ -32,6 +32,7 @@ export const TrieveModalSearch = ({
32
32
const [ query , setQuery ] = useState ( "" ) ;
33
33
const [ results , setResults ] = useState < ChunkWithHighlights [ ] > ( [ ] ) ;
34
34
const [ open , setOpen ] = useState ( false ) ;
35
+ const inputRef = useRef < HTMLInputElement > ( null ) ;
35
36
36
37
const search = async ( ) => {
37
38
const results = await trieve . search ( {
@@ -68,19 +69,16 @@ export const TrieveModalSearch = ({
68
69
} ;
69
70
70
71
const onUpOrDownClicked = ( index : number , code : string ) => {
71
- console . log ( "clicked" ) ;
72
72
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 ( ) ;
77
76
}
78
77
79
78
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 ( ) ;
84
82
}
85
83
} ;
86
84
@@ -89,7 +87,7 @@ export const TrieveModalSearch = ({
89
87
return ( ) => {
90
88
document . removeEventListener ( "keydown" , checkForCMDK ) ;
91
89
} ;
92
- } ) ;
90
+ } , [ ] ) ;
93
91
94
92
return (
95
93
< Dialog . Root open = { open } onOpenChange = { setOpen } >
@@ -143,6 +141,7 @@ export const TrieveModalSearch = ({
143
141
< path d = "m21 21-4.3-4.3" > </ path >
144
142
</ svg >
145
143
< input
144
+ ref = { inputRef }
146
145
value = { query }
147
146
onChange = { ( e ) => setQuery ( e . target . value ) }
148
147
placeholder = { placeholder || "sup" }
0 commit comments