43 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

/* Copyright (C) 2024-2025 LibreWeddingPlanner contributors*/
2024-10-27 21:11:45 +00:00
2024-08-11 12:03:11 +02:00
'use client';
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
2024-08-11 12:34:16 +02:00
import { useSearchParams, usePathname, useRouter } from 'next/navigation';
import { useDebouncedCallback } from 'use-debounce';
2024-08-11 12:03:11 +02:00
export default function Search({ placeholder }: { placeholder: string }) {
2024-08-11 12:34:16 +02:00
const searchParams = useSearchParams();
const pathname = usePathname();
const { replace } = useRouter();
const handleSearch = useDebouncedCallback((term) => {
const params = new URLSearchParams(searchParams);
if (term) {
params.set('query', term);
} else {
params.delete('query');
}
replace(`${pathname}?${params.toString()}`);
}, 300);
2024-08-11 12:03:11 +02:00
return (
<div className="relative flex flex-1 flex-shrink-0">
<label htmlFor="search" className="sr-only">
Search
</label>
<input
className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500"
placeholder={placeholder}
2024-08-11 12:34:16 +02:00
onChange={(e) => {
handleSearch(e.target.value)
}}
defaultValue={searchParams.get('query')?.toString()}
2024-08-11 12:03:11 +02:00
/>
<MagnifyingGlassIcon className="absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" />
</div>
);
}