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