Skip to content

Commit

Permalink
Fix search bar errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenOlano committed Nov 22, 2024
1 parent a98a2dd commit f008207
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/app/directory/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ type Params = {

const clubSearch = async (props: Params) => {
const params = await props.searchParams;
const userSearch = params['search'] || '';
const userSearch = params['q'] || '';
const session = await getServerAuthSession();

return (
<main className="md:pl-72">
<main className="md:pl-72 min-h-screen">
<div>
<Header />
<ClubSearchComponent userSearch={userSearch} session={session} />
Expand Down
8 changes: 5 additions & 3 deletions src/components/searchBar/ClubSearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import { useRouter, useSearchParams } from 'next/navigation';
import type { SelectClub as Club } from '@src/server/db/models';
import { api } from '@src/trpc/react';
import { DebouncedSearchBar } from './DebouncedSearchBar';

export const ClubSearchBar = () => {
const router = useRouter();
const [search, setSearch] = useState<string>('');
const searchParams = useSearchParams();
const [search, setSearch] = useState(searchParams.get('q') ?? '');
const { data } = api.club.byName.useQuery(
{ name: search },
{ enabled: !!search },
Expand All @@ -19,11 +20,12 @@ export const ClubSearchBar = () => {
<DebouncedSearchBar
placeholder="Search for Clubs"
setSearch={setSearch}
value={search}
searchResults={data || []}
onClick={onClickSearchResult}
submitButton
submitLogic={() => {
router.push(`/directory/search?search=${encodeURIComponent(search)}`);
router.push(`/directory/search?q=${encodeURIComponent(search)}`);
}}
/>
);
Expand Down
41 changes: 36 additions & 5 deletions src/components/searchBar/DebouncedSearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useEffect,
type Dispatch,
type SetStateAction,
type KeyboardEvent,
} from 'react';
import { SearchBar } from '.';

Expand Down Expand Up @@ -32,20 +33,44 @@ export const DebouncedSearchBar = <T extends SearchElement>({
}: DebouncedSearchBarProps<T>) => {
const [input, setInput] = useState<string>(value ?? '');
const [focused, setFocused] = useState(false);
const [selectedIndex, setSelectedIndex] = useState(-1);

const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
setInput(e.target.value);
};
useEffect(() => {
const handler = setTimeout(() => {
setSearch(input);
setSelectedIndex(-1);
}, 300);
return () => {
clearTimeout(handler);
};
}, [input, setSearch]);

function handleKeyDown(e: KeyboardEvent<HTMLDivElement>) {
switch(e.key) {
case 'ArrowDown':
e.preventDefault();
setSelectedIndex(Math.min(selectedIndex + 1, searchResults?.length ?? 0));
break;
case 'ArrowUp':
e.preventDefault();
setSelectedIndex(Math.max(selectedIndex - 1, 0));
break;
case 'Enter':
if (searchResults && searchResults[selectedIndex]) {
onClick?.(searchResults[selectedIndex]);
}
break;
}
}

return (
<div className="relative mr-3 w-full max-w-xs md:max-w-sm lg:max-w-md">
<div
className="relative mr-3 w-full max-w-xs md:max-w-sm lg:max-w-md"
onKeyDown={handleKeyDown}
>
<SearchBar
placeholder={placeholder}
tabIndex={0}
Expand All @@ -56,12 +81,18 @@ export const DebouncedSearchBar = <T extends SearchElement>({
submitLogic={submitLogic}
/>
{input && focused && searchResults && searchResults.length > 0 && (
<div className="absolute left-0 right-0 top-full z-50 mt-1 rounded-sm shadow-lg">
{searchResults.map((item) => (
<div className="absolute left-0 right-0 top-full z-50 mt-2 max-h-[300px] overflow-y-auto rounded-lg border border-gray-200 bg-white shadow-lg ring-1 ring-black ring-opacity-5">
{searchResults.map((item, i) => (
<button
type="button"
key={item.name}
className="w-full bg-gray-50 px-4 py-2 text-left font-semibold hover:bg-gray-200"
key={item.id}
className={`w-full px-4 py-2.5 text-left text-sm transition-colors duration-150
${selectedIndex === i
? 'bg-blue-50 text-blue-700'
: 'text-gray-700 hover:bg-gray-50'
}
${i !== searchResults.length - 1 ? 'border-b border-gray-100' : ''}
`}
onClick={() => (onClick ? onClick(item) : null)}
>
{item.name}
Expand Down
1 change: 1 addition & 0 deletions src/components/searchBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client';
import { RightArrowIcon, SearchIcon } from '@src/icons/Icons';
import { type ComponentProps } from 'react';

Expand Down

0 comments on commit f008207

Please sign in to comment.