From 32c2cfe9626fe4d23153db844892bfb35a7f0659 Mon Sep 17 00:00:00 2001 From: Nikolay Bonev Date: Mon, 28 Oct 2024 12:50:11 +0200 Subject: [PATCH] making sure page param is cleared when a new search query is made --- app/components/list/filters/search-form.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/components/list/filters/search-form.tsx b/app/components/list/filters/search-form.tsx index a0a0a3c79..7e5a0460a 100644 --- a/app/components/list/filters/search-form.tsx +++ b/app/components/list/filters/search-form.tsx @@ -21,17 +21,24 @@ export const SearchForm = ({ className }: { className?: string }) => { const label = searchFieldLabel ? searchFieldLabel : `Search by ${singular}`; + /** + * Clears the search parameter and page parameter from the URL + * to ensure we start from the first page of results + */ function clearSearch() { setSearchParams((prev) => { prev.delete("s"); - + prev.delete("page"); // Reset page when clearing search return prev; }); if (searchInputRef.current) { searchInputRef.current.value = ""; } } - + /** + * Handles search input changes with debouncing + * Resets page to 1 whenever search query changes + */ const debouncedHandleChange = ( e: React.ChangeEvent ) => { @@ -41,6 +48,7 @@ export const SearchForm = ({ className }: { className?: string }) => { } else { setSearchParams((prev) => { prev.set("s", searchQuery); + prev.delete("page"); // Reset to page 1 when search query changes return prev; }); }