From f05ab79ef6451927f38c8466c29e466b20cc62d2 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Mon, 1 May 2023 11:58:55 -0600 Subject: [PATCH] fix: Cross-platform FormSubmitEvent via Register --- packages/form-core/src/FormApi.ts | 12 +++++++++++- packages/react-form/src/useForm.tsx | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 526bffe..99eb990 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -4,6 +4,16 @@ import type { DeepKeys, DeepValue, Updater } from './utils' import { functionalUpdate, getBy, setBy } from './utils' import type { FieldApi, FieldMeta, ValidationCause } from './FieldApi' +export interface Register { + // FormSubmitEvent +} + +export type FormSubmitEvent = Register extends { + FormSubmitEvent: infer E +} + ? E + : Event + export type FormOptions = { defaultValues?: TData defaultState?: Partial> @@ -245,7 +255,7 @@ export class FormApi { return this.validationMeta.validationPromise } - handleSubmit = async (e: Event) => { + handleSubmit = async (e: FormSubmitEvent) => { e.preventDefault() e.stopPropagation() diff --git a/packages/react-form/src/useForm.tsx b/packages/react-form/src/useForm.tsx index 4e88946..41c6a37 100644 --- a/packages/react-form/src/useForm.tsx +++ b/packages/react-form/src/useForm.tsx @@ -8,6 +8,10 @@ import { createUseField, type UseField } from './useField' import { formContext } from './formContext' declare module '@tanstack/form-core' { + interface Register { + FormSubmitEvent: React.FormEvent + } + // eslint-disable-next-line no-shadow interface FormApi { Form: FormComponent