@tanstack/form-core 0.0.9 → 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.
- package/build/cjs/FieldApi.js +106 -111
- package/build/cjs/FieldApi.js.map +1 -1
- package/build/cjs/FormApi.js +69 -126
- package/build/cjs/FormApi.js.map +1 -1
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +70 -9
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/build/esm/index.js +243 -251
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +74 -68
- package/build/types/FieldApi.d.ts +19 -13
- package/build/types/FormApi.d.ts +13 -9
- package/build/umd/index.development.js +257 -263
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/FieldApi.ts +99 -102
- package/src/FormApi.ts +51 -105
|
@@ -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
|
-
|
|
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
|
|
55
|
-
|
|
56
|
-
this.
|
|
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
|
|
129
|
+
if (this.#batching) return cb();
|
|
130
|
+
this.#batching = true;
|
|
60
131
|
cb();
|
|
61
|
-
this
|
|
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
|
|
164
|
-
|
|
165
|
-
this
|
|
236
|
+
_defineProperty(this, "options", {});
|
|
237
|
+
|
|
238
|
+
_defineProperty(this, "store", void 0);
|
|
239
|
+
|
|
240
|
+
_defineProperty(this, "state", void 0);
|
|
166
241
|
|
|
167
|
-
this
|
|
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(
|
|
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
|
|
264
|
+
_defineProperty(this, "reset", () => this.store.setState(() => getDefaultFormState(this.options.defaultValues)));
|
|
186
265
|
|
|
187
|
-
this
|
|
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
|
-
|
|
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
|
|
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$
|
|
315
|
+
var _this$options$onSubmi, _this$options;
|
|
293
316
|
|
|
294
317
|
done();
|
|
295
|
-
(_this$options$
|
|
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$
|
|
325
|
+
var _this$options$onSubmi2, _this$options2;
|
|
304
326
|
|
|
305
327
|
done();
|
|
306
|
-
(_this$options$
|
|
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$
|
|
333
|
+
var _this$options$onSubmi3, _this$options3;
|
|
312
334
|
|
|
313
335
|
// Run the submit code
|
|
314
|
-
await ((_this$options$
|
|
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
|
|
349
|
+
_defineProperty(this, "getFieldValue", field => getBy(this.state.values, field));
|
|
328
350
|
|
|
329
|
-
this
|
|
351
|
+
_defineProperty(this, "getFieldMeta", field => {
|
|
330
352
|
return this.state.fieldMeta[field];
|
|
331
|
-
};
|
|
353
|
+
});
|
|
332
354
|
|
|
333
|
-
this
|
|
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
|
|
363
|
+
_defineProperty(this, "setFieldMeta", (field, updater) => {
|
|
342
364
|
this.store.setState(prev => {
|
|
343
365
|
return { ...prev,
|
|
344
366
|
fieldMeta: { ...prev.fieldMeta,
|
|
@@ -346,85 +368,76 @@
|
|
|
346
368
|
}
|
|
347
369
|
};
|
|
348
370
|
});
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
this.setFieldValue = (field, updater, opts) => {
|
|
352
|
-
var _opts$touch;
|
|
371
|
+
});
|
|
353
372
|
|
|
354
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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:
|
|
416
|
+
isFormValid: true
|
|
405
417
|
}), {
|
|
406
|
-
onUpdate:
|
|
407
|
-
|
|
408
|
-
|
|
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 ||
|
|
413
|
-
const isFormValid = !
|
|
427
|
+
const isValidating = isFieldsValidating || state.isFormValidating;
|
|
428
|
+
const isFormValid = !state.formError;
|
|
414
429
|
const isValid = isFieldsValid && isFormValid;
|
|
415
|
-
const canSubmit =
|
|
416
|
-
|
|
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
|
-
};
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
this.store.state = next;
|
|
427
|
-
this.state = next;
|
|
438
|
+
};
|
|
439
|
+
this.store.state = state;
|
|
440
|
+
this.state = state;
|
|
428
441
|
}
|
|
429
442
|
});
|
|
430
443
|
this.state = this.store.state;
|
|
@@ -433,51 +446,39 @@
|
|
|
433
446
|
|
|
434
447
|
}
|
|
435
448
|
|
|
436
|
-
var id = 0;
|
|
437
|
-
|
|
438
|
-
function _classPrivateFieldLooseKey(name) {
|
|
439
|
-
return "__private_" + id++ + "_" + name;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
function _classPrivateFieldLooseBase(receiver, privateKey) {
|
|
443
|
-
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
|
|
444
|
-
throw new TypeError("attempted to use private field on non-instance");
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
return receiver;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
449
|
let uid = 0;
|
|
451
450
|
|
|
452
|
-
var
|
|
451
|
+
var _prevState = /*#__PURE__*/new WeakMap();
|
|
453
452
|
|
|
454
|
-
var _leaseValidateAsync = /*#__PURE__*/
|
|
453
|
+
var _leaseValidateAsync = /*#__PURE__*/new WeakMap();
|
|
455
454
|
|
|
456
455
|
class FieldApi {
|
|
457
456
|
constructor(_opts) {
|
|
458
457
|
var _this$getMeta;
|
|
459
458
|
|
|
460
|
-
this
|
|
459
|
+
_defineProperty(this, "uid", void 0);
|
|
461
460
|
|
|
462
|
-
this
|
|
463
|
-
const info = this.getInfo();
|
|
464
|
-
info.instances[this.uid] = this;
|
|
465
|
-
const unsubscribe = this.form.store.subscribe(() => {
|
|
466
|
-
_classPrivateFieldLooseBase(this, _updateStore)[_updateStore]();
|
|
467
|
-
});
|
|
468
|
-
return () => {
|
|
469
|
-
unsubscribe();
|
|
470
|
-
delete info.instances[this.uid];
|
|
461
|
+
_defineProperty(this, "form", void 0);
|
|
471
462
|
|
|
472
|
-
|
|
473
|
-
delete this.form.fieldInfo[this.name];
|
|
474
|
-
}
|
|
475
|
-
};
|
|
476
|
-
};
|
|
463
|
+
_defineProperty(this, "name", void 0);
|
|
477
464
|
|
|
478
|
-
|
|
465
|
+
_defineProperty(this, "store", void 0);
|
|
466
|
+
|
|
467
|
+
_defineProperty(this, "state", void 0);
|
|
468
|
+
|
|
469
|
+
_classPrivateFieldInitSpec(this, _prevState, {
|
|
479
470
|
writable: true,
|
|
480
|
-
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(() => {
|
|
481
482
|
this.store.batch(() => {
|
|
482
483
|
const nextValue = this.getValue();
|
|
483
484
|
const nextMeta = this.getMeta();
|
|
@@ -494,17 +495,25 @@
|
|
|
494
495
|
}));
|
|
495
496
|
}
|
|
496
497
|
});
|
|
497
|
-
}
|
|
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
|
+
};
|
|
498
508
|
});
|
|
499
509
|
|
|
500
|
-
this
|
|
501
|
-
var _this$form$options$
|
|
510
|
+
_defineProperty(this, "update", opts => {
|
|
511
|
+
var _this$form$options$as, _this$form$options$on, _this$form$options$on2;
|
|
502
512
|
|
|
503
513
|
this.options = {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
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,
|
|
508
517
|
...opts
|
|
509
518
|
}; // Default Value
|
|
510
519
|
|
|
@@ -517,44 +526,42 @@
|
|
|
517
526
|
if (this.getMeta() === undefined) {
|
|
518
527
|
this.setMeta(this.state.meta);
|
|
519
528
|
}
|
|
520
|
-
};
|
|
529
|
+
});
|
|
521
530
|
|
|
522
|
-
this
|
|
531
|
+
_defineProperty(this, "getValue", () => {
|
|
523
532
|
return this.form.getFieldValue(this.name);
|
|
524
|
-
};
|
|
533
|
+
});
|
|
525
534
|
|
|
526
|
-
this
|
|
535
|
+
_defineProperty(this, "setValue", (updater, options) => this.form.setFieldValue(this.name, updater, options));
|
|
527
536
|
|
|
528
|
-
this
|
|
537
|
+
_defineProperty(this, "getMeta", () => this.form.getFieldMeta(this.name));
|
|
529
538
|
|
|
530
|
-
this
|
|
539
|
+
_defineProperty(this, "setMeta", updater => this.form.setFieldMeta(this.name, updater));
|
|
531
540
|
|
|
532
|
-
this
|
|
541
|
+
_defineProperty(this, "getInfo", () => this.form.getFieldInfo(this.name));
|
|
533
542
|
|
|
534
|
-
this
|
|
543
|
+
_defineProperty(this, "pushValue", value => this.form.pushFieldValue(this.name, value));
|
|
535
544
|
|
|
536
|
-
this
|
|
545
|
+
_defineProperty(this, "insertValue", (index, value) => this.form.insertFieldValue(this.name, index, value));
|
|
537
546
|
|
|
538
|
-
this
|
|
547
|
+
_defineProperty(this, "removeValue", index => this.form.removeFieldValue(this.name, index));
|
|
539
548
|
|
|
540
|
-
this
|
|
549
|
+
_defineProperty(this, "swapValues", (aIndex, bIndex) => this.form.swapFieldValues(this.name, aIndex, bIndex));
|
|
541
550
|
|
|
542
|
-
this
|
|
551
|
+
_defineProperty(this, "getSubField", name => new FieldApi({
|
|
543
552
|
name: this.name + "." + name,
|
|
544
553
|
form: this.form
|
|
545
|
-
});
|
|
554
|
+
}));
|
|
546
555
|
|
|
547
|
-
this
|
|
556
|
+
_defineProperty(this, "validateSync", async (value = this.state.value, cause) => {
|
|
548
557
|
const {
|
|
549
|
-
|
|
558
|
+
onChange,
|
|
559
|
+
onBlur
|
|
550
560
|
} = this.options;
|
|
551
|
-
|
|
552
|
-
if (!validate)
|
|
553
|
-
return;
|
|
554
|
-
} // 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
|
|
555
563
|
// track freshness of the validation
|
|
556
564
|
|
|
557
|
-
|
|
558
565
|
const validationCount = (this.getInfo().validationCount || 0) + 1;
|
|
559
566
|
this.getInfo().validationCount = validationCount;
|
|
560
567
|
const error = normalizeError(validate(value, this));
|
|
@@ -569,9 +576,9 @@
|
|
|
569
576
|
if (this.state.meta.error) {
|
|
570
577
|
this.cancelValidateAsync();
|
|
571
578
|
}
|
|
572
|
-
};
|
|
579
|
+
});
|
|
573
580
|
|
|
574
|
-
|
|
581
|
+
_classPrivateFieldInitSpec(this, _leaseValidateAsync, {
|
|
575
582
|
writable: true,
|
|
576
583
|
value: () => {
|
|
577
584
|
const count = (this.getInfo().validationAsyncCount || 0) + 1;
|
|
@@ -580,32 +587,36 @@
|
|
|
580
587
|
}
|
|
581
588
|
});
|
|
582
589
|
|
|
583
|
-
this
|
|
590
|
+
_defineProperty(this, "cancelValidateAsync", () => {
|
|
584
591
|
// Lease a new validation count to ignore any pending validations
|
|
585
|
-
|
|
592
|
+
_classPrivateFieldGet(this, _leaseValidateAsync).call(this); // Cancel any pending validation state
|
|
586
593
|
|
|
587
594
|
|
|
588
595
|
this.setMeta(prev => ({ ...prev,
|
|
589
596
|
isValidating: false
|
|
590
597
|
}));
|
|
591
|
-
};
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
_defineProperty(this, "validateAsync", async (value = this.state.value, cause) => {
|
|
601
|
+
var _ref, _ref2;
|
|
592
602
|
|
|
593
|
-
this.validateAsync = async (value = this.state.value) => {
|
|
594
603
|
const {
|
|
595
|
-
|
|
596
|
-
|
|
604
|
+
onChangeAsync,
|
|
605
|
+
onBlurAsync,
|
|
606
|
+
onSubmitAsync,
|
|
607
|
+
asyncDebounceMs,
|
|
608
|
+
onBlurAsyncDebounceMs,
|
|
609
|
+
onChangeAsyncDebounceMs
|
|
597
610
|
} = this.options;
|
|
598
|
-
|
|
599
|
-
if (!
|
|
600
|
-
|
|
601
|
-
}
|
|
602
|
-
|
|
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;
|
|
603
614
|
if (this.state.meta.isValidating !== true) this.setMeta(prev => ({ ...prev,
|
|
604
615
|
isValidating: true
|
|
605
616
|
})); // Use the validationCount for all field instances to
|
|
606
617
|
// track freshness of the validation
|
|
607
618
|
|
|
608
|
-
const validationAsyncCount =
|
|
619
|
+
const validationAsyncCount = _classPrivateFieldGet(this, _leaseValidateAsync).call(this);
|
|
609
620
|
|
|
610
621
|
const checkLatest = () => validationAsyncCount === this.getInfo().validationAsyncCount;
|
|
611
622
|
|
|
@@ -616,14 +627,14 @@
|
|
|
616
627
|
});
|
|
617
628
|
}
|
|
618
629
|
|
|
619
|
-
if (
|
|
620
|
-
await new Promise(r => setTimeout(r,
|
|
630
|
+
if (debounceMs > 0) {
|
|
631
|
+
await new Promise(r => setTimeout(r, debounceMs));
|
|
621
632
|
} // Only kick off validation if this validation is the latest attempt
|
|
622
633
|
|
|
623
634
|
|
|
624
635
|
if (checkLatest()) {
|
|
625
636
|
try {
|
|
626
|
-
const rawError = await
|
|
637
|
+
const rawError = await validate(value, this);
|
|
627
638
|
|
|
628
639
|
if (checkLatest()) {
|
|
629
640
|
var _this$getInfo$validat, _this$getInfo;
|
|
@@ -654,43 +665,25 @@
|
|
|
654
665
|
|
|
655
666
|
|
|
656
667
|
return this.getInfo().validationPromise;
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
this.shouldValidate = (isAsync, cause) => {
|
|
660
|
-
const {
|
|
661
|
-
validateOn,
|
|
662
|
-
validateAsyncOn
|
|
663
|
-
} = this.options;
|
|
664
|
-
const level = getValidationCauseLevel(cause); // Must meet *at least* the validation level to validate,
|
|
665
|
-
// e.g. if validateOn is 'change' and validateCause is 'blur',
|
|
666
|
-
// the field will still validate
|
|
667
|
-
|
|
668
|
-
return Object.keys(validateCauseLevels).some(d => isAsync ? validateAsyncOn : validateOn === d && level >= validateCauseLevels[d]);
|
|
669
|
-
};
|
|
668
|
+
});
|
|
670
669
|
|
|
671
|
-
this
|
|
670
|
+
_defineProperty(this, "validate", (cause, value) => {
|
|
672
671
|
// If the field is pristine and validatePristine is false, do not validate
|
|
673
|
-
if (!this.
|
|
674
|
-
|
|
675
|
-
if (this.shouldValidate(false, cause)) {
|
|
676
|
-
this.validateSync(value);
|
|
677
|
-
} // If there is an error, return it, do not attempt async validation
|
|
672
|
+
if (!this.state.meta.isTouched) return; // Attempt to sync validate first
|
|
678
673
|
|
|
674
|
+
this.validateSync(value, cause); // If there is an error, return it, do not attempt async validation
|
|
679
675
|
|
|
680
676
|
if (this.state.meta.error) {
|
|
681
|
-
|
|
677
|
+
if (!this.options.asyncAlways) {
|
|
678
|
+
return this.state.meta.error;
|
|
679
|
+
}
|
|
682
680
|
} // No error? Attempt async validation
|
|
683
681
|
|
|
684
682
|
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
} // If there is no sync error or async validation attempt, there is no error
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
return undefined;
|
|
691
|
-
};
|
|
683
|
+
return this.validateAsync(value, cause);
|
|
684
|
+
});
|
|
692
685
|
|
|
693
|
-
this
|
|
686
|
+
_defineProperty(this, "getChangeProps", (props = {}) => {
|
|
694
687
|
return { ...props,
|
|
695
688
|
value: this.state.value,
|
|
696
689
|
onChange: value => {
|
|
@@ -698,16 +691,21 @@
|
|
|
698
691
|
props.onChange == null ? void 0 : props.onChange(value);
|
|
699
692
|
},
|
|
700
693
|
onBlur: e => {
|
|
694
|
+
const prevTouched = this.state.meta.isTouched;
|
|
701
695
|
this.setMeta(prev => ({ ...prev,
|
|
702
696
|
isTouched: true
|
|
703
697
|
}));
|
|
698
|
+
|
|
699
|
+
if (!prevTouched) {
|
|
700
|
+
this.validate('change');
|
|
701
|
+
}
|
|
702
|
+
|
|
704
703
|
this.validate('blur');
|
|
705
|
-
props.onBlur == null ? void 0 : props.onBlur(e);
|
|
706
704
|
}
|
|
707
705
|
};
|
|
708
|
-
};
|
|
706
|
+
});
|
|
709
707
|
|
|
710
|
-
this
|
|
708
|
+
_defineProperty(this, "getInputProps", (props = {}) => {
|
|
711
709
|
return { ...props,
|
|
712
710
|
value: String(this.state.value),
|
|
713
711
|
onChange: e => {
|
|
@@ -716,7 +714,7 @@
|
|
|
716
714
|
},
|
|
717
715
|
onBlur: this.getChangeProps(props).onBlur
|
|
718
716
|
};
|
|
719
|
-
};
|
|
717
|
+
});
|
|
720
718
|
|
|
721
719
|
this.form = _opts.form;
|
|
722
720
|
this.uid = uid++; // Support field prefixing from FieldScope
|
|
@@ -735,31 +733,27 @@
|
|
|
735
733
|
...this.options.defaultMeta
|
|
736
734
|
}
|
|
737
735
|
}, {
|
|
738
|
-
onUpdate:
|
|
739
|
-
|
|
736
|
+
onUpdate: () => {
|
|
737
|
+
const state = this.store.state;
|
|
738
|
+
state.meta.touchedError = state.meta.isTouched ? state.meta.error : undefined;
|
|
740
739
|
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
if (next.value !== prevState.value) {
|
|
745
|
-
this.validate('change', next.value);
|
|
740
|
+
if (state.value !== _classPrivateFieldGet(this, _prevState).value) {
|
|
741
|
+
this.validate('change', state.value);
|
|
746
742
|
}
|
|
743
|
+
|
|
744
|
+
_classPrivateFieldSet(this, _prevState, state);
|
|
745
|
+
|
|
746
|
+
this.state = state;
|
|
747
747
|
}
|
|
748
748
|
});
|
|
749
749
|
this.state = this.store.state;
|
|
750
|
+
|
|
751
|
+
_classPrivateFieldSet(this, _prevState, this.state);
|
|
752
|
+
|
|
750
753
|
this.update(_opts);
|
|
751
754
|
}
|
|
752
755
|
|
|
753
756
|
}
|
|
754
|
-
const validateCauseLevels = {
|
|
755
|
-
change: 0,
|
|
756
|
-
blur: 1,
|
|
757
|
-
submit: 2
|
|
758
|
-
};
|
|
759
|
-
|
|
760
|
-
function getValidationCauseLevel(cause) {
|
|
761
|
-
return !cause ? 3 : validateCauseLevels[cause];
|
|
762
|
-
}
|
|
763
757
|
|
|
764
758
|
function normalizeError(rawError) {
|
|
765
759
|
if (rawError) {
|