@tanstack/form-core 1.24.3 → 1.24.5

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
@@ -1,5 +1,5 @@
1
1
  import { Derived, Store, batch } from '@tanstack/store'
2
-
2
+ import { throttle } from '@tanstack/pacer'
3
3
  import {
4
4
  deleteBy,
5
5
  determineFormLevelErrorSourceAndValue,
@@ -22,11 +22,6 @@ import {
22
22
  } from './standardSchemaValidator'
23
23
  import { defaultFieldMeta, metaHelper } from './metaHelper'
24
24
  import { formEventClient } from './EventClient'
25
- import type {
26
- RequestFormForceReset,
27
- RequestFormReset,
28
- RequestFormState,
29
- } from './EventClient'
30
25
  import type { ValidationLogicFn } from './ValidationLogic'
31
26
  import type {
32
27
  StandardSchemaV1,
@@ -623,7 +618,7 @@ export type BaseFormState<
623
618
  /**
624
619
  * A record of field metadata for each field in the form, not including the derived properties, like `errors` and such
625
620
  */
626
- fieldMetaBase: Record<DeepKeys<TFormData>, AnyFieldMetaBase>
621
+ fieldMetaBase: Partial<Record<DeepKeys<TFormData>, AnyFieldMetaBase>>
627
622
  /**
628
623
  * A boolean indicating if the form is currently in the process of being submitted after `handleSubmit` is called.
629
624
  *
@@ -738,7 +733,7 @@ export type DerivedFormState<
738
733
  /**
739
734
  * A record of field metadata for each field in the form.
740
735
  */
741
- fieldMeta: Record<DeepKeys<TFormData>, AnyFieldMeta>
736
+ fieldMeta: Partial<Record<DeepKeys<TFormData>, AnyFieldMeta>>
742
737
  }
743
738
 
744
739
  export interface FormState<
@@ -929,8 +924,22 @@ export class FormApi<
929
924
  TOnServer
930
925
  >
931
926
  >
932
- fieldMetaDerived!: Derived<Record<DeepKeys<TFormData>, AnyFieldMeta>>
933
- store!: Derived<
927
+ fieldMetaDerived: Derived<
928
+ FormState<
929
+ TFormData,
930
+ TOnMount,
931
+ TOnChange,
932
+ TOnChangeAsync,
933
+ TOnBlur,
934
+ TOnBlurAsync,
935
+ TOnSubmit,
936
+ TOnSubmitAsync,
937
+ TOnDynamic,
938
+ TOnDynamicAsync,
939
+ TOnServer
940
+ >['fieldMeta']
941
+ >
942
+ store: Derived<
934
943
  FormState<
935
944
  TFormData,
936
945
  TOnMount,
@@ -1024,7 +1033,7 @@ export class FormApi<
1024
1033
 
1025
1034
  let originalMetaCount = 0
1026
1035
 
1027
- const fieldMeta = {} as FormState<
1036
+ const fieldMeta: FormState<
1028
1037
  TFormData,
1029
1038
  TOnMount,
1030
1039
  TOnChange,
@@ -1036,7 +1045,7 @@ export class FormApi<
1036
1045
  TOnDynamic,
1037
1046
  TOnDynamicAsync,
1038
1047
  TOnServer
1039
- >['fieldMeta']
1048
+ >['fieldMeta'] = {}
1040
1049
 
1041
1050
  for (const fieldName of Object.keys(
1042
1051
  currBaseStore.fieldMetaBase,
@@ -1297,17 +1306,26 @@ export class FormApi<
1297
1306
 
1298
1307
  this.update(opts || {})
1299
1308
 
1309
+ const debouncedDevtoolState = throttle(
1310
+ (state: AnyFormState) =>
1311
+ formEventClient.emit('form-state', {
1312
+ id: this._formId,
1313
+ state: state,
1314
+ }),
1315
+ {
1316
+ wait: 300,
1317
+ },
1318
+ )
1319
+
1320
+ // devtool broadcasts
1300
1321
  this.store.subscribe(() => {
1301
- formEventClient.emit('form-state-change', {
1302
- id: this._formId,
1303
- state: this.store.state,
1304
- options: this.options,
1305
- })
1322
+ debouncedDevtoolState(this.store.state)
1306
1323
  })
1307
1324
 
1325
+ // devtool requests
1308
1326
  formEventClient.on('request-form-state', (e) => {
1309
1327
  if (e.payload.id === this._formId) {
1310
- formEventClient.emit('form-state-change', {
1328
+ formEventClient.emit('form-api', {
1311
1329
  id: this._formId,
1312
1330
  state: this.store.state,
1313
1331
  options: this.options,
@@ -1377,7 +1395,7 @@ export class FormApi<
1377
1395
  const { onMount } = this.options.validators || {}
1378
1396
 
1379
1397
  // broadcast form state for devtools on mounting
1380
- formEventClient.emit('form-state-change', {
1398
+ formEventClient.emit('form-api', {
1381
1399
  id: this._formId,
1382
1400
  state: this.store.state,
1383
1401
  options: this.options,
@@ -1455,6 +1473,12 @@ export class FormApi<
1455
1473
  ),
1456
1474
  )
1457
1475
  })
1476
+
1477
+ formEventClient.emit('form-api', {
1478
+ id: this._formId,
1479
+ state: this.store.state,
1480
+ options: this.options,
1481
+ })
1458
1482
  }
1459
1483
 
1460
1484
  /**
@@ -1642,7 +1666,6 @@ export class FormApi<
1642
1666
  for (const field of Object.keys(
1643
1667
  this.state.fieldMeta,
1644
1668
  ) as DeepKeys<TFormData>[]) {
1645
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1646
1669
  if (this.baseStore.state.fieldMetaBase[field] === undefined) {
1647
1670
  continue
1648
1671
  }
@@ -1850,7 +1873,6 @@ export class FormApi<
1850
1873
  for (const field of Object.keys(
1851
1874
  this.state.fieldMeta,
1852
1875
  ) as DeepKeys<TFormData>[]) {
1853
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1854
1876
  if (this.baseStore.state.fieldMetaBase[field] === undefined) {
1855
1877
  continue
1856
1878
  }
@@ -2053,7 +2075,7 @@ export class FormApi<
2053
2075
  meta: submitMetaArg,
2054
2076
  })
2055
2077
 
2056
- formEventClient.emit('form-submission-state-change', {
2078
+ formEventClient.emit('form-submission', {
2057
2079
  id: this._formId,
2058
2080
  submissionAttempt: this.state.submissionAttempts,
2059
2081
  successful: false,
@@ -2077,7 +2099,7 @@ export class FormApi<
2077
2099
  meta: submitMetaArg,
2078
2100
  })
2079
2101
 
2080
- formEventClient.emit('form-submission-state-change', {
2102
+ formEventClient.emit('form-submission', {
2081
2103
  id: this._formId,
2082
2104
  submissionAttempt: this.state.submissionAttempts,
2083
2105
  successful: false,
@@ -2116,7 +2138,7 @@ export class FormApi<
2116
2138
  isSubmitSuccessful: true, // Set isSubmitSuccessful to true on successful submission
2117
2139
  }))
2118
2140
 
2119
- formEventClient.emit('form-submission-state-change', {
2141
+ formEventClient.emit('form-submission', {
2120
2142
  id: this._formId,
2121
2143
  submissionAttempt: this.state.submissionAttempts,
2122
2144
  successful: true,
@@ -2130,7 +2152,7 @@ export class FormApi<
2130
2152
  isSubmitSuccessful: false, // Ensure isSubmitSuccessful is false if an error occurs
2131
2153
  }))
2132
2154
 
2133
- formEventClient.emit('form-submission-state-change', {
2155
+ formEventClient.emit('form-submission', {
2134
2156
  id: this._formId,
2135
2157
  submissionAttempt: this.state.submissionAttempts,
2136
2158
  successful: false,
@@ -2205,15 +2227,15 @@ export class FormApi<
2205
2227
  * resets every field's meta
2206
2228
  */
2207
2229
  resetFieldMeta = <TField extends DeepKeys<TFormData>>(
2208
- fieldMeta: Record<TField, AnyFieldMeta>,
2209
- ): Record<TField, AnyFieldMeta> => {
2230
+ fieldMeta: Partial<Record<TField, AnyFieldMeta>>,
2231
+ ): Partial<Record<TField, AnyFieldMeta>> => {
2210
2232
  return Object.keys(fieldMeta).reduce(
2211
- (acc: Record<TField, AnyFieldMeta>, key) => {
2233
+ (acc, key) => {
2212
2234
  const fieldKey = key as TField
2213
2235
  acc[fieldKey] = defaultFieldMeta
2214
2236
  return acc
2215
2237
  },
2216
- {} as Record<TField, AnyFieldMeta>,
2238
+ {} as Partial<Record<TField, AnyFieldMeta>>,
2217
2239
  )
2218
2240
  }
2219
2241