@tanstack/form-core 1.24.2 → 1.24.4
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/dist/cjs/EventClient.cjs.map +1 -1
- package/dist/cjs/EventClient.d.cts +12 -16
- package/dist/cjs/FormApi.cjs +55 -24
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/esm/EventClient.d.ts +12 -16
- package/dist/esm/EventClient.js.map +1 -1
- package/dist/esm/FormApi.js +55 -24
- package/dist/esm/FormApi.js.map +1 -1
- package/package.json +3 -2
- package/src/EventClient.ts +14 -18
- package/src/FormApi.ts +68 -38
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,
|
|
@@ -1297,17 +1292,26 @@ export class FormApi<
|
|
|
1297
1292
|
|
|
1298
1293
|
this.update(opts || {})
|
|
1299
1294
|
|
|
1295
|
+
const debouncedDevtoolState = throttle(
|
|
1296
|
+
(state: AnyFormState) =>
|
|
1297
|
+
formEventClient.emit('form-state', {
|
|
1298
|
+
id: this._formId,
|
|
1299
|
+
state: state,
|
|
1300
|
+
}),
|
|
1301
|
+
{
|
|
1302
|
+
wait: 300,
|
|
1303
|
+
},
|
|
1304
|
+
)
|
|
1305
|
+
|
|
1306
|
+
// devtool broadcasts
|
|
1300
1307
|
this.store.subscribe(() => {
|
|
1301
|
-
|
|
1302
|
-
id: this._formId,
|
|
1303
|
-
state: this.store.state,
|
|
1304
|
-
options: this.options,
|
|
1305
|
-
})
|
|
1308
|
+
debouncedDevtoolState(this.store.state)
|
|
1306
1309
|
})
|
|
1307
1310
|
|
|
1311
|
+
// devtool requests
|
|
1308
1312
|
formEventClient.on('request-form-state', (e) => {
|
|
1309
1313
|
if (e.payload.id === this._formId) {
|
|
1310
|
-
formEventClient.emit('form-
|
|
1314
|
+
formEventClient.emit('form-api', {
|
|
1311
1315
|
id: this._formId,
|
|
1312
1316
|
state: this.store.state,
|
|
1313
1317
|
options: this.options,
|
|
@@ -1377,7 +1381,7 @@ export class FormApi<
|
|
|
1377
1381
|
const { onMount } = this.options.validators || {}
|
|
1378
1382
|
|
|
1379
1383
|
// broadcast form state for devtools on mounting
|
|
1380
|
-
formEventClient.emit('form-
|
|
1384
|
+
formEventClient.emit('form-api', {
|
|
1381
1385
|
id: this._formId,
|
|
1382
1386
|
state: this.store.state,
|
|
1383
1387
|
options: this.options,
|
|
@@ -1455,6 +1459,12 @@ export class FormApi<
|
|
|
1455
1459
|
),
|
|
1456
1460
|
)
|
|
1457
1461
|
})
|
|
1462
|
+
|
|
1463
|
+
formEventClient.emit('form-api', {
|
|
1464
|
+
id: this._formId,
|
|
1465
|
+
state: this.store.state,
|
|
1466
|
+
options: this.options,
|
|
1467
|
+
})
|
|
1458
1468
|
}
|
|
1459
1469
|
|
|
1460
1470
|
/**
|
|
@@ -2053,7 +2063,7 @@ export class FormApi<
|
|
|
2053
2063
|
meta: submitMetaArg,
|
|
2054
2064
|
})
|
|
2055
2065
|
|
|
2056
|
-
formEventClient.emit('form-submission
|
|
2066
|
+
formEventClient.emit('form-submission', {
|
|
2057
2067
|
id: this._formId,
|
|
2058
2068
|
submissionAttempt: this.state.submissionAttempts,
|
|
2059
2069
|
successful: false,
|
|
@@ -2077,7 +2087,7 @@ export class FormApi<
|
|
|
2077
2087
|
meta: submitMetaArg,
|
|
2078
2088
|
})
|
|
2079
2089
|
|
|
2080
|
-
formEventClient.emit('form-submission
|
|
2090
|
+
formEventClient.emit('form-submission', {
|
|
2081
2091
|
id: this._formId,
|
|
2082
2092
|
submissionAttempt: this.state.submissionAttempts,
|
|
2083
2093
|
successful: false,
|
|
@@ -2116,7 +2126,7 @@ export class FormApi<
|
|
|
2116
2126
|
isSubmitSuccessful: true, // Set isSubmitSuccessful to true on successful submission
|
|
2117
2127
|
}))
|
|
2118
2128
|
|
|
2119
|
-
formEventClient.emit('form-submission
|
|
2129
|
+
formEventClient.emit('form-submission', {
|
|
2120
2130
|
id: this._formId,
|
|
2121
2131
|
submissionAttempt: this.state.submissionAttempts,
|
|
2122
2132
|
successful: true,
|
|
@@ -2130,7 +2140,7 @@ export class FormApi<
|
|
|
2130
2140
|
isSubmitSuccessful: false, // Ensure isSubmitSuccessful is false if an error occurs
|
|
2131
2141
|
}))
|
|
2132
2142
|
|
|
2133
|
-
formEventClient.emit('form-submission
|
|
2143
|
+
formEventClient.emit('form-submission', {
|
|
2134
2144
|
id: this._formId,
|
|
2135
2145
|
submissionAttempt: this.state.submissionAttempts,
|
|
2136
2146
|
successful: false,
|
|
@@ -2318,13 +2328,18 @@ export class FormApi<
|
|
|
2318
2328
|
mergeOpts(options, { dontValidate: true }),
|
|
2319
2329
|
)
|
|
2320
2330
|
|
|
2321
|
-
|
|
2322
|
-
|
|
2331
|
+
const dontValidate = options?.dontValidate ?? false
|
|
2332
|
+
if (!dontValidate) {
|
|
2333
|
+
// Validate the whole array + all fields that have shifted
|
|
2334
|
+
await this.validateField(field, 'change')
|
|
2335
|
+
}
|
|
2323
2336
|
|
|
2324
2337
|
// Shift down all meta after validating to make sure the new field has been mounted
|
|
2325
2338
|
metaHelper(this).handleArrayFieldMetaShift(field, index, 'insert')
|
|
2326
2339
|
|
|
2327
|
-
|
|
2340
|
+
if (!dontValidate) {
|
|
2341
|
+
await this.validateArrayFieldsStartingFrom(field, index, 'change')
|
|
2342
|
+
}
|
|
2328
2343
|
}
|
|
2329
2344
|
|
|
2330
2345
|
/**
|
|
@@ -2348,9 +2363,12 @@ export class FormApi<
|
|
|
2348
2363
|
mergeOpts(options, { dontValidate: true }),
|
|
2349
2364
|
)
|
|
2350
2365
|
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2366
|
+
const dontValidate = options?.dontValidate ?? false
|
|
2367
|
+
if (!dontValidate) {
|
|
2368
|
+
// Validate the whole array + all fields that have shifted
|
|
2369
|
+
await this.validateField(field, 'change')
|
|
2370
|
+
await this.validateArrayFieldsStartingFrom(field, index, 'change')
|
|
2371
|
+
}
|
|
2354
2372
|
}
|
|
2355
2373
|
|
|
2356
2374
|
/**
|
|
@@ -2385,9 +2403,12 @@ export class FormApi<
|
|
|
2385
2403
|
this.deleteField(start as never)
|
|
2386
2404
|
}
|
|
2387
2405
|
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2406
|
+
const dontValidate = options?.dontValidate ?? false
|
|
2407
|
+
if (!dontValidate) {
|
|
2408
|
+
// Validate the whole array + all fields that have shifted
|
|
2409
|
+
await this.validateField(field, 'change')
|
|
2410
|
+
await this.validateArrayFieldsStartingFrom(field, index, 'change')
|
|
2411
|
+
}
|
|
2391
2412
|
}
|
|
2392
2413
|
|
|
2393
2414
|
/**
|
|
@@ -2412,11 +2433,14 @@ export class FormApi<
|
|
|
2412
2433
|
// Swap meta
|
|
2413
2434
|
metaHelper(this).handleArrayFieldMetaShift(field, index1, 'swap', index2)
|
|
2414
2435
|
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2436
|
+
const dontValidate = options?.dontValidate ?? false
|
|
2437
|
+
if (!dontValidate) {
|
|
2438
|
+
// Validate the whole array
|
|
2439
|
+
this.validateField(field, 'change')
|
|
2440
|
+
// Validate the swapped fields
|
|
2441
|
+
this.validateField(`${field}[${index1}]` as DeepKeys<TFormData>, 'change')
|
|
2442
|
+
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
|
|
2443
|
+
}
|
|
2420
2444
|
}
|
|
2421
2445
|
|
|
2422
2446
|
/**
|
|
@@ -2441,11 +2465,14 @@ export class FormApi<
|
|
|
2441
2465
|
// Move meta between index1 and index2
|
|
2442
2466
|
metaHelper(this).handleArrayFieldMetaShift(field, index1, 'move', index2)
|
|
2443
2467
|
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2468
|
+
const dontValidate = options?.dontValidate ?? false
|
|
2469
|
+
if (!dontValidate) {
|
|
2470
|
+
// Validate the whole array
|
|
2471
|
+
this.validateField(field, 'change')
|
|
2472
|
+
// Validate the moved fields
|
|
2473
|
+
this.validateField(`${field}[${index1}]` as DeepKeys<TFormData>, 'change')
|
|
2474
|
+
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
|
|
2475
|
+
}
|
|
2449
2476
|
}
|
|
2450
2477
|
|
|
2451
2478
|
/**
|
|
@@ -2474,8 +2501,11 @@ export class FormApi<
|
|
|
2474
2501
|
}
|
|
2475
2502
|
}
|
|
2476
2503
|
|
|
2477
|
-
|
|
2478
|
-
|
|
2504
|
+
const dontValidate = options?.dontValidate ?? false
|
|
2505
|
+
if (!dontValidate) {
|
|
2506
|
+
// validate array change
|
|
2507
|
+
this.validateField(field, 'change')
|
|
2508
|
+
}
|
|
2479
2509
|
}
|
|
2480
2510
|
|
|
2481
2511
|
/**
|