@tanstack/form-core 0.0.2 → 0.0.3

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
@@ -3,7 +3,7 @@ import type { DeepKeys, DeepValue, RequiredByKey, Updater } from './utils'
3
3
  import type { FormApi, ValidationError } from './FormApi'
4
4
  import { Store } from '@tanstack/store'
5
5
 
6
- type ValidateOn = 'change' | 'blur' | 'submit'
6
+ export type ValidationCause = 'change' | 'blur' | 'submit'
7
7
 
8
8
  export type FieldOptions<TData, TFormData> = {
9
9
  name: unknown extends TFormData ? string : DeepKeys<TFormData>
@@ -18,14 +18,10 @@ export type FieldOptions<TData, TFormData> = {
18
18
  fieldApi: FieldApi<TData, TFormData>,
19
19
  ) => ValidationError | Promise<ValidationError>
20
20
  validatePristine?: boolean // Default: false
21
- validateOn?: ValidateOn // Default: 'change'
22
- validateAsyncOn?: ValidateOn // Default: 'blur'
21
+ validateOn?: ValidationCause // Default: 'change'
22
+ validateAsyncOn?: ValidationCause // Default: 'blur'
23
23
  validateAsyncDebounceMs?: number
24
- filterValue?: (value: TData) => TData
25
24
  defaultMeta?: Partial<FieldMeta>
26
- change?: boolean
27
- blur?: boolean
28
- submit?: boolean
29
25
  }
30
26
 
31
27
  export type FieldMeta = {
@@ -65,7 +61,10 @@ export class FieldApi<TData, TFormData> {
65
61
  state!: FieldState<TData>
66
62
  options: RequiredByKey<
67
63
  FieldOptions<TData, TFormData>,
68
- 'validateOn' | 'validateAsyncOn'
64
+ | 'validatePristine'
65
+ | 'validateOn'
66
+ | 'validateAsyncOn'
67
+ | 'validateAsyncDebounceMs'
69
68
  > = {} as any
70
69
 
71
70
  constructor(opts: FieldApiOptions<TData, TFormData>) {
@@ -96,21 +95,12 @@ export class FieldApi<TData, TFormData> {
96
95
  : undefined
97
96
 
98
97
  // Do not validate pristine fields
99
- if (!this.options.validatePristine && !next.meta.isTouched) return
100
-
101
- // If validateOn is set to a variation of change, run the validation
102
- if (
103
- this.options.validateOn === 'change' ||
104
- this.options.validateOn.split('-')[0] === 'change'
105
- ) {
106
- try {
107
- this.validate()
108
- } catch (err) {
109
- console.error('An error occurred during validation', err)
110
- }
111
- }
112
-
98
+ const prevState = this.state
113
99
  this.state = next
100
+ if (next.value !== prevState.value) {
101
+ console.log('change')
102
+ this.validate('change', next.value)
103
+ }
114
104
  },
115
105
  },
116
106
  )
@@ -153,9 +143,11 @@ export class FieldApi<TData, TFormData> {
153
143
 
154
144
  update = (opts: FieldApiOptions<TData, TFormData>) => {
155
145
  this.options = {
156
- validateOn: 'change',
157
- validateAsyncOn: 'blur',
158
- validateAsyncDebounceMs: 0,
146
+ validatePristine: this.form.options.defaultValidatePristine ?? false,
147
+ validateOn: this.form.options.defaultValidateOn ?? 'change',
148
+ validateAsyncOn: this.form.options.defaultValidateAsyncOn ?? 'blur',
149
+ validateAsyncDebounceMs:
150
+ this.form.options.defaultValidateAsyncDebounceMs ?? 0,
159
151
  ...opts,
160
152
  }
161
153
 
@@ -200,20 +192,64 @@ export class FieldApi<TData, TFormData> {
200
192
  form: this.form,
201
193
  })
202
194
 
203
- #validate = async (isAsync: boolean) => {
204
- if (!this.options.validate) {
195
+ validateSync = async (value = this.state.value) => {
196
+ const { validate } = this.options
197
+
198
+ if (!validate) {
205
199
  return
206
200
  }
207
201
 
208
- this.setMeta((prev) => ({ ...prev, isValidating: true }))
209
-
210
202
  // Use the validationCount for all field instances to
211
203
  // track freshness of the validation
212
204
  const validationCount = (this.getInfo().validationCount || 0) + 1
213
-
214
205
  this.getInfo().validationCount = validationCount
206
+ const error = normalizeError(validate(value, this))
207
+
208
+ if (this.state.meta.error !== error) {
209
+ this.setMeta((prev) => ({
210
+ ...prev,
211
+ error,
212
+ }))
213
+ }
214
+
215
+ // If a sync error is encountered, cancel any async validation
216
+ if (this.state.meta.error) {
217
+ this.cancelValidateAsync()
218
+ }
219
+ }
220
+
221
+ #leaseValidateAsync = () => {
222
+ const count = (this.getInfo().validationAsyncCount || 0) + 1
223
+ this.getInfo().validationAsyncCount = count
224
+ return count
225
+ }
226
+
227
+ cancelValidateAsync = () => {
228
+ // Lease a new validation count to ignore any pending validations
229
+ this.#leaseValidateAsync()
230
+ // Cancel any pending validation state
231
+ this.setMeta((prev) => ({
232
+ ...prev,
233
+ isValidating: false,
234
+ }))
235
+ }
215
236
 
216
- const checkLatest = () => validationCount === this.getInfo().validationCount
237
+ validateAsync = async (value = this.state.value) => {
238
+ const { validateAsync, validateAsyncDebounceMs } = this.options
239
+
240
+ if (!validateAsync) {
241
+ return
242
+ }
243
+
244
+ if (this.state.meta.isValidating !== true)
245
+ this.setMeta((prev) => ({ ...prev, isValidating: true }))
246
+
247
+ // Use the validationCount for all field instances to
248
+ // track freshness of the validation
249
+ const validationAsyncCount = this.#leaseValidateAsync()
250
+
251
+ const checkLatest = () =>
252
+ validationAsyncCount === this.getInfo().validationAsyncCount
217
253
 
218
254
  if (!this.getInfo().validationPromise) {
219
255
  this.getInfo().validationPromise = new Promise((resolve, reject) => {
@@ -222,46 +258,80 @@ export class FieldApi<TData, TFormData> {
222
258
  })
223
259
  }
224
260
 
225
- try {
226
- const rawError = await this.options.validate(this.state.value, this)
227
-
228
- if (checkLatest()) {
229
- const error = (() => {
230
- if (rawError) {
231
- if (typeof rawError !== 'string') {
232
- return 'Invalid Form Values'
233
- }
234
-
235
- return rawError
236
- }
237
-
238
- return undefined
239
- })()
261
+ if (validateAsyncDebounceMs > 0) {
262
+ await new Promise((r) => setTimeout(r, validateAsyncDebounceMs))
263
+ }
240
264
 
241
- this.setMeta((prev) => ({
242
- ...prev,
243
- isValidating: false,
244
- error,
245
- }))
246
- this.getInfo().validationResolve?.(error)
247
- }
248
- } catch (error) {
249
- if (checkLatest()) {
250
- this.getInfo().validationReject?.(error)
251
- throw error
252
- }
253
- } finally {
254
- if (checkLatest()) {
255
- this.setMeta((prev) => ({ ...prev, isValidating: false }))
256
- delete this.getInfo().validationPromise
265
+ // Only kick off validation if this validation is the latest attempt
266
+ if (checkLatest()) {
267
+ try {
268
+ const rawError = await validateAsync(value, this)
269
+
270
+ if (checkLatest()) {
271
+ const error = normalizeError(rawError)
272
+ this.setMeta((prev) => ({
273
+ ...prev,
274
+ isValidating: false,
275
+ error,
276
+ }))
277
+ this.getInfo().validationResolve?.(error)
278
+ }
279
+ } catch (error) {
280
+ if (checkLatest()) {
281
+ this.getInfo().validationReject?.(error)
282
+ throw error
283
+ }
284
+ } finally {
285
+ if (checkLatest()) {
286
+ this.setMeta((prev) => ({ ...prev, isValidating: false }))
287
+ delete this.getInfo().validationPromise
288
+ }
257
289
  }
258
290
  }
259
291
 
292
+ // Always return the latest validation promise to the caller
260
293
  return this.getInfo().validationPromise
261
294
  }
262
295
 
263
- validate = () => this.#validate(false)
264
- validateAsync = () => this.#validate(true)
296
+ shouldValidate = (isAsync: boolean, cause?: ValidationCause) => {
297
+ const { validateOn, validateAsyncOn } = this.options
298
+ const level = getValidationCauseLevel(cause)
299
+
300
+ // Must meet *at least* the validation level to validate,
301
+ // e.g. if validateOn is 'change' and validateCause is 'blur',
302
+ // the field will still validate
303
+ return Object.keys(validateCauseLevels).some((d) =>
304
+ isAsync
305
+ ? validateAsyncOn
306
+ : validateOn === d && level >= validateCauseLevels[d],
307
+ )
308
+ }
309
+
310
+ validate = async (
311
+ cause?: ValidationCause,
312
+ value?: TData,
313
+ ): Promise<ValidationError> => {
314
+ // If the field is pristine and validatePristine is false, do not validate
315
+ if (!this.options.validatePristine && !this.state.meta.isTouched) return
316
+
317
+ // Attempt to sync validate first
318
+ if (this.shouldValidate(false, cause)) {
319
+ this.validateSync(value)
320
+ }
321
+
322
+ // If there is an error, return it, do not attempt async validation
323
+ if (this.state.meta.error) {
324
+ return this.state.meta.error
325
+ }
326
+
327
+ // No error? Attempt async validation
328
+ if (this.shouldValidate(true, cause)) {
329
+ return this.validateAsync(value)
330
+ }
331
+
332
+ // If there is no sync error or async validation attempt, there is no error
333
+ return undefined
334
+ }
265
335
 
266
336
  getChangeProps = <T extends ChangeProps<any>>(
267
337
  props: T = {} as T,
@@ -275,13 +345,7 @@ export class FieldApi<TData, TFormData> {
275
345
  },
276
346
  onBlur: (e) => {
277
347
  this.setMeta((prev) => ({ ...prev, isTouched: true }))
278
-
279
- const { validateOn } = this.options
280
-
281
- if (validateOn === 'blur' || validateOn.split('-')[0] === 'blur') {
282
- this.validate()
283
- }
284
-
348
+ this.validate('blur')
285
349
  props.onBlur?.(e)
286
350
  },
287
351
  } as ChangeProps<TData> & Omit<T, keyof ChangeProps<TData>>
@@ -301,3 +365,25 @@ export class FieldApi<TData, TFormData> {
301
365
  }
302
366
  }
303
367
  }
368
+
369
+ const validateCauseLevels = {
370
+ change: 0,
371
+ blur: 1,
372
+ submit: 2,
373
+ }
374
+
375
+ function getValidationCauseLevel(cause?: ValidationCause) {
376
+ return !cause ? 3 : validateCauseLevels[cause]
377
+ }
378
+
379
+ function normalizeError(rawError?: ValidationError) {
380
+ if (rawError) {
381
+ if (typeof rawError !== 'string') {
382
+ return 'Invalid Form Values'
383
+ }
384
+
385
+ return rawError
386
+ }
387
+
388
+ return undefined
389
+ }
package/src/FormApi.ts CHANGED
@@ -3,16 +3,19 @@ import { Store } from '@tanstack/store'
3
3
  //
4
4
  import type { DeepKeys, DeepValue, Updater } from './utils'
5
5
  import { functionalUpdate, getBy, setBy } from './utils'
6
- import type { FieldApi, FieldMeta } from './FieldApi'
6
+ import type { FieldApi, FieldMeta, ValidationCause } from './FieldApi'
7
7
 
8
8
  export type FormOptions<TData> = {
9
9
  defaultValues?: TData
10
10
  defaultState?: Partial<FormState<TData>>
11
- onSubmit?: (values: TData, formApi: FormApi<TData>) => Promise<any>
11
+ onSubmit?: (values: TData, formApi: FormApi<TData>) => void
12
12
  onInvalidSubmit?: (values: TData, formApi: FormApi<TData>) => void
13
13
  validate?: (values: TData, formApi: FormApi<TData>) => Promise<any>
14
14
  debugForm?: boolean
15
- validatePristine?: boolean
15
+ defaultValidatePristine?: boolean
16
+ defaultValidateOn?: ValidationCause
17
+ defaultValidateAsyncOn?: ValidationCause
18
+ defaultValidateAsyncDebounceMs?: number
16
19
  }
17
20
 
18
21
  export type FieldInfo<TFormData> = {
@@ -21,6 +24,7 @@ export type FieldInfo<TFormData> = {
21
24
 
22
25
  export type ValidationMeta = {
23
26
  validationCount?: number
27
+ validationAsyncCount?: number
24
28
  validationPromise?: Promise<ValidationError>
25
29
  validationResolve?: (error: ValidationError) => void
26
30
  validationReject?: (error: unknown) => void