1- import { useCallback , useEffect , useRef , useState } from "react" ;
1+ import { useCallback , useEffect , useRef } from "react" ;
22import { useSearchParams } from "react-router-dom" ;
33
44import { useAppContext } from "@contexts/AppContext" ;
@@ -13,8 +13,6 @@ const SearchInput = () => {
1313
1414 const inputRef = useRef < HTMLInputElement | null > ( null ) ;
1515
16- const [ inputVal , setInputVal ] = useState < string > ( "" ) ;
17-
1816 const handleSearchFieldClick = ( ) => {
1917 inputRef . current ?. focus ( ) ;
2018 } ;
@@ -27,7 +25,6 @@ const SearchInput = () => {
2725 } ;
2826
2927 const clearSearch = useCallback ( ( ) => {
30- setInputVal ( "" ) ;
3128 setSearchText ( "" ) ;
3229 searchParams . delete ( QueryParams . SEARCH ) ;
3330 setSearchParams ( searchParams ) ;
@@ -38,6 +35,7 @@ const SearchInput = () => {
3835 if ( e . key !== "Escape" ) {
3936 return ;
4037 }
38+
4139 // check if the input is focused
4240 if ( document . activeElement !== inputRef . current ) {
4341 return ;
@@ -55,12 +53,13 @@ const SearchInput = () => {
5553 if ( e . key !== "Enter" ) {
5654 return ;
5755 }
56+
5857 // check if the input is focused
5958 if ( document . activeElement !== inputRef . current ) {
6059 return ;
6160 }
6261
63- const formattedVal = inputVal . trim ( ) . toLowerCase ( ) ;
62+ const formattedVal = searchText . trim ( ) . toLowerCase ( ) || "" ;
6463
6564 setSearchText ( formattedVal ) ;
6665 if ( ! formattedVal ) {
@@ -71,7 +70,7 @@ const SearchInput = () => {
7170 setSearchParams ( searchParams ) ;
7271 }
7372 } ,
74- [ inputVal , searchParams , setSearchParams , setSearchText ]
73+ [ searchParams , searchText , setSearchParams , setSearchText ]
7574 ) ;
7675
7776 useEffect ( ( ) => {
@@ -87,11 +86,14 @@ const SearchInput = () => {
8786 } , [ handleEscapePress , handleReturnPress ] ) ;
8887
8988 /**
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
9190 */
9291 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+
9597 setSearchText ( searchQuery ) ;
9698 // eslint-disable-next-line react-hooks/exhaustive-deps
9799 } , [ ] ) ;
@@ -101,26 +103,20 @@ const SearchInput = () => {
101103 < SearchIcon />
102104 < input
103105 ref = { inputRef }
106+ value = { searchText }
104107 type = "search"
105108 id = "search"
106109 autoComplete = "off"
107- value = { inputVal }
108110 onChange = { ( e ) => {
109111 const newValue = e . target . value ;
110112 if ( ! newValue ) {
111113 clearSearch ( ) ;
112114 return ;
113115 }
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 ) ;
121117 } }
122118 />
123- { ! inputVal && ! searchText && (
119+ { ! searchText && (
124120 < label htmlFor = "search" >
125121 Type < kbd > /</ kbd > to search
126122 </ label >
0 commit comments