@tanstack/form-core 0.36.2 → 0.37.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/FieldApi.ts CHANGED
@@ -133,6 +133,24 @@ export type FieldAsyncValidateOrFn<
133
133
  TData
134
134
  >
135
135
 
136
+ /**
137
+ * @private
138
+ */
139
+ export type FieldListenerFn<
140
+ TParentData,
141
+ TName extends DeepKeys<TParentData>,
142
+ TFieldValidator extends
143
+ | Validator<DeepValue<TParentData, TName>, unknown>
144
+ | undefined = undefined,
145
+ TFormValidator extends
146
+ | Validator<TParentData, unknown>
147
+ | undefined = undefined,
148
+ TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,
149
+ > = (props: {
150
+ value: TData
151
+ fieldApi: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>
152
+ }) => void
153
+
136
154
  export interface FieldValidators<
137
155
  TParentData,
138
156
  TName extends DeepKeys<TParentData>,
@@ -255,6 +273,47 @@ export interface FieldValidators<
255
273
  >
256
274
  }
257
275
 
276
+ export interface FieldListeners<
277
+ TParentData,
278
+ TName extends DeepKeys<TParentData>,
279
+ TFieldValidator extends
280
+ | Validator<DeepValue<TParentData, TName>, unknown>
281
+ | undefined = undefined,
282
+ TFormValidator extends
283
+ | Validator<TParentData, unknown>
284
+ | undefined = undefined,
285
+ TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,
286
+ > {
287
+ onChange?: FieldListenerFn<
288
+ TParentData,
289
+ TName,
290
+ TFieldValidator,
291
+ TFormValidator,
292
+ TData
293
+ >
294
+ onBlur?: FieldListenerFn<
295
+ TParentData,
296
+ TName,
297
+ TFieldValidator,
298
+ TFormValidator,
299
+ TData
300
+ >
301
+ onMount?: FieldListenerFn<
302
+ TParentData,
303
+ TName,
304
+ TFieldValidator,
305
+ TFormValidator,
306
+ TData
307
+ >
308
+ onSubmit?: FieldListenerFn<
309
+ TParentData,
310
+ TName,
311
+ TFieldValidator,
312
+ TFormValidator,
313
+ TData
314
+ >
315
+ }
316
+
258
317
  /**
259
318
  * An object type representing the options for a field in a form.
260
319
  */
@@ -303,6 +362,16 @@ export interface FieldOptions<
303
362
  * An optional object with default metadata for the field.
304
363
  */
305
364
  defaultMeta?: Partial<FieldMeta>
365
+ /**
366
+ * A list of listeners which attach to the corresponding events
367
+ */
368
+ listeners?: FieldListeners<
369
+ TParentData,
370
+ TName,
371
+ TFieldValidator,
372
+ TFormValidator,
373
+ TData
374
+ >
306
375
  }
307
376
 
308
377
  /**
@@ -568,6 +637,11 @@ export class FieldApi<
568
637
  }
569
638
  }
570
639
 
640
+ this.options.listeners?.onMount?.({
641
+ value: this.state.value,
642
+ fieldApi: this,
643
+ })
644
+
571
645
  return () => {
572
646
  unsubscribe()
573
647
  }
@@ -622,6 +696,12 @@ export class FieldApi<
622
696
  */
623
697
  setValue = (updater: Updater<TData>, options?: UpdateMetaOptions) => {
624
698
  this.form.setFieldValue(this.name, updater as never, options)
699
+
700
+ this.options.listeners?.onChange?.({
701
+ value: this.state.value,
702
+ fieldApi: this,
703
+ })
704
+
625
705
  this.validate('change')
626
706
  }
627
707
 
@@ -1017,6 +1097,11 @@ export class FieldApi<
1017
1097
  this.setMeta((prev) => ({ ...prev, isBlurred: true }))
1018
1098
  }
1019
1099
  this.validate('blur')
1100
+
1101
+ this.options.listeners?.onBlur?.({
1102
+ value: this.state.value,
1103
+ fieldApi: this,
1104
+ })
1020
1105
  }
1021
1106
 
1022
1107
  /**
package/src/FormApi.ts CHANGED
@@ -980,6 +980,17 @@ export class FormApi<
980
980
  return
981
981
  }
982
982
 
983
+ this.store.batch(() => {
984
+ void (
985
+ Object.values(this.fieldInfo) as FieldInfo<TFormData, TFormValidator>[]
986
+ ).forEach((field) => {
987
+ field.instance?.options.listeners?.onSubmit?.({
988
+ value: field.instance.state.value,
989
+ fieldApi: field.instance,
990
+ })
991
+ })
992
+ })
993
+
983
994
  try {
984
995
  // Run the submit code
985
996
  await this.options.onSubmit?.({ value: this.state.values, formApi: this })