Add submit function dependencies to useFormSubmit hook.
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:45:41 +02:00
parent 2350d50b3e
commit 8b57732722
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
2 changed files with 5 additions and 4 deletions

View file

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

View file

@ -1,4 +1,4 @@
import React, {FormEvent, useCallback, useEffect, useRef} from "react";
import React, {DependencyList, FormEvent, useCallback, useEffect, useRef} from "react";
export type Modify<T, R> = Omit<T, keyof R> & R;
@ -54,8 +54,9 @@ export function usePreviousValue<T>(currentValue: T): T
/**
* Create a callback for a form submit function that fully overrides default form behavior.
* @param submitFunction The function to call on form submit.
* @param dependencies Submit function dependencies.
*/
export function useFormSubmit(submitFunction: () => void): React.FormEventHandler<HTMLFormElement>
export function useFormSubmit(submitFunction: () => void, dependencies: DependencyList = []): React.FormEventHandler<HTMLFormElement>
{
// Use a memoized callback.
return useCallback((event: React.FormEvent<HTMLFormElement>) => {
@ -63,5 +64,5 @@ export function useFormSubmit(submitFunction: () => void): React.FormEventHandle
event.preventDefault();
submitFunction();
return false;
}, [submitFunction]);
}, [submitFunction, ...dependencies]);
}