|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useRef, useEffect } from "react"; |
| 4 | +import { Search } from "lucide-react"; |
| 5 | +import { useRouter } from "next/navigation"; |
| 6 | +import { Input } from "@/components/ui/input"; |
| 7 | + |
| 8 | +function SearchBarChild({ initialSubjects }: { initialSubjects: string[] }) { |
| 9 | + const router = useRouter(); |
| 10 | + const [searchText, setSearchText] = useState(""); |
| 11 | + const [suggestions, setSuggestions] = useState<string[]>([]); |
| 12 | + const [isSearching, setIsSearching] = useState(false); |
| 13 | + const [subjects] = useState<string[]>(initialSubjects); |
| 14 | + const suggestionsRef = useRef<HTMLUListElement | null>(null); |
| 15 | + const inputRef = useRef<HTMLInputElement | null>(null); |
| 16 | + |
| 17 | + const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 18 | + const text = e.target.value; |
| 19 | + setSearchText(text); |
| 20 | + setIsSearching(true); |
| 21 | + |
| 22 | + if (text.length > 1 && subjects.length > 0) { |
| 23 | + const filteredSuggestions = subjects.filter((subject) => |
| 24 | + subject.toLowerCase().includes(text.toLowerCase()), |
| 25 | + ); |
| 26 | + setSuggestions(filteredSuggestions); |
| 27 | + } else { |
| 28 | + setSuggestions([]); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + const handleSelectSuggestion = (suggestion: string) => { |
| 33 | + setSearchText(suggestion); |
| 34 | + setSuggestions([]); |
| 35 | + setIsSearching(false); |
| 36 | + router.push(`/catalogue?subject=${encodeURIComponent(suggestion)}`); |
| 37 | + }; |
| 38 | + |
| 39 | + const handleClickOutside = (event: MouseEvent) => { |
| 40 | + if ( |
| 41 | + suggestionsRef.current && |
| 42 | + !suggestionsRef.current.contains(event.target as Node) && |
| 43 | + !inputRef.current?.contains(event.target as Node) |
| 44 | + ) { |
| 45 | + setSuggestions([]); |
| 46 | + setIsSearching(false); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + document.addEventListener("mousedown", handleClickOutside); |
| 52 | + return () => { |
| 53 | + document.removeEventListener("mousedown", handleClickOutside); |
| 54 | + }; |
| 55 | + }, []); |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className="mx-4 md:mx-0"> |
| 59 | + <form |
| 60 | + className="w-full max-w-xl" |
| 61 | + onSubmit={(e) => { |
| 62 | + e.preventDefault(); |
| 63 | + if (searchText) { |
| 64 | + setIsSearching(false); |
| 65 | + router.push(`/catalogue?subject=${encodeURIComponent(searchText)}`); |
| 66 | + } |
| 67 | + }} |
| 68 | + > |
| 69 | + <div className="relative"> |
| 70 | + <Input |
| 71 | + ref={inputRef} |
| 72 | + type="text" |
| 73 | + value={searchText} |
| 74 | + onChange={handleSearchChange} |
| 75 | + onFocus={() => setIsSearching(true)} |
| 76 | + placeholder="Search by subject..." |
| 77 | + 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" |
| 78 | + /> |
| 79 | + <button |
| 80 | + type="submit" |
| 81 | + className="absolute inset-y-0 right-0 flex items-center pr-3" |
| 82 | + > |
| 83 | + <Search className="h-5 w-5 text-white" /> |
| 84 | + </button> |
| 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 |
| 105 | + </li> |
| 106 | + )} |
| 107 | + </ul> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + </form> |
| 111 | + </div> |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +export default SearchBarChild; |
0 commit comments