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