@tanstack/form-core 1.6.3 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/FormApi.ts CHANGED
@@ -22,7 +22,12 @@ import type {
22
22
  StandardSchemaV1Issue,
23
23
  TStandardSchemaValidatorValue,
24
24
  } from './standardSchemaValidator'
25
- import type { AnyFieldMeta, AnyFieldMetaBase, FieldApi } from './FieldApi'
25
+ import type {
26
+ AnyFieldApi,
27
+ AnyFieldMeta,
28
+ AnyFieldMetaBase,
29
+ FieldApi,
30
+ } from './FieldApi'
26
31
  import type {
27
32
  FormValidationError,
28
33
  FormValidationErrorMap,
@@ -233,6 +238,83 @@ export interface FormTransform<
233
238
  deps: unknown[]
234
239
  }
235
240
 
241
+ export interface FormListeners<
242
+ TFormData,
243
+ TOnMount extends undefined | FormValidateOrFn<TFormData>,
244
+ TOnChange extends undefined | FormValidateOrFn<TFormData>,
245
+ TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
246
+ TOnBlur extends undefined | FormValidateOrFn<TFormData>,
247
+ TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
248
+ TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
249
+ TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
250
+ TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
251
+ TSubmitMeta = never,
252
+ > {
253
+ onChange?: (props: {
254
+ formApi: FormApi<
255
+ TFormData,
256
+ TOnMount,
257
+ TOnChange,
258
+ TOnChangeAsync,
259
+ TOnBlur,
260
+ TOnBlurAsync,
261
+ TOnSubmit,
262
+ TOnSubmitAsync,
263
+ TOnServer,
264
+ TSubmitMeta
265
+ >
266
+ fieldApi: AnyFieldApi
267
+ }) => void
268
+ onChangeDebounceMs?: number
269
+
270
+ onBlur?: (props: {
271
+ formApi: FormApi<
272
+ TFormData,
273
+ TOnMount,
274
+ TOnChange,
275
+ TOnChangeAsync,
276
+ TOnBlur,
277
+ TOnBlurAsync,
278
+ TOnSubmit,
279
+ TOnSubmitAsync,
280
+ TOnServer,
281
+ TSubmitMeta
282
+ >
283
+ fieldApi: AnyFieldApi
284
+ }) => void
285
+ onBlurDebounceMs?: number
286
+
287
+ onMount?: (props: {
288
+ formApi: FormApi<
289
+ TFormData,
290
+ TOnMount,
291
+ TOnChange,
292
+ TOnChangeAsync,
293
+ TOnBlur,
294
+ TOnBlurAsync,
295
+ TOnSubmit,
296
+ TOnSubmitAsync,
297
+ TOnServer,
298
+ TSubmitMeta
299
+ >
300
+ }) => void
301
+
302
+ onSubmit?: (props: {
303
+ formApi: FormApi<
304
+ TFormData,
305
+ TOnMount,
306
+ TOnChange,
307
+ TOnChangeAsync,
308
+ TOnBlur,
309
+ TOnBlurAsync,
310
+ TOnSubmit,
311
+ TOnSubmitAsync,
312
+ TOnServer,
313
+ TSubmitMeta
314
+ >
315
+ }) => void
316
+ }
317
+
236
318
  /**
237
319
  * An object representing the options for a form.
238
320
  */
@@ -299,6 +381,22 @@ export interface FormOptions<
299
381
  */
300
382
  onSubmitMeta?: TSubmitMeta
301
383
 
384
+ /**
385
+ * form level listeners
386
+ */
387
+ listeners?: FormListeners<
388
+ TFormData,
389
+ TOnMount,
390
+ TOnChange,
391
+ TOnChangeAsync,
392
+ TOnBlur,
393
+ TOnBlurAsync,
394
+ TOnSubmit,
395
+ TOnSubmitAsync,
396
+ TOnServer,
397
+ TSubmitMeta
398
+ >
399
+
302
400
  /**
303
401
  * A function to be called when the form is submitted, what should happen once the user submits a valid form returns `any` or a promise `Promise<any>`
304
402
  */
@@ -1049,6 +1147,9 @@ export class FormApi<
1049
1147
  cleanupFieldMetaDerived()
1050
1148
  cleanupStoreDerived()
1051
1149
  }
1150
+
1151
+ this.options.listeners?.onMount?.({ formApi: this })
1152
+
1052
1153
  const { onMount } = this.options.validators || {}
1053
1154
  if (!onMount) return cleanup
1054
1155
  this.validateSync('mount')
@@ -1607,7 +1708,7 @@ export class FormApi<
1607
1708
  }
1608
1709
 
1609
1710
  /**
1610
- * Handles the form submission, performs validation, and calls the appropriate onSubmit or onInvalidSubmit callbacks.
1711
+ * Handles the form submission, performs validation, and calls the appropriate onSubmit or onSubmitInvalid callbacks.
1611
1712
  */
1612
1713
  handleSubmit(): Promise<void>
1613
1714
  handleSubmit(submitMeta: TSubmitMeta): Promise<void>
@@ -1676,6 +1777,8 @@ export class FormApi<
1676
1777
  )
1677
1778
  })
1678
1779
 
1780
+ this.options.listeners?.onSubmit?.({ formApi: this })
1781
+
1679
1782
  try {
1680
1783
  // Run the submit code
1681
1784
  await this.options.onSubmit?.({