1
- import { forwardRef , useState , useCallback , useMemo , useEffect , Ref } from 'react' ;
1
+ import React , { forwardRef , useState , useCallback , useMemo , useEffect , useRef } from 'react' ;
2
2
import debounce from 'lodash/debounce' ;
3
+ import { useRecoilState } from 'recoil' ;
3
4
import { Search , X } from 'lucide-react' ;
4
- import { useSetRecoilState , useRecoilValue } from 'recoil' ;
5
5
import { QueryKeys } from 'librechat-data-provider' ;
6
6
import { useQueryClient } from '@tanstack/react-query' ;
7
7
import { useLocation , useNavigate } from 'react-router-dom' ;
@@ -13,19 +13,19 @@ type SearchBarProps = {
13
13
isSmallScreen ?: boolean ;
14
14
} ;
15
15
16
- const SearchBar = forwardRef ( ( props : SearchBarProps , ref : Ref < HTMLDivElement > ) => {
16
+ const SearchBar = forwardRef ( ( props : SearchBarProps , ref : React . Ref < HTMLDivElement > ) => {
17
17
const localize = useLocalize ( ) ;
18
18
const location = useLocation ( ) ;
19
19
const queryClient = useQueryClient ( ) ;
20
20
const navigate = useNavigate ( ) ;
21
21
const { isSmallScreen } = props ;
22
22
23
23
const [ text , setText ] = useState ( '' ) ;
24
+ const inputRef = useRef < HTMLInputElement > ( null ) ;
24
25
const [ showClearIcon , setShowClearIcon ] = useState ( false ) ;
25
26
26
27
const { newConversation } = useNewConvo ( ) ;
27
- const setSearchState = useSetRecoilState ( store . search ) ;
28
- const search = useRecoilValue ( store . search ) ;
28
+ const [ search , setSearchState ] = useRecoilState ( store . search ) ;
29
29
30
30
const clearSearch = useCallback ( ( ) => {
31
31
if ( location . pathname . includes ( '/search' ) ) {
@@ -44,6 +44,7 @@ const SearchBar = forwardRef((props: SearchBarProps, ref: Ref<HTMLDivElement>) =
44
44
isTyping : false ,
45
45
} ) ) ;
46
46
clearSearch ( ) ;
47
+ inputRef . current ?. focus ( ) ;
47
48
} , [ setSearchState , clearSearch ] ) ;
48
49
49
50
const handleKeyUp = ( e : React . KeyboardEvent < HTMLInputElement > ) => {
@@ -108,6 +109,7 @@ const SearchBar = forwardRef((props: SearchBarProps, ref: Ref<HTMLDivElement>) =
108
109
< Search className = "absolute left-3 h-4 w-4 text-text-secondary group-focus-within:text-text-primary group-hover:text-text-primary" />
109
110
< input
110
111
type = "text"
112
+ ref = { inputRef }
111
113
className = "m-0 mr-0 w-full border-none bg-transparent p-0 pl-7 text-sm leading-tight placeholder-text-secondary placeholder-opacity-100 focus-visible:outline-none group-focus-within:placeholder-text-primary group-hover:placeholder-text-primary"
112
114
value = { text }
113
115
onChange = { onChange }
@@ -122,14 +124,20 @@ const SearchBar = forwardRef((props: SearchBarProps, ref: Ref<HTMLDivElement>) =
122
124
autoComplete = "off"
123
125
dir = "auto"
124
126
/>
125
- < X
127
+ < button
128
+ type = "button"
129
+ aria-label = { `${ localize ( 'com_ui_clear' ) } ${ localize ( 'com_ui_search' ) } ` }
126
130
className = { cn (
127
- 'absolute right-[7px] h-5 w-5 cursor-pointer transition-opacity duration-200' ,
131
+ 'absolute right-[7px] flex h-5 w-5 items-center justify-center rounded-full border-none bg-transparent p-0 transition-opacity duration-200' ,
128
132
showClearIcon ? 'opacity-100' : 'opacity-0' ,
129
133
isSmallScreen === true ? 'right-[16px]' : '' ,
130
134
) }
131
135
onClick = { clearText }
132
- />
136
+ tabIndex = { showClearIcon ? 0 : - 1 }
137
+ disabled = { ! showClearIcon }
138
+ >
139
+ < X className = "h-5 w-5 cursor-pointer" />
140
+ </ button >
133
141
</ div >
134
142
) ;
135
143
} ) ;
0 commit comments