@nutui/nutui-react-taro 2.3.5 → 2.3.6

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.
@@ -0,0 +1,1406 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+ import React__default, { createContext, useRef } from "react";
8
+ import { C as Cell } from "./cell.taro-jD6MoFTj.js";
9
+ import { C as ComponentDefaults } from "./typings-buMpDfSR.js";
10
+ const Context = createContext({});
11
+ function _extends() {
12
+ _extends = Object.assign ? Object.assign.bind() : function(target) {
13
+ for (var i = 1; i < arguments.length; i++) {
14
+ var source = arguments[i];
15
+ for (var key in source) {
16
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
17
+ target[key] = source[key];
18
+ }
19
+ }
20
+ }
21
+ return target;
22
+ };
23
+ return _extends.apply(this, arguments);
24
+ }
25
+ function _inheritsLoose(subClass, superClass) {
26
+ subClass.prototype = Object.create(superClass.prototype);
27
+ subClass.prototype.constructor = subClass;
28
+ _setPrototypeOf(subClass, superClass);
29
+ }
30
+ function _getPrototypeOf(o) {
31
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf2(o2) {
32
+ return o2.__proto__ || Object.getPrototypeOf(o2);
33
+ };
34
+ return _getPrototypeOf(o);
35
+ }
36
+ function _setPrototypeOf(o, p) {
37
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf2(o2, p2) {
38
+ o2.__proto__ = p2;
39
+ return o2;
40
+ };
41
+ return _setPrototypeOf(o, p);
42
+ }
43
+ function _isNativeReflectConstruct() {
44
+ if (typeof Reflect === "undefined" || !Reflect.construct)
45
+ return false;
46
+ if (Reflect.construct.sham)
47
+ return false;
48
+ if (typeof Proxy === "function")
49
+ return true;
50
+ try {
51
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {
52
+ }));
53
+ return true;
54
+ } catch (e) {
55
+ return false;
56
+ }
57
+ }
58
+ function _construct(Parent, args, Class) {
59
+ if (_isNativeReflectConstruct()) {
60
+ _construct = Reflect.construct.bind();
61
+ } else {
62
+ _construct = function _construct2(Parent2, args2, Class2) {
63
+ var a = [null];
64
+ a.push.apply(a, args2);
65
+ var Constructor = Function.bind.apply(Parent2, a);
66
+ var instance = new Constructor();
67
+ if (Class2)
68
+ _setPrototypeOf(instance, Class2.prototype);
69
+ return instance;
70
+ };
71
+ }
72
+ return _construct.apply(null, arguments);
73
+ }
74
+ function _isNativeFunction(fn) {
75
+ return Function.toString.call(fn).indexOf("[native code]") !== -1;
76
+ }
77
+ function _wrapNativeSuper(Class) {
78
+ var _cache = typeof Map === "function" ? /* @__PURE__ */ new Map() : void 0;
79
+ _wrapNativeSuper = function _wrapNativeSuper2(Class2) {
80
+ if (Class2 === null || !_isNativeFunction(Class2))
81
+ return Class2;
82
+ if (typeof Class2 !== "function") {
83
+ throw new TypeError("Super expression must either be null or a function");
84
+ }
85
+ if (typeof _cache !== "undefined") {
86
+ if (_cache.has(Class2))
87
+ return _cache.get(Class2);
88
+ _cache.set(Class2, Wrapper);
89
+ }
90
+ function Wrapper() {
91
+ return _construct(Class2, arguments, _getPrototypeOf(this).constructor);
92
+ }
93
+ Wrapper.prototype = Object.create(Class2.prototype, {
94
+ constructor: {
95
+ value: Wrapper,
96
+ enumerable: false,
97
+ writable: true,
98
+ configurable: true
99
+ }
100
+ });
101
+ return _setPrototypeOf(Wrapper, Class2);
102
+ };
103
+ return _wrapNativeSuper(Class);
104
+ }
105
+ var formatRegExp = /%[sdj%]/g;
106
+ var warning = function warning2() {
107
+ };
108
+ if (typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" && typeof window !== "undefined" && typeof document !== "undefined") {
109
+ warning = function warning3(type4, errors) {
110
+ if (typeof console !== "undefined" && console.warn && typeof ASYNC_VALIDATOR_NO_WARNING === "undefined") {
111
+ if (errors.every(function(e) {
112
+ return typeof e === "string";
113
+ })) {
114
+ console.warn(type4, errors);
115
+ }
116
+ }
117
+ };
118
+ }
119
+ function convertFieldsError(errors) {
120
+ if (!errors || !errors.length)
121
+ return null;
122
+ var fields = {};
123
+ errors.forEach(function(error) {
124
+ var field = error.field;
125
+ fields[field] = fields[field] || [];
126
+ fields[field].push(error);
127
+ });
128
+ return fields;
129
+ }
130
+ function format(template) {
131
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
132
+ args[_key - 1] = arguments[_key];
133
+ }
134
+ var i = 0;
135
+ var len = args.length;
136
+ if (typeof template === "function") {
137
+ return template.apply(null, args);
138
+ }
139
+ if (typeof template === "string") {
140
+ var str = template.replace(formatRegExp, function(x) {
141
+ if (x === "%%") {
142
+ return "%";
143
+ }
144
+ if (i >= len) {
145
+ return x;
146
+ }
147
+ switch (x) {
148
+ case "%s":
149
+ return String(args[i++]);
150
+ case "%d":
151
+ return Number(args[i++]);
152
+ case "%j":
153
+ try {
154
+ return JSON.stringify(args[i++]);
155
+ } catch (_) {
156
+ return "[Circular]";
157
+ }
158
+ break;
159
+ default:
160
+ return x;
161
+ }
162
+ });
163
+ return str;
164
+ }
165
+ return template;
166
+ }
167
+ function isNativeStringType(type4) {
168
+ return type4 === "string" || type4 === "url" || type4 === "hex" || type4 === "email" || type4 === "date" || type4 === "pattern";
169
+ }
170
+ function isEmptyValue(value, type4) {
171
+ if (value === void 0 || value === null) {
172
+ return true;
173
+ }
174
+ if (type4 === "array" && Array.isArray(value) && !value.length) {
175
+ return true;
176
+ }
177
+ if (isNativeStringType(type4) && typeof value === "string" && !value) {
178
+ return true;
179
+ }
180
+ return false;
181
+ }
182
+ function asyncParallelArray(arr, func, callback) {
183
+ var results = [];
184
+ var total = 0;
185
+ var arrLength = arr.length;
186
+ function count(errors) {
187
+ results.push.apply(results, errors || []);
188
+ total++;
189
+ if (total === arrLength) {
190
+ callback(results);
191
+ }
192
+ }
193
+ arr.forEach(function(a) {
194
+ func(a, count);
195
+ });
196
+ }
197
+ function asyncSerialArray(arr, func, callback) {
198
+ var index = 0;
199
+ var arrLength = arr.length;
200
+ function next(errors) {
201
+ if (errors && errors.length) {
202
+ callback(errors);
203
+ return;
204
+ }
205
+ var original = index;
206
+ index = index + 1;
207
+ if (original < arrLength) {
208
+ func(arr[original], next);
209
+ } else {
210
+ callback([]);
211
+ }
212
+ }
213
+ next([]);
214
+ }
215
+ function flattenObjArr(objArr) {
216
+ var ret = [];
217
+ Object.keys(objArr).forEach(function(k) {
218
+ ret.push.apply(ret, objArr[k] || []);
219
+ });
220
+ return ret;
221
+ }
222
+ var AsyncValidationError = /* @__PURE__ */ function(_Error) {
223
+ _inheritsLoose(AsyncValidationError2, _Error);
224
+ function AsyncValidationError2(errors, fields) {
225
+ var _this;
226
+ _this = _Error.call(this, "Async Validation Error") || this;
227
+ _this.errors = errors;
228
+ _this.fields = fields;
229
+ return _this;
230
+ }
231
+ return AsyncValidationError2;
232
+ }(/* @__PURE__ */ _wrapNativeSuper(Error));
233
+ function asyncMap(objArr, option, func, callback, source) {
234
+ if (option.first) {
235
+ var _pending = new Promise(function(resolve, reject) {
236
+ var next = function next2(errors) {
237
+ callback(errors);
238
+ return errors.length ? reject(new AsyncValidationError(errors, convertFieldsError(errors))) : resolve(source);
239
+ };
240
+ var flattenArr = flattenObjArr(objArr);
241
+ asyncSerialArray(flattenArr, func, next);
242
+ });
243
+ _pending["catch"](function(e) {
244
+ return e;
245
+ });
246
+ return _pending;
247
+ }
248
+ var firstFields = option.firstFields === true ? Object.keys(objArr) : option.firstFields || [];
249
+ var objArrKeys = Object.keys(objArr);
250
+ var objArrLength = objArrKeys.length;
251
+ var total = 0;
252
+ var results = [];
253
+ var pending = new Promise(function(resolve, reject) {
254
+ var next = function next2(errors) {
255
+ results.push.apply(results, errors);
256
+ total++;
257
+ if (total === objArrLength) {
258
+ callback(results);
259
+ return results.length ? reject(new AsyncValidationError(results, convertFieldsError(results))) : resolve(source);
260
+ }
261
+ };
262
+ if (!objArrKeys.length) {
263
+ callback(results);
264
+ resolve(source);
265
+ }
266
+ objArrKeys.forEach(function(key) {
267
+ var arr = objArr[key];
268
+ if (firstFields.indexOf(key) !== -1) {
269
+ asyncSerialArray(arr, func, next);
270
+ } else {
271
+ asyncParallelArray(arr, func, next);
272
+ }
273
+ });
274
+ });
275
+ pending["catch"](function(e) {
276
+ return e;
277
+ });
278
+ return pending;
279
+ }
280
+ function isErrorObj(obj) {
281
+ return !!(obj && obj.message !== void 0);
282
+ }
283
+ function getValue(value, path) {
284
+ var v = value;
285
+ for (var i = 0; i < path.length; i++) {
286
+ if (v == void 0) {
287
+ return v;
288
+ }
289
+ v = v[path[i]];
290
+ }
291
+ return v;
292
+ }
293
+ function complementError(rule, source) {
294
+ return function(oe) {
295
+ var fieldValue;
296
+ if (rule.fullFields) {
297
+ fieldValue = getValue(source, rule.fullFields);
298
+ } else {
299
+ fieldValue = source[oe.field || rule.fullField];
300
+ }
301
+ if (isErrorObj(oe)) {
302
+ oe.field = oe.field || rule.fullField;
303
+ oe.fieldValue = fieldValue;
304
+ return oe;
305
+ }
306
+ return {
307
+ message: typeof oe === "function" ? oe() : oe,
308
+ fieldValue,
309
+ field: oe.field || rule.fullField
310
+ };
311
+ };
312
+ }
313
+ function deepMerge(target, source) {
314
+ if (source) {
315
+ for (var s in source) {
316
+ if (source.hasOwnProperty(s)) {
317
+ var value = source[s];
318
+ if (typeof value === "object" && typeof target[s] === "object") {
319
+ target[s] = _extends({}, target[s], value);
320
+ } else {
321
+ target[s] = value;
322
+ }
323
+ }
324
+ }
325
+ }
326
+ return target;
327
+ }
328
+ var required$1 = function required(rule, value, source, errors, options, type4) {
329
+ if (rule.required && (!source.hasOwnProperty(rule.field) || isEmptyValue(value, type4 || rule.type))) {
330
+ errors.push(format(options.messages.required, rule.fullField));
331
+ }
332
+ };
333
+ var whitespace = function whitespace2(rule, value, source, errors, options) {
334
+ if (/^\s+$/.test(value) || value === "") {
335
+ errors.push(format(options.messages.whitespace, rule.fullField));
336
+ }
337
+ };
338
+ var urlReg;
339
+ var getUrlRegex = function() {
340
+ if (urlReg) {
341
+ return urlReg;
342
+ }
343
+ var word = "[a-fA-F\\d:]";
344
+ var b = function b2(options) {
345
+ return options && options.includeBoundaries ? "(?:(?<=\\s|^)(?=" + word + ")|(?<=" + word + ")(?=\\s|$))" : "";
346
+ };
347
+ var v4 = "(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}";
348
+ var v6seg = "[a-fA-F\\d]{1,4}";
349
+ var v6 = ("\n(?:\n(?:" + v6seg + ":){7}(?:" + v6seg + "|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8\n(?:" + v6seg + ":){6}(?:" + v4 + "|:" + v6seg + "|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4\n(?:" + v6seg + ":){5}(?::" + v4 + "|(?::" + v6seg + "){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4\n(?:" + v6seg + ":){4}(?:(?::" + v6seg + "){0,1}:" + v4 + "|(?::" + v6seg + "){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4\n(?:" + v6seg + ":){3}(?:(?::" + v6seg + "){0,2}:" + v4 + "|(?::" + v6seg + "){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4\n(?:" + v6seg + ":){2}(?:(?::" + v6seg + "){0,3}:" + v4 + "|(?::" + v6seg + "){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4\n(?:" + v6seg + ":){1}(?:(?::" + v6seg + "){0,4}:" + v4 + "|(?::" + v6seg + "){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4\n(?::(?:(?::" + v6seg + "){0,5}:" + v4 + "|(?::" + v6seg + "){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4\n)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1\n").replace(/\s*\/\/.*$/gm, "").replace(/\n/g, "").trim();
350
+ var v46Exact = new RegExp("(?:^" + v4 + "$)|(?:^" + v6 + "$)");
351
+ var v4exact = new RegExp("^" + v4 + "$");
352
+ var v6exact = new RegExp("^" + v6 + "$");
353
+ var ip = function ip2(options) {
354
+ return options && options.exact ? v46Exact : new RegExp("(?:" + b(options) + v4 + b(options) + ")|(?:" + b(options) + v6 + b(options) + ")", "g");
355
+ };
356
+ ip.v4 = function(options) {
357
+ return options && options.exact ? v4exact : new RegExp("" + b(options) + v4 + b(options), "g");
358
+ };
359
+ ip.v6 = function(options) {
360
+ return options && options.exact ? v6exact : new RegExp("" + b(options) + v6 + b(options), "g");
361
+ };
362
+ var protocol = "(?:(?:[a-z]+:)?//)";
363
+ var auth = "(?:\\S+(?::\\S*)?@)?";
364
+ var ipv4 = ip.v4().source;
365
+ var ipv6 = ip.v6().source;
366
+ var host = "(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)";
367
+ var domain = "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*";
368
+ var tld = "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))";
369
+ var port = "(?::\\d{2,5})?";
370
+ var path = '(?:[/?#][^\\s"]*)?';
371
+ var regex = "(?:" + protocol + "|www\\.)" + auth + "(?:localhost|" + ipv4 + "|" + ipv6 + "|" + host + domain + tld + ")" + port + path;
372
+ urlReg = new RegExp("(?:^" + regex + "$)", "i");
373
+ return urlReg;
374
+ };
375
+ var pattern$2 = {
376
+ // http://emailregex.com/
377
+ email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/,
378
+ // url: new RegExp(
379
+ // '^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
380
+ // 'i',
381
+ // ),
382
+ hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i
383
+ };
384
+ var types = {
385
+ integer: function integer(value) {
386
+ return types.number(value) && parseInt(value, 10) === value;
387
+ },
388
+ "float": function float(value) {
389
+ return types.number(value) && !types.integer(value);
390
+ },
391
+ array: function array(value) {
392
+ return Array.isArray(value);
393
+ },
394
+ regexp: function regexp(value) {
395
+ if (value instanceof RegExp) {
396
+ return true;
397
+ }
398
+ try {
399
+ return !!new RegExp(value);
400
+ } catch (e) {
401
+ return false;
402
+ }
403
+ },
404
+ date: function date(value) {
405
+ return typeof value.getTime === "function" && typeof value.getMonth === "function" && typeof value.getYear === "function" && !isNaN(value.getTime());
406
+ },
407
+ number: function number(value) {
408
+ if (isNaN(value)) {
409
+ return false;
410
+ }
411
+ return typeof value === "number";
412
+ },
413
+ object: function object(value) {
414
+ return typeof value === "object" && !types.array(value);
415
+ },
416
+ method: function method(value) {
417
+ return typeof value === "function";
418
+ },
419
+ email: function email(value) {
420
+ return typeof value === "string" && value.length <= 320 && !!value.match(pattern$2.email);
421
+ },
422
+ url: function url(value) {
423
+ return typeof value === "string" && value.length <= 2048 && !!value.match(getUrlRegex());
424
+ },
425
+ hex: function hex(value) {
426
+ return typeof value === "string" && !!value.match(pattern$2.hex);
427
+ }
428
+ };
429
+ var type$1 = function type(rule, value, source, errors, options) {
430
+ if (rule.required && value === void 0) {
431
+ required$1(rule, value, source, errors, options);
432
+ return;
433
+ }
434
+ var custom = ["integer", "float", "array", "regexp", "object", "method", "email", "number", "date", "url", "hex"];
435
+ var ruleType = rule.type;
436
+ if (custom.indexOf(ruleType) > -1) {
437
+ if (!types[ruleType](value)) {
438
+ errors.push(format(options.messages.types[ruleType], rule.fullField, rule.type));
439
+ }
440
+ } else if (ruleType && typeof value !== rule.type) {
441
+ errors.push(format(options.messages.types[ruleType], rule.fullField, rule.type));
442
+ }
443
+ };
444
+ var range = function range2(rule, value, source, errors, options) {
445
+ var len = typeof rule.len === "number";
446
+ var min = typeof rule.min === "number";
447
+ var max = typeof rule.max === "number";
448
+ var spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
449
+ var val = value;
450
+ var key = null;
451
+ var num = typeof value === "number";
452
+ var str = typeof value === "string";
453
+ var arr = Array.isArray(value);
454
+ if (num) {
455
+ key = "number";
456
+ } else if (str) {
457
+ key = "string";
458
+ } else if (arr) {
459
+ key = "array";
460
+ }
461
+ if (!key) {
462
+ return false;
463
+ }
464
+ if (arr) {
465
+ val = value.length;
466
+ }
467
+ if (str) {
468
+ val = value.replace(spRegexp, "_").length;
469
+ }
470
+ if (len) {
471
+ if (val !== rule.len) {
472
+ errors.push(format(options.messages[key].len, rule.fullField, rule.len));
473
+ }
474
+ } else if (min && !max && val < rule.min) {
475
+ errors.push(format(options.messages[key].min, rule.fullField, rule.min));
476
+ } else if (max && !min && val > rule.max) {
477
+ errors.push(format(options.messages[key].max, rule.fullField, rule.max));
478
+ } else if (min && max && (val < rule.min || val > rule.max)) {
479
+ errors.push(format(options.messages[key].range, rule.fullField, rule.min, rule.max));
480
+ }
481
+ };
482
+ var ENUM$1 = "enum";
483
+ var enumerable$1 = function enumerable(rule, value, source, errors, options) {
484
+ rule[ENUM$1] = Array.isArray(rule[ENUM$1]) ? rule[ENUM$1] : [];
485
+ if (rule[ENUM$1].indexOf(value) === -1) {
486
+ errors.push(format(options.messages[ENUM$1], rule.fullField, rule[ENUM$1].join(", ")));
487
+ }
488
+ };
489
+ var pattern$1 = function pattern(rule, value, source, errors, options) {
490
+ if (rule.pattern) {
491
+ if (rule.pattern instanceof RegExp) {
492
+ rule.pattern.lastIndex = 0;
493
+ if (!rule.pattern.test(value)) {
494
+ errors.push(format(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
495
+ }
496
+ } else if (typeof rule.pattern === "string") {
497
+ var _pattern = new RegExp(rule.pattern);
498
+ if (!_pattern.test(value)) {
499
+ errors.push(format(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
500
+ }
501
+ }
502
+ }
503
+ };
504
+ var rules = {
505
+ required: required$1,
506
+ whitespace,
507
+ type: type$1,
508
+ range,
509
+ "enum": enumerable$1,
510
+ pattern: pattern$1
511
+ };
512
+ var string = function string2(rule, value, callback, source, options) {
513
+ var errors = [];
514
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
515
+ if (validate) {
516
+ if (isEmptyValue(value, "string") && !rule.required) {
517
+ return callback();
518
+ }
519
+ rules.required(rule, value, source, errors, options, "string");
520
+ if (!isEmptyValue(value, "string")) {
521
+ rules.type(rule, value, source, errors, options);
522
+ rules.range(rule, value, source, errors, options);
523
+ rules.pattern(rule, value, source, errors, options);
524
+ if (rule.whitespace === true) {
525
+ rules.whitespace(rule, value, source, errors, options);
526
+ }
527
+ }
528
+ }
529
+ callback(errors);
530
+ };
531
+ var method2 = function method3(rule, value, callback, source, options) {
532
+ var errors = [];
533
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
534
+ if (validate) {
535
+ if (isEmptyValue(value) && !rule.required) {
536
+ return callback();
537
+ }
538
+ rules.required(rule, value, source, errors, options);
539
+ if (value !== void 0) {
540
+ rules.type(rule, value, source, errors, options);
541
+ }
542
+ }
543
+ callback(errors);
544
+ };
545
+ var number2 = function number3(rule, value, callback, source, options) {
546
+ var errors = [];
547
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
548
+ if (validate) {
549
+ if (value === "") {
550
+ value = void 0;
551
+ }
552
+ if (isEmptyValue(value) && !rule.required) {
553
+ return callback();
554
+ }
555
+ rules.required(rule, value, source, errors, options);
556
+ if (value !== void 0) {
557
+ rules.type(rule, value, source, errors, options);
558
+ rules.range(rule, value, source, errors, options);
559
+ }
560
+ }
561
+ callback(errors);
562
+ };
563
+ var _boolean = function _boolean2(rule, value, callback, source, options) {
564
+ var errors = [];
565
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
566
+ if (validate) {
567
+ if (isEmptyValue(value) && !rule.required) {
568
+ return callback();
569
+ }
570
+ rules.required(rule, value, source, errors, options);
571
+ if (value !== void 0) {
572
+ rules.type(rule, value, source, errors, options);
573
+ }
574
+ }
575
+ callback(errors);
576
+ };
577
+ var regexp2 = function regexp3(rule, value, callback, source, options) {
578
+ var errors = [];
579
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
580
+ if (validate) {
581
+ if (isEmptyValue(value) && !rule.required) {
582
+ return callback();
583
+ }
584
+ rules.required(rule, value, source, errors, options);
585
+ if (!isEmptyValue(value)) {
586
+ rules.type(rule, value, source, errors, options);
587
+ }
588
+ }
589
+ callback(errors);
590
+ };
591
+ var integer2 = function integer3(rule, value, callback, source, options) {
592
+ var errors = [];
593
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
594
+ if (validate) {
595
+ if (isEmptyValue(value) && !rule.required) {
596
+ return callback();
597
+ }
598
+ rules.required(rule, value, source, errors, options);
599
+ if (value !== void 0) {
600
+ rules.type(rule, value, source, errors, options);
601
+ rules.range(rule, value, source, errors, options);
602
+ }
603
+ }
604
+ callback(errors);
605
+ };
606
+ var floatFn = function floatFn2(rule, value, callback, source, options) {
607
+ var errors = [];
608
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
609
+ if (validate) {
610
+ if (isEmptyValue(value) && !rule.required) {
611
+ return callback();
612
+ }
613
+ rules.required(rule, value, source, errors, options);
614
+ if (value !== void 0) {
615
+ rules.type(rule, value, source, errors, options);
616
+ rules.range(rule, value, source, errors, options);
617
+ }
618
+ }
619
+ callback(errors);
620
+ };
621
+ var array2 = function array3(rule, value, callback, source, options) {
622
+ var errors = [];
623
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
624
+ if (validate) {
625
+ if ((value === void 0 || value === null) && !rule.required) {
626
+ return callback();
627
+ }
628
+ rules.required(rule, value, source, errors, options, "array");
629
+ if (value !== void 0 && value !== null) {
630
+ rules.type(rule, value, source, errors, options);
631
+ rules.range(rule, value, source, errors, options);
632
+ }
633
+ }
634
+ callback(errors);
635
+ };
636
+ var object2 = function object3(rule, value, callback, source, options) {
637
+ var errors = [];
638
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
639
+ if (validate) {
640
+ if (isEmptyValue(value) && !rule.required) {
641
+ return callback();
642
+ }
643
+ rules.required(rule, value, source, errors, options);
644
+ if (value !== void 0) {
645
+ rules.type(rule, value, source, errors, options);
646
+ }
647
+ }
648
+ callback(errors);
649
+ };
650
+ var ENUM = "enum";
651
+ var enumerable2 = function enumerable3(rule, value, callback, source, options) {
652
+ var errors = [];
653
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
654
+ if (validate) {
655
+ if (isEmptyValue(value) && !rule.required) {
656
+ return callback();
657
+ }
658
+ rules.required(rule, value, source, errors, options);
659
+ if (value !== void 0) {
660
+ rules[ENUM](rule, value, source, errors, options);
661
+ }
662
+ }
663
+ callback(errors);
664
+ };
665
+ var pattern2 = function pattern3(rule, value, callback, source, options) {
666
+ var errors = [];
667
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
668
+ if (validate) {
669
+ if (isEmptyValue(value, "string") && !rule.required) {
670
+ return callback();
671
+ }
672
+ rules.required(rule, value, source, errors, options);
673
+ if (!isEmptyValue(value, "string")) {
674
+ rules.pattern(rule, value, source, errors, options);
675
+ }
676
+ }
677
+ callback(errors);
678
+ };
679
+ var date2 = function date3(rule, value, callback, source, options) {
680
+ var errors = [];
681
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
682
+ if (validate) {
683
+ if (isEmptyValue(value, "date") && !rule.required) {
684
+ return callback();
685
+ }
686
+ rules.required(rule, value, source, errors, options);
687
+ if (!isEmptyValue(value, "date")) {
688
+ var dateObject;
689
+ if (value instanceof Date) {
690
+ dateObject = value;
691
+ } else {
692
+ dateObject = new Date(value);
693
+ }
694
+ rules.type(rule, dateObject, source, errors, options);
695
+ if (dateObject) {
696
+ rules.range(rule, dateObject.getTime(), source, errors, options);
697
+ }
698
+ }
699
+ }
700
+ callback(errors);
701
+ };
702
+ var required2 = function required3(rule, value, callback, source, options) {
703
+ var errors = [];
704
+ var type4 = Array.isArray(value) ? "array" : typeof value;
705
+ rules.required(rule, value, source, errors, options, type4);
706
+ callback(errors);
707
+ };
708
+ var type2 = function type3(rule, value, callback, source, options) {
709
+ var ruleType = rule.type;
710
+ var errors = [];
711
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
712
+ if (validate) {
713
+ if (isEmptyValue(value, ruleType) && !rule.required) {
714
+ return callback();
715
+ }
716
+ rules.required(rule, value, source, errors, options, ruleType);
717
+ if (!isEmptyValue(value, ruleType)) {
718
+ rules.type(rule, value, source, errors, options);
719
+ }
720
+ }
721
+ callback(errors);
722
+ };
723
+ var any = function any2(rule, value, callback, source, options) {
724
+ var errors = [];
725
+ var validate = rule.required || !rule.required && source.hasOwnProperty(rule.field);
726
+ if (validate) {
727
+ if (isEmptyValue(value) && !rule.required) {
728
+ return callback();
729
+ }
730
+ rules.required(rule, value, source, errors, options);
731
+ }
732
+ callback(errors);
733
+ };
734
+ var validators = {
735
+ string,
736
+ method: method2,
737
+ number: number2,
738
+ "boolean": _boolean,
739
+ regexp: regexp2,
740
+ integer: integer2,
741
+ "float": floatFn,
742
+ array: array2,
743
+ object: object2,
744
+ "enum": enumerable2,
745
+ pattern: pattern2,
746
+ date: date2,
747
+ url: type2,
748
+ hex: type2,
749
+ email: type2,
750
+ required: required2,
751
+ any
752
+ };
753
+ function newMessages() {
754
+ return {
755
+ "default": "Validation error on field %s",
756
+ required: "%s is required",
757
+ "enum": "%s must be one of %s",
758
+ whitespace: "%s cannot be empty",
759
+ date: {
760
+ format: "%s date %s is invalid for format %s",
761
+ parse: "%s date could not be parsed, %s is invalid ",
762
+ invalid: "%s date %s is invalid"
763
+ },
764
+ types: {
765
+ string: "%s is not a %s",
766
+ method: "%s is not a %s (function)",
767
+ array: "%s is not an %s",
768
+ object: "%s is not an %s",
769
+ number: "%s is not a %s",
770
+ date: "%s is not a %s",
771
+ "boolean": "%s is not a %s",
772
+ integer: "%s is not an %s",
773
+ "float": "%s is not a %s",
774
+ regexp: "%s is not a valid %s",
775
+ email: "%s is not a valid %s",
776
+ url: "%s is not a valid %s",
777
+ hex: "%s is not a valid %s"
778
+ },
779
+ string: {
780
+ len: "%s must be exactly %s characters",
781
+ min: "%s must be at least %s characters",
782
+ max: "%s cannot be longer than %s characters",
783
+ range: "%s must be between %s and %s characters"
784
+ },
785
+ number: {
786
+ len: "%s must equal %s",
787
+ min: "%s cannot be less than %s",
788
+ max: "%s cannot be greater than %s",
789
+ range: "%s must be between %s and %s"
790
+ },
791
+ array: {
792
+ len: "%s must be exactly %s in length",
793
+ min: "%s cannot be less than %s in length",
794
+ max: "%s cannot be greater than %s in length",
795
+ range: "%s must be between %s and %s in length"
796
+ },
797
+ pattern: {
798
+ mismatch: "%s value %s does not match pattern %s"
799
+ },
800
+ clone: function clone() {
801
+ var cloned = JSON.parse(JSON.stringify(this));
802
+ cloned.clone = this.clone;
803
+ return cloned;
804
+ }
805
+ };
806
+ }
807
+ var messages = newMessages();
808
+ var Schema = /* @__PURE__ */ function() {
809
+ function Schema2(descriptor) {
810
+ this.rules = null;
811
+ this._messages = messages;
812
+ this.define(descriptor);
813
+ }
814
+ var _proto = Schema2.prototype;
815
+ _proto.define = function define(rules2) {
816
+ var _this = this;
817
+ if (!rules2) {
818
+ throw new Error("Cannot configure a schema with no rules");
819
+ }
820
+ if (typeof rules2 !== "object" || Array.isArray(rules2)) {
821
+ throw new Error("Rules must be an object");
822
+ }
823
+ this.rules = {};
824
+ Object.keys(rules2).forEach(function(name) {
825
+ var item = rules2[name];
826
+ _this.rules[name] = Array.isArray(item) ? item : [item];
827
+ });
828
+ };
829
+ _proto.messages = function messages2(_messages) {
830
+ if (_messages) {
831
+ this._messages = deepMerge(newMessages(), _messages);
832
+ }
833
+ return this._messages;
834
+ };
835
+ _proto.validate = function validate(source_, o, oc) {
836
+ var _this2 = this;
837
+ if (o === void 0) {
838
+ o = {};
839
+ }
840
+ if (oc === void 0) {
841
+ oc = function oc2() {
842
+ };
843
+ }
844
+ var source = source_;
845
+ var options = o;
846
+ var callback = oc;
847
+ if (typeof options === "function") {
848
+ callback = options;
849
+ options = {};
850
+ }
851
+ if (!this.rules || Object.keys(this.rules).length === 0) {
852
+ if (callback) {
853
+ callback(null, source);
854
+ }
855
+ return Promise.resolve(source);
856
+ }
857
+ function complete(results) {
858
+ var errors = [];
859
+ var fields = {};
860
+ function add(e) {
861
+ if (Array.isArray(e)) {
862
+ var _errors;
863
+ errors = (_errors = errors).concat.apply(_errors, e);
864
+ } else {
865
+ errors.push(e);
866
+ }
867
+ }
868
+ for (var i = 0; i < results.length; i++) {
869
+ add(results[i]);
870
+ }
871
+ if (!errors.length) {
872
+ callback(null, source);
873
+ } else {
874
+ fields = convertFieldsError(errors);
875
+ callback(errors, fields);
876
+ }
877
+ }
878
+ if (options.messages) {
879
+ var messages$1 = this.messages();
880
+ if (messages$1 === messages) {
881
+ messages$1 = newMessages();
882
+ }
883
+ deepMerge(messages$1, options.messages);
884
+ options.messages = messages$1;
885
+ } else {
886
+ options.messages = this.messages();
887
+ }
888
+ var series = {};
889
+ var keys = options.keys || Object.keys(this.rules);
890
+ keys.forEach(function(z) {
891
+ var arr = _this2.rules[z];
892
+ var value = source[z];
893
+ arr.forEach(function(r) {
894
+ var rule = r;
895
+ if (typeof rule.transform === "function") {
896
+ if (source === source_) {
897
+ source = _extends({}, source);
898
+ }
899
+ value = source[z] = rule.transform(value);
900
+ }
901
+ if (typeof rule === "function") {
902
+ rule = {
903
+ validator: rule
904
+ };
905
+ } else {
906
+ rule = _extends({}, rule);
907
+ }
908
+ rule.validator = _this2.getValidationMethod(rule);
909
+ if (!rule.validator) {
910
+ return;
911
+ }
912
+ rule.field = z;
913
+ rule.fullField = rule.fullField || z;
914
+ rule.type = _this2.getType(rule);
915
+ series[z] = series[z] || [];
916
+ series[z].push({
917
+ rule,
918
+ value,
919
+ source,
920
+ field: z
921
+ });
922
+ });
923
+ });
924
+ var errorFields = {};
925
+ return asyncMap(series, options, function(data, doIt) {
926
+ var rule = data.rule;
927
+ var deep = (rule.type === "object" || rule.type === "array") && (typeof rule.fields === "object" || typeof rule.defaultField === "object");
928
+ deep = deep && (rule.required || !rule.required && data.value);
929
+ rule.field = data.field;
930
+ function addFullField(key, schema) {
931
+ return _extends({}, schema, {
932
+ fullField: rule.fullField + "." + key,
933
+ fullFields: rule.fullFields ? [].concat(rule.fullFields, [key]) : [key]
934
+ });
935
+ }
936
+ function cb(e) {
937
+ if (e === void 0) {
938
+ e = [];
939
+ }
940
+ var errorList = Array.isArray(e) ? e : [e];
941
+ if (!options.suppressWarning && errorList.length) {
942
+ Schema2.warning("async-validator:", errorList);
943
+ }
944
+ if (errorList.length && rule.message !== void 0) {
945
+ errorList = [].concat(rule.message);
946
+ }
947
+ var filledErrors = errorList.map(complementError(rule, source));
948
+ if (options.first && filledErrors.length) {
949
+ errorFields[rule.field] = 1;
950
+ return doIt(filledErrors);
951
+ }
952
+ if (!deep) {
953
+ doIt(filledErrors);
954
+ } else {
955
+ if (rule.required && !data.value) {
956
+ if (rule.message !== void 0) {
957
+ filledErrors = [].concat(rule.message).map(complementError(rule, source));
958
+ } else if (options.error) {
959
+ filledErrors = [options.error(rule, format(options.messages.required, rule.field))];
960
+ }
961
+ return doIt(filledErrors);
962
+ }
963
+ var fieldsSchema = {};
964
+ if (rule.defaultField) {
965
+ Object.keys(data.value).map(function(key) {
966
+ fieldsSchema[key] = rule.defaultField;
967
+ });
968
+ }
969
+ fieldsSchema = _extends({}, fieldsSchema, data.rule.fields);
970
+ var paredFieldsSchema = {};
971
+ Object.keys(fieldsSchema).forEach(function(field) {
972
+ var fieldSchema = fieldsSchema[field];
973
+ var fieldSchemaList = Array.isArray(fieldSchema) ? fieldSchema : [fieldSchema];
974
+ paredFieldsSchema[field] = fieldSchemaList.map(addFullField.bind(null, field));
975
+ });
976
+ var schema = new Schema2(paredFieldsSchema);
977
+ schema.messages(options.messages);
978
+ if (data.rule.options) {
979
+ data.rule.options.messages = options.messages;
980
+ data.rule.options.error = options.error;
981
+ }
982
+ schema.validate(data.value, data.rule.options || options, function(errs) {
983
+ var finalErrors = [];
984
+ if (filledErrors && filledErrors.length) {
985
+ finalErrors.push.apply(finalErrors, filledErrors);
986
+ }
987
+ if (errs && errs.length) {
988
+ finalErrors.push.apply(finalErrors, errs);
989
+ }
990
+ doIt(finalErrors.length ? finalErrors : null);
991
+ });
992
+ }
993
+ }
994
+ var res;
995
+ if (rule.asyncValidator) {
996
+ res = rule.asyncValidator(rule, data.value, cb, data.source, options);
997
+ } else if (rule.validator) {
998
+ try {
999
+ res = rule.validator(rule, data.value, cb, data.source, options);
1000
+ } catch (error) {
1001
+ console.error == null ? void 0 : console.error(error);
1002
+ if (!options.suppressValidatorError) {
1003
+ setTimeout(function() {
1004
+ throw error;
1005
+ }, 0);
1006
+ }
1007
+ cb(error.message);
1008
+ }
1009
+ if (res === true) {
1010
+ cb();
1011
+ } else if (res === false) {
1012
+ cb(typeof rule.message === "function" ? rule.message(rule.fullField || rule.field) : rule.message || (rule.fullField || rule.field) + " fails");
1013
+ } else if (res instanceof Array) {
1014
+ cb(res);
1015
+ } else if (res instanceof Error) {
1016
+ cb(res.message);
1017
+ }
1018
+ }
1019
+ if (res && res.then) {
1020
+ res.then(function() {
1021
+ return cb();
1022
+ }, function(e) {
1023
+ return cb(e);
1024
+ });
1025
+ }
1026
+ }, function(results) {
1027
+ complete(results);
1028
+ }, source);
1029
+ };
1030
+ _proto.getType = function getType(rule) {
1031
+ if (rule.type === void 0 && rule.pattern instanceof RegExp) {
1032
+ rule.type = "pattern";
1033
+ }
1034
+ if (typeof rule.validator !== "function" && rule.type && !validators.hasOwnProperty(rule.type)) {
1035
+ throw new Error(format("Unknown rule type %s", rule.type));
1036
+ }
1037
+ return rule.type || "string";
1038
+ };
1039
+ _proto.getValidationMethod = function getValidationMethod(rule) {
1040
+ if (typeof rule.validator === "function") {
1041
+ return rule.validator;
1042
+ }
1043
+ var keys = Object.keys(rule);
1044
+ var messageIndex = keys.indexOf("message");
1045
+ if (messageIndex !== -1) {
1046
+ keys.splice(messageIndex, 1);
1047
+ }
1048
+ if (keys.length === 1 && keys[0] === "required") {
1049
+ return validators.required;
1050
+ }
1051
+ return validators[this.getType(rule)] || void 0;
1052
+ };
1053
+ return Schema2;
1054
+ }();
1055
+ Schema.register = function register(type4, validator) {
1056
+ if (typeof validator !== "function") {
1057
+ throw new Error("Cannot register a validator by type, validator is not a function");
1058
+ }
1059
+ validators[type4] = validator;
1060
+ };
1061
+ Schema.warning = warning;
1062
+ Schema.messages = messages;
1063
+ Schema.validators = validators;
1064
+ const SECRET = "NUT_FORM_INTERNAL";
1065
+ class FormStore {
1066
+ constructor() {
1067
+ // 初始化数据
1068
+ __publicField(this, "initialValues", {});
1069
+ // 存放表单中所有的数据 eg. {password: "ddd",username: "123"}
1070
+ __publicField(this, "store", {});
1071
+ // 所有的组件实例
1072
+ __publicField(this, "fieldEntities", []);
1073
+ // 校验成功或失败的回调,onFinish、onFinishFailed
1074
+ __publicField(this, "callbacks", {});
1075
+ __publicField(this, "errors", {});
1076
+ /**
1077
+ * 注册组件实例
1078
+ * @param field
1079
+ */
1080
+ __publicField(this, "registerField", (field) => {
1081
+ this.fieldEntities.push(field);
1082
+ return () => {
1083
+ this.fieldEntities = this.fieldEntities.filter((item) => item !== field);
1084
+ if (this.store) {
1085
+ delete this.store[field.props.name];
1086
+ }
1087
+ };
1088
+ });
1089
+ /**
1090
+ * 获取 formItem 的值
1091
+ * @param name
1092
+ */
1093
+ __publicField(this, "getFieldValue", (name) => {
1094
+ var _a;
1095
+ return (_a = this.store) == null ? void 0 : _a[name];
1096
+ });
1097
+ /**
1098
+ * 获取全部字段
1099
+ */
1100
+ __publicField(this, "getFieldsValue", (nameList) => {
1101
+ if (typeof nameList === "boolean") {
1102
+ return JSON.parse(JSON.stringify(this.store));
1103
+ }
1104
+ const fieldsValue = {};
1105
+ nameList.forEach((field) => {
1106
+ fieldsValue[field] = this.getFieldValue(field);
1107
+ });
1108
+ return fieldsValue;
1109
+ });
1110
+ /**
1111
+ * 设置 form 的初始值,之后在 reset 的时候使用
1112
+ * @param values
1113
+ * @param init
1114
+ */
1115
+ __publicField(this, "setInitialValues", (values, init) => {
1116
+ if (init) {
1117
+ this.initialValues = values;
1118
+ this.store = values;
1119
+ }
1120
+ });
1121
+ /**
1122
+ * 存储组件数据
1123
+ * @param newStore { [name]: newValue }
1124
+ */
1125
+ __publicField(this, "setFieldsValue", (newStore) => {
1126
+ this.store = {
1127
+ ...this.store,
1128
+ ...newStore
1129
+ };
1130
+ this.fieldEntities.forEach((entity) => {
1131
+ const { name } = entity.props;
1132
+ Object.keys(newStore).forEach((key) => {
1133
+ if (key === name) {
1134
+ entity.onStoreChange("update");
1135
+ }
1136
+ });
1137
+ });
1138
+ });
1139
+ __publicField(this, "setCallback", (callback) => {
1140
+ this.callbacks = {
1141
+ ...this.callbacks,
1142
+ ...callback
1143
+ };
1144
+ });
1145
+ __publicField(this, "validateEntities", async (entity, errs) => {
1146
+ var _a;
1147
+ const { name, rules: rules2 = [] } = entity.props;
1148
+ const descriptor = {};
1149
+ if (rules2.length) {
1150
+ if (rules2.length > 1) {
1151
+ descriptor[name] = [];
1152
+ rules2.forEach((v) => {
1153
+ descriptor[name].push(v);
1154
+ });
1155
+ } else {
1156
+ descriptor[name] = rules2[0];
1157
+ }
1158
+ }
1159
+ const validator = new Schema(descriptor);
1160
+ try {
1161
+ await validator.validate({ [name]: (_a = this.store) == null ? void 0 : _a[name] });
1162
+ } catch ({ errors }) {
1163
+ if (errors) {
1164
+ errs.push(...errors);
1165
+ this.errors[name] = errors;
1166
+ }
1167
+ } finally {
1168
+ if (!errs || errs.length === 0) {
1169
+ this.errors[name] = [];
1170
+ }
1171
+ }
1172
+ entity.onStoreChange("validate");
1173
+ });
1174
+ __publicField(this, "validateFields", async (nameList) => {
1175
+ let filterEntities = [];
1176
+ this.errors.length = 0;
1177
+ if (!nameList || nameList.length === 0) {
1178
+ filterEntities = this.fieldEntities;
1179
+ } else {
1180
+ filterEntities = this.fieldEntities.filter(({ props: { name } }) => nameList.includes(name));
1181
+ }
1182
+ const errs = [];
1183
+ filterEntities.forEach((entity) => {
1184
+ this.validateEntities(entity, errs);
1185
+ });
1186
+ return errs;
1187
+ });
1188
+ __publicField(this, "submit", async () => {
1189
+ var _a, _b, _c, _d;
1190
+ const errors = await this.validateFields();
1191
+ if (errors.length === 0) {
1192
+ (_b = (_a = this.callbacks).onFinish) == null ? void 0 : _b.call(_a, this.store);
1193
+ } else if (errors.length > 0) {
1194
+ (_d = (_c = this.callbacks).onFinishFailed) == null ? void 0 : _d.call(_c, this.store, errors);
1195
+ }
1196
+ });
1197
+ __publicField(this, "resetFields", () => {
1198
+ this.errors.length = 0;
1199
+ this.store = this.initialValues;
1200
+ this.fieldEntities.forEach((entity) => {
1201
+ entity.onStoreChange("reset");
1202
+ });
1203
+ });
1204
+ __publicField(this, "dispatch", ({ name }) => {
1205
+ this.validateFields([name]);
1206
+ });
1207
+ __publicField(this, "getInternal", (key) => {
1208
+ if (key === SECRET) {
1209
+ return {
1210
+ registerField: this.registerField,
1211
+ setCallback: this.setCallback,
1212
+ setInitialValues: this.setInitialValues,
1213
+ dispatch: this.dispatch,
1214
+ store: this.store,
1215
+ fieldEntities: this.fieldEntities
1216
+ };
1217
+ }
1218
+ });
1219
+ __publicField(this, "getForm", () => {
1220
+ return {
1221
+ getFieldValue: this.getFieldValue,
1222
+ getFieldsValue: this.getFieldsValue,
1223
+ setFieldsValue: this.setFieldsValue,
1224
+ resetFields: this.resetFields,
1225
+ validateFields: this.validateFields,
1226
+ submit: this.submit,
1227
+ errors: this.errors,
1228
+ getInternal: this.getInternal
1229
+ };
1230
+ });
1231
+ this.callbacks = {
1232
+ onFinish: () => {
1233
+ },
1234
+ onFinishFailed: () => {
1235
+ }
1236
+ };
1237
+ }
1238
+ }
1239
+ const useForm = (form) => {
1240
+ const formRef = useRef();
1241
+ if (!formRef.current) {
1242
+ if (form) {
1243
+ formRef.current = form;
1244
+ } else {
1245
+ const formStore = new FormStore();
1246
+ formRef.current = formStore.getForm();
1247
+ }
1248
+ }
1249
+ return [formRef.current];
1250
+ };
1251
+ function isForwardRefComponent(component) {
1252
+ return component.type && component.type.$$typeof && // eslint-disable-next-line react/display-name
1253
+ React__default.forwardRef(
1254
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1255
+ // @ts-ignore
1256
+ () => {
1257
+ }
1258
+ ).$$typeof === component.type.$$typeof;
1259
+ }
1260
+ const defaultProps = {
1261
+ ...ComponentDefaults,
1262
+ required: false,
1263
+ name: "",
1264
+ label: "",
1265
+ rules: [{ required: false, message: "" }],
1266
+ errorMessageAlign: "left",
1267
+ validateTrigger: "onChange"
1268
+ };
1269
+ class FormItem extends React__default.Component {
1270
+ constructor(props) {
1271
+ super(props);
1272
+ __publicField(this, "cancelRegister");
1273
+ __publicField(this, "componentRef");
1274
+ // children添加value属性和onChange事件
1275
+ __publicField(this, "getControlled", (children) => {
1276
+ var _a;
1277
+ const { setFieldsValue, getFieldValue } = this.context;
1278
+ const { dispatch } = this.context.getInternal(SECRET);
1279
+ const { name = "" } = this.props;
1280
+ if ((_a = children == null ? void 0 : children.props) == null ? void 0 : _a.defaultValue) {
1281
+ console.warn("通过 initialValue 设置初始值");
1282
+ }
1283
+ const fieldValue = getFieldValue(name);
1284
+ const controlled = {
1285
+ ...children.props,
1286
+ [this.props.valuePropName || "value"]: fieldValue !== void 0 ? fieldValue : this.props.initialValue,
1287
+ [this.props.trigger || "onChange"]: (...args) => {
1288
+ const originOnChange = children.props[this.props.trigger || "onChange"];
1289
+ if (originOnChange) {
1290
+ originOnChange(...args);
1291
+ }
1292
+ let [next] = args;
1293
+ if (this.props.getValueFromEvent) {
1294
+ next = this.props.getValueFromEvent(...args);
1295
+ }
1296
+ setFieldsValue({ [name]: next });
1297
+ }
1298
+ };
1299
+ const { validateTrigger } = this.props;
1300
+ let validateTriggers = [this.props.trigger || "onChange"];
1301
+ if (validateTrigger) {
1302
+ validateTriggers = typeof validateTrigger === "string" ? [...validateTriggers, validateTrigger] : [...validateTriggers, ...validateTrigger];
1303
+ validateTriggers.forEach((trigger) => {
1304
+ const originTrigger = controlled[trigger];
1305
+ controlled[trigger] = (...args) => {
1306
+ if (originTrigger) {
1307
+ originTrigger(...args);
1308
+ }
1309
+ if (this.props.rules && this.props.rules.length) {
1310
+ dispatch({
1311
+ name: this.props.name
1312
+ });
1313
+ }
1314
+ };
1315
+ });
1316
+ }
1317
+ if (isForwardRefComponent(children)) {
1318
+ controlled.ref = (componentInstance) => {
1319
+ const originRef = children.ref;
1320
+ if (originRef) {
1321
+ if (typeof originRef === "function") {
1322
+ originRef(componentInstance);
1323
+ }
1324
+ if ("current" in originRef) {
1325
+ originRef.current = componentInstance;
1326
+ }
1327
+ }
1328
+ this.componentRef = componentInstance;
1329
+ };
1330
+ }
1331
+ return controlled;
1332
+ });
1333
+ __publicField(this, "refresh", () => {
1334
+ this.setState(({ resetCount }) => ({
1335
+ resetCount: resetCount + 1
1336
+ }));
1337
+ });
1338
+ __publicField(this, "onStoreChange", (type4) => {
1339
+ if (type4 === "reset") {
1340
+ this.context.errors[this.props.name] = [];
1341
+ this.refresh();
1342
+ } else {
1343
+ this.forceUpdate();
1344
+ }
1345
+ });
1346
+ __publicField(this, "renderLayout", (childNode) => {
1347
+ var _a;
1348
+ const { label, name, required: required4, rules: rules2, className, style, errorMessageAlign } = {
1349
+ ...defaultProps,
1350
+ ...this.props
1351
+ };
1352
+ const requiredInRules = rules2 == null ? void 0 : rules2.some((rule) => rule.required);
1353
+ const item = name ? this.context.errors[name] : [];
1354
+ const { starPosition } = this.context;
1355
+ const renderStar = (required4 || requiredInRules) && React__default.createElement("i", { className: "required" });
1356
+ const renderLabel = React__default.createElement(
1357
+ React__default.Fragment,
1358
+ null,
1359
+ starPosition === "left" ? renderStar : null,
1360
+ label,
1361
+ starPosition === "right" ? renderStar : null
1362
+ );
1363
+ return React__default.createElement(
1364
+ Cell,
1365
+ { className: `nut-form-item ${className}`, style, onClick: (e) => this.props.onClick && this.props.onClick(e, this.componentRef) },
1366
+ label ? React__default.createElement("div", { className: "nut-cell-title nut-form-item-label" }, renderLabel) : null,
1367
+ React__default.createElement(
1368
+ "div",
1369
+ { className: "nut-cell-value nut-form-item-body" },
1370
+ React__default.createElement("div", { className: "nut-form-item-body-slots" }, childNode),
1371
+ React__default.createElement("div", { className: "nut-form-item-body-tips", style: {
1372
+ textAlign: errorMessageAlign,
1373
+ display: (item == null ? void 0 : item.length) ? "initial" : "none"
1374
+ } }, (_a = item == null ? void 0 : item[0]) == null ? void 0 : _a.message)
1375
+ )
1376
+ );
1377
+ });
1378
+ this.componentRef = React__default.createRef();
1379
+ this.state = {
1380
+ resetCount: 1
1381
+ };
1382
+ }
1383
+ componentDidMount() {
1384
+ const { registerField } = this.context.getInternal(SECRET);
1385
+ this.cancelRegister = registerField(this);
1386
+ }
1387
+ componentWillUnmount() {
1388
+ if (this.cancelRegister) {
1389
+ this.cancelRegister();
1390
+ }
1391
+ }
1392
+ render() {
1393
+ const { children } = this.props;
1394
+ const child = Array.isArray(children) ? children[0] : children;
1395
+ const returnChildNode = React__default.cloneElement(child, this.getControlled(child));
1396
+ return React__default.createElement(React__default.Fragment, { key: this.state.resetCount }, this.renderLayout(returnChildNode));
1397
+ }
1398
+ }
1399
+ __publicField(FormItem, "defaultProps", defaultProps);
1400
+ __publicField(FormItem, "contextType", Context);
1401
+ export {
1402
+ Context as C,
1403
+ FormItem as F,
1404
+ SECRET as S,
1405
+ useForm as u
1406
+ };