Add useFormSubmit hook to ease forms submit override.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
Madeorsk 2024-09-23 18:40:22 +02:00
parent bdb0da5ae0
commit 2350d50b3e
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
2 changed files with 17 additions and 2 deletions

View file

@ -1,5 +1,5 @@
{ {
"version": "1.2.6", "version": "1.3.0",
"name": "@kernelui/core", "name": "@kernelui/core",
"description": "Kernel UI Core.", "description": "Kernel UI Core.",
"scripts": { "scripts": {

View file

@ -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; 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. // Get the previous value.
return ref?.current ?? undefined; 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]);
}