@tanstack/react-form 0.0.10 → 0.0.11

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.
@@ -49,11 +49,14 @@
49
49
  */
50
50
  class Store {
51
51
  listeners = new Set();
52
- batching = false;
53
- queue = [];
52
+ #batching = false;
53
+ #flushing = 0;
54
54
  constructor(initialState, options) {
55
55
  this.state = initialState;
56
56
  this.options = options;
57
+ if (this.options?.onUpdate) {
58
+ this.subscribe(this.options?.onUpdate);
59
+ }
57
60
  }
58
61
  subscribe = listener => {
59
62
  this.listeners.add(listener);
@@ -66,22 +69,21 @@
66
69
  setState = updater => {
67
70
  const previous = this.state;
68
71
  this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
69
- if (this.state === previous) return;
70
- this.options?.onUpdate?.(this.state, previous);
71
- this.queue.push(() => {
72
- this.listeners.forEach(listener => listener(this.state, previous));
73
- });
74
72
  this.#flush();
75
73
  };
76
74
  #flush = () => {
77
- if (this.batching) return;
78
- this.queue.forEach(cb => cb());
79
- this.queue = [];
75
+ if (this.#batching) return;
76
+ const flushId = ++this.#flushing;
77
+ this.listeners.forEach(listener => {
78
+ if (this.#flushing !== flushId) return;
79
+ listener();
80
+ });
80
81
  };
81
82
  batch = cb => {
82
- this.batching = true;
83
+ if (this.#batching) return cb();
84
+ this.#batching = true;
83
85
  cb();
84
- this.batching = false;
86
+ this.#batching = false;
85
87
  this.#flush();
86
88
  };
87
89
  }
@@ -97,6 +99,75 @@
97
99
  * @license MIT
98
100
  */
99
101
 
102
+ function _defineProperty(obj, key, value) {
103
+ if (key in obj) {
104
+ Object.defineProperty(obj, key, {
105
+ value: value,
106
+ enumerable: true,
107
+ configurable: true,
108
+ writable: true
109
+ });
110
+ } else {
111
+ obj[key] = value;
112
+ }
113
+
114
+ return obj;
115
+ }
116
+
117
+ function _classPrivateFieldGet(receiver, privateMap) {
118
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
119
+
120
+ return _classApplyDescriptorGet(receiver, descriptor);
121
+ }
122
+
123
+ function _classPrivateFieldSet(receiver, privateMap, value) {
124
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
125
+
126
+ _classApplyDescriptorSet(receiver, descriptor, value);
127
+
128
+ return value;
129
+ }
130
+
131
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
132
+ if (!privateMap.has(receiver)) {
133
+ throw new TypeError("attempted to " + action + " private field on non-instance");
134
+ }
135
+
136
+ return privateMap.get(receiver);
137
+ }
138
+
139
+ function _classApplyDescriptorGet(receiver, descriptor) {
140
+ if (descriptor.get) {
141
+ return descriptor.get.call(receiver);
142
+ }
143
+
144
+ return descriptor.value;
145
+ }
146
+
147
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
148
+ if (descriptor.set) {
149
+ descriptor.set.call(receiver, value);
150
+ } else {
151
+ if (!descriptor.writable) {
152
+ throw new TypeError("attempted to set read only private field");
153
+ }
154
+
155
+ descriptor.value = value;
156
+ }
157
+ }
158
+
159
+ function _checkPrivateRedeclaration(obj, privateCollection) {
160
+ if (privateCollection.has(obj)) {
161
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
162
+ }
163
+ }
164
+
165
+ function _classPrivateFieldInitSpec(obj, privateMap, value) {
166
+ _checkPrivateRedeclaration(obj, privateMap);
167
+
168
+ privateMap.set(obj, value);
169
+ }
170
+
100
171
  function functionalUpdate(updater, input) {
101
172
  return typeof updater === 'function' ? updater(input) : updater;
102
173
  }
@@ -191,14 +262,24 @@
191
262
 
192
263
  class FormApi {
193
264
  // // This carries the context for nested fields
265
+ // Do not use __state directly, as it is not reactive.
266
+ // Please use form.useStore() utility to subscribe to state
194
267
  constructor(_opts) {
195
268
  var _opts$defaultValues, _opts$defaultState;
196
269
 
197
- this.options = {};
198
- this.fieldInfo = {};
199
- this.validationMeta = {};
270
+ _defineProperty(this, "options", {});
271
+
272
+ _defineProperty(this, "store", void 0);
273
+
274
+ _defineProperty(this, "state", void 0);
275
+
276
+ _defineProperty(this, "fieldInfo", {});
200
277
 
201
- this.update = options => {
278
+ _defineProperty(this, "fieldName", void 0);
279
+
280
+ _defineProperty(this, "validationMeta", {});
281
+
282
+ _defineProperty(this, "update", options => {
202
283
  if (!options) return;
203
284
  this.store.batch(() => {
204
285
  if (options.defaultState && options.defaultState !== this.options.defaultState) {
@@ -208,17 +289,15 @@
208
289
  }
209
290
 
210
291
  if (options.defaultValues !== this.options.defaultValues) {
211
- this.store.setState(prev => ({ ...prev,
212
- values: options.defaultValues
213
- }));
292
+ this.store.setState(() => getDefaultFormState(options.defaultValues));
214
293
  }
215
294
  });
216
295
  this.options = options;
217
- };
296
+ });
218
297
 
219
- this.reset = () => this.store.setState(() => getDefaultFormState(this.options.defaultValues));
298
+ _defineProperty(this, "reset", () => this.store.setState(() => getDefaultFormState(this.options.defaultValues)));
220
299
 
221
- this.validateAllFields = async () => {
300
+ _defineProperty(this, "validateAllFields", async cause => {
222
301
  const fieldValidationPromises = [];
223
302
  this.store.batch(() => {
224
303
  void Object.values(this.fieldInfo).forEach(field => {
@@ -230,71 +309,15 @@
230
309
  isTouched: true
231
310
  })); // Validate the field
232
311
 
233
- if (instance.options.validate) {
234
- fieldValidationPromises.push(instance.validate());
235
- }
312
+ fieldValidationPromises.push(Promise.resolve().then(() => instance.validate(cause)));
236
313
  }
237
314
  });
238
315
  });
239
316
  });
240
317
  return Promise.all(fieldValidationPromises);
241
- };
242
-
243
- this.validateForm = async () => {
244
- const {
245
- validate
246
- } = this.options;
247
-
248
- if (!validate) {
249
- return;
250
- } // Use the formValidationCount for all field instances to
251
- // track freshness of the validation
252
-
253
-
254
- this.store.setState(prev => ({ ...prev,
255
- isValidating: true,
256
- formValidationCount: prev.formValidationCount + 1
257
- }));
258
- const formValidationCount = this.state.formValidationCount;
259
-
260
- const checkLatest = () => formValidationCount === this.state.formValidationCount;
261
-
262
- if (!this.validationMeta.validationPromise) {
263
- this.validationMeta.validationPromise = new Promise((resolve, reject) => {
264
- this.validationMeta.validationResolve = resolve;
265
- this.validationMeta.validationReject = reject;
266
- });
267
- }
268
-
269
- const doValidation = async () => {
270
- try {
271
- const error = await validate(this.state.values, this);
272
-
273
- if (checkLatest()) {
274
- var _this$validationMeta$, _this$validationMeta;
275
-
276
- this.store.setState(prev => ({ ...prev,
277
- isValidating: false,
278
- error: error ? typeof error === 'string' ? error : 'Invalid Form Values' : null
279
- }));
280
- (_this$validationMeta$ = (_this$validationMeta = this.validationMeta).validationResolve) == null ? void 0 : _this$validationMeta$.call(_this$validationMeta, error);
281
- }
282
- } catch (err) {
283
- if (checkLatest()) {
284
- var _this$validationMeta$2, _this$validationMeta2;
285
-
286
- (_this$validationMeta$2 = (_this$validationMeta2 = this.validationMeta).validationReject) == null ? void 0 : _this$validationMeta$2.call(_this$validationMeta2, err);
287
- }
288
- } finally {
289
- delete this.validationMeta.validationPromise;
290
- }
291
- };
292
-
293
- doValidation();
294
- return this.validationMeta.validationPromise;
295
- };
318
+ });
296
319
 
297
- this.handleSubmit = async e => {
320
+ _defineProperty(this, "handleSubmit", async e => {
298
321
  e.preventDefault();
299
322
  e.stopPropagation(); // Check to see that the form and all fields have been touched
300
323
  // If they have not, touch them all and run validation
@@ -320,32 +343,31 @@
320
343
  }; // Validate all fields
321
344
 
322
345
 
323
- await this.validateAllFields(); // Fields are invalid, do not submit
346
+ await this.validateAllFields('submit'); // Fields are invalid, do not submit
324
347
 
325
348
  if (!this.state.isFieldsValid) {
326
- var _this$options$onInval, _this$options;
349
+ var _this$options$onSubmi, _this$options;
327
350
 
328
351
  done();
329
- (_this$options$onInval = (_this$options = this.options).onInvalidSubmit) == null ? void 0 : _this$options$onInval.call(_this$options, this.state.values, this);
352
+ (_this$options$onSubmi = (_this$options = this.options).onSubmitInvalid) == null ? void 0 : _this$options$onSubmi.call(_this$options, this.state.values, this);
330
353
  return;
331
354
  } // Run validation for the form
355
+ // await this.validateForm()
332
356
 
333
357
 
334
- await this.validateForm();
335
-
336
358
  if (!this.state.isValid) {
337
- var _this$options$onInval2, _this$options2;
359
+ var _this$options$onSubmi2, _this$options2;
338
360
 
339
361
  done();
340
- (_this$options$onInval2 = (_this$options2 = this.options).onInvalidSubmit) == null ? void 0 : _this$options$onInval2.call(_this$options2, this.state.values, this);
362
+ (_this$options$onSubmi2 = (_this$options2 = this.options).onSubmitInvalid) == null ? void 0 : _this$options$onSubmi2.call(_this$options2, this.state.values, this);
341
363
  return;
342
364
  }
343
365
 
344
366
  try {
345
- var _this$options$onSubmi, _this$options3;
367
+ var _this$options$onSubmi3, _this$options3;
346
368
 
347
369
  // Run the submit code
348
- await ((_this$options$onSubmi = (_this$options3 = this.options).onSubmit) == null ? void 0 : _this$options$onSubmi.call(_this$options3, this.state.values, this));
370
+ await ((_this$options$onSubmi3 = (_this$options3 = this.options).onSubmit) == null ? void 0 : _this$options$onSubmi3.call(_this$options3, this.state.values, this));
349
371
  this.store.batch(() => {
350
372
  this.store.setState(prev => ({ ...prev,
351
373
  isSubmitted: true
@@ -356,23 +378,23 @@
356
378
  done();
357
379
  throw err;
358
380
  }
359
- };
381
+ });
360
382
 
361
- this.getFieldValue = field => getBy(this.state.values, field);
383
+ _defineProperty(this, "getFieldValue", field => getBy(this.state.values, field));
362
384
 
363
- this.getFieldMeta = field => {
385
+ _defineProperty(this, "getFieldMeta", field => {
364
386
  return this.state.fieldMeta[field];
365
- };
387
+ });
366
388
 
367
- this.getFieldInfo = field => {
389
+ _defineProperty(this, "getFieldInfo", field => {
368
390
  var _this$fieldInfo;
369
391
 
370
392
  return (_this$fieldInfo = this.fieldInfo)[field] || (_this$fieldInfo[field] = {
371
393
  instances: {}
372
394
  });
373
- };
395
+ });
374
396
 
375
- this.setFieldMeta = (field, updater) => {
397
+ _defineProperty(this, "setFieldMeta", (field, updater) => {
376
398
  this.store.setState(prev => {
377
399
  return { ...prev,
378
400
  fieldMeta: { ...prev.fieldMeta,
@@ -380,85 +402,76 @@
380
402
  }
381
403
  };
382
404
  });
383
- };
384
-
385
- this.setFieldValue = (field, updater, opts) => {
386
- var _opts$touch;
405
+ });
387
406
 
388
- const touch = (_opts$touch = opts == null ? void 0 : opts.touch) != null ? _opts$touch : true;
407
+ _defineProperty(this, "setFieldValue", (field, updater, opts) => {
408
+ const touch = opts == null ? void 0 : opts.touch;
389
409
  this.store.batch(() => {
390
- this.store.setState(prev => {
391
- return { ...prev,
392
- values: setBy(prev.values, field, updater)
393
- };
394
- });
395
-
396
410
  if (touch) {
397
411
  this.setFieldMeta(field, prev => ({ ...prev,
398
412
  isTouched: true
399
413
  }));
400
414
  }
415
+
416
+ this.store.setState(prev => {
417
+ return { ...prev,
418
+ values: setBy(prev.values, field, updater)
419
+ };
420
+ });
401
421
  });
402
- };
422
+ });
403
423
 
404
- this.pushFieldValue = (field, value, opts) => {
424
+ _defineProperty(this, "pushFieldValue", (field, value, opts) => {
405
425
  return this.setFieldValue(field, prev => [...(Array.isArray(prev) ? prev : []), value], opts);
406
- };
426
+ });
407
427
 
408
- this.insertFieldValue = (field, index, value, opts) => {
428
+ _defineProperty(this, "insertFieldValue", (field, index, value, opts) => {
409
429
  this.setFieldValue(field, prev => {
410
- // invariant( // TODO: bring in invariant
411
- // Array.isArray(prev),
412
- // `Cannot insert a field value into a non-array field. Check that this field's existing value is an array: ${field}.`
413
- // )
414
430
  return prev.map((d, i) => i === index ? value : d);
415
431
  }, opts);
416
- };
432
+ });
417
433
 
418
- this.removeFieldValue = (field, index, opts) => {
434
+ _defineProperty(this, "removeFieldValue", (field, index, opts) => {
419
435
  this.setFieldValue(field, prev => {
420
- // invariant( // TODO: bring in invariant
421
- // Array.isArray(prev),
422
- // `Cannot insert a field value into a non-array field. Check that this field's existing value is an array: ${field}.`
423
- // )
424
436
  return prev.filter((_d, i) => i !== index);
425
437
  }, opts);
426
- };
438
+ });
427
439
 
428
- this.swapFieldValues = (field, index1, index2) => {
440
+ _defineProperty(this, "swapFieldValues", (field, index1, index2) => {
429
441
  this.setFieldValue(field, prev => {
430
442
  const prev1 = prev[index1];
431
443
  const prev2 = prev[index2];
432
444
  return setBy(setBy(prev, [index1], prev2), [index2], prev1);
433
445
  });
434
- };
446
+ });
435
447
 
436
448
  this.store = new Store(getDefaultFormState({ ...(_opts == null ? void 0 : _opts.defaultState),
437
449
  values: (_opts$defaultValues = _opts == null ? void 0 : _opts.defaultValues) != null ? _opts$defaultValues : _opts == null ? void 0 : (_opts$defaultState = _opts.defaultState) == null ? void 0 : _opts$defaultState.values,
438
- isFormValid: !(_opts != null && _opts.validate)
450
+ isFormValid: true
439
451
  }), {
440
- onUpdate: next => {
441
- // Computed state
442
- const fieldMetaValues = Object.values(next.fieldMeta);
452
+ onUpdate: () => {
453
+ let {
454
+ state
455
+ } = this.store; // Computed state
456
+
457
+ const fieldMetaValues = Object.values(state.fieldMeta);
443
458
  const isFieldsValidating = fieldMetaValues.some(field => field == null ? void 0 : field.isValidating);
444
459
  const isFieldsValid = !fieldMetaValues.some(field => field == null ? void 0 : field.error);
445
460
  const isTouched = fieldMetaValues.some(field => field == null ? void 0 : field.isTouched);
446
- const isValidating = isFieldsValidating || next.isFormValidating;
447
- const isFormValid = !next.formError;
461
+ const isValidating = isFieldsValidating || state.isFormValidating;
462
+ const isFormValid = !state.formError;
448
463
  const isValid = isFieldsValid && isFormValid;
449
- const canSubmit = next.submissionAttempts === 0 && !isTouched || !isValidating && !next.isSubmitting && isValid;
450
- next = { ...next,
464
+ const canSubmit = state.submissionAttempts === 0 && !isTouched || !isValidating && !state.isSubmitting && isValid;
465
+ state = { ...state,
451
466
  isFieldsValidating,
452
467
  isFieldsValid,
453
468
  isFormValid,
454
469
  isValid,
455
470
  canSubmit,
456
471
  isTouched
457
- }; // Create a shortcut for the state
458
- // Write it back to the store
459
-
460
- this.store.state = next;
461
- this.state = next;
472
+ };
473
+ this.store.state = state;
474
+ this.state = state;
462
475
  }
463
476
  });
464
477
  this.state = this.store.state;
@@ -467,51 +480,39 @@
467
480
 
468
481
  }
469
482
 
470
- var id = 0;
471
-
472
- function _classPrivateFieldLooseKey(name) {
473
- return "__private_" + id++ + "_" + name;
474
- }
475
-
476
- function _classPrivateFieldLooseBase(receiver, privateKey) {
477
- if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
478
- throw new TypeError("attempted to use private field on non-instance");
479
- }
480
-
481
- return receiver;
482
- }
483
-
484
483
  let uid = 0;
485
484
 
486
- var _updateStore = /*#__PURE__*/_classPrivateFieldLooseKey("updateStore");
485
+ var _prevState = /*#__PURE__*/new WeakMap();
487
486
 
488
- var _leaseValidateAsync = /*#__PURE__*/_classPrivateFieldLooseKey("leaseValidateAsync");
487
+ var _leaseValidateAsync = /*#__PURE__*/new WeakMap();
489
488
 
490
489
  class FieldApi {
491
490
  constructor(_opts) {
492
491
  var _this$getMeta;
493
492
 
494
- this.options = {};
493
+ _defineProperty(this, "uid", void 0);
495
494
 
496
- this.mount = () => {
497
- const info = this.getInfo();
498
- info.instances[this.uid] = this;
499
- const unsubscribe = this.form.store.subscribe(() => {
500
- _classPrivateFieldLooseBase(this, _updateStore)[_updateStore]();
501
- });
502
- return () => {
503
- unsubscribe();
504
- delete info.instances[this.uid];
495
+ _defineProperty(this, "form", void 0);
505
496
 
506
- if (!Object.keys(info.instances).length) {
507
- delete this.form.fieldInfo[this.name];
508
- }
509
- };
510
- };
497
+ _defineProperty(this, "name", void 0);
498
+
499
+ _defineProperty(this, "store", void 0);
511
500
 
512
- Object.defineProperty(this, _updateStore, {
501
+ _defineProperty(this, "state", void 0);
502
+
503
+ _classPrivateFieldInitSpec(this, _prevState, {
513
504
  writable: true,
514
- value: () => {
505
+ value: void 0
506
+ });
507
+
508
+ _defineProperty(this, "options", {});
509
+
510
+ _defineProperty(this, "mount", () => {
511
+ var _this$options$onMount, _this$options;
512
+
513
+ const info = this.getInfo();
514
+ info.instances[this.uid] = this;
515
+ const unsubscribe = this.form.store.subscribe(() => {
515
516
  this.store.batch(() => {
516
517
  const nextValue = this.getValue();
517
518
  const nextMeta = this.getMeta();
@@ -528,17 +529,25 @@
528
529
  }));
529
530
  }
530
531
  });
531
- }
532
+ });
533
+ (_this$options$onMount = (_this$options = this.options).onMount) == null ? void 0 : _this$options$onMount.call(_this$options, this);
534
+ return () => {
535
+ unsubscribe();
536
+ delete info.instances[this.uid];
537
+
538
+ if (!Object.keys(info.instances).length) {
539
+ delete this.form.fieldInfo[this.name];
540
+ }
541
+ };
532
542
  });
533
543
 
534
- this.update = opts => {
535
- var _this$form$options$de, _this$form$options$de2, _this$form$options$de3, _this$form$options$de4;
544
+ _defineProperty(this, "update", opts => {
545
+ var _this$form$options$as, _this$form$options$on, _this$form$options$on2;
536
546
 
537
547
  this.options = {
538
- validatePristine: (_this$form$options$de = this.form.options.defaultValidatePristine) != null ? _this$form$options$de : false,
539
- validateOn: (_this$form$options$de2 = this.form.options.defaultValidateOn) != null ? _this$form$options$de2 : 'change',
540
- validateAsyncOn: (_this$form$options$de3 = this.form.options.defaultValidateAsyncOn) != null ? _this$form$options$de3 : 'blur',
541
- validateAsyncDebounceMs: (_this$form$options$de4 = this.form.options.defaultValidateAsyncDebounceMs) != null ? _this$form$options$de4 : 0,
548
+ asyncDebounceMs: (_this$form$options$as = this.form.options.asyncDebounceMs) != null ? _this$form$options$as : 0,
549
+ onChangeAsyncDebounceMs: (_this$form$options$on = this.form.options.onChangeAsyncDebounceMs) != null ? _this$form$options$on : 0,
550
+ onBlurAsyncDebounceMs: (_this$form$options$on2 = this.form.options.onBlurAsyncDebounceMs) != null ? _this$form$options$on2 : 0,
542
551
  ...opts
543
552
  }; // Default Value
544
553
 
@@ -551,44 +560,42 @@
551
560
  if (this.getMeta() === undefined) {
552
561
  this.setMeta(this.state.meta);
553
562
  }
554
- };
563
+ });
555
564
 
556
- this.getValue = () => {
565
+ _defineProperty(this, "getValue", () => {
557
566
  return this.form.getFieldValue(this.name);
558
- };
567
+ });
559
568
 
560
- this.setValue = (updater, options) => this.form.setFieldValue(this.name, updater, options);
569
+ _defineProperty(this, "setValue", (updater, options) => this.form.setFieldValue(this.name, updater, options));
561
570
 
562
- this.getMeta = () => this.form.getFieldMeta(this.name);
571
+ _defineProperty(this, "getMeta", () => this.form.getFieldMeta(this.name));
563
572
 
564
- this.setMeta = updater => this.form.setFieldMeta(this.name, updater);
573
+ _defineProperty(this, "setMeta", updater => this.form.setFieldMeta(this.name, updater));
565
574
 
566
- this.getInfo = () => this.form.getFieldInfo(this.name);
575
+ _defineProperty(this, "getInfo", () => this.form.getFieldInfo(this.name));
567
576
 
568
- this.pushValue = value => this.form.pushFieldValue(this.name, value);
577
+ _defineProperty(this, "pushValue", value => this.form.pushFieldValue(this.name, value));
569
578
 
570
- this.insertValue = (index, value) => this.form.insertFieldValue(this.name, index, value);
579
+ _defineProperty(this, "insertValue", (index, value) => this.form.insertFieldValue(this.name, index, value));
571
580
 
572
- this.removeValue = index => this.form.removeFieldValue(this.name, index);
581
+ _defineProperty(this, "removeValue", index => this.form.removeFieldValue(this.name, index));
573
582
 
574
- this.swapValues = (aIndex, bIndex) => this.form.swapFieldValues(this.name, aIndex, bIndex);
583
+ _defineProperty(this, "swapValues", (aIndex, bIndex) => this.form.swapFieldValues(this.name, aIndex, bIndex));
575
584
 
576
- this.getSubField = name => new FieldApi({
585
+ _defineProperty(this, "getSubField", name => new FieldApi({
577
586
  name: this.name + "." + name,
578
587
  form: this.form
579
- });
588
+ }));
580
589
 
581
- this.validateSync = async (value = this.state.value) => {
590
+ _defineProperty(this, "validateSync", async (value = this.state.value, cause) => {
582
591
  const {
583
- validate
592
+ onChange,
593
+ onBlur
584
594
  } = this.options;
585
-
586
- if (!validate) {
587
- return;
588
- } // Use the validationCount for all field instances to
595
+ const validate = cause === 'submit' ? undefined : cause === 'change' ? onChange : onBlur;
596
+ if (!validate) return; // Use the validationCount for all field instances to
589
597
  // track freshness of the validation
590
598
 
591
-
592
599
  const validationCount = (this.getInfo().validationCount || 0) + 1;
593
600
  this.getInfo().validationCount = validationCount;
594
601
  const error = normalizeError(validate(value, this));
@@ -603,9 +610,9 @@
603
610
  if (this.state.meta.error) {
604
611
  this.cancelValidateAsync();
605
612
  }
606
- };
613
+ });
607
614
 
608
- Object.defineProperty(this, _leaseValidateAsync, {
615
+ _classPrivateFieldInitSpec(this, _leaseValidateAsync, {
609
616
  writable: true,
610
617
  value: () => {
611
618
  const count = (this.getInfo().validationAsyncCount || 0) + 1;
@@ -614,32 +621,36 @@
614
621
  }
615
622
  });
616
623
 
617
- this.cancelValidateAsync = () => {
624
+ _defineProperty(this, "cancelValidateAsync", () => {
618
625
  // Lease a new validation count to ignore any pending validations
619
- _classPrivateFieldLooseBase(this, _leaseValidateAsync)[_leaseValidateAsync](); // Cancel any pending validation state
626
+ _classPrivateFieldGet(this, _leaseValidateAsync).call(this); // Cancel any pending validation state
620
627
 
621
628
 
622
629
  this.setMeta(prev => ({ ...prev,
623
630
  isValidating: false
624
631
  }));
625
- };
632
+ });
633
+
634
+ _defineProperty(this, "validateAsync", async (value = this.state.value, cause) => {
635
+ var _ref, _ref2;
626
636
 
627
- this.validateAsync = async (value = this.state.value) => {
628
637
  const {
629
- validateAsync,
630
- validateAsyncDebounceMs
638
+ onChangeAsync,
639
+ onBlurAsync,
640
+ onSubmitAsync,
641
+ asyncDebounceMs,
642
+ onBlurAsyncDebounceMs,
643
+ onChangeAsyncDebounceMs
631
644
  } = this.options;
632
-
633
- if (!validateAsync) {
634
- return;
635
- }
636
-
645
+ const validate = cause === 'change' ? onChangeAsync : cause === 'submit' ? onSubmitAsync : onBlurAsync;
646
+ if (!validate) return;
647
+ const debounceMs = cause === 'submit' ? 0 : (_ref = (_ref2 = cause === 'change' ? onChangeAsyncDebounceMs : onBlurAsyncDebounceMs) != null ? _ref2 : asyncDebounceMs) != null ? _ref : 500;
637
648
  if (this.state.meta.isValidating !== true) this.setMeta(prev => ({ ...prev,
638
649
  isValidating: true
639
650
  })); // Use the validationCount for all field instances to
640
651
  // track freshness of the validation
641
652
 
642
- const validationAsyncCount = _classPrivateFieldLooseBase(this, _leaseValidateAsync)[_leaseValidateAsync]();
653
+ const validationAsyncCount = _classPrivateFieldGet(this, _leaseValidateAsync).call(this);
643
654
 
644
655
  const checkLatest = () => validationAsyncCount === this.getInfo().validationAsyncCount;
645
656
 
@@ -650,14 +661,14 @@
650
661
  });
651
662
  }
652
663
 
653
- if (validateAsyncDebounceMs > 0) {
654
- await new Promise(r => setTimeout(r, validateAsyncDebounceMs));
664
+ if (debounceMs > 0) {
665
+ await new Promise(r => setTimeout(r, debounceMs));
655
666
  } // Only kick off validation if this validation is the latest attempt
656
667
 
657
668
 
658
669
  if (checkLatest()) {
659
670
  try {
660
- const rawError = await validateAsync(value, this);
671
+ const rawError = await validate(value, this);
661
672
 
662
673
  if (checkLatest()) {
663
674
  var _this$getInfo$validat, _this$getInfo;
@@ -688,43 +699,25 @@
688
699
 
689
700
 
690
701
  return this.getInfo().validationPromise;
691
- };
692
-
693
- this.shouldValidate = (isAsync, cause) => {
694
- const {
695
- validateOn,
696
- validateAsyncOn
697
- } = this.options;
698
- const level = getValidationCauseLevel(cause); // Must meet *at least* the validation level to validate,
699
- // e.g. if validateOn is 'change' and validateCause is 'blur',
700
- // the field will still validate
701
-
702
- return Object.keys(validateCauseLevels).some(d => isAsync ? validateAsyncOn : validateOn === d && level >= validateCauseLevels[d]);
703
- };
702
+ });
704
703
 
705
- this.validate = async (cause, value) => {
704
+ _defineProperty(this, "validate", (cause, value) => {
706
705
  // If the field is pristine and validatePristine is false, do not validate
707
- if (!this.options.validatePristine && !this.state.meta.isTouched) return; // Attempt to sync validate first
708
-
709
- if (this.shouldValidate(false, cause)) {
710
- this.validateSync(value);
711
- } // If there is an error, return it, do not attempt async validation
706
+ if (!this.state.meta.isTouched) return; // Attempt to sync validate first
712
707
 
708
+ this.validateSync(value, cause); // If there is an error, return it, do not attempt async validation
713
709
 
714
710
  if (this.state.meta.error) {
715
- return this.state.meta.error;
711
+ if (!this.options.asyncAlways) {
712
+ return this.state.meta.error;
713
+ }
716
714
  } // No error? Attempt async validation
717
715
 
718
716
 
719
- if (this.shouldValidate(true, cause)) {
720
- return this.validateAsync(value);
721
- } // If there is no sync error or async validation attempt, there is no error
722
-
723
-
724
- return undefined;
725
- };
717
+ return this.validateAsync(value, cause);
718
+ });
726
719
 
727
- this.getChangeProps = (props = {}) => {
720
+ _defineProperty(this, "getChangeProps", (props = {}) => {
728
721
  return { ...props,
729
722
  value: this.state.value,
730
723
  onChange: value => {
@@ -732,16 +725,21 @@
732
725
  props.onChange == null ? void 0 : props.onChange(value);
733
726
  },
734
727
  onBlur: e => {
728
+ const prevTouched = this.state.meta.isTouched;
735
729
  this.setMeta(prev => ({ ...prev,
736
730
  isTouched: true
737
731
  }));
732
+
733
+ if (!prevTouched) {
734
+ this.validate('change');
735
+ }
736
+
738
737
  this.validate('blur');
739
- props.onBlur == null ? void 0 : props.onBlur(e);
740
738
  }
741
739
  };
742
- };
740
+ });
743
741
 
744
- this.getInputProps = (props = {}) => {
742
+ _defineProperty(this, "getInputProps", (props = {}) => {
745
743
  return { ...props,
746
744
  value: String(this.state.value),
747
745
  onChange: e => {
@@ -750,7 +748,7 @@
750
748
  },
751
749
  onBlur: this.getChangeProps(props).onBlur
752
750
  };
753
- };
751
+ });
754
752
 
755
753
  this.form = _opts.form;
756
754
  this.uid = uid++; // Support field prefixing from FieldScope
@@ -769,31 +767,27 @@
769
767
  ...this.options.defaultMeta
770
768
  }
771
769
  }, {
772
- onUpdate: next => {
773
- next.meta.touchedError = next.meta.isTouched ? next.meta.error : undefined; // Do not validate pristine fields
770
+ onUpdate: () => {
771
+ const state = this.store.state;
772
+ state.meta.touchedError = state.meta.isTouched ? state.meta.error : undefined;
774
773
 
775
- const prevState = this.state;
776
- this.state = next;
777
-
778
- if (next.value !== prevState.value) {
779
- this.validate('change', next.value);
774
+ if (state.value !== _classPrivateFieldGet(this, _prevState).value) {
775
+ this.validate('change', state.value);
780
776
  }
777
+
778
+ _classPrivateFieldSet(this, _prevState, state);
779
+
780
+ this.state = state;
781
781
  }
782
782
  });
783
783
  this.state = this.store.state;
784
+
785
+ _classPrivateFieldSet(this, _prevState, this.state);
786
+
784
787
  this.update(_opts);
785
788
  }
786
789
 
787
790
  }
788
- const validateCauseLevels = {
789
- change: 0,
790
- blur: 1,
791
- submit: 2
792
- };
793
-
794
- function getValidationCauseLevel(cause) {
795
- return !cause ? 3 : validateCauseLevels[cause];
796
- }
797
791
 
798
792
  function normalizeError(rawError) {
799
793
  if (rawError) {