diff --git a/README.md b/README.md index 7dec831..7f0a9c3 100644 --- a/README.md +++ b/README.md @@ -578,6 +578,49 @@ You can use `error.code` to check one of the error codes above, and `error.messa > **Warning** > Don't send those error messages to the end-user, they are meant to be used for debugging purposes only. +### Existing Search Params + +```ts +import { ExistingSearchParams } from "remix-utils/existing-search-params"; +``` + +> **Note** +> This depends on `react` and `@remix-run/react` + +When you submit a GET form, the browser will replace all of the search params in the URL with your form data. This component copies existing search params into hidden inputs so they will not be overwritten. + +The `exclude` prop accepts an array of search params to exclude from the hidden inputs + +- add params handled by this form to this list +- add params from other forms you want to clear on submit + +For example, imagine a table of data with separate form components for pagination and filtering and searching. Changing the page number should not affect the search or filter params. + +```tsx +
+``` + +By excluding the `page` param, from the search form, the user will return to the first page of search result. + +```tsx + +``` + ### External Scripts > **Note** diff --git a/package.json b/package.json index d06d87b..c827aed 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "./typed-cookie": "./build/server/typed-cookie.js", "./typed-session": "./build/server/typed-session.js", "./client-only": "./build/react/client-only.js", + "./existing-search-params": "./build/react/existing-search-params.js", "./external-scripts": "./build/react/external-scripts.js", "./fetcher-type": "./build/react/fetcher-type.js", "./server-only": "./build/react/server-only.js", diff --git a/src/react/existing-search-params.tsx b/src/react/existing-search-params.tsx new file mode 100644 index 0000000..771e53e --- /dev/null +++ b/src/react/existing-search-params.tsx @@ -0,0 +1,56 @@ +import * as React from "react"; +import { useSearchParams } from "@remix-run/react"; + +type Props = { + /** + * These query params will not be included as hidden inputs. + * - add params handled by this form to this list + * - any params from other forms you want to clear on submit + */ + exclude?: Array