45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
"use client"
|
|
|
|
import { SearchIcon } from "lucide-react";
|
|
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/ui/input-group";
|
|
import { Field } from "@/components/ui/field";
|
|
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
|
import { useState, useTransition } from "react";
|
|
|
|
export default function SearchBar() {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const searchParams = useSearchParams();
|
|
const [value, setValue] = useState(searchParams.get("q") ?? "");
|
|
const [, startTransition] = useTransition();
|
|
|
|
function handleChange(next: string) {
|
|
setValue(next);
|
|
|
|
const params = new URLSearchParams(searchParams.toString());
|
|
if (next) {
|
|
params.set("q", next);
|
|
} else {
|
|
params.delete("q");
|
|
}
|
|
|
|
startTransition(() => {
|
|
router.replace(`${pathname}?${params.toString()}`, { scroll: false });
|
|
});
|
|
}
|
|
|
|
return (
|
|
<Field className="max-w-sm">
|
|
<InputGroup>
|
|
<InputGroupInput
|
|
placeholder="Search..."
|
|
value={value}
|
|
onChange={(e) => handleChange(e.target.value)}
|
|
/>
|
|
<InputGroupAddon align="inline-start">
|
|
<SearchIcon className="text-muted-foreground" />
|
|
</InputGroupAddon>
|
|
</InputGroup>
|
|
</Field>
|
|
)
|
|
} |