@tanstack/form-core 1.27.4 → 1.27.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
@@ -1310,36 +1310,6 @@ export class FormApi<
1310
1310
  this.handleSubmit = this.handleSubmit.bind(this)
1311
1311
 
1312
1312
  this.update(opts || {})
1313
-
1314
- // devtool broadcasts
1315
- this.store.subscribe(() => {
1316
- throttleFormState(this)
1317
- })
1318
-
1319
- // devtool requests
1320
- formEventClient.on('request-form-state', (e) => {
1321
- if (e.payload.id === this._formId) {
1322
- formEventClient.emit('form-api', {
1323
- id: this._formId,
1324
- state: this.store.state,
1325
- options: this.options,
1326
- })
1327
- }
1328
- })
1329
-
1330
- formEventClient.on('request-form-reset', (e) => {
1331
- if (e.payload.id === this._formId) {
1332
- this.reset()
1333
- }
1334
- })
1335
-
1336
- formEventClient.on('request-form-force-submit', (e) => {
1337
- if (e.payload.id === this._formId) {
1338
- this._devtoolsSubmissionOverride = true
1339
- this.handleSubmit()
1340
- this._devtoolsSubmissionOverride = false
1341
- }
1342
- })
1343
1313
  }
1344
1314
 
1345
1315
  get formId(): string {
@@ -1374,7 +1344,51 @@ export class FormApi<
1374
1344
  mount = () => {
1375
1345
  const cleanupFieldMetaDerived = this.fieldMetaDerived.mount()
1376
1346
  const cleanupStoreDerived = this.store.mount()
1347
+
1348
+ // devtool broadcasts
1349
+ const cleanupDevtoolBroadcast = this.store.subscribe(() => {
1350
+ throttleFormState(this)
1351
+ })
1352
+
1353
+ // devtool requests
1354
+ const cleanupFormStateListener = formEventClient.on(
1355
+ 'request-form-state',
1356
+ (e) => {
1357
+ if (e.payload.id === this._formId) {
1358
+ formEventClient.emit('form-api', {
1359
+ id: this._formId,
1360
+ state: this.store.state,
1361
+ options: this.options,
1362
+ })
1363
+ }
1364
+ },
1365
+ )
1366
+
1367
+ const cleanupFormResetListener = formEventClient.on(
1368
+ 'request-form-reset',
1369
+ (e) => {
1370
+ if (e.payload.id === this._formId) {
1371
+ this.reset()
1372
+ }
1373
+ },
1374
+ )
1375
+
1376
+ const cleanupFormForceSubmitListener = formEventClient.on(
1377
+ 'request-form-force-submit',
1378
+ (e) => {
1379
+ if (e.payload.id === this._formId) {
1380
+ this._devtoolsSubmissionOverride = true
1381
+ this.handleSubmit()
1382
+ this._devtoolsSubmissionOverride = false
1383
+ }
1384
+ },
1385
+ )
1386
+
1377
1387
  const cleanup = () => {
1388
+ cleanupFormForceSubmitListener()
1389
+ cleanupFormResetListener()
1390
+ cleanupFormStateListener()
1391
+ cleanupDevtoolBroadcast()
1378
1392
  cleanupFieldMetaDerived()
1379
1393
  cleanupStoreDerived()
1380
1394
 
@@ -1657,16 +1671,20 @@ export class FormApi<
1657
1671
 
1658
1672
  const errorMapKey = getErrorMapKey(validateObj.cause)
1659
1673
 
1660
- for (const field of Object.keys(
1661
- this.state.fieldMeta,
1662
- ) as DeepKeys<TFormData>[]) {
1663
- if (this.baseStore.state.fieldMetaBase[field] === undefined) {
1674
+ const allFieldsToProcess = new Set([
1675
+ ...Object.keys(this.state.fieldMeta),
1676
+ ...Object.keys(fieldErrors || {}),
1677
+ ] as DeepKeys<TFormData>[])
1678
+
1679
+ for (const field of allFieldsToProcess) {
1680
+ if (
1681
+ this.baseStore.state.fieldMetaBase[field] === undefined &&
1682
+ !fieldErrors?.[field]
1683
+ ) {
1664
1684
  continue
1665
1685
  }
1666
1686
 
1667
- const fieldMeta = this.getFieldMeta(field)
1668
- if (!fieldMeta) continue
1669
-
1687
+ const fieldMeta = this.getFieldMeta(field) ?? defaultFieldMeta
1670
1688
  const {
1671
1689
  errorMap: currentErrorMap,
1672
1690
  errorSourceMap: currentErrorMapSource,
@@ -1678,10 +1696,8 @@ export class FormApi<
1678
1696
  determineFormLevelErrorSourceAndValue({
1679
1697
  newFormValidatorError,
1680
1698
  isPreviousErrorFromFormValidator:
1681
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1682
- currentErrorMapSource?.[errorMapKey] === 'form',
1683
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1684
- previousErrorValue: currentErrorMap?.[errorMapKey],
1699
+ currentErrorMapSource[errorMapKey] === 'form',
1700
+ previousErrorValue: currentErrorMap[errorMapKey],
1685
1701
  })
1686
1702
 
1687
1703
  if (newSource === 'form') {
@@ -1691,11 +1707,8 @@ export class FormApi<
1691
1707
  }
1692
1708
  }
1693
1709
 
1694
- if (
1695
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1696
- currentErrorMap?.[errorMapKey] !== newErrorValue
1697
- ) {
1698
- this.setFieldMeta(field, (prev) => ({
1710
+ if (currentErrorMap[errorMapKey] !== newErrorValue) {
1711
+ this.setFieldMeta(field, (prev = defaultFieldMeta) => ({
1699
1712
  ...prev,
1700
1713
  errorMap: {
1701
1714
  ...prev.errorMap,