-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch.tsx
More file actions
37 lines (33 loc) · 1.05 KB
/
search.tsx
File metadata and controls
37 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"use client";
import { usePathname, useSearchParams } from "next/navigation";
import { MdOutlineSearch } from "react-icons/md";
type Props = { placeholder: string; setSearchString: (s: string) => void };
export default function Search({ placeholder, setSearchString }: Props) {
const searchParams = useSearchParams();
const pathname = usePathname();
function handleSearch(term: string) {
setSearchString(term);
const params = new URLSearchParams(searchParams);
if (term) {
params.set("query", term);
} else {
params.delete("query");
}
const newUrl = `${pathname}?${params.toString()}`;
history.replaceState(undefined, "", newUrl);
}
return (
<label className="input input-bordered flex items-center gap-2">
<MdOutlineSearch className="text-gray-500" />
<input
type="text"
className="grow"
placeholder={placeholder}
onChange={(e) => {
handleSearch(e.target.value);
}}
defaultValue={searchParams.get("query")?.toString()}
/>
</label>
);
}