Skip to content

Commit a35c05d

Browse files
committed
fix: bug in catalogue page searching + build
1 parent 4161675 commit a35c05d

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

src/components/CatalogueContent.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useRouter } from "next/navigation";
1010
import SearchBarChild from "./Searchbar/searchbar-child";
1111
import Loader from "./ui/loader";
1212
import { campuses, semesters } from "./select_options";
13+
import SearchBar from "./Searchbar/searchbar";
1314

1415
const CatalogueContent = () => {
1516
const searchParams = useSearchParams();
@@ -165,7 +166,7 @@ const CatalogueContent = () => {
165166
<div className="min-h-screen px-2 md:p-8">
166167
<div className="mb-10 flex w-full flex-row items-center md:justify-between md:gap-10">
167168
<div className="w-[120%] md:w-[576px]">
168-
<SearchBarChild />
169+
<SearchBar />
169170
</div>
170171
<div className="flex gap-8">
171172
{subject && filterOptions && (

src/components/Searchbar/searchbar-child.tsx

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)