@@ -9,12 +9,15 @@ function SearchBarChild({ initialSubjects }: { initialSubjects: string[] }) {
99 const router = useRouter ( ) ;
1010 const [ searchText , setSearchText ] = useState ( "" ) ;
1111 const [ suggestions , setSuggestions ] = useState < string [ ] > ( [ ] ) ;
12+ const [ isSearching , setIsSearching ] = useState ( false ) ;
1213 const [ subjects ] = useState < string [ ] > ( initialSubjects ) ;
1314 const suggestionsRef = useRef < HTMLUListElement | null > ( null ) ;
15+ const inputRef = useRef < HTMLInputElement | null > ( null ) ;
1416
1517 const handleSearchChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
1618 const text = e . target . value ;
1719 setSearchText ( text ) ;
20+ setIsSearching ( true ) ;
1821
1922 if ( text . length > 1 && subjects . length > 0 ) {
2023 const filteredSuggestions = subjects . filter ( ( subject ) =>
@@ -29,15 +32,18 @@ function SearchBarChild({ initialSubjects }: { initialSubjects: string[] }) {
2932 const handleSelectSuggestion = ( suggestion : string ) => {
3033 setSearchText ( suggestion ) ;
3134 setSuggestions ( [ ] ) ;
35+ setIsSearching ( false ) ;
3236 router . push ( `/catalogue?subject=${ encodeURIComponent ( suggestion ) } ` ) ;
3337 } ;
3438
3539 const handleClickOutside = ( event : MouseEvent ) => {
3640 if (
3741 suggestionsRef . current &&
38- ! suggestionsRef . current . contains ( event . target as Node )
42+ ! suggestionsRef . current . contains ( event . target as Node ) &&
43+ ! inputRef . current ?. contains ( event . target as Node )
3944 ) {
4045 setSuggestions ( [ ] ) ;
46+ setIsSearching ( false ) ;
4147 }
4248 } ;
4349
@@ -55,15 +61,18 @@ function SearchBarChild({ initialSubjects }: { initialSubjects: string[] }) {
5561 onSubmit = { ( e ) => {
5662 e . preventDefault ( ) ;
5763 if ( searchText ) {
64+ setIsSearching ( false ) ;
5865 router . push ( `/catalogue?subject=${ encodeURIComponent ( searchText ) } ` ) ;
5966 }
6067 } }
6168 >
6269 < div className = "relative" >
6370 < Input
71+ ref = { inputRef }
6472 type = "text"
6573 value = { searchText }
6674 onChange = { handleSearchChange }
75+ onFocus = { ( ) => setIsSearching ( true ) }
6776 placeholder = "Search by subject..."
6877 className = "text-md w-full rounded-full border bg-[#434dba] px-4 py-6 pr-10 font-sans tracking-wider text-white shadow-sm placeholder:text-white focus:outline-none focus:ring-2"
6978 />
@@ -73,29 +82,30 @@ function SearchBarChild({ initialSubjects }: { initialSubjects: string[] }) {
7382 >
7483 < Search className = "h-5 w-5 text-white" />
7584 </ button >
76- { ( suggestions . length > 0 ||
77- ( searchText . length > 1 && subjects . length > 0 ) ) && (
78- < ul
79- ref = { suggestionsRef }
80- className = "absolute z-20 mx-0.5 mt-2 w-full max-w-xl rounded-md border border-[#434dba] bg-white text-center shadow-lg dark:bg-[#030712] md:mx-0"
81- >
82- { suggestions . length > 0 ? (
83- suggestions . map ( ( suggestion , index ) => (
84- < li
85- key = { index }
86- onClick = { ( ) => handleSelectSuggestion ( suggestion ) }
87- className = "cursor-pointer truncate p-2 hover:bg-gray-100 dark:hover:bg-gray-800"
88- >
89- { suggestion }
85+ { isSearching &&
86+ ( suggestions . length > 0 ||
87+ ( searchText . length > 1 && subjects . length > 0 ) ) && (
88+ < ul
89+ ref = { suggestionsRef }
90+ className = "absolute z-20 mx-0.5 mt-2 w-full max-w-xl rounded-md border border-[#434dba] bg-white text-center shadow-lg dark:bg-[#030712] md:mx-0"
91+ >
92+ { suggestions . length > 0 ? (
93+ suggestions . map ( ( suggestion , index ) => (
94+ < li
95+ key = { index }
96+ onClick = { ( ) => handleSelectSuggestion ( suggestion ) }
97+ className = "cursor-pointer truncate p-2 hover:bg-gray-100 dark:hover:bg-gray-800"
98+ >
99+ { suggestion }
100+ </ li >
101+ ) )
102+ ) : (
103+ < li className = "p-2 text-gray-500 dark:text-gray-400" >
104+ No subjects found
90105 </ li >
91- ) )
92- ) : (
93- < li className = "p-2 text-gray-500 dark:text-gray-400" >
94- No subjects found
95- </ li >
96- ) }
97- </ ul >
98- ) }
106+ ) }
107+ </ ul >
108+ ) }
99109 </ div >
100110 </ form >
101111 </ div >
0 commit comments