1
- import { useCallback , useEffect , useRef , useState } from "react" ;
1
+ import { useCallback , useEffect , useRef } from "react" ;
2
2
import { useSearchParams } from "react-router-dom" ;
3
3
4
4
import { useAppContext } from "@contexts/AppContext" ;
@@ -13,8 +13,6 @@ const SearchInput = () => {
13
13
14
14
const inputRef = useRef < HTMLInputElement | null > ( null ) ;
15
15
16
- const [ inputVal , setInputVal ] = useState < string > ( "" ) ;
17
-
18
16
const handleSearchFieldClick = ( ) => {
19
17
inputRef . current ?. focus ( ) ;
20
18
} ;
@@ -27,7 +25,6 @@ const SearchInput = () => {
27
25
} ;
28
26
29
27
const clearSearch = useCallback ( ( ) => {
30
- setInputVal ( "" ) ;
31
28
setSearchText ( "" ) ;
32
29
searchParams . delete ( QueryParams . SEARCH ) ;
33
30
setSearchParams ( searchParams ) ;
@@ -38,6 +35,7 @@ const SearchInput = () => {
38
35
if ( e . key !== "Escape" ) {
39
36
return ;
40
37
}
38
+
41
39
// check if the input is focused
42
40
if ( document . activeElement !== inputRef . current ) {
43
41
return ;
@@ -55,12 +53,13 @@ const SearchInput = () => {
55
53
if ( e . key !== "Enter" ) {
56
54
return ;
57
55
}
56
+
58
57
// check if the input is focused
59
58
if ( document . activeElement !== inputRef . current ) {
60
59
return ;
61
60
}
62
61
63
- const formattedVal = inputVal . trim ( ) . toLowerCase ( ) ;
62
+ const formattedVal = searchText . trim ( ) . toLowerCase ( ) || "" ;
64
63
65
64
setSearchText ( formattedVal ) ;
66
65
if ( ! formattedVal ) {
@@ -71,7 +70,7 @@ const SearchInput = () => {
71
70
setSearchParams ( searchParams ) ;
72
71
}
73
72
} ,
74
- [ inputVal , searchParams , setSearchParams , setSearchText ]
73
+ [ searchParams , searchText , setSearchParams , setSearchText ]
75
74
) ;
76
75
77
76
useEffect ( ( ) => {
@@ -87,11 +86,14 @@ const SearchInput = () => {
87
86
} , [ handleEscapePress , handleReturnPress ] ) ;
88
87
89
88
/**
90
- * Set the input value and search text to the search query from the URL
89
+ * Set the search text to the search query from the URL
91
90
*/
92
91
useEffect ( ( ) => {
93
- const searchQuery = searchParams . get ( QueryParams . SEARCH ) || "" ;
94
- setInputVal ( searchQuery ) ;
92
+ const searchQuery = searchParams . get ( QueryParams . SEARCH ) ;
93
+ if ( ! searchQuery ) {
94
+ return ;
95
+ }
96
+
95
97
setSearchText ( searchQuery ) ;
96
98
// eslint-disable-next-line react-hooks/exhaustive-deps
97
99
} , [ ] ) ;
@@ -101,26 +103,20 @@ const SearchInput = () => {
101
103
< SearchIcon />
102
104
< input
103
105
ref = { inputRef }
106
+ value = { searchText }
104
107
type = "search"
105
108
id = "search"
106
109
autoComplete = "off"
107
- value = { inputVal }
108
110
onChange = { ( e ) => {
109
111
const newValue = e . target . value ;
110
112
if ( ! newValue ) {
111
113
clearSearch ( ) ;
112
114
return ;
113
115
}
114
- setInputVal ( newValue ) ;
115
- } }
116
- onBlur = { ( ) => {
117
- // ensure the input value is always in sync with the search text
118
- if ( inputVal !== searchText ) {
119
- setInputVal ( searchText ) ;
120
- }
116
+ setSearchText ( newValue ) ;
121
117
} }
122
118
/>
123
- { ! inputVal && ! searchText && (
119
+ { ! searchText && (
124
120
< label htmlFor = "search" >
125
121
Type < kbd > /</ kbd > to search
126
122
</ label >
0 commit comments