Skip to content

Commit

Permalink
making sure page param is cleared when a new search query is made
Browse files Browse the repository at this point in the history
  • Loading branch information
DonKoko committed Oct 28, 2024
1 parent e23af94 commit 32c2cfe
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/components/list/filters/search-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLInputElement | HTMLTextAreaElement>
) => {
Expand All @@ -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;
});
}
Expand Down

0 comments on commit 32c2cfe

Please sign in to comment.