Add useFormSubmit hook to ease forms submit override.
This commit is contained in:
parent
bdb0da5ae0
commit
2350d50b3e
2 changed files with 17 additions and 2 deletions
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"version": "1.2.6",
|
||||
"version": "1.3.0",
|
||||
"name": "@kernelui/core",
|
||||
"description": "Kernel UI Core.",
|
||||
"scripts": {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {useEffect, useRef} from "react";
|
||||
import React, {FormEvent, useCallback, useEffect, useRef} from "react";
|
||||
|
||||
export type Modify<T, R> = Omit<T, keyof R> & R;
|
||||
|
||||
|
@ -50,3 +50,18 @@ export function usePreviousValue<T>(currentValue: T): T
|
|||
// Get the previous value.
|
||||
return ref?.current ?? undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a callback for a form submit function that fully overrides default form behavior.
|
||||
* @param submitFunction The function to call on form submit.
|
||||
*/
|
||||
export function useFormSubmit(submitFunction: () => void): React.FormEventHandler<HTMLFormElement>
|
||||
{
|
||||
// Use a memoized callback.
|
||||
return useCallback((event: React.FormEvent<HTMLFormElement>) => {
|
||||
// Prevent default behavior, then call submit function.
|
||||
event.preventDefault();
|
||||
submitFunction();
|
||||
return false;
|
||||
}, [submitFunction]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue