forked from sergiodxa/remix-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useDebounceSubmit (sergiodxa#291)
Since we can now set `navigate: false` on useSubmit to get it to use fetchers under the hood (and not navigate the user) use-cases for debounced submission have opened up This is identical to the useDebounceFetcher hook except it modifies useSubmit instead
- Loading branch information
1 parent
8028e92
commit b3a1253
Showing
3 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { SubmitOptions, SubmitFunction } from "@remix-run/react"; | ||
import { useSubmit } from "@remix-run/react"; | ||
import { useCallback, useEffect, useRef } from "react"; | ||
|
||
type SubmitTarget = Parameters<SubmitFunction>["0"]; | ||
|
||
export function useDebounceSubmit() { | ||
let timeoutRef = useRef<NodeJS.Timeout | undefined>(); | ||
|
||
useEffect(() => { | ||
// no initialize step required since timeoutRef defaults undefined | ||
let timeout = timeoutRef.current; | ||
return () => { | ||
if (timeout) clearTimeout(timeout); | ||
}; | ||
}, [timeoutRef]); | ||
|
||
// Clone the original submit to avoid a recursive loop | ||
const originalSubmit = useSubmit(); | ||
|
||
const submit = useCallback( | ||
( | ||
/** | ||
* Specifies the `<form>` to be submitted to the server, a specific | ||
* `<button>` or `<input type="submit">` to use to submit the form, or some | ||
* arbitrary data to submit. | ||
* | ||
* Note: When using a `<button>` its `name` and `value` will also be | ||
* included in the form data that is submitted. | ||
*/ | ||
target: SubmitTarget, | ||
/** | ||
* Options that override the `<form>`'s own attributes. Required when | ||
* submitting arbitrary data without a backing `<form>`. Additionally, you | ||
* can specify a `debounceTimeout` to delay the submission of the data. | ||
*/ | ||
options?: SubmitOptions & { | ||
/** Submissions within this timeout will be canceled */ | ||
debounceTimeout?: number; | ||
}, | ||
) => { | ||
if (timeoutRef.current) clearTimeout(timeoutRef.current); | ||
if (!options?.debounceTimeout || options.debounceTimeout <= 0) { | ||
return originalSubmit(target, options); | ||
} | ||
|
||
timeoutRef.current = setTimeout(() => { | ||
originalSubmit(target, options); | ||
}, options.debounceTimeout); | ||
}, | ||
[originalSubmit], | ||
); | ||
|
||
return submit; | ||
} |