@sunggang/ui-lib 0.3.65 → 0.4.0
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/BaseCkeditor.cjs.js +66855 -0
- package/BaseCkeditor.esm.js +15 -18
- package/BigCalender.cjs.d.ts +1 -0
- package/BigCalender.cjs.default.js +1 -0
- package/BigCalender.cjs.js +20404 -0
- package/BigCalender.cjs.mjs +2 -0
- package/BigCalender.esm.d.ts +1 -0
- package/BigCalender.esm.js +20393 -0
- package/CkEditor.cjs.d.ts +1 -0
- package/CkEditor.cjs.default.js +1 -0
- package/CkEditor.cjs.js +31 -0
- package/CkEditor.cjs.mjs +2 -0
- package/CkEditor.esm.d.ts +1 -0
- package/CkEditor.esm.js +23 -0
- package/DataTable.cjs.d.ts +1 -0
- package/DataTable.cjs.default.js +1 -0
- package/DataTable.cjs.js +968 -0
- package/DataTable.cjs.mjs +2 -0
- package/DataTable.esm.d.ts +1 -0
- package/DataTable.esm.js +943 -0
- package/DropImage.cjs.d.ts +1 -0
- package/DropImage.cjs.default.js +1 -0
- package/DropImage.cjs.js +16845 -0
- package/DropImage.cjs.mjs +2 -0
- package/DropImage.esm.d.ts +1 -0
- package/DropImage.esm.js +16834 -0
- package/Form.cjs.d.ts +1 -0
- package/Form.cjs.default.js +1 -0
- package/Form.cjs.js +402 -0
- package/Form.cjs.mjs +2 -0
- package/Form.esm.d.ts +1 -0
- package/Form.esm.js +390 -0
- package/FormProvider.cjs.d.ts +1 -0
- package/FormProvider.cjs.default.js +1 -0
- package/FormProvider.cjs.js +81 -0
- package/FormProvider.cjs.mjs +2 -0
- package/FormProvider.esm.d.ts +1 -0
- package/FormProvider.esm.js +71 -0
- package/LiffProvider.cjs.d.ts +1 -0
- package/LiffProvider.cjs.default.js +1 -0
- package/LiffProvider.cjs.js +1120 -0
- package/LiffProvider.cjs.mjs +2 -0
- package/LiffProvider.esm.d.ts +1 -0
- package/LiffProvider.esm.js +1114 -0
- package/Modal.cjs.js +168 -0
- package/Modal.esm.js +164 -0
- package/Tabs.cjs.d.ts +1 -0
- package/Tabs.cjs.default.js +1 -0
- package/Tabs.cjs.js +62 -0
- package/Tabs.cjs.mjs +2 -0
- package/Tabs.esm.d.ts +1 -0
- package/Tabs.esm.js +58 -0
- package/_commonjsHelpers.cjs.js +30 -0
- package/_commonjsHelpers.esm.js +25 -0
- package/_tslib.cjs.js +232 -0
- package/_tslib.esm.js +224 -0
- package/baseSwitch.cjs.js +25830 -0
- package/baseSwitch.esm.js +25785 -0
- package/index.cjs.css +4832 -0
- package/index.cjs.d.ts +1 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +1962 -0
- package/index.cjs.mjs +2 -0
- package/index.cjs2.js +1064 -0
- package/index.cjs3.js +6424 -0
- package/index.esm.cjs.js +3406 -0
- package/index.esm.css +3624 -3632
- package/index.esm.esm.js +3375 -0
- package/index.esm.js +1902 -22
- package/index.esm2.js +131 -81165
- package/index.esm3.js +2 -25
- package/inheritsLoose.cjs.js +9 -0
- package/inheritsLoose.esm.js +7 -0
- package/input.cjs.js +244 -0
- package/input.esm.js +220 -0
- package/memoize-one.esm.cjs.js +204 -0
- package/memoize-one.esm.esm.js +189 -0
- package/package.json +57 -5
- package/setPrototypeOf.cjs.js +91 -0
- package/setPrototypeOf.esm.js +83 -0
- package/src/BigCalender.d.ts +2 -0
- package/src/CkEditor.d.ts +1 -0
- package/src/DataTable.d.ts +2 -0
- package/src/DropImage.d.ts +2 -0
- package/src/Form.d.ts +3 -0
- package/src/FormProvider.d.ts +1 -0
- package/src/LiffProvider.d.ts +1 -0
- package/src/Tabs.d.ts +1 -0
- package/src/index.d.ts +8 -21
- package/src/lib/Form/types.d.ts +1 -0
- package/styled-components.esm.cjs.js +1621 -0
- package/styled-components.esm.esm.js +1615 -0
- package/utils.cjs.js +13 -0
- package/utils.esm.js +11 -0
package/index.esm.esm.js
ADDED
|
@@ -0,0 +1,3375 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import React__default from 'react';
|
|
3
|
+
|
|
4
|
+
function _array_like_to_array(arr, len) {
|
|
5
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
6
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
7
|
+
return arr2;
|
|
8
|
+
}
|
|
9
|
+
function _array_with_holes(arr) {
|
|
10
|
+
if (Array.isArray(arr)) return arr;
|
|
11
|
+
}
|
|
12
|
+
function _array_without_holes(arr) {
|
|
13
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
14
|
+
}
|
|
15
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
16
|
+
try {
|
|
17
|
+
var info = gen[key](arg);
|
|
18
|
+
var value1 = info.value;
|
|
19
|
+
} catch (error) {
|
|
20
|
+
reject(error);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (info.done) {
|
|
24
|
+
resolve(value1);
|
|
25
|
+
} else {
|
|
26
|
+
Promise.resolve(value1).then(_next, _throw);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function _async_to_generator(fn) {
|
|
30
|
+
return function() {
|
|
31
|
+
var self = this, args = arguments;
|
|
32
|
+
return new Promise(function(resolve, reject) {
|
|
33
|
+
var gen = fn.apply(self, args);
|
|
34
|
+
function _next(value1) {
|
|
35
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value1);
|
|
36
|
+
}
|
|
37
|
+
function _throw(err) {
|
|
38
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
39
|
+
}
|
|
40
|
+
_next(undefined);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function _define_property(obj, key, value1) {
|
|
45
|
+
if (key in obj) {
|
|
46
|
+
Object.defineProperty(obj, key, {
|
|
47
|
+
value: value1,
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
obj[key] = value1;
|
|
54
|
+
}
|
|
55
|
+
return obj;
|
|
56
|
+
}
|
|
57
|
+
function _instanceof(left, right) {
|
|
58
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
59
|
+
return !!right[Symbol.hasInstance](left);
|
|
60
|
+
} else {
|
|
61
|
+
return left instanceof right;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function _iterable_to_array(iter) {
|
|
65
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
66
|
+
}
|
|
67
|
+
function _iterable_to_array_limit(arr, i) {
|
|
68
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
69
|
+
if (_i == null) return;
|
|
70
|
+
var _arr = [];
|
|
71
|
+
var _n = true;
|
|
72
|
+
var _d = false;
|
|
73
|
+
var _s, _e;
|
|
74
|
+
try {
|
|
75
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
76
|
+
_arr.push(_s.value);
|
|
77
|
+
if (i && _arr.length === i) break;
|
|
78
|
+
}
|
|
79
|
+
} catch (err) {
|
|
80
|
+
_d = true;
|
|
81
|
+
_e = err;
|
|
82
|
+
} finally{
|
|
83
|
+
try {
|
|
84
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
85
|
+
} finally{
|
|
86
|
+
if (_d) throw _e;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return _arr;
|
|
90
|
+
}
|
|
91
|
+
function _non_iterable_rest() {
|
|
92
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
93
|
+
}
|
|
94
|
+
function _non_iterable_spread() {
|
|
95
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
96
|
+
}
|
|
97
|
+
function _object_spread(target) {
|
|
98
|
+
for(var i = 1; i < arguments.length; i++){
|
|
99
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
100
|
+
var ownKeys = Object.keys(source);
|
|
101
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
102
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
103
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
ownKeys.forEach(function(key) {
|
|
107
|
+
_define_property(target, key, source[key]);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return target;
|
|
111
|
+
}
|
|
112
|
+
function ownKeys(object, enumerableOnly) {
|
|
113
|
+
var keys = Object.keys(object);
|
|
114
|
+
if (Object.getOwnPropertySymbols) {
|
|
115
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
116
|
+
if (enumerableOnly) {
|
|
117
|
+
symbols = symbols.filter(function(sym) {
|
|
118
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
keys.push.apply(keys, symbols);
|
|
122
|
+
}
|
|
123
|
+
return keys;
|
|
124
|
+
}
|
|
125
|
+
function _object_spread_props(target, source) {
|
|
126
|
+
source = source != null ? source : {};
|
|
127
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
128
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
129
|
+
} else {
|
|
130
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
131
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return target;
|
|
135
|
+
}
|
|
136
|
+
function _object_without_properties(source, excluded) {
|
|
137
|
+
if (source == null) return {};
|
|
138
|
+
var target = _object_without_properties_loose(source, excluded);
|
|
139
|
+
var key, i;
|
|
140
|
+
if (Object.getOwnPropertySymbols) {
|
|
141
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
142
|
+
for(i = 0; i < sourceSymbolKeys.length; i++){
|
|
143
|
+
key = sourceSymbolKeys[i];
|
|
144
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
145
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
146
|
+
target[key] = source[key];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return target;
|
|
150
|
+
}
|
|
151
|
+
function _object_without_properties_loose(source, excluded) {
|
|
152
|
+
if (source == null) return {};
|
|
153
|
+
var target = {};
|
|
154
|
+
var sourceKeys = Object.keys(source);
|
|
155
|
+
var key, i;
|
|
156
|
+
for(i = 0; i < sourceKeys.length; i++){
|
|
157
|
+
key = sourceKeys[i];
|
|
158
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
159
|
+
target[key] = source[key];
|
|
160
|
+
}
|
|
161
|
+
return target;
|
|
162
|
+
}
|
|
163
|
+
function _sliced_to_array(arr, i) {
|
|
164
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
165
|
+
}
|
|
166
|
+
function _to_consumable_array(arr) {
|
|
167
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
168
|
+
}
|
|
169
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
170
|
+
if (!o) return;
|
|
171
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
172
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
173
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
174
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
175
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
176
|
+
}
|
|
177
|
+
function _ts_generator(thisArg, body) {
|
|
178
|
+
var f, y, t, g, _ = {
|
|
179
|
+
label: 0,
|
|
180
|
+
sent: function() {
|
|
181
|
+
if (t[0] & 1) throw t[1];
|
|
182
|
+
return t[1];
|
|
183
|
+
},
|
|
184
|
+
trys: [],
|
|
185
|
+
ops: []
|
|
186
|
+
};
|
|
187
|
+
return g = {
|
|
188
|
+
next: verb(0),
|
|
189
|
+
"throw": verb(1),
|
|
190
|
+
"return": verb(2)
|
|
191
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
192
|
+
return this;
|
|
193
|
+
}), g;
|
|
194
|
+
function verb(n) {
|
|
195
|
+
return function(v) {
|
|
196
|
+
return step([
|
|
197
|
+
n,
|
|
198
|
+
v
|
|
199
|
+
]);
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function step(op) {
|
|
203
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
204
|
+
while(_)try {
|
|
205
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
206
|
+
if (y = 0, t) op = [
|
|
207
|
+
op[0] & 2,
|
|
208
|
+
t.value
|
|
209
|
+
];
|
|
210
|
+
switch(op[0]){
|
|
211
|
+
case 0:
|
|
212
|
+
case 1:
|
|
213
|
+
t = op;
|
|
214
|
+
break;
|
|
215
|
+
case 4:
|
|
216
|
+
_.label++;
|
|
217
|
+
return {
|
|
218
|
+
value: op[1],
|
|
219
|
+
done: false
|
|
220
|
+
};
|
|
221
|
+
case 5:
|
|
222
|
+
_.label++;
|
|
223
|
+
y = op[1];
|
|
224
|
+
op = [
|
|
225
|
+
0
|
|
226
|
+
];
|
|
227
|
+
continue;
|
|
228
|
+
case 7:
|
|
229
|
+
op = _.ops.pop();
|
|
230
|
+
_.trys.pop();
|
|
231
|
+
continue;
|
|
232
|
+
default:
|
|
233
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
234
|
+
_ = 0;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
238
|
+
_.label = op[1];
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
242
|
+
_.label = t[1];
|
|
243
|
+
t = op;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
if (t && _.label < t[2]) {
|
|
247
|
+
_.label = t[2];
|
|
248
|
+
_.ops.push(op);
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
if (t[2]) _.ops.pop();
|
|
252
|
+
_.trys.pop();
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
op = body.call(thisArg, _);
|
|
256
|
+
} catch (e) {
|
|
257
|
+
op = [
|
|
258
|
+
6,
|
|
259
|
+
e
|
|
260
|
+
];
|
|
261
|
+
y = 0;
|
|
262
|
+
} finally{
|
|
263
|
+
f = t = 0;
|
|
264
|
+
}
|
|
265
|
+
if (op[0] & 5) throw op[1];
|
|
266
|
+
return {
|
|
267
|
+
value: op[0] ? op[1] : void 0,
|
|
268
|
+
done: true
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
var isCheckBoxInput = function(element) {
|
|
273
|
+
return element.type === "checkbox";
|
|
274
|
+
};
|
|
275
|
+
var isDateObject = function(value1) {
|
|
276
|
+
return _instanceof(value1, Date);
|
|
277
|
+
};
|
|
278
|
+
var isNullOrUndefined = function(value1) {
|
|
279
|
+
return value1 == null;
|
|
280
|
+
};
|
|
281
|
+
var isObjectType = function(value1) {
|
|
282
|
+
return typeof value1 === "object";
|
|
283
|
+
};
|
|
284
|
+
var isObject = function(value1) {
|
|
285
|
+
return !isNullOrUndefined(value1) && !Array.isArray(value1) && isObjectType(value1) && !isDateObject(value1);
|
|
286
|
+
};
|
|
287
|
+
var getEventValue = function(event) {
|
|
288
|
+
return isObject(event) && event.target ? isCheckBoxInput(event.target) ? event.target.checked : event.target.value : event;
|
|
289
|
+
};
|
|
290
|
+
var getNodeParentName = function(name) {
|
|
291
|
+
return name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
|
292
|
+
};
|
|
293
|
+
var isNameInFieldArray = function(names, name) {
|
|
294
|
+
return names.has(getNodeParentName(name));
|
|
295
|
+
};
|
|
296
|
+
var isPlainObject = function(tempObject) {
|
|
297
|
+
var prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
298
|
+
return isObject(prototypeCopy) && prototypeCopy.hasOwnProperty("isPrototypeOf");
|
|
299
|
+
};
|
|
300
|
+
var isWeb = typeof window !== "undefined" && typeof window.HTMLElement !== "undefined" && typeof document !== "undefined";
|
|
301
|
+
function cloneObject(data) {
|
|
302
|
+
var copy;
|
|
303
|
+
var isArray = Array.isArray(data);
|
|
304
|
+
var isFileListInstance = typeof FileList !== "undefined" ? _instanceof(data, FileList) : false;
|
|
305
|
+
if (_instanceof(data, Date)) {
|
|
306
|
+
copy = new Date(data);
|
|
307
|
+
} else if (_instanceof(data, Set)) {
|
|
308
|
+
copy = new Set(data);
|
|
309
|
+
} else if (!(isWeb && (_instanceof(data, Blob) || isFileListInstance)) && (isArray || isObject(data))) {
|
|
310
|
+
copy = isArray ? [] : {};
|
|
311
|
+
if (!isArray && !isPlainObject(data)) {
|
|
312
|
+
copy = data;
|
|
313
|
+
} else {
|
|
314
|
+
for(var key in data){
|
|
315
|
+
if (data.hasOwnProperty(key)) {
|
|
316
|
+
copy[key] = cloneObject(data[key]);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
} else {
|
|
321
|
+
return data;
|
|
322
|
+
}
|
|
323
|
+
return copy;
|
|
324
|
+
}
|
|
325
|
+
var compact = function(value1) {
|
|
326
|
+
return Array.isArray(value1) ? value1.filter(Boolean) : [];
|
|
327
|
+
};
|
|
328
|
+
var isUndefined = function(val) {
|
|
329
|
+
return val === undefined;
|
|
330
|
+
};
|
|
331
|
+
var get = function(object, path, defaultValue) {
|
|
332
|
+
if (!path || !isObject(object)) {
|
|
333
|
+
return defaultValue;
|
|
334
|
+
}
|
|
335
|
+
var result = compact(path.split(/[,[\].]+?/)).reduce(function(result, key) {
|
|
336
|
+
return isNullOrUndefined(result) ? result : result[key];
|
|
337
|
+
}, object);
|
|
338
|
+
return isUndefined(result) || result === object ? isUndefined(object[path]) ? defaultValue : object[path] : result;
|
|
339
|
+
};
|
|
340
|
+
var isBoolean = function(value1) {
|
|
341
|
+
return typeof value1 === "boolean";
|
|
342
|
+
};
|
|
343
|
+
var isKey = function(value1) {
|
|
344
|
+
return /^\w*$/.test(value1);
|
|
345
|
+
};
|
|
346
|
+
var stringToPath = function(input) {
|
|
347
|
+
return compact(input.replace(/["|']|\]/g, "").split(/\.|\[/));
|
|
348
|
+
};
|
|
349
|
+
var set = function(object, path, value1) {
|
|
350
|
+
var index = -1;
|
|
351
|
+
var tempPath = isKey(path) ? [
|
|
352
|
+
path
|
|
353
|
+
] : stringToPath(path);
|
|
354
|
+
var length = tempPath.length;
|
|
355
|
+
var lastIndex = length - 1;
|
|
356
|
+
while(++index < length){
|
|
357
|
+
var key = tempPath[index];
|
|
358
|
+
var newValue = value1;
|
|
359
|
+
if (index !== lastIndex) {
|
|
360
|
+
var objValue = object[key];
|
|
361
|
+
newValue = isObject(objValue) || Array.isArray(objValue) ? objValue : !isNaN(+tempPath[index + 1]) ? [] : {};
|
|
362
|
+
}
|
|
363
|
+
if (key === "__proto__" || key === "constructor" || key === "prototype") {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
object[key] = newValue;
|
|
367
|
+
object = object[key];
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
var EVENTS = {
|
|
371
|
+
BLUR: "blur",
|
|
372
|
+
FOCUS_OUT: "focusout",
|
|
373
|
+
CHANGE: "change"
|
|
374
|
+
};
|
|
375
|
+
var VALIDATION_MODE = {
|
|
376
|
+
onBlur: "onBlur",
|
|
377
|
+
onChange: "onChange",
|
|
378
|
+
onSubmit: "onSubmit",
|
|
379
|
+
onTouched: "onTouched",
|
|
380
|
+
all: "all"
|
|
381
|
+
};
|
|
382
|
+
var INPUT_VALIDATION_RULES = {
|
|
383
|
+
max: "max",
|
|
384
|
+
min: "min",
|
|
385
|
+
maxLength: "maxLength",
|
|
386
|
+
minLength: "minLength",
|
|
387
|
+
pattern: "pattern",
|
|
388
|
+
required: "required",
|
|
389
|
+
validate: "validate"
|
|
390
|
+
};
|
|
391
|
+
var HookFormContext = React__default.createContext(null);
|
|
392
|
+
/**
|
|
393
|
+
* This custom hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the context as a prop. To be used with {@link FormProvider}.
|
|
394
|
+
*
|
|
395
|
+
* @remarks
|
|
396
|
+
* [API](https://react-hook-form.com/docs/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
|
397
|
+
*
|
|
398
|
+
* @returns return all useForm methods
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```tsx
|
|
402
|
+
* function App() {
|
|
403
|
+
* const methods = useForm();
|
|
404
|
+
* const onSubmit = data => console.log(data);
|
|
405
|
+
*
|
|
406
|
+
* return (
|
|
407
|
+
* <FormProvider {...methods} >
|
|
408
|
+
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
|
409
|
+
* <NestedInput />
|
|
410
|
+
* <input type="submit" />
|
|
411
|
+
* </form>
|
|
412
|
+
* </FormProvider>
|
|
413
|
+
* );
|
|
414
|
+
* }
|
|
415
|
+
*
|
|
416
|
+
* function NestedInput() {
|
|
417
|
+
* const { register } = useFormContext(); // retrieve all hook methods
|
|
418
|
+
* return <input {...register("test")} />;
|
|
419
|
+
* }
|
|
420
|
+
* ```
|
|
421
|
+
*/ var useFormContext = function() {
|
|
422
|
+
return React__default.useContext(HookFormContext);
|
|
423
|
+
};
|
|
424
|
+
/**
|
|
425
|
+
* A provider component that propagates the `useForm` methods to all children components via [React Context](https://reactjs.org/docs/context.html) API. To be used with {@link useFormContext}.
|
|
426
|
+
*
|
|
427
|
+
* @remarks
|
|
428
|
+
* [API](https://react-hook-form.com/docs/useformcontext) • [Demo](https://codesandbox.io/s/react-hook-form-v7-form-context-ytudi)
|
|
429
|
+
*
|
|
430
|
+
* @param props - all useForm methods
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* ```tsx
|
|
434
|
+
* function App() {
|
|
435
|
+
* const methods = useForm();
|
|
436
|
+
* const onSubmit = data => console.log(data);
|
|
437
|
+
*
|
|
438
|
+
* return (
|
|
439
|
+
* <FormProvider {...methods} >
|
|
440
|
+
* <form onSubmit={methods.handleSubmit(onSubmit)}>
|
|
441
|
+
* <NestedInput />
|
|
442
|
+
* <input type="submit" />
|
|
443
|
+
* </form>
|
|
444
|
+
* </FormProvider>
|
|
445
|
+
* );
|
|
446
|
+
* }
|
|
447
|
+
*
|
|
448
|
+
* function NestedInput() {
|
|
449
|
+
* const { register } = useFormContext(); // retrieve all hook methods
|
|
450
|
+
* return <input {...register("test")} />;
|
|
451
|
+
* }
|
|
452
|
+
* ```
|
|
453
|
+
*/ var FormProvider = function(props) {
|
|
454
|
+
var children = props.children, data = _object_without_properties(props, [
|
|
455
|
+
"children"
|
|
456
|
+
]);
|
|
457
|
+
return React__default.createElement(HookFormContext.Provider, {
|
|
458
|
+
value: data
|
|
459
|
+
}, children);
|
|
460
|
+
};
|
|
461
|
+
var getProxyFormState = function(formState, control, localProxyFormState) {
|
|
462
|
+
var _loop = function(key) {
|
|
463
|
+
Object.defineProperty(result, key, {
|
|
464
|
+
get: function() {
|
|
465
|
+
var _key = key;
|
|
466
|
+
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
|
467
|
+
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
|
468
|
+
}
|
|
469
|
+
localProxyFormState && (localProxyFormState[_key] = true);
|
|
470
|
+
return formState[_key];
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
};
|
|
474
|
+
var isRoot = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : true;
|
|
475
|
+
var result = {
|
|
476
|
+
defaultValues: control._defaultValues
|
|
477
|
+
};
|
|
478
|
+
for(var key in formState)_loop(key);
|
|
479
|
+
return result;
|
|
480
|
+
};
|
|
481
|
+
var useIsomorphicLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
|
|
482
|
+
/**
|
|
483
|
+
* This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
|
|
484
|
+
*
|
|
485
|
+
* @remarks
|
|
486
|
+
* [API](https://react-hook-form.com/docs/useformstate) • [Demo](https://codesandbox.io/s/useformstate-75xly)
|
|
487
|
+
*
|
|
488
|
+
* @param props - include options on specify fields to subscribe. {@link UseFormStateReturn}
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```tsx
|
|
492
|
+
* function App() {
|
|
493
|
+
* const { register, handleSubmit, control } = useForm({
|
|
494
|
+
* defaultValues: {
|
|
495
|
+
* firstName: "firstName"
|
|
496
|
+
* }});
|
|
497
|
+
* const { dirtyFields } = useFormState({
|
|
498
|
+
* control
|
|
499
|
+
* });
|
|
500
|
+
* const onSubmit = (data) => console.log(data);
|
|
501
|
+
*
|
|
502
|
+
* return (
|
|
503
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
|
504
|
+
* <input {...register("firstName")} placeholder="First Name" />
|
|
505
|
+
* {dirtyFields.firstName && <p>Field is dirty.</p>}
|
|
506
|
+
* <input type="submit" />
|
|
507
|
+
* </form>
|
|
508
|
+
* );
|
|
509
|
+
* }
|
|
510
|
+
* ```
|
|
511
|
+
*/ function useFormState(props) {
|
|
512
|
+
var methods = useFormContext();
|
|
513
|
+
var _ref = props || {}, _ref_control = _ref.control, control = _ref_control === void 0 ? methods.control : _ref_control, disabled = _ref.disabled, name = _ref.name, exact = _ref.exact;
|
|
514
|
+
var _React__default_useState = _sliced_to_array(React__default.useState(control._formState), 2), formState = _React__default_useState[0], updateFormState = _React__default_useState[1];
|
|
515
|
+
var _localProxyFormState = React__default.useRef({
|
|
516
|
+
isDirty: false,
|
|
517
|
+
isLoading: false,
|
|
518
|
+
dirtyFields: false,
|
|
519
|
+
touchedFields: false,
|
|
520
|
+
validatingFields: false,
|
|
521
|
+
isValidating: false,
|
|
522
|
+
isValid: false,
|
|
523
|
+
errors: false
|
|
524
|
+
});
|
|
525
|
+
useIsomorphicLayoutEffect(function() {
|
|
526
|
+
return control._subscribe({
|
|
527
|
+
name: name,
|
|
528
|
+
formState: _localProxyFormState.current,
|
|
529
|
+
exact: exact,
|
|
530
|
+
callback: function(formState) {
|
|
531
|
+
!disabled && updateFormState(_object_spread({}, control._formState, formState));
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
}, [
|
|
535
|
+
name,
|
|
536
|
+
disabled,
|
|
537
|
+
exact
|
|
538
|
+
]);
|
|
539
|
+
React__default.useEffect(function() {
|
|
540
|
+
_localProxyFormState.current.isValid && control._setValid(true);
|
|
541
|
+
}, [
|
|
542
|
+
control
|
|
543
|
+
]);
|
|
544
|
+
return React__default.useMemo(function() {
|
|
545
|
+
return getProxyFormState(formState, control, _localProxyFormState.current, false);
|
|
546
|
+
}, [
|
|
547
|
+
formState,
|
|
548
|
+
control
|
|
549
|
+
]);
|
|
550
|
+
}
|
|
551
|
+
var isString = function(value1) {
|
|
552
|
+
return typeof value1 === "string";
|
|
553
|
+
};
|
|
554
|
+
var generateWatchOutput = function(names, _names, formValues, isGlobal, defaultValue) {
|
|
555
|
+
if (isString(names)) {
|
|
556
|
+
isGlobal && _names.watch.add(names);
|
|
557
|
+
return get(formValues, names, defaultValue);
|
|
558
|
+
}
|
|
559
|
+
if (Array.isArray(names)) {
|
|
560
|
+
return names.map(function(fieldName) {
|
|
561
|
+
return isGlobal && _names.watch.add(fieldName), get(formValues, fieldName);
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
isGlobal && (_names.watchAll = true);
|
|
565
|
+
return formValues;
|
|
566
|
+
};
|
|
567
|
+
/**
|
|
568
|
+
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
|
569
|
+
*
|
|
570
|
+
* @remarks
|
|
571
|
+
*
|
|
572
|
+
* [API](https://react-hook-form.com/docs/usewatch) • [Demo](https://codesandbox.io/s/react-hook-form-v7-ts-usewatch-h9i5e)
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ```tsx
|
|
576
|
+
* const { control } = useForm();
|
|
577
|
+
* const values = useWatch({
|
|
578
|
+
* name: "fieldName"
|
|
579
|
+
* control,
|
|
580
|
+
* })
|
|
581
|
+
* ```
|
|
582
|
+
*/ function useWatch(props) {
|
|
583
|
+
var methods = useFormContext();
|
|
584
|
+
var _ref = props || {}, _ref_control = _ref.control, control = _ref_control === void 0 ? methods.control : _ref_control, name = _ref.name, defaultValue = _ref.defaultValue, disabled = _ref.disabled, exact = _ref.exact;
|
|
585
|
+
var _defaultValue = React__default.useRef(defaultValue);
|
|
586
|
+
var _React__default_useState = _sliced_to_array(React__default.useState(control._getWatch(name, _defaultValue.current)), 2), value1 = _React__default_useState[0], updateValue = _React__default_useState[1];
|
|
587
|
+
useIsomorphicLayoutEffect(function() {
|
|
588
|
+
return control._subscribe({
|
|
589
|
+
name: name,
|
|
590
|
+
formState: {
|
|
591
|
+
values: true
|
|
592
|
+
},
|
|
593
|
+
exact: exact,
|
|
594
|
+
callback: function(formState) {
|
|
595
|
+
return !disabled && updateValue(generateWatchOutput(name, control._names, formState.values || control._formValues, false, _defaultValue.current));
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
}, [
|
|
599
|
+
name,
|
|
600
|
+
control,
|
|
601
|
+
disabled,
|
|
602
|
+
exact
|
|
603
|
+
]);
|
|
604
|
+
React__default.useEffect(function() {
|
|
605
|
+
return control._removeUnmounted();
|
|
606
|
+
});
|
|
607
|
+
return value1;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
|
|
611
|
+
*
|
|
612
|
+
* @remarks
|
|
613
|
+
* [API](https://react-hook-form.com/docs/usecontroller) • [Demo](https://codesandbox.io/s/usecontroller-0o8px)
|
|
614
|
+
*
|
|
615
|
+
* @param props - the path name to the form field value, and validation rules.
|
|
616
|
+
*
|
|
617
|
+
* @returns field properties, field and form state. {@link UseControllerReturn}
|
|
618
|
+
*
|
|
619
|
+
* @example
|
|
620
|
+
* ```tsx
|
|
621
|
+
* function Input(props) {
|
|
622
|
+
* const { field, fieldState, formState } = useController(props);
|
|
623
|
+
* return (
|
|
624
|
+
* <div>
|
|
625
|
+
* <input {...field} placeholder={props.name} />
|
|
626
|
+
* <p>{fieldState.isTouched && "Touched"}</p>
|
|
627
|
+
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
|
628
|
+
* </div>
|
|
629
|
+
* );
|
|
630
|
+
* }
|
|
631
|
+
* ```
|
|
632
|
+
*/ function useController(props) {
|
|
633
|
+
var methods = useFormContext();
|
|
634
|
+
var name = props.name, disabled = props.disabled, _props_control = props.control, control = _props_control === void 0 ? methods.control : _props_control, shouldUnregister = props.shouldUnregister;
|
|
635
|
+
var isArrayField = isNameInFieldArray(control._names.array, name);
|
|
636
|
+
var value1 = useWatch({
|
|
637
|
+
control: control,
|
|
638
|
+
name: name,
|
|
639
|
+
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
|
640
|
+
exact: true
|
|
641
|
+
});
|
|
642
|
+
var formState = useFormState({
|
|
643
|
+
control: control,
|
|
644
|
+
name: name,
|
|
645
|
+
exact: true
|
|
646
|
+
});
|
|
647
|
+
var _props = React__default.useRef(props);
|
|
648
|
+
var _registerProps = React__default.useRef(control.register(name, _object_spread(_object_spread_props(_object_spread({}, props.rules), {
|
|
649
|
+
value: value1
|
|
650
|
+
}), isBoolean(props.disabled) ? {
|
|
651
|
+
disabled: props.disabled
|
|
652
|
+
} : {})));
|
|
653
|
+
var fieldState = React__default.useMemo(function() {
|
|
654
|
+
return Object.defineProperties({}, {
|
|
655
|
+
invalid: {
|
|
656
|
+
enumerable: true,
|
|
657
|
+
get: function() {
|
|
658
|
+
return !!get(formState.errors, name);
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
isDirty: {
|
|
662
|
+
enumerable: true,
|
|
663
|
+
get: function() {
|
|
664
|
+
return !!get(formState.dirtyFields, name);
|
|
665
|
+
}
|
|
666
|
+
},
|
|
667
|
+
isTouched: {
|
|
668
|
+
enumerable: true,
|
|
669
|
+
get: function() {
|
|
670
|
+
return !!get(formState.touchedFields, name);
|
|
671
|
+
}
|
|
672
|
+
},
|
|
673
|
+
isValidating: {
|
|
674
|
+
enumerable: true,
|
|
675
|
+
get: function() {
|
|
676
|
+
return !!get(formState.validatingFields, name);
|
|
677
|
+
}
|
|
678
|
+
},
|
|
679
|
+
error: {
|
|
680
|
+
enumerable: true,
|
|
681
|
+
get: function() {
|
|
682
|
+
return get(formState.errors, name);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
}, [
|
|
687
|
+
formState,
|
|
688
|
+
name
|
|
689
|
+
]);
|
|
690
|
+
var onChange = React__default.useCallback(function(event) {
|
|
691
|
+
return _registerProps.current.onChange({
|
|
692
|
+
target: {
|
|
693
|
+
value: getEventValue(event),
|
|
694
|
+
name: name
|
|
695
|
+
},
|
|
696
|
+
type: EVENTS.CHANGE
|
|
697
|
+
});
|
|
698
|
+
}, [
|
|
699
|
+
name
|
|
700
|
+
]);
|
|
701
|
+
var onBlur = React__default.useCallback(function() {
|
|
702
|
+
return _registerProps.current.onBlur({
|
|
703
|
+
target: {
|
|
704
|
+
value: get(control._formValues, name),
|
|
705
|
+
name: name
|
|
706
|
+
},
|
|
707
|
+
type: EVENTS.BLUR
|
|
708
|
+
});
|
|
709
|
+
}, [
|
|
710
|
+
name,
|
|
711
|
+
control._formValues
|
|
712
|
+
]);
|
|
713
|
+
var ref = React__default.useCallback(function(elm) {
|
|
714
|
+
var field = get(control._fields, name);
|
|
715
|
+
if (field && elm) {
|
|
716
|
+
field._f.ref = {
|
|
717
|
+
focus: function() {
|
|
718
|
+
return elm.focus();
|
|
719
|
+
},
|
|
720
|
+
select: function() {
|
|
721
|
+
return elm.select();
|
|
722
|
+
},
|
|
723
|
+
setCustomValidity: function(message) {
|
|
724
|
+
return elm.setCustomValidity(message);
|
|
725
|
+
},
|
|
726
|
+
reportValidity: function() {
|
|
727
|
+
return elm.reportValidity();
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
}, [
|
|
732
|
+
control._fields,
|
|
733
|
+
name
|
|
734
|
+
]);
|
|
735
|
+
var field = React__default.useMemo(function() {
|
|
736
|
+
return _object_spread_props(_object_spread({
|
|
737
|
+
name: name,
|
|
738
|
+
value: value1
|
|
739
|
+
}, isBoolean(disabled) || formState.disabled ? {
|
|
740
|
+
disabled: formState.disabled || disabled
|
|
741
|
+
} : {}), {
|
|
742
|
+
onChange: onChange,
|
|
743
|
+
onBlur: onBlur,
|
|
744
|
+
ref: ref
|
|
745
|
+
});
|
|
746
|
+
}, [
|
|
747
|
+
name,
|
|
748
|
+
disabled,
|
|
749
|
+
formState.disabled,
|
|
750
|
+
onChange,
|
|
751
|
+
onBlur,
|
|
752
|
+
ref,
|
|
753
|
+
value1
|
|
754
|
+
]);
|
|
755
|
+
React__default.useEffect(function() {
|
|
756
|
+
var _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
757
|
+
control.register(name, _object_spread({}, _props.current.rules, isBoolean(_props.current.disabled) ? {
|
|
758
|
+
disabled: _props.current.disabled
|
|
759
|
+
} : {}));
|
|
760
|
+
var updateMounted = function(name, value1) {
|
|
761
|
+
var field = get(control._fields, name);
|
|
762
|
+
if (field && field._f) {
|
|
763
|
+
field._f.mount = value1;
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
updateMounted(name, true);
|
|
767
|
+
if (_shouldUnregisterField) {
|
|
768
|
+
var value1 = cloneObject(get(control._options.defaultValues, name));
|
|
769
|
+
set(control._defaultValues, name, value1);
|
|
770
|
+
if (isUndefined(get(control._formValues, name))) {
|
|
771
|
+
set(control._formValues, name, value1);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
!isArrayField && control.register(name);
|
|
775
|
+
return function() {
|
|
776
|
+
(isArrayField ? _shouldUnregisterField && !control._state.action : _shouldUnregisterField) ? control.unregister(name) : updateMounted(name, false);
|
|
777
|
+
};
|
|
778
|
+
}, [
|
|
779
|
+
name,
|
|
780
|
+
control,
|
|
781
|
+
isArrayField,
|
|
782
|
+
shouldUnregister
|
|
783
|
+
]);
|
|
784
|
+
React__default.useEffect(function() {
|
|
785
|
+
control._setDisabledField({
|
|
786
|
+
disabled: disabled,
|
|
787
|
+
name: name
|
|
788
|
+
});
|
|
789
|
+
}, [
|
|
790
|
+
disabled,
|
|
791
|
+
name,
|
|
792
|
+
control
|
|
793
|
+
]);
|
|
794
|
+
return React__default.useMemo(function() {
|
|
795
|
+
return {
|
|
796
|
+
field: field,
|
|
797
|
+
formState: formState,
|
|
798
|
+
fieldState: fieldState
|
|
799
|
+
};
|
|
800
|
+
}, [
|
|
801
|
+
field,
|
|
802
|
+
formState,
|
|
803
|
+
fieldState
|
|
804
|
+
]);
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Component based on `useController` hook to work with controlled component.
|
|
808
|
+
*
|
|
809
|
+
* @remarks
|
|
810
|
+
* [API](https://react-hook-form.com/docs/usecontroller/controller) • [Demo](https://codesandbox.io/s/react-hook-form-v6-controller-ts-jwyzw) • [Video](https://www.youtube.com/watch?v=N2UNk_UCVyA)
|
|
811
|
+
*
|
|
812
|
+
* @param props - the path name to the form field value, and validation rules.
|
|
813
|
+
*
|
|
814
|
+
* @returns provide field handler functions, field and form state.
|
|
815
|
+
*
|
|
816
|
+
* @example
|
|
817
|
+
* ```tsx
|
|
818
|
+
* function App() {
|
|
819
|
+
* const { control } = useForm<FormValues>({
|
|
820
|
+
* defaultValues: {
|
|
821
|
+
* test: ""
|
|
822
|
+
* }
|
|
823
|
+
* });
|
|
824
|
+
*
|
|
825
|
+
* return (
|
|
826
|
+
* <form>
|
|
827
|
+
* <Controller
|
|
828
|
+
* control={control}
|
|
829
|
+
* name="test"
|
|
830
|
+
* render={({ field: { onChange, onBlur, value, ref }, formState, fieldState }) => (
|
|
831
|
+
* <>
|
|
832
|
+
* <input
|
|
833
|
+
* onChange={onChange} // send value to hook form
|
|
834
|
+
* onBlur={onBlur} // notify when input is touched
|
|
835
|
+
* value={value} // return updated value
|
|
836
|
+
* ref={ref} // set ref for focus management
|
|
837
|
+
* />
|
|
838
|
+
* <p>{formState.isSubmitted ? "submitted" : ""}</p>
|
|
839
|
+
* <p>{fieldState.isTouched ? "touched" : ""}</p>
|
|
840
|
+
* </>
|
|
841
|
+
* )}
|
|
842
|
+
* />
|
|
843
|
+
* </form>
|
|
844
|
+
* );
|
|
845
|
+
* }
|
|
846
|
+
* ```
|
|
847
|
+
*/ var Controller = function(props) {
|
|
848
|
+
return props.render(useController(props));
|
|
849
|
+
};
|
|
850
|
+
var appendErrors = function(name, validateAllFieldCriteria, errors, type, message) {
|
|
851
|
+
return validateAllFieldCriteria ? _object_spread_props(_object_spread({}, errors[name]), {
|
|
852
|
+
types: _object_spread_props(_object_spread({}, errors[name] && errors[name].types ? errors[name].types : {}), _define_property({}, type, message || true))
|
|
853
|
+
}) : {};
|
|
854
|
+
};
|
|
855
|
+
var convertToArrayPayload = function(value1) {
|
|
856
|
+
return Array.isArray(value1) ? value1 : [
|
|
857
|
+
value1
|
|
858
|
+
];
|
|
859
|
+
};
|
|
860
|
+
var createSubject = function() {
|
|
861
|
+
var _observers = [];
|
|
862
|
+
var next = function(value1) {
|
|
863
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
864
|
+
try {
|
|
865
|
+
for(var _iterator = _observers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
866
|
+
var observer = _step.value;
|
|
867
|
+
observer.next && observer.next(value1);
|
|
868
|
+
}
|
|
869
|
+
} catch (err) {
|
|
870
|
+
_didIteratorError = true;
|
|
871
|
+
_iteratorError = err;
|
|
872
|
+
} finally{
|
|
873
|
+
try {
|
|
874
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
875
|
+
_iterator.return();
|
|
876
|
+
}
|
|
877
|
+
} finally{
|
|
878
|
+
if (_didIteratorError) {
|
|
879
|
+
throw _iteratorError;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
var subscribe = function(observer) {
|
|
885
|
+
_observers.push(observer);
|
|
886
|
+
return {
|
|
887
|
+
unsubscribe: function() {
|
|
888
|
+
_observers = _observers.filter(function(o) {
|
|
889
|
+
return o !== observer;
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
};
|
|
894
|
+
var unsubscribe = function() {
|
|
895
|
+
_observers = [];
|
|
896
|
+
};
|
|
897
|
+
return {
|
|
898
|
+
get observers () {
|
|
899
|
+
return _observers;
|
|
900
|
+
},
|
|
901
|
+
next: next,
|
|
902
|
+
subscribe: subscribe,
|
|
903
|
+
unsubscribe: unsubscribe
|
|
904
|
+
};
|
|
905
|
+
};
|
|
906
|
+
var isPrimitive = function(value1) {
|
|
907
|
+
return isNullOrUndefined(value1) || !isObjectType(value1);
|
|
908
|
+
};
|
|
909
|
+
function deepEqual(object1, object2) {
|
|
910
|
+
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
911
|
+
return object1 === object2;
|
|
912
|
+
}
|
|
913
|
+
if (isDateObject(object1) && isDateObject(object2)) {
|
|
914
|
+
return object1.getTime() === object2.getTime();
|
|
915
|
+
}
|
|
916
|
+
var keys1 = Object.keys(object1);
|
|
917
|
+
var keys2 = Object.keys(object2);
|
|
918
|
+
if (keys1.length !== keys2.length) {
|
|
919
|
+
return false;
|
|
920
|
+
}
|
|
921
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
922
|
+
try {
|
|
923
|
+
for(var _iterator = keys1[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
924
|
+
var key = _step.value;
|
|
925
|
+
var val1 = object1[key];
|
|
926
|
+
if (!keys2.includes(key)) {
|
|
927
|
+
return false;
|
|
928
|
+
}
|
|
929
|
+
if (key !== "ref") {
|
|
930
|
+
var val2 = object2[key];
|
|
931
|
+
if (isDateObject(val1) && isDateObject(val2) || isObject(val1) && isObject(val2) || Array.isArray(val1) && Array.isArray(val2) ? !deepEqual(val1, val2) : val1 !== val2) {
|
|
932
|
+
return false;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
} catch (err) {
|
|
937
|
+
_didIteratorError = true;
|
|
938
|
+
_iteratorError = err;
|
|
939
|
+
} finally{
|
|
940
|
+
try {
|
|
941
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
942
|
+
_iterator.return();
|
|
943
|
+
}
|
|
944
|
+
} finally{
|
|
945
|
+
if (_didIteratorError) {
|
|
946
|
+
throw _iteratorError;
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return true;
|
|
951
|
+
}
|
|
952
|
+
var isEmptyObject = function(value1) {
|
|
953
|
+
return isObject(value1) && !Object.keys(value1).length;
|
|
954
|
+
};
|
|
955
|
+
var isFileInput = function(element) {
|
|
956
|
+
return element.type === "file";
|
|
957
|
+
};
|
|
958
|
+
var isFunction = function(value1) {
|
|
959
|
+
return typeof value1 === "function";
|
|
960
|
+
};
|
|
961
|
+
var isHTMLElement = function(value1) {
|
|
962
|
+
if (!isWeb) {
|
|
963
|
+
return false;
|
|
964
|
+
}
|
|
965
|
+
var owner = value1 ? value1.ownerDocument : 0;
|
|
966
|
+
return _instanceof(value1, owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement);
|
|
967
|
+
};
|
|
968
|
+
var isMultipleSelect = function(element) {
|
|
969
|
+
return element.type === "select-multiple";
|
|
970
|
+
};
|
|
971
|
+
var isRadioInput = function(element) {
|
|
972
|
+
return element.type === "radio";
|
|
973
|
+
};
|
|
974
|
+
var isRadioOrCheckbox = function(ref) {
|
|
975
|
+
return isRadioInput(ref) || isCheckBoxInput(ref);
|
|
976
|
+
};
|
|
977
|
+
var live = function(ref) {
|
|
978
|
+
return isHTMLElement(ref) && ref.isConnected;
|
|
979
|
+
};
|
|
980
|
+
function baseGet(object, updatePath) {
|
|
981
|
+
var length = updatePath.slice(0, -1).length;
|
|
982
|
+
var index = 0;
|
|
983
|
+
while(index < length){
|
|
984
|
+
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
985
|
+
}
|
|
986
|
+
return object;
|
|
987
|
+
}
|
|
988
|
+
function isEmptyArray(obj) {
|
|
989
|
+
for(var key in obj){
|
|
990
|
+
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
991
|
+
return false;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return true;
|
|
995
|
+
}
|
|
996
|
+
function unset(object, path) {
|
|
997
|
+
var paths = Array.isArray(path) ? path : isKey(path) ? [
|
|
998
|
+
path
|
|
999
|
+
] : stringToPath(path);
|
|
1000
|
+
var childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
1001
|
+
var index = paths.length - 1;
|
|
1002
|
+
var key = paths[index];
|
|
1003
|
+
if (childObject) {
|
|
1004
|
+
delete childObject[key];
|
|
1005
|
+
}
|
|
1006
|
+
if (index !== 0 && (isObject(childObject) && isEmptyObject(childObject) || Array.isArray(childObject) && isEmptyArray(childObject))) {
|
|
1007
|
+
unset(object, paths.slice(0, -1));
|
|
1008
|
+
}
|
|
1009
|
+
return object;
|
|
1010
|
+
}
|
|
1011
|
+
var objectHasFunction = function(data) {
|
|
1012
|
+
for(var key in data){
|
|
1013
|
+
if (isFunction(data[key])) {
|
|
1014
|
+
return true;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
return false;
|
|
1018
|
+
};
|
|
1019
|
+
function markFieldsDirty(data) {
|
|
1020
|
+
var fields = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
1021
|
+
var isParentNodeArray = Array.isArray(data);
|
|
1022
|
+
if (isObject(data) || isParentNodeArray) {
|
|
1023
|
+
for(var key in data){
|
|
1024
|
+
if (Array.isArray(data[key]) || isObject(data[key]) && !objectHasFunction(data[key])) {
|
|
1025
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
|
1026
|
+
markFieldsDirty(data[key], fields[key]);
|
|
1027
|
+
} else if (!isNullOrUndefined(data[key])) {
|
|
1028
|
+
fields[key] = true;
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
return fields;
|
|
1033
|
+
}
|
|
1034
|
+
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
|
1035
|
+
var isParentNodeArray = Array.isArray(data);
|
|
1036
|
+
if (isObject(data) || isParentNodeArray) {
|
|
1037
|
+
for(var key in data){
|
|
1038
|
+
if (Array.isArray(data[key]) || isObject(data[key]) && !objectHasFunction(data[key])) {
|
|
1039
|
+
if (isUndefined(formValues) || isPrimitive(dirtyFieldsFromValues[key])) {
|
|
1040
|
+
dirtyFieldsFromValues[key] = Array.isArray(data[key]) ? markFieldsDirty(data[key], []) : _object_spread({}, markFieldsDirty(data[key]));
|
|
1041
|
+
} else {
|
|
1042
|
+
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
|
1043
|
+
}
|
|
1044
|
+
} else {
|
|
1045
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return dirtyFieldsFromValues;
|
|
1050
|
+
}
|
|
1051
|
+
var getDirtyFields = function(defaultValues, formValues) {
|
|
1052
|
+
return getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
|
1053
|
+
};
|
|
1054
|
+
var defaultResult = {
|
|
1055
|
+
value: false,
|
|
1056
|
+
isValid: false
|
|
1057
|
+
};
|
|
1058
|
+
var validResult = {
|
|
1059
|
+
value: true,
|
|
1060
|
+
isValid: true
|
|
1061
|
+
};
|
|
1062
|
+
var getCheckboxValue = function(options) {
|
|
1063
|
+
if (Array.isArray(options)) {
|
|
1064
|
+
if (options.length > 1) {
|
|
1065
|
+
var values = options.filter(function(option) {
|
|
1066
|
+
return option && option.checked && !option.disabled;
|
|
1067
|
+
}).map(function(option) {
|
|
1068
|
+
return option.value;
|
|
1069
|
+
});
|
|
1070
|
+
return {
|
|
1071
|
+
value: values,
|
|
1072
|
+
isValid: !!values.length
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
return options[0].checked && !options[0].disabled ? options[0].attributes && !isUndefined(options[0].attributes.value) ? isUndefined(options[0].value) || options[0].value === "" ? validResult : {
|
|
1076
|
+
value: options[0].value,
|
|
1077
|
+
isValid: true
|
|
1078
|
+
} : validResult : defaultResult;
|
|
1079
|
+
}
|
|
1080
|
+
return defaultResult;
|
|
1081
|
+
};
|
|
1082
|
+
var getFieldValueAs = function(value1, param) {
|
|
1083
|
+
var valueAsNumber = param.valueAsNumber, valueAsDate = param.valueAsDate, setValueAs = param.setValueAs;
|
|
1084
|
+
return isUndefined(value1) ? value1 : valueAsNumber ? value1 === "" ? NaN : value1 ? +value1 : value1 : valueAsDate && isString(value1) ? new Date(value1) : setValueAs ? setValueAs(value1) : value1;
|
|
1085
|
+
};
|
|
1086
|
+
var defaultReturn = {
|
|
1087
|
+
isValid: false,
|
|
1088
|
+
value: null
|
|
1089
|
+
};
|
|
1090
|
+
var getRadioValue = function(options) {
|
|
1091
|
+
return Array.isArray(options) ? options.reduce(function(previous, option) {
|
|
1092
|
+
return option && option.checked && !option.disabled ? {
|
|
1093
|
+
isValid: true,
|
|
1094
|
+
value: option.value
|
|
1095
|
+
} : previous;
|
|
1096
|
+
}, defaultReturn) : defaultReturn;
|
|
1097
|
+
};
|
|
1098
|
+
function getFieldValue(_f) {
|
|
1099
|
+
var ref = _f.ref;
|
|
1100
|
+
if (isFileInput(ref)) {
|
|
1101
|
+
return ref.files;
|
|
1102
|
+
}
|
|
1103
|
+
if (isRadioInput(ref)) {
|
|
1104
|
+
return getRadioValue(_f.refs).value;
|
|
1105
|
+
}
|
|
1106
|
+
if (isMultipleSelect(ref)) {
|
|
1107
|
+
return _to_consumable_array(ref.selectedOptions).map(function(param) {
|
|
1108
|
+
var value1 = param.value;
|
|
1109
|
+
return value1;
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
if (isCheckBoxInput(ref)) {
|
|
1113
|
+
return getCheckboxValue(_f.refs).value;
|
|
1114
|
+
}
|
|
1115
|
+
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
|
1116
|
+
}
|
|
1117
|
+
var getResolverOptions = function(fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) {
|
|
1118
|
+
var fields = {};
|
|
1119
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
1120
|
+
try {
|
|
1121
|
+
for(var _iterator = fieldsNames[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
1122
|
+
var name = _step.value;
|
|
1123
|
+
var field = get(_fields, name);
|
|
1124
|
+
field && set(fields, name, field._f);
|
|
1125
|
+
}
|
|
1126
|
+
} catch (err) {
|
|
1127
|
+
_didIteratorError = true;
|
|
1128
|
+
_iteratorError = err;
|
|
1129
|
+
} finally{
|
|
1130
|
+
try {
|
|
1131
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
1132
|
+
_iterator.return();
|
|
1133
|
+
}
|
|
1134
|
+
} finally{
|
|
1135
|
+
if (_didIteratorError) {
|
|
1136
|
+
throw _iteratorError;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
return {
|
|
1141
|
+
criteriaMode: criteriaMode,
|
|
1142
|
+
names: _to_consumable_array(fieldsNames),
|
|
1143
|
+
fields: fields,
|
|
1144
|
+
shouldUseNativeValidation: shouldUseNativeValidation
|
|
1145
|
+
};
|
|
1146
|
+
};
|
|
1147
|
+
var isRegex = function(value1) {
|
|
1148
|
+
return _instanceof(value1, RegExp);
|
|
1149
|
+
};
|
|
1150
|
+
var getRuleValue = function(rule) {
|
|
1151
|
+
return isUndefined(rule) ? rule : isRegex(rule) ? rule.source : isObject(rule) ? isRegex(rule.value) ? rule.value.source : rule.value : rule;
|
|
1152
|
+
};
|
|
1153
|
+
var getValidationModes = function(mode) {
|
|
1154
|
+
return {
|
|
1155
|
+
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
1156
|
+
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
1157
|
+
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
1158
|
+
isOnAll: mode === VALIDATION_MODE.all,
|
|
1159
|
+
isOnTouch: mode === VALIDATION_MODE.onTouched
|
|
1160
|
+
};
|
|
1161
|
+
};
|
|
1162
|
+
var ASYNC_FUNCTION = "AsyncFunction";
|
|
1163
|
+
var hasPromiseValidation = function(fieldReference) {
|
|
1164
|
+
return !!fieldReference && !!fieldReference.validate && !!(isFunction(fieldReference.validate) && fieldReference.validate.constructor.name === ASYNC_FUNCTION || isObject(fieldReference.validate) && Object.values(fieldReference.validate).find(function(validateFunction) {
|
|
1165
|
+
return validateFunction.constructor.name === ASYNC_FUNCTION;
|
|
1166
|
+
}));
|
|
1167
|
+
};
|
|
1168
|
+
var hasValidation = function(options) {
|
|
1169
|
+
return options.mount && (options.required || options.min || options.max || options.maxLength || options.minLength || options.pattern || options.validate);
|
|
1170
|
+
};
|
|
1171
|
+
var isWatched = function(name, _names, isBlurEvent) {
|
|
1172
|
+
return !isBlurEvent && (_names.watchAll || _names.watch.has(name) || _to_consumable_array(_names.watch).some(function(watchName) {
|
|
1173
|
+
return name.startsWith(watchName) && /^\.\w+/.test(name.slice(watchName.length));
|
|
1174
|
+
}));
|
|
1175
|
+
};
|
|
1176
|
+
var iterateFieldsByAction = function(fields, action, fieldsNames, abortEarly) {
|
|
1177
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
1178
|
+
try {
|
|
1179
|
+
for(var _iterator = (fieldsNames || Object.keys(fields))[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
1180
|
+
var _ref = _step.value;
|
|
1181
|
+
var key = _ref;
|
|
1182
|
+
var field = get(fields, key);
|
|
1183
|
+
if (field) {
|
|
1184
|
+
var _f = field._f, currentField = _object_without_properties(field, [
|
|
1185
|
+
"_f"
|
|
1186
|
+
]);
|
|
1187
|
+
if (_f) {
|
|
1188
|
+
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
|
|
1189
|
+
return true;
|
|
1190
|
+
} else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
|
|
1191
|
+
return true;
|
|
1192
|
+
} else {
|
|
1193
|
+
if (iterateFieldsByAction(currentField, action)) {
|
|
1194
|
+
break;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
} else if (isObject(currentField)) {
|
|
1198
|
+
if (iterateFieldsByAction(currentField, action)) {
|
|
1199
|
+
break;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
} catch (err) {
|
|
1205
|
+
_didIteratorError = true;
|
|
1206
|
+
_iteratorError = err;
|
|
1207
|
+
} finally{
|
|
1208
|
+
try {
|
|
1209
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
1210
|
+
_iterator.return();
|
|
1211
|
+
}
|
|
1212
|
+
} finally{
|
|
1213
|
+
if (_didIteratorError) {
|
|
1214
|
+
throw _iteratorError;
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
return;
|
|
1219
|
+
};
|
|
1220
|
+
function schemaErrorLookup(errors, _fields, name) {
|
|
1221
|
+
var error = get(errors, name);
|
|
1222
|
+
if (error || isKey(name)) {
|
|
1223
|
+
return {
|
|
1224
|
+
error: error,
|
|
1225
|
+
name: name
|
|
1226
|
+
};
|
|
1227
|
+
}
|
|
1228
|
+
var names = name.split(".");
|
|
1229
|
+
while(names.length){
|
|
1230
|
+
var fieldName = names.join(".");
|
|
1231
|
+
var field = get(_fields, fieldName);
|
|
1232
|
+
var foundError = get(errors, fieldName);
|
|
1233
|
+
if (field && !Array.isArray(field) && name !== fieldName) {
|
|
1234
|
+
return {
|
|
1235
|
+
name: name
|
|
1236
|
+
};
|
|
1237
|
+
}
|
|
1238
|
+
if (foundError && foundError.type) {
|
|
1239
|
+
return {
|
|
1240
|
+
name: fieldName,
|
|
1241
|
+
error: foundError
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
names.pop();
|
|
1245
|
+
}
|
|
1246
|
+
return {
|
|
1247
|
+
name: name
|
|
1248
|
+
};
|
|
1249
|
+
}
|
|
1250
|
+
var shouldRenderFormState = function(formStateData, _proxyFormState, updateFormState, isRoot) {
|
|
1251
|
+
updateFormState(formStateData);
|
|
1252
|
+
formStateData.name; var formState = _object_without_properties(formStateData, [
|
|
1253
|
+
"name"
|
|
1254
|
+
]);
|
|
1255
|
+
return isEmptyObject(formState) || Object.keys(formState).length >= Object.keys(_proxyFormState).length || Object.keys(formState).find(function(key) {
|
|
1256
|
+
return _proxyFormState[key] === (!isRoot || VALIDATION_MODE.all);
|
|
1257
|
+
});
|
|
1258
|
+
};
|
|
1259
|
+
var shouldSubscribeByName = function(name, signalName, exact) {
|
|
1260
|
+
return !name || !signalName || name === signalName || convertToArrayPayload(name).some(function(currentName) {
|
|
1261
|
+
return currentName && (exact ? currentName === signalName : currentName.startsWith(signalName) || signalName.startsWith(currentName));
|
|
1262
|
+
});
|
|
1263
|
+
};
|
|
1264
|
+
var skipValidation = function(isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) {
|
|
1265
|
+
if (mode.isOnAll) {
|
|
1266
|
+
return false;
|
|
1267
|
+
} else if (!isSubmitted && mode.isOnTouch) {
|
|
1268
|
+
return !(isTouched || isBlurEvent);
|
|
1269
|
+
} else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
|
1270
|
+
return !isBlurEvent;
|
|
1271
|
+
} else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
|
1272
|
+
return isBlurEvent;
|
|
1273
|
+
}
|
|
1274
|
+
return true;
|
|
1275
|
+
};
|
|
1276
|
+
var unsetEmptyArray = function(ref, name) {
|
|
1277
|
+
return !compact(get(ref, name)).length && unset(ref, name);
|
|
1278
|
+
};
|
|
1279
|
+
var updateFieldArrayRootError = function(errors, error, name) {
|
|
1280
|
+
var fieldArrayErrors = convertToArrayPayload(get(errors, name));
|
|
1281
|
+
set(fieldArrayErrors, "root", error[name]);
|
|
1282
|
+
set(errors, name, fieldArrayErrors);
|
|
1283
|
+
return errors;
|
|
1284
|
+
};
|
|
1285
|
+
var isMessage = function(value1) {
|
|
1286
|
+
return isString(value1);
|
|
1287
|
+
};
|
|
1288
|
+
function getValidateError(result, ref) {
|
|
1289
|
+
var type = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "validate";
|
|
1290
|
+
if (isMessage(result) || Array.isArray(result) && result.every(isMessage) || isBoolean(result) && !result) {
|
|
1291
|
+
return {
|
|
1292
|
+
type: type,
|
|
1293
|
+
message: isMessage(result) ? result : "",
|
|
1294
|
+
ref: ref
|
|
1295
|
+
};
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
var getValueAndMessage = function(validationData) {
|
|
1299
|
+
return isObject(validationData) && !isRegex(validationData) ? validationData : {
|
|
1300
|
+
value: validationData,
|
|
1301
|
+
message: ""
|
|
1302
|
+
};
|
|
1303
|
+
};
|
|
1304
|
+
var validateField = function() {
|
|
1305
|
+
var _ref = _async_to_generator(function(field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) {
|
|
1306
|
+
var _field__f, ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, inputValue, inputRef, setCustomValidity, error, isRadio, isCheckBox, isRadioOrCheckbox, isEmpty, appendErrorsCurry, getMinMaxMessage, _ref, value1, message, exceedMax, exceedMin, maxOutput, minOutput, valueNumber, valueDate, convertTimeToDate, isTime, isWeek, maxLengthOutput, minLengthOutput, exceedMax1, exceedMin1, _getValueAndMessage, patternValue, message1, result, validateError, validationResult, _tmp, _tmp1, _i, key, validateError1;
|
|
1307
|
+
return _ts_generator(this, function(_state) {
|
|
1308
|
+
switch(_state.label){
|
|
1309
|
+
case 0:
|
|
1310
|
+
_field__f = field._f, ref = _field__f.ref, refs = _field__f.refs, required = _field__f.required, maxLength = _field__f.maxLength, minLength = _field__f.minLength, min = _field__f.min, max = _field__f.max, pattern = _field__f.pattern, validate = _field__f.validate, name = _field__f.name, valueAsNumber = _field__f.valueAsNumber, mount = _field__f.mount;
|
|
1311
|
+
inputValue = get(formValues, name);
|
|
1312
|
+
if (!mount || disabledFieldNames.has(name)) {
|
|
1313
|
+
return [
|
|
1314
|
+
2,
|
|
1315
|
+
{}
|
|
1316
|
+
];
|
|
1317
|
+
}
|
|
1318
|
+
inputRef = refs ? refs[0] : ref;
|
|
1319
|
+
setCustomValidity = function(message) {
|
|
1320
|
+
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
|
1321
|
+
inputRef.setCustomValidity(isBoolean(message) ? "" : message || "");
|
|
1322
|
+
inputRef.reportValidity();
|
|
1323
|
+
}
|
|
1324
|
+
};
|
|
1325
|
+
error = {};
|
|
1326
|
+
isRadio = isRadioInput(ref);
|
|
1327
|
+
isCheckBox = isCheckBoxInput(ref);
|
|
1328
|
+
isRadioOrCheckbox = isRadio || isCheckBox;
|
|
1329
|
+
isEmpty = (valueAsNumber || isFileInput(ref)) && isUndefined(ref.value) && isUndefined(inputValue) || isHTMLElement(ref) && ref.value === "" || inputValue === "" || Array.isArray(inputValue) && !inputValue.length;
|
|
1330
|
+
appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
1331
|
+
getMinMaxMessage = function(exceedMax, maxLengthMessage, minLengthMessage) {
|
|
1332
|
+
var maxType = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : INPUT_VALIDATION_RULES.maxLength, minType = arguments.length > 4 && arguments[4] !== void 0 ? arguments[4] : INPUT_VALIDATION_RULES.minLength;
|
|
1333
|
+
var message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
1334
|
+
error[name] = _object_spread({
|
|
1335
|
+
type: exceedMax ? maxType : minType,
|
|
1336
|
+
message: message,
|
|
1337
|
+
ref: ref
|
|
1338
|
+
}, appendErrorsCurry(exceedMax ? maxType : minType, message));
|
|
1339
|
+
};
|
|
1340
|
+
if (isFieldArray ? !Array.isArray(inputValue) || !inputValue.length : required && (!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue)) || isBoolean(inputValue) && !inputValue || isCheckBox && !getCheckboxValue(refs).isValid || isRadio && !getRadioValue(refs).isValid)) {
|
|
1341
|
+
_ref = isMessage(required) ? {
|
|
1342
|
+
value: !!required,
|
|
1343
|
+
message: required
|
|
1344
|
+
} : getValueAndMessage(required), value1 = _ref.value, message = _ref.message;
|
|
1345
|
+
if (value1) {
|
|
1346
|
+
error[name] = _object_spread({
|
|
1347
|
+
type: INPUT_VALIDATION_RULES.required,
|
|
1348
|
+
message: message,
|
|
1349
|
+
ref: inputRef
|
|
1350
|
+
}, appendErrorsCurry(INPUT_VALIDATION_RULES.required, message));
|
|
1351
|
+
if (!validateAllFieldCriteria) {
|
|
1352
|
+
setCustomValidity(message);
|
|
1353
|
+
return [
|
|
1354
|
+
2,
|
|
1355
|
+
error
|
|
1356
|
+
];
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
|
1361
|
+
maxOutput = getValueAndMessage(max);
|
|
1362
|
+
minOutput = getValueAndMessage(min);
|
|
1363
|
+
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
|
1364
|
+
valueNumber = ref.valueAsNumber || (inputValue ? +inputValue : inputValue);
|
|
1365
|
+
if (!isNullOrUndefined(maxOutput.value)) {
|
|
1366
|
+
exceedMax = valueNumber > maxOutput.value;
|
|
1367
|
+
}
|
|
1368
|
+
if (!isNullOrUndefined(minOutput.value)) {
|
|
1369
|
+
exceedMin = valueNumber < minOutput.value;
|
|
1370
|
+
}
|
|
1371
|
+
} else {
|
|
1372
|
+
valueDate = ref.valueAsDate || new Date(inputValue);
|
|
1373
|
+
convertTimeToDate = function(time) {
|
|
1374
|
+
return new Date(new Date().toDateString() + " " + time);
|
|
1375
|
+
};
|
|
1376
|
+
isTime = ref.type == "time";
|
|
1377
|
+
isWeek = ref.type == "week";
|
|
1378
|
+
if (isString(maxOutput.value) && inputValue) {
|
|
1379
|
+
exceedMax = isTime ? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value) : isWeek ? inputValue > maxOutput.value : valueDate > new Date(maxOutput.value);
|
|
1380
|
+
}
|
|
1381
|
+
if (isString(minOutput.value) && inputValue) {
|
|
1382
|
+
exceedMin = isTime ? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value) : isWeek ? inputValue < minOutput.value : valueDate < new Date(minOutput.value);
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
if (exceedMax || exceedMin) {
|
|
1386
|
+
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
1387
|
+
if (!validateAllFieldCriteria) {
|
|
1388
|
+
setCustomValidity(error[name].message);
|
|
1389
|
+
return [
|
|
1390
|
+
2,
|
|
1391
|
+
error
|
|
1392
|
+
];
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
if ((maxLength || minLength) && !isEmpty && (isString(inputValue) || isFieldArray && Array.isArray(inputValue))) {
|
|
1397
|
+
maxLengthOutput = getValueAndMessage(maxLength);
|
|
1398
|
+
minLengthOutput = getValueAndMessage(minLength);
|
|
1399
|
+
exceedMax1 = !isNullOrUndefined(maxLengthOutput.value) && inputValue.length > +maxLengthOutput.value;
|
|
1400
|
+
exceedMin1 = !isNullOrUndefined(minLengthOutput.value) && inputValue.length < +minLengthOutput.value;
|
|
1401
|
+
if (exceedMax1 || exceedMin1) {
|
|
1402
|
+
getMinMaxMessage(exceedMax1, maxLengthOutput.message, minLengthOutput.message);
|
|
1403
|
+
if (!validateAllFieldCriteria) {
|
|
1404
|
+
setCustomValidity(error[name].message);
|
|
1405
|
+
return [
|
|
1406
|
+
2,
|
|
1407
|
+
error
|
|
1408
|
+
];
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
if (pattern && !isEmpty && isString(inputValue)) {
|
|
1413
|
+
_getValueAndMessage = getValueAndMessage(pattern), patternValue = _getValueAndMessage.value, message1 = _getValueAndMessage.message;
|
|
1414
|
+
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
|
1415
|
+
error[name] = _object_spread({
|
|
1416
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
|
1417
|
+
message: message1,
|
|
1418
|
+
ref: ref
|
|
1419
|
+
}, appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message1));
|
|
1420
|
+
if (!validateAllFieldCriteria) {
|
|
1421
|
+
setCustomValidity(message1);
|
|
1422
|
+
return [
|
|
1423
|
+
2,
|
|
1424
|
+
error
|
|
1425
|
+
];
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
if (!validate) return [
|
|
1430
|
+
3,
|
|
1431
|
+
7
|
|
1432
|
+
];
|
|
1433
|
+
if (!isFunction(validate)) return [
|
|
1434
|
+
3,
|
|
1435
|
+
2
|
|
1436
|
+
];
|
|
1437
|
+
return [
|
|
1438
|
+
4,
|
|
1439
|
+
validate(inputValue, formValues)
|
|
1440
|
+
];
|
|
1441
|
+
case 1:
|
|
1442
|
+
result = _state.sent();
|
|
1443
|
+
validateError = getValidateError(result, inputRef);
|
|
1444
|
+
if (validateError) {
|
|
1445
|
+
error[name] = _object_spread({}, validateError, appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message));
|
|
1446
|
+
if (!validateAllFieldCriteria) {
|
|
1447
|
+
setCustomValidity(validateError.message);
|
|
1448
|
+
return [
|
|
1449
|
+
2,
|
|
1450
|
+
error
|
|
1451
|
+
];
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
return [
|
|
1455
|
+
3,
|
|
1456
|
+
7
|
|
1457
|
+
];
|
|
1458
|
+
case 2:
|
|
1459
|
+
if (!isObject(validate)) return [
|
|
1460
|
+
3,
|
|
1461
|
+
7
|
|
1462
|
+
];
|
|
1463
|
+
validationResult = {};
|
|
1464
|
+
_tmp = [];
|
|
1465
|
+
for(_tmp1 in validate)_tmp.push(_tmp1);
|
|
1466
|
+
_i = 0;
|
|
1467
|
+
_state.label = 3;
|
|
1468
|
+
case 3:
|
|
1469
|
+
if (!(_i < _tmp.length)) return [
|
|
1470
|
+
3,
|
|
1471
|
+
6
|
|
1472
|
+
];
|
|
1473
|
+
key = _tmp[_i];
|
|
1474
|
+
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
1475
|
+
return [
|
|
1476
|
+
3,
|
|
1477
|
+
6
|
|
1478
|
+
];
|
|
1479
|
+
}
|
|
1480
|
+
return [
|
|
1481
|
+
4,
|
|
1482
|
+
validate[key](inputValue, formValues)
|
|
1483
|
+
];
|
|
1484
|
+
case 4:
|
|
1485
|
+
validateError1 = getValidateError.apply(void 0, [
|
|
1486
|
+
_state.sent(),
|
|
1487
|
+
inputRef,
|
|
1488
|
+
key
|
|
1489
|
+
]);
|
|
1490
|
+
if (validateError1) {
|
|
1491
|
+
validationResult = _object_spread({}, validateError1, appendErrorsCurry(key, validateError1.message));
|
|
1492
|
+
setCustomValidity(validateError1.message);
|
|
1493
|
+
if (validateAllFieldCriteria) {
|
|
1494
|
+
error[name] = validationResult;
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
_state.label = 5;
|
|
1498
|
+
case 5:
|
|
1499
|
+
_i++;
|
|
1500
|
+
return [
|
|
1501
|
+
3,
|
|
1502
|
+
3
|
|
1503
|
+
];
|
|
1504
|
+
case 6:
|
|
1505
|
+
if (!isEmptyObject(validationResult)) {
|
|
1506
|
+
error[name] = _object_spread({
|
|
1507
|
+
ref: inputRef
|
|
1508
|
+
}, validationResult);
|
|
1509
|
+
if (!validateAllFieldCriteria) {
|
|
1510
|
+
return [
|
|
1511
|
+
2,
|
|
1512
|
+
error
|
|
1513
|
+
];
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
_state.label = 7;
|
|
1517
|
+
case 7:
|
|
1518
|
+
setCustomValidity(true);
|
|
1519
|
+
return [
|
|
1520
|
+
2,
|
|
1521
|
+
error
|
|
1522
|
+
];
|
|
1523
|
+
}
|
|
1524
|
+
});
|
|
1525
|
+
});
|
|
1526
|
+
return function validateField(field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) {
|
|
1527
|
+
return _ref.apply(this, arguments);
|
|
1528
|
+
};
|
|
1529
|
+
}();
|
|
1530
|
+
var defaultOptions = {
|
|
1531
|
+
mode: VALIDATION_MODE.onSubmit,
|
|
1532
|
+
reValidateMode: VALIDATION_MODE.onChange,
|
|
1533
|
+
shouldFocusError: true
|
|
1534
|
+
};
|
|
1535
|
+
function createFormControl() {
|
|
1536
|
+
var props = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
1537
|
+
var _options = _object_spread({}, defaultOptions, props);
|
|
1538
|
+
var _formState = {
|
|
1539
|
+
submitCount: 0,
|
|
1540
|
+
isDirty: false,
|
|
1541
|
+
isReady: false,
|
|
1542
|
+
isLoading: isFunction(_options.defaultValues),
|
|
1543
|
+
isValidating: false,
|
|
1544
|
+
isSubmitted: false,
|
|
1545
|
+
isSubmitting: false,
|
|
1546
|
+
isSubmitSuccessful: false,
|
|
1547
|
+
isValid: false,
|
|
1548
|
+
touchedFields: {},
|
|
1549
|
+
dirtyFields: {},
|
|
1550
|
+
validatingFields: {},
|
|
1551
|
+
errors: _options.errors || {},
|
|
1552
|
+
disabled: _options.disabled || false
|
|
1553
|
+
};
|
|
1554
|
+
var _fields = {};
|
|
1555
|
+
var _defaultValues = isObject(_options.defaultValues) || isObject(_options.values) ? cloneObject(_options.defaultValues || _options.values) || {} : {};
|
|
1556
|
+
var _formValues = _options.shouldUnregister ? {} : cloneObject(_defaultValues);
|
|
1557
|
+
var _state = {
|
|
1558
|
+
action: false,
|
|
1559
|
+
mount: false,
|
|
1560
|
+
watch: false
|
|
1561
|
+
};
|
|
1562
|
+
var _names = {
|
|
1563
|
+
mount: new Set(),
|
|
1564
|
+
disabled: new Set(),
|
|
1565
|
+
unMount: new Set(),
|
|
1566
|
+
array: new Set(),
|
|
1567
|
+
watch: new Set()
|
|
1568
|
+
};
|
|
1569
|
+
var delayErrorCallback;
|
|
1570
|
+
var timer = 0;
|
|
1571
|
+
var _proxyFormState = {
|
|
1572
|
+
isDirty: false,
|
|
1573
|
+
dirtyFields: false,
|
|
1574
|
+
validatingFields: false,
|
|
1575
|
+
touchedFields: false,
|
|
1576
|
+
isValidating: false,
|
|
1577
|
+
isValid: false,
|
|
1578
|
+
errors: false
|
|
1579
|
+
};
|
|
1580
|
+
var _proxySubscribeFormState = _object_spread({}, _proxyFormState);
|
|
1581
|
+
var _subjects = {
|
|
1582
|
+
array: createSubject(),
|
|
1583
|
+
state: createSubject()
|
|
1584
|
+
};
|
|
1585
|
+
var shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
|
1586
|
+
var debounce = function(callback) {
|
|
1587
|
+
return function(wait) {
|
|
1588
|
+
clearTimeout(timer);
|
|
1589
|
+
timer = setTimeout(callback, wait);
|
|
1590
|
+
};
|
|
1591
|
+
};
|
|
1592
|
+
var _setValid = function() {
|
|
1593
|
+
var _ref = _async_to_generator(function(shouldUpdateValid) {
|
|
1594
|
+
var isValid, _tmp;
|
|
1595
|
+
return _ts_generator(this, function(_state) {
|
|
1596
|
+
switch(_state.label){
|
|
1597
|
+
case 0:
|
|
1598
|
+
if (!(!_options.disabled && (_proxyFormState.isValid || _proxySubscribeFormState.isValid || shouldUpdateValid))) return [
|
|
1599
|
+
3,
|
|
1600
|
+
5
|
|
1601
|
+
];
|
|
1602
|
+
if (!_options.resolver) return [
|
|
1603
|
+
3,
|
|
1604
|
+
2
|
|
1605
|
+
];
|
|
1606
|
+
return [
|
|
1607
|
+
4,
|
|
1608
|
+
_runSchema()
|
|
1609
|
+
];
|
|
1610
|
+
case 1:
|
|
1611
|
+
_tmp = isEmptyObject.apply(void 0, [
|
|
1612
|
+
_state.sent().errors
|
|
1613
|
+
]);
|
|
1614
|
+
return [
|
|
1615
|
+
3,
|
|
1616
|
+
4
|
|
1617
|
+
];
|
|
1618
|
+
case 2:
|
|
1619
|
+
return [
|
|
1620
|
+
4,
|
|
1621
|
+
executeBuiltInValidation(_fields, true)
|
|
1622
|
+
];
|
|
1623
|
+
case 3:
|
|
1624
|
+
_tmp = _state.sent();
|
|
1625
|
+
_state.label = 4;
|
|
1626
|
+
case 4:
|
|
1627
|
+
isValid = _tmp;
|
|
1628
|
+
if (isValid !== _formState.isValid) {
|
|
1629
|
+
_subjects.state.next({
|
|
1630
|
+
isValid: isValid
|
|
1631
|
+
});
|
|
1632
|
+
}
|
|
1633
|
+
_state.label = 5;
|
|
1634
|
+
case 5:
|
|
1635
|
+
return [
|
|
1636
|
+
2
|
|
1637
|
+
];
|
|
1638
|
+
}
|
|
1639
|
+
});
|
|
1640
|
+
});
|
|
1641
|
+
return function _setValid(shouldUpdateValid) {
|
|
1642
|
+
return _ref.apply(this, arguments);
|
|
1643
|
+
};
|
|
1644
|
+
}();
|
|
1645
|
+
var _updateIsValidating = function(names, isValidating) {
|
|
1646
|
+
if (!_options.disabled && (_proxyFormState.isValidating || _proxyFormState.validatingFields || _proxySubscribeFormState.isValidating || _proxySubscribeFormState.validatingFields)) {
|
|
1647
|
+
(names || Array.from(_names.mount)).forEach(function(name) {
|
|
1648
|
+
if (name) {
|
|
1649
|
+
isValidating ? set(_formState.validatingFields, name, isValidating) : unset(_formState.validatingFields, name);
|
|
1650
|
+
}
|
|
1651
|
+
});
|
|
1652
|
+
_subjects.state.next({
|
|
1653
|
+
validatingFields: _formState.validatingFields,
|
|
1654
|
+
isValidating: !isEmptyObject(_formState.validatingFields)
|
|
1655
|
+
});
|
|
1656
|
+
}
|
|
1657
|
+
};
|
|
1658
|
+
var _setFieldArray = function(name) {
|
|
1659
|
+
var values = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [], method = arguments.length > 2 ? arguments[2] : void 0, args = arguments.length > 3 ? arguments[3] : void 0, shouldSetValues = arguments.length > 4 && arguments[4] !== void 0 ? arguments[4] : true, shouldUpdateFieldsAndState = arguments.length > 5 && arguments[5] !== void 0 ? arguments[5] : true;
|
|
1660
|
+
if (args && method && !_options.disabled) {
|
|
1661
|
+
_state.action = true;
|
|
1662
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
|
1663
|
+
var fieldValues = method(get(_fields, name), args.argA, args.argB);
|
|
1664
|
+
shouldSetValues && set(_fields, name, fieldValues);
|
|
1665
|
+
}
|
|
1666
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_formState.errors, name))) {
|
|
1667
|
+
var errors = method(get(_formState.errors, name), args.argA, args.argB);
|
|
1668
|
+
shouldSetValues && set(_formState.errors, name, errors);
|
|
1669
|
+
unsetEmptyArray(_formState.errors, name);
|
|
1670
|
+
}
|
|
1671
|
+
if ((_proxyFormState.touchedFields || _proxySubscribeFormState.touchedFields) && shouldUpdateFieldsAndState && Array.isArray(get(_formState.touchedFields, name))) {
|
|
1672
|
+
var touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
|
1673
|
+
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
|
1674
|
+
}
|
|
1675
|
+
if (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) {
|
|
1676
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
|
1677
|
+
}
|
|
1678
|
+
_subjects.state.next({
|
|
1679
|
+
name: name,
|
|
1680
|
+
isDirty: _getDirty(name, values),
|
|
1681
|
+
dirtyFields: _formState.dirtyFields,
|
|
1682
|
+
errors: _formState.errors,
|
|
1683
|
+
isValid: _formState.isValid
|
|
1684
|
+
});
|
|
1685
|
+
} else {
|
|
1686
|
+
set(_formValues, name, values);
|
|
1687
|
+
}
|
|
1688
|
+
};
|
|
1689
|
+
var updateErrors = function(name, error) {
|
|
1690
|
+
set(_formState.errors, name, error);
|
|
1691
|
+
_subjects.state.next({
|
|
1692
|
+
errors: _formState.errors
|
|
1693
|
+
});
|
|
1694
|
+
};
|
|
1695
|
+
var _setErrors = function(errors) {
|
|
1696
|
+
_formState.errors = errors;
|
|
1697
|
+
_subjects.state.next({
|
|
1698
|
+
errors: _formState.errors,
|
|
1699
|
+
isValid: false
|
|
1700
|
+
});
|
|
1701
|
+
};
|
|
1702
|
+
var updateValidAndValue = function(name, shouldSkipSetValueAs, value1, ref) {
|
|
1703
|
+
var field = get(_fields, name);
|
|
1704
|
+
if (field) {
|
|
1705
|
+
var defaultValue = get(_formValues, name, isUndefined(value1) ? get(_defaultValues, name) : value1);
|
|
1706
|
+
isUndefined(defaultValue) || ref && ref.defaultChecked || shouldSkipSetValueAs ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f)) : setFieldValue(name, defaultValue);
|
|
1707
|
+
_state.mount && _setValid();
|
|
1708
|
+
}
|
|
1709
|
+
};
|
|
1710
|
+
var updateTouchAndDirty = function(name, fieldValue, isBlurEvent, shouldDirty, shouldRender) {
|
|
1711
|
+
var shouldUpdateField = false;
|
|
1712
|
+
var isPreviousDirty = false;
|
|
1713
|
+
var output = {
|
|
1714
|
+
name: name
|
|
1715
|
+
};
|
|
1716
|
+
if (!_options.disabled) {
|
|
1717
|
+
if (!isBlurEvent || shouldDirty) {
|
|
1718
|
+
if (_proxyFormState.isDirty || _proxySubscribeFormState.isDirty) {
|
|
1719
|
+
isPreviousDirty = _formState.isDirty;
|
|
1720
|
+
_formState.isDirty = output.isDirty = _getDirty();
|
|
1721
|
+
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
|
1722
|
+
}
|
|
1723
|
+
var isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
|
|
1724
|
+
isPreviousDirty = !!get(_formState.dirtyFields, name);
|
|
1725
|
+
isCurrentFieldPristine ? unset(_formState.dirtyFields, name) : set(_formState.dirtyFields, name, true);
|
|
1726
|
+
output.dirtyFields = _formState.dirtyFields;
|
|
1727
|
+
shouldUpdateField = shouldUpdateField || (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) && isPreviousDirty !== !isCurrentFieldPristine;
|
|
1728
|
+
}
|
|
1729
|
+
if (isBlurEvent) {
|
|
1730
|
+
var isPreviousFieldTouched = get(_formState.touchedFields, name);
|
|
1731
|
+
if (!isPreviousFieldTouched) {
|
|
1732
|
+
set(_formState.touchedFields, name, isBlurEvent);
|
|
1733
|
+
output.touchedFields = _formState.touchedFields;
|
|
1734
|
+
shouldUpdateField = shouldUpdateField || (_proxyFormState.touchedFields || _proxySubscribeFormState.touchedFields) && isPreviousFieldTouched !== isBlurEvent;
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
|
1738
|
+
}
|
|
1739
|
+
return shouldUpdateField ? output : {};
|
|
1740
|
+
};
|
|
1741
|
+
var shouldRenderByError = function(name, isValid, error, fieldState) {
|
|
1742
|
+
var previousFieldError = get(_formState.errors, name);
|
|
1743
|
+
var shouldUpdateValid = (_proxyFormState.isValid || _proxySubscribeFormState.isValid) && isBoolean(isValid) && _formState.isValid !== isValid;
|
|
1744
|
+
if (_options.delayError && error) {
|
|
1745
|
+
delayErrorCallback = debounce(function() {
|
|
1746
|
+
return updateErrors(name, error);
|
|
1747
|
+
});
|
|
1748
|
+
delayErrorCallback(_options.delayError);
|
|
1749
|
+
} else {
|
|
1750
|
+
clearTimeout(timer);
|
|
1751
|
+
delayErrorCallback = null;
|
|
1752
|
+
error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
|
|
1753
|
+
}
|
|
1754
|
+
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) || !isEmptyObject(fieldState) || shouldUpdateValid) {
|
|
1755
|
+
var updatedFormState = _object_spread_props(_object_spread({}, fieldState, shouldUpdateValid && isBoolean(isValid) ? {
|
|
1756
|
+
isValid: isValid
|
|
1757
|
+
} : {}), {
|
|
1758
|
+
errors: _formState.errors,
|
|
1759
|
+
name: name
|
|
1760
|
+
});
|
|
1761
|
+
_formState = _object_spread({}, _formState, updatedFormState);
|
|
1762
|
+
_subjects.state.next(updatedFormState);
|
|
1763
|
+
}
|
|
1764
|
+
};
|
|
1765
|
+
var _runSchema = function() {
|
|
1766
|
+
var _ref = _async_to_generator(function(name) {
|
|
1767
|
+
var result;
|
|
1768
|
+
return _ts_generator(this, function(_state) {
|
|
1769
|
+
switch(_state.label){
|
|
1770
|
+
case 0:
|
|
1771
|
+
_updateIsValidating(name, true);
|
|
1772
|
+
return [
|
|
1773
|
+
4,
|
|
1774
|
+
_options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation))
|
|
1775
|
+
];
|
|
1776
|
+
case 1:
|
|
1777
|
+
result = _state.sent();
|
|
1778
|
+
_updateIsValidating(name);
|
|
1779
|
+
return [
|
|
1780
|
+
2,
|
|
1781
|
+
result
|
|
1782
|
+
];
|
|
1783
|
+
}
|
|
1784
|
+
});
|
|
1785
|
+
});
|
|
1786
|
+
return function _runSchema(name) {
|
|
1787
|
+
return _ref.apply(this, arguments);
|
|
1788
|
+
};
|
|
1789
|
+
}();
|
|
1790
|
+
var executeSchemaAndUpdateState = function() {
|
|
1791
|
+
var _ref = _async_to_generator(function(names) {
|
|
1792
|
+
var errors, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, name, error;
|
|
1793
|
+
return _ts_generator(this, function(_state) {
|
|
1794
|
+
switch(_state.label){
|
|
1795
|
+
case 0:
|
|
1796
|
+
return [
|
|
1797
|
+
4,
|
|
1798
|
+
_runSchema(names)
|
|
1799
|
+
];
|
|
1800
|
+
case 1:
|
|
1801
|
+
errors = _state.sent().errors;
|
|
1802
|
+
if (names) {
|
|
1803
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
1804
|
+
try {
|
|
1805
|
+
for(_iterator = names[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
1806
|
+
name = _step.value;
|
|
1807
|
+
error = get(errors, name);
|
|
1808
|
+
error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
|
|
1809
|
+
}
|
|
1810
|
+
} catch (err) {
|
|
1811
|
+
_didIteratorError = true;
|
|
1812
|
+
_iteratorError = err;
|
|
1813
|
+
} finally{
|
|
1814
|
+
try {
|
|
1815
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
1816
|
+
_iterator.return();
|
|
1817
|
+
}
|
|
1818
|
+
} finally{
|
|
1819
|
+
if (_didIteratorError) {
|
|
1820
|
+
throw _iteratorError;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
} else {
|
|
1825
|
+
_formState.errors = errors;
|
|
1826
|
+
}
|
|
1827
|
+
return [
|
|
1828
|
+
2,
|
|
1829
|
+
errors
|
|
1830
|
+
];
|
|
1831
|
+
}
|
|
1832
|
+
});
|
|
1833
|
+
});
|
|
1834
|
+
return function executeSchemaAndUpdateState(names) {
|
|
1835
|
+
return _ref.apply(this, arguments);
|
|
1836
|
+
};
|
|
1837
|
+
}();
|
|
1838
|
+
var executeBuiltInValidation = function() {
|
|
1839
|
+
var _ref = _async_to_generator(function(fields, shouldOnlyCheckValid) {
|
|
1840
|
+
var context, _tmp, _tmp1, _i, _ref, name, field, _f, fieldValue, isFieldArrayRoot, isPromiseFunction, fieldError, _tmp2;
|
|
1841
|
+
var _arguments = arguments;
|
|
1842
|
+
return _ts_generator(this, function(_state) {
|
|
1843
|
+
switch(_state.label){
|
|
1844
|
+
case 0:
|
|
1845
|
+
context = _arguments.length > 2 && _arguments[2] !== void 0 ? _arguments[2] : {
|
|
1846
|
+
valid: true
|
|
1847
|
+
};
|
|
1848
|
+
_tmp = [];
|
|
1849
|
+
for(_tmp1 in fields)_tmp.push(_tmp1);
|
|
1850
|
+
_i = 0;
|
|
1851
|
+
_state.label = 1;
|
|
1852
|
+
case 1:
|
|
1853
|
+
if (!(_i < _tmp.length)) return [
|
|
1854
|
+
3,
|
|
1855
|
+
7
|
|
1856
|
+
];
|
|
1857
|
+
_ref = _tmp[_i];
|
|
1858
|
+
name = _ref;
|
|
1859
|
+
field = fields[name];
|
|
1860
|
+
if (!field) return [
|
|
1861
|
+
3,
|
|
1862
|
+
6
|
|
1863
|
+
];
|
|
1864
|
+
_f = field._f, fieldValue = _object_without_properties(field, [
|
|
1865
|
+
"_f"
|
|
1866
|
+
]);
|
|
1867
|
+
if (!_f) return [
|
|
1868
|
+
3,
|
|
1869
|
+
3
|
|
1870
|
+
];
|
|
1871
|
+
isFieldArrayRoot = _names.array.has(_f.name);
|
|
1872
|
+
isPromiseFunction = field._f && hasPromiseValidation(field._f);
|
|
1873
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
|
1874
|
+
_updateIsValidating([
|
|
1875
|
+
name
|
|
1876
|
+
], true);
|
|
1877
|
+
}
|
|
1878
|
+
return [
|
|
1879
|
+
4,
|
|
1880
|
+
validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot)
|
|
1881
|
+
];
|
|
1882
|
+
case 2:
|
|
1883
|
+
fieldError = _state.sent();
|
|
1884
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
|
1885
|
+
_updateIsValidating([
|
|
1886
|
+
name
|
|
1887
|
+
]);
|
|
1888
|
+
}
|
|
1889
|
+
if (fieldError[_f.name]) {
|
|
1890
|
+
context.valid = false;
|
|
1891
|
+
if (shouldOnlyCheckValid) {
|
|
1892
|
+
return [
|
|
1893
|
+
3,
|
|
1894
|
+
7
|
|
1895
|
+
];
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
!shouldOnlyCheckValid && (get(fieldError, _f.name) ? isFieldArrayRoot ? updateFieldArrayRootError(_formState.errors, fieldError, _f.name) : set(_formState.errors, _f.name, fieldError[_f.name]) : unset(_formState.errors, _f.name));
|
|
1899
|
+
_state.label = 3;
|
|
1900
|
+
case 3:
|
|
1901
|
+
_tmp2 = !isEmptyObject(fieldValue);
|
|
1902
|
+
if (!_tmp2) return [
|
|
1903
|
+
3,
|
|
1904
|
+
5
|
|
1905
|
+
];
|
|
1906
|
+
return [
|
|
1907
|
+
4,
|
|
1908
|
+
executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context)
|
|
1909
|
+
];
|
|
1910
|
+
case 4:
|
|
1911
|
+
_tmp2 = _state.sent();
|
|
1912
|
+
_state.label = 5;
|
|
1913
|
+
case 5:
|
|
1914
|
+
_state.label = 6;
|
|
1915
|
+
case 6:
|
|
1916
|
+
_i++;
|
|
1917
|
+
return [
|
|
1918
|
+
3,
|
|
1919
|
+
1
|
|
1920
|
+
];
|
|
1921
|
+
case 7:
|
|
1922
|
+
return [
|
|
1923
|
+
2,
|
|
1924
|
+
context.valid
|
|
1925
|
+
];
|
|
1926
|
+
}
|
|
1927
|
+
});
|
|
1928
|
+
});
|
|
1929
|
+
return function executeBuiltInValidation(fields, shouldOnlyCheckValid) {
|
|
1930
|
+
return _ref.apply(this, arguments);
|
|
1931
|
+
};
|
|
1932
|
+
}();
|
|
1933
|
+
var _removeUnmounted = function() {
|
|
1934
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
1935
|
+
try {
|
|
1936
|
+
for(var _iterator = _names.unMount[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
1937
|
+
var name = _step.value;
|
|
1938
|
+
var field = get(_fields, name);
|
|
1939
|
+
field && (field._f.refs ? field._f.refs.every(function(ref) {
|
|
1940
|
+
return !live(ref);
|
|
1941
|
+
}) : !live(field._f.ref)) && unregister(name);
|
|
1942
|
+
}
|
|
1943
|
+
} catch (err) {
|
|
1944
|
+
_didIteratorError = true;
|
|
1945
|
+
_iteratorError = err;
|
|
1946
|
+
} finally{
|
|
1947
|
+
try {
|
|
1948
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
1949
|
+
_iterator.return();
|
|
1950
|
+
}
|
|
1951
|
+
} finally{
|
|
1952
|
+
if (_didIteratorError) {
|
|
1953
|
+
throw _iteratorError;
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
_names.unMount = new Set();
|
|
1958
|
+
};
|
|
1959
|
+
var _getDirty = function(name, data) {
|
|
1960
|
+
return !_options.disabled && (name && data && set(_formValues, name, data), !deepEqual(getValues(), _defaultValues));
|
|
1961
|
+
};
|
|
1962
|
+
var _getWatch = function(names, defaultValue, isGlobal) {
|
|
1963
|
+
return generateWatchOutput(names, _names, _object_spread({}, _state.mount ? _formValues : isUndefined(defaultValue) ? _defaultValues : isString(names) ? _define_property({}, names, defaultValue) : defaultValue), isGlobal, defaultValue);
|
|
1964
|
+
};
|
|
1965
|
+
var _getFieldArray = function(name) {
|
|
1966
|
+
return compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
|
|
1967
|
+
};
|
|
1968
|
+
var setFieldValue = function(name, value1) {
|
|
1969
|
+
var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
1970
|
+
var field = get(_fields, name);
|
|
1971
|
+
var fieldValue = value1;
|
|
1972
|
+
if (field) {
|
|
1973
|
+
var fieldReference = field._f;
|
|
1974
|
+
if (fieldReference) {
|
|
1975
|
+
!fieldReference.disabled && set(_formValues, name, getFieldValueAs(value1, fieldReference));
|
|
1976
|
+
fieldValue = isHTMLElement(fieldReference.ref) && isNullOrUndefined(value1) ? "" : value1;
|
|
1977
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
|
1978
|
+
_to_consumable_array(fieldReference.ref.options).forEach(function(optionRef) {
|
|
1979
|
+
return optionRef.selected = fieldValue.includes(optionRef.value);
|
|
1980
|
+
});
|
|
1981
|
+
} else if (fieldReference.refs) {
|
|
1982
|
+
if (isCheckBoxInput(fieldReference.ref)) {
|
|
1983
|
+
fieldReference.refs.forEach(function(checkboxRef) {
|
|
1984
|
+
if (!checkboxRef.defaultChecked || !checkboxRef.disabled) {
|
|
1985
|
+
if (Array.isArray(fieldValue)) {
|
|
1986
|
+
checkboxRef.checked = !!fieldValue.find(function(data) {
|
|
1987
|
+
return data === checkboxRef.value;
|
|
1988
|
+
});
|
|
1989
|
+
} else {
|
|
1990
|
+
checkboxRef.checked = fieldValue === checkboxRef.value || !!fieldValue;
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
});
|
|
1994
|
+
} else {
|
|
1995
|
+
fieldReference.refs.forEach(function(radioRef) {
|
|
1996
|
+
return radioRef.checked = radioRef.value === fieldValue;
|
|
1997
|
+
});
|
|
1998
|
+
}
|
|
1999
|
+
} else if (isFileInput(fieldReference.ref)) {
|
|
2000
|
+
fieldReference.ref.value = "";
|
|
2001
|
+
} else {
|
|
2002
|
+
fieldReference.ref.value = fieldValue;
|
|
2003
|
+
if (!fieldReference.ref.type) {
|
|
2004
|
+
_subjects.state.next({
|
|
2005
|
+
name: name,
|
|
2006
|
+
values: cloneObject(_formValues)
|
|
2007
|
+
});
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
(options.shouldDirty || options.shouldTouch) && updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
|
2013
|
+
options.shouldValidate && trigger(name);
|
|
2014
|
+
};
|
|
2015
|
+
var setValues = function(name, value1, options) {
|
|
2016
|
+
for(var fieldKey in value1){
|
|
2017
|
+
if (!value1.hasOwnProperty(fieldKey)) {
|
|
2018
|
+
return;
|
|
2019
|
+
}
|
|
2020
|
+
var fieldValue = value1[fieldKey];
|
|
2021
|
+
var fieldName = "".concat(name, ".").concat(fieldKey);
|
|
2022
|
+
var field = get(_fields, fieldName);
|
|
2023
|
+
(_names.array.has(name) || isObject(fieldValue) || field && !field._f) && !isDateObject(fieldValue) ? setValues(fieldName, fieldValue, options) : setFieldValue(fieldName, fieldValue, options);
|
|
2024
|
+
}
|
|
2025
|
+
};
|
|
2026
|
+
var setValue = function(name, value1) {
|
|
2027
|
+
var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
2028
|
+
var field = get(_fields, name);
|
|
2029
|
+
var isFieldArray = _names.array.has(name);
|
|
2030
|
+
var cloneValue = cloneObject(value1);
|
|
2031
|
+
set(_formValues, name, cloneValue);
|
|
2032
|
+
if (isFieldArray) {
|
|
2033
|
+
_subjects.array.next({
|
|
2034
|
+
name: name,
|
|
2035
|
+
values: cloneObject(_formValues)
|
|
2036
|
+
});
|
|
2037
|
+
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields || _proxySubscribeFormState.isDirty || _proxySubscribeFormState.dirtyFields) && options.shouldDirty) {
|
|
2038
|
+
_subjects.state.next({
|
|
2039
|
+
name: name,
|
|
2040
|
+
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
|
2041
|
+
isDirty: _getDirty(name, cloneValue)
|
|
2042
|
+
});
|
|
2043
|
+
}
|
|
2044
|
+
} else {
|
|
2045
|
+
field && !field._f && !isNullOrUndefined(cloneValue) ? setValues(name, cloneValue, options) : setFieldValue(name, cloneValue, options);
|
|
2046
|
+
}
|
|
2047
|
+
isWatched(name, _names) && _subjects.state.next(_object_spread({}, _formState));
|
|
2048
|
+
_subjects.state.next({
|
|
2049
|
+
name: _state.mount ? name : undefined,
|
|
2050
|
+
values: cloneObject(_formValues)
|
|
2051
|
+
});
|
|
2052
|
+
};
|
|
2053
|
+
var onChange = function() {
|
|
2054
|
+
var _ref = _async_to_generator(function(event) {
|
|
2055
|
+
var target, name, isFieldValueUpdated, field, _updateIsFieldValueUpdated, validationModeBeforeSubmit, validationModeAfterSubmit, error, isValid, fieldValue, isBlurEvent, shouldSkipValidation, watched, fieldState, shouldRender, errors, previousErrorLookupResult, errorLookupResult;
|
|
2056
|
+
return _ts_generator(this, function(_state1) {
|
|
2057
|
+
switch(_state1.label){
|
|
2058
|
+
case 0:
|
|
2059
|
+
_state.mount = true;
|
|
2060
|
+
target = event.target;
|
|
2061
|
+
name = target.name;
|
|
2062
|
+
isFieldValueUpdated = true;
|
|
2063
|
+
field = get(_fields, name);
|
|
2064
|
+
_updateIsFieldValueUpdated = function(fieldValue) {
|
|
2065
|
+
isFieldValueUpdated = Number.isNaN(fieldValue) || isDateObject(fieldValue) && isNaN(fieldValue.getTime()) || deepEqual(fieldValue, get(_formValues, name, fieldValue));
|
|
2066
|
+
};
|
|
2067
|
+
validationModeBeforeSubmit = getValidationModes(_options.mode);
|
|
2068
|
+
validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
|
2069
|
+
if (!field) return [
|
|
2070
|
+
3,
|
|
2071
|
+
7
|
|
2072
|
+
];
|
|
2073
|
+
fieldValue = target.type ? getFieldValue(field._f) : getEventValue(event);
|
|
2074
|
+
isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
|
2075
|
+
shouldSkipValidation = !hasValidation(field._f) && !_options.resolver && !get(_formState.errors, name) && !field._f.deps || skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
|
2076
|
+
watched = isWatched(name, _names, isBlurEvent);
|
|
2077
|
+
set(_formValues, name, fieldValue);
|
|
2078
|
+
if (isBlurEvent) {
|
|
2079
|
+
field._f.onBlur && field._f.onBlur(event);
|
|
2080
|
+
delayErrorCallback && delayErrorCallback(0);
|
|
2081
|
+
} else if (field._f.onChange) {
|
|
2082
|
+
field._f.onChange(event);
|
|
2083
|
+
}
|
|
2084
|
+
fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent);
|
|
2085
|
+
shouldRender = !isEmptyObject(fieldState) || watched;
|
|
2086
|
+
!isBlurEvent && _subjects.state.next({
|
|
2087
|
+
name: name,
|
|
2088
|
+
type: event.type,
|
|
2089
|
+
values: cloneObject(_formValues)
|
|
2090
|
+
});
|
|
2091
|
+
if (shouldSkipValidation) {
|
|
2092
|
+
if (_proxyFormState.isValid || _proxySubscribeFormState.isValid) {
|
|
2093
|
+
if (_options.mode === "onBlur") {
|
|
2094
|
+
if (isBlurEvent) {
|
|
2095
|
+
_setValid();
|
|
2096
|
+
}
|
|
2097
|
+
} else if (!isBlurEvent) {
|
|
2098
|
+
_setValid();
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
return [
|
|
2102
|
+
2,
|
|
2103
|
+
shouldRender && _subjects.state.next(_object_spread({
|
|
2104
|
+
name: name
|
|
2105
|
+
}, watched ? {} : fieldState))
|
|
2106
|
+
];
|
|
2107
|
+
}
|
|
2108
|
+
!isBlurEvent && watched && _subjects.state.next(_object_spread({}, _formState));
|
|
2109
|
+
if (!_options.resolver) return [
|
|
2110
|
+
3,
|
|
2111
|
+
2
|
|
2112
|
+
];
|
|
2113
|
+
return [
|
|
2114
|
+
4,
|
|
2115
|
+
_runSchema([
|
|
2116
|
+
name
|
|
2117
|
+
])
|
|
2118
|
+
];
|
|
2119
|
+
case 1:
|
|
2120
|
+
errors = _state1.sent().errors;
|
|
2121
|
+
_updateIsFieldValueUpdated(fieldValue);
|
|
2122
|
+
if (isFieldValueUpdated) {
|
|
2123
|
+
previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
|
2124
|
+
errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
|
2125
|
+
error = errorLookupResult.error;
|
|
2126
|
+
name = errorLookupResult.name;
|
|
2127
|
+
isValid = isEmptyObject(errors);
|
|
2128
|
+
}
|
|
2129
|
+
return [
|
|
2130
|
+
3,
|
|
2131
|
+
6
|
|
2132
|
+
];
|
|
2133
|
+
case 2:
|
|
2134
|
+
_updateIsValidating([
|
|
2135
|
+
name
|
|
2136
|
+
], true);
|
|
2137
|
+
return [
|
|
2138
|
+
4,
|
|
2139
|
+
validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation)
|
|
2140
|
+
];
|
|
2141
|
+
case 3:
|
|
2142
|
+
error = _state1.sent()[name];
|
|
2143
|
+
_updateIsValidating([
|
|
2144
|
+
name
|
|
2145
|
+
]);
|
|
2146
|
+
_updateIsFieldValueUpdated(fieldValue);
|
|
2147
|
+
if (!isFieldValueUpdated) return [
|
|
2148
|
+
3,
|
|
2149
|
+
6
|
|
2150
|
+
];
|
|
2151
|
+
if (!error) return [
|
|
2152
|
+
3,
|
|
2153
|
+
4
|
|
2154
|
+
];
|
|
2155
|
+
isValid = false;
|
|
2156
|
+
return [
|
|
2157
|
+
3,
|
|
2158
|
+
6
|
|
2159
|
+
];
|
|
2160
|
+
case 4:
|
|
2161
|
+
if (!(_proxyFormState.isValid || _proxySubscribeFormState.isValid)) return [
|
|
2162
|
+
3,
|
|
2163
|
+
6
|
|
2164
|
+
];
|
|
2165
|
+
return [
|
|
2166
|
+
4,
|
|
2167
|
+
executeBuiltInValidation(_fields, true)
|
|
2168
|
+
];
|
|
2169
|
+
case 5:
|
|
2170
|
+
isValid = _state1.sent();
|
|
2171
|
+
_state1.label = 6;
|
|
2172
|
+
case 6:
|
|
2173
|
+
if (isFieldValueUpdated) {
|
|
2174
|
+
field._f.deps && trigger(field._f.deps);
|
|
2175
|
+
shouldRenderByError(name, isValid, error, fieldState);
|
|
2176
|
+
}
|
|
2177
|
+
_state1.label = 7;
|
|
2178
|
+
case 7:
|
|
2179
|
+
return [
|
|
2180
|
+
2
|
|
2181
|
+
];
|
|
2182
|
+
}
|
|
2183
|
+
});
|
|
2184
|
+
});
|
|
2185
|
+
return function onChange(event) {
|
|
2186
|
+
return _ref.apply(this, arguments);
|
|
2187
|
+
};
|
|
2188
|
+
}();
|
|
2189
|
+
var _focusInput = function(ref, key) {
|
|
2190
|
+
if (get(_formState.errors, key) && ref.focus) {
|
|
2191
|
+
ref.focus();
|
|
2192
|
+
return 1;
|
|
2193
|
+
}
|
|
2194
|
+
return;
|
|
2195
|
+
};
|
|
2196
|
+
var trigger = function() {
|
|
2197
|
+
var _ref = _async_to_generator(function(name) {
|
|
2198
|
+
var options, isValid, validationResult, fieldNames, errors;
|
|
2199
|
+
var _arguments = arguments;
|
|
2200
|
+
return _ts_generator(this, function(_state) {
|
|
2201
|
+
switch(_state.label){
|
|
2202
|
+
case 0:
|
|
2203
|
+
options = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
|
|
2204
|
+
fieldNames = convertToArrayPayload(name);
|
|
2205
|
+
if (!_options.resolver) return [
|
|
2206
|
+
3,
|
|
2207
|
+
2
|
|
2208
|
+
];
|
|
2209
|
+
return [
|
|
2210
|
+
4,
|
|
2211
|
+
executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames)
|
|
2212
|
+
];
|
|
2213
|
+
case 1:
|
|
2214
|
+
errors = _state.sent();
|
|
2215
|
+
isValid = isEmptyObject(errors);
|
|
2216
|
+
validationResult = name ? !fieldNames.some(function(name) {
|
|
2217
|
+
return get(errors, name);
|
|
2218
|
+
}) : isValid;
|
|
2219
|
+
return [
|
|
2220
|
+
3,
|
|
2221
|
+
6
|
|
2222
|
+
];
|
|
2223
|
+
case 2:
|
|
2224
|
+
if (!name) return [
|
|
2225
|
+
3,
|
|
2226
|
+
4
|
|
2227
|
+
];
|
|
2228
|
+
return [
|
|
2229
|
+
4,
|
|
2230
|
+
Promise.all(fieldNames.map(function() {
|
|
2231
|
+
var _ref = _async_to_generator(function(fieldName) {
|
|
2232
|
+
var field;
|
|
2233
|
+
return _ts_generator(this, function(_state) {
|
|
2234
|
+
switch(_state.label){
|
|
2235
|
+
case 0:
|
|
2236
|
+
field = get(_fields, fieldName);
|
|
2237
|
+
return [
|
|
2238
|
+
4,
|
|
2239
|
+
executeBuiltInValidation(field && field._f ? _define_property({}, fieldName, field) : field)
|
|
2240
|
+
];
|
|
2241
|
+
case 1:
|
|
2242
|
+
return [
|
|
2243
|
+
2,
|
|
2244
|
+
_state.sent()
|
|
2245
|
+
];
|
|
2246
|
+
}
|
|
2247
|
+
});
|
|
2248
|
+
});
|
|
2249
|
+
return function(fieldName) {
|
|
2250
|
+
return _ref.apply(this, arguments);
|
|
2251
|
+
};
|
|
2252
|
+
}()))
|
|
2253
|
+
];
|
|
2254
|
+
case 3:
|
|
2255
|
+
validationResult = _state.sent().every(Boolean);
|
|
2256
|
+
!(!validationResult && !_formState.isValid) && _setValid();
|
|
2257
|
+
return [
|
|
2258
|
+
3,
|
|
2259
|
+
6
|
|
2260
|
+
];
|
|
2261
|
+
case 4:
|
|
2262
|
+
return [
|
|
2263
|
+
4,
|
|
2264
|
+
executeBuiltInValidation(_fields)
|
|
2265
|
+
];
|
|
2266
|
+
case 5:
|
|
2267
|
+
validationResult = isValid = _state.sent();
|
|
2268
|
+
_state.label = 6;
|
|
2269
|
+
case 6:
|
|
2270
|
+
_subjects.state.next(_object_spread_props(_object_spread({}, !isString(name) || (_proxyFormState.isValid || _proxySubscribeFormState.isValid) && isValid !== _formState.isValid ? {} : {
|
|
2271
|
+
name: name
|
|
2272
|
+
}, _options.resolver || !name ? {
|
|
2273
|
+
isValid: isValid
|
|
2274
|
+
} : {}), {
|
|
2275
|
+
errors: _formState.errors
|
|
2276
|
+
}));
|
|
2277
|
+
options.shouldFocus && !validationResult && iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
|
|
2278
|
+
return [
|
|
2279
|
+
2,
|
|
2280
|
+
validationResult
|
|
2281
|
+
];
|
|
2282
|
+
}
|
|
2283
|
+
});
|
|
2284
|
+
});
|
|
2285
|
+
return function trigger(name) {
|
|
2286
|
+
return _ref.apply(this, arguments);
|
|
2287
|
+
};
|
|
2288
|
+
}();
|
|
2289
|
+
var getValues = function(fieldNames) {
|
|
2290
|
+
var values = _object_spread({}, _state.mount ? _formValues : _defaultValues);
|
|
2291
|
+
return isUndefined(fieldNames) ? values : isString(fieldNames) ? get(values, fieldNames) : fieldNames.map(function(name) {
|
|
2292
|
+
return get(values, name);
|
|
2293
|
+
});
|
|
2294
|
+
};
|
|
2295
|
+
var getFieldState = function(name, formState) {
|
|
2296
|
+
return {
|
|
2297
|
+
invalid: !!get((formState || _formState).errors, name),
|
|
2298
|
+
isDirty: !!get((formState || _formState).dirtyFields, name),
|
|
2299
|
+
error: get((formState || _formState).errors, name),
|
|
2300
|
+
isValidating: !!get(_formState.validatingFields, name),
|
|
2301
|
+
isTouched: !!get((formState || _formState).touchedFields, name)
|
|
2302
|
+
};
|
|
2303
|
+
};
|
|
2304
|
+
var clearErrors = function(name) {
|
|
2305
|
+
name && convertToArrayPayload(name).forEach(function(inputName) {
|
|
2306
|
+
return unset(_formState.errors, inputName);
|
|
2307
|
+
});
|
|
2308
|
+
_subjects.state.next({
|
|
2309
|
+
errors: name ? _formState.errors : {}
|
|
2310
|
+
});
|
|
2311
|
+
};
|
|
2312
|
+
var setError = function(name, error, options) {
|
|
2313
|
+
var ref = (get(_fields, name, {
|
|
2314
|
+
_f: {}
|
|
2315
|
+
})._f || {}).ref;
|
|
2316
|
+
var currentError = get(_formState.errors, name) || {};
|
|
2317
|
+
// Don't override existing error messages elsewhere in the object tree.
|
|
2318
|
+
currentError.ref; currentError.message; currentError.type; var restOfErrorTree = _object_without_properties(currentError, [
|
|
2319
|
+
"ref",
|
|
2320
|
+
"message",
|
|
2321
|
+
"type"
|
|
2322
|
+
]);
|
|
2323
|
+
set(_formState.errors, name, _object_spread_props(_object_spread({}, restOfErrorTree, error), {
|
|
2324
|
+
ref: ref
|
|
2325
|
+
}));
|
|
2326
|
+
_subjects.state.next({
|
|
2327
|
+
name: name,
|
|
2328
|
+
errors: _formState.errors,
|
|
2329
|
+
isValid: false
|
|
2330
|
+
});
|
|
2331
|
+
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
|
2332
|
+
};
|
|
2333
|
+
var watch = function(name, defaultValue) {
|
|
2334
|
+
return isFunction(name) ? _subjects.state.subscribe({
|
|
2335
|
+
next: function(payload) {
|
|
2336
|
+
return name(_getWatch(undefined, defaultValue), payload);
|
|
2337
|
+
}
|
|
2338
|
+
}) : _getWatch(name, defaultValue, true);
|
|
2339
|
+
};
|
|
2340
|
+
var _subscribe = function(props) {
|
|
2341
|
+
return _subjects.state.subscribe({
|
|
2342
|
+
next: function(formState) {
|
|
2343
|
+
if (shouldSubscribeByName(props.name, formState.name, props.exact) && shouldRenderFormState(formState, props.formState || _proxyFormState, _setFormState, props.reRenderRoot)) {
|
|
2344
|
+
props.callback(_object_spread({
|
|
2345
|
+
values: _object_spread({}, _formValues)
|
|
2346
|
+
}, _formState, formState));
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
}).unsubscribe;
|
|
2350
|
+
};
|
|
2351
|
+
var subscribe = function(props) {
|
|
2352
|
+
_state.mount = true;
|
|
2353
|
+
_proxySubscribeFormState = _object_spread({}, _proxySubscribeFormState, props.formState);
|
|
2354
|
+
return _subscribe(_object_spread_props(_object_spread({}, props), {
|
|
2355
|
+
formState: _proxySubscribeFormState
|
|
2356
|
+
}));
|
|
2357
|
+
};
|
|
2358
|
+
var unregister = function(name) {
|
|
2359
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
2360
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
2361
|
+
try {
|
|
2362
|
+
for(var _iterator = (name ? convertToArrayPayload(name) : _names.mount)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
2363
|
+
var fieldName = _step.value;
|
|
2364
|
+
_names.mount.delete(fieldName);
|
|
2365
|
+
_names.array.delete(fieldName);
|
|
2366
|
+
if (!options.keepValue) {
|
|
2367
|
+
unset(_fields, fieldName);
|
|
2368
|
+
unset(_formValues, fieldName);
|
|
2369
|
+
}
|
|
2370
|
+
!options.keepError && unset(_formState.errors, fieldName);
|
|
2371
|
+
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
|
2372
|
+
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
|
2373
|
+
!options.keepIsValidating && unset(_formState.validatingFields, fieldName);
|
|
2374
|
+
!_options.shouldUnregister && !options.keepDefaultValue && unset(_defaultValues, fieldName);
|
|
2375
|
+
}
|
|
2376
|
+
} catch (err) {
|
|
2377
|
+
_didIteratorError = true;
|
|
2378
|
+
_iteratorError = err;
|
|
2379
|
+
} finally{
|
|
2380
|
+
try {
|
|
2381
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
2382
|
+
_iterator.return();
|
|
2383
|
+
}
|
|
2384
|
+
} finally{
|
|
2385
|
+
if (_didIteratorError) {
|
|
2386
|
+
throw _iteratorError;
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
}
|
|
2390
|
+
_subjects.state.next({
|
|
2391
|
+
values: cloneObject(_formValues)
|
|
2392
|
+
});
|
|
2393
|
+
_subjects.state.next(_object_spread({}, _formState, !options.keepDirty ? {} : {
|
|
2394
|
+
isDirty: _getDirty()
|
|
2395
|
+
}));
|
|
2396
|
+
!options.keepIsValid && _setValid();
|
|
2397
|
+
};
|
|
2398
|
+
var _setDisabledField = function(param) {
|
|
2399
|
+
var disabled = param.disabled, name = param.name;
|
|
2400
|
+
if (isBoolean(disabled) && _state.mount || !!disabled || _names.disabled.has(name)) {
|
|
2401
|
+
disabled ? _names.disabled.add(name) : _names.disabled.delete(name);
|
|
2402
|
+
}
|
|
2403
|
+
};
|
|
2404
|
+
var register = function(name) {
|
|
2405
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
2406
|
+
var field = get(_fields, name);
|
|
2407
|
+
var disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
|
|
2408
|
+
set(_fields, name, _object_spread_props(_object_spread({}, field || {}), {
|
|
2409
|
+
_f: _object_spread(_object_spread_props(_object_spread({}, field && field._f ? field._f : {
|
|
2410
|
+
ref: {
|
|
2411
|
+
name: name
|
|
2412
|
+
}
|
|
2413
|
+
}), {
|
|
2414
|
+
name: name,
|
|
2415
|
+
mount: true
|
|
2416
|
+
}), options)
|
|
2417
|
+
}));
|
|
2418
|
+
_names.mount.add(name);
|
|
2419
|
+
if (field) {
|
|
2420
|
+
_setDisabledField({
|
|
2421
|
+
disabled: isBoolean(options.disabled) ? options.disabled : _options.disabled,
|
|
2422
|
+
name: name
|
|
2423
|
+
});
|
|
2424
|
+
} else {
|
|
2425
|
+
updateValidAndValue(name, true, options.value);
|
|
2426
|
+
}
|
|
2427
|
+
return _object_spread_props(_object_spread({}, disabledIsDefined ? {
|
|
2428
|
+
disabled: options.disabled || _options.disabled
|
|
2429
|
+
} : {}, _options.progressive ? {
|
|
2430
|
+
required: !!options.required,
|
|
2431
|
+
min: getRuleValue(options.min),
|
|
2432
|
+
max: getRuleValue(options.max),
|
|
2433
|
+
minLength: getRuleValue(options.minLength),
|
|
2434
|
+
maxLength: getRuleValue(options.maxLength),
|
|
2435
|
+
pattern: getRuleValue(options.pattern)
|
|
2436
|
+
} : {}), {
|
|
2437
|
+
name: name,
|
|
2438
|
+
onChange: onChange,
|
|
2439
|
+
onBlur: onChange,
|
|
2440
|
+
ref: function(ref) {
|
|
2441
|
+
if (ref) {
|
|
2442
|
+
register(name, options);
|
|
2443
|
+
field = get(_fields, name);
|
|
2444
|
+
var fieldRef = isUndefined(ref.value) ? ref.querySelectorAll ? ref.querySelectorAll("input,select,textarea")[0] || ref : ref : ref;
|
|
2445
|
+
var radioOrCheckbox = isRadioOrCheckbox(fieldRef);
|
|
2446
|
+
var refs = field._f.refs || [];
|
|
2447
|
+
if (radioOrCheckbox ? refs.find(function(option) {
|
|
2448
|
+
return option === fieldRef;
|
|
2449
|
+
}) : fieldRef === field._f.ref) {
|
|
2450
|
+
return;
|
|
2451
|
+
}
|
|
2452
|
+
set(_fields, name, {
|
|
2453
|
+
_f: _object_spread({}, field._f, radioOrCheckbox ? {
|
|
2454
|
+
refs: _to_consumable_array(refs.filter(live)).concat([
|
|
2455
|
+
fieldRef
|
|
2456
|
+
], _to_consumable_array(Array.isArray(get(_defaultValues, name)) ? [
|
|
2457
|
+
{}
|
|
2458
|
+
] : [])),
|
|
2459
|
+
ref: {
|
|
2460
|
+
type: fieldRef.type,
|
|
2461
|
+
name: name
|
|
2462
|
+
}
|
|
2463
|
+
} : {
|
|
2464
|
+
ref: fieldRef
|
|
2465
|
+
})
|
|
2466
|
+
});
|
|
2467
|
+
updateValidAndValue(name, false, undefined, fieldRef);
|
|
2468
|
+
} else {
|
|
2469
|
+
field = get(_fields, name, {});
|
|
2470
|
+
if (field._f) {
|
|
2471
|
+
field._f.mount = false;
|
|
2472
|
+
}
|
|
2473
|
+
(_options.shouldUnregister || options.shouldUnregister) && !(isNameInFieldArray(_names.array, name) && _state.action) && _names.unMount.add(name);
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
});
|
|
2477
|
+
};
|
|
2478
|
+
var _focusError = function() {
|
|
2479
|
+
return _options.shouldFocusError && iterateFieldsByAction(_fields, _focusInput, _names.mount);
|
|
2480
|
+
};
|
|
2481
|
+
var _disableForm = function(disabled) {
|
|
2482
|
+
if (isBoolean(disabled)) {
|
|
2483
|
+
_subjects.state.next({
|
|
2484
|
+
disabled: disabled
|
|
2485
|
+
});
|
|
2486
|
+
iterateFieldsByAction(_fields, function(ref, name) {
|
|
2487
|
+
var currentField = get(_fields, name);
|
|
2488
|
+
if (currentField) {
|
|
2489
|
+
ref.disabled = currentField._f.disabled || disabled;
|
|
2490
|
+
if (Array.isArray(currentField._f.refs)) {
|
|
2491
|
+
currentField._f.refs.forEach(function(inputRef) {
|
|
2492
|
+
inputRef.disabled = currentField._f.disabled || disabled;
|
|
2493
|
+
});
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
}, 0, false);
|
|
2497
|
+
}
|
|
2498
|
+
};
|
|
2499
|
+
var handleSubmit = function(onValid, onInvalid) {
|
|
2500
|
+
return function() {
|
|
2501
|
+
var _ref = _async_to_generator(function(e) {
|
|
2502
|
+
var onValidError, fieldValues, _ref, errors, values, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, name, error;
|
|
2503
|
+
return _ts_generator(this, function(_state) {
|
|
2504
|
+
switch(_state.label){
|
|
2505
|
+
case 0:
|
|
2506
|
+
onValidError = undefined;
|
|
2507
|
+
if (e) {
|
|
2508
|
+
e.preventDefault && e.preventDefault();
|
|
2509
|
+
e.persist && e.persist();
|
|
2510
|
+
}
|
|
2511
|
+
fieldValues = cloneObject(_formValues);
|
|
2512
|
+
_subjects.state.next({
|
|
2513
|
+
isSubmitting: true
|
|
2514
|
+
});
|
|
2515
|
+
if (!_options.resolver) return [
|
|
2516
|
+
3,
|
|
2517
|
+
2
|
|
2518
|
+
];
|
|
2519
|
+
return [
|
|
2520
|
+
4,
|
|
2521
|
+
_runSchema()
|
|
2522
|
+
];
|
|
2523
|
+
case 1:
|
|
2524
|
+
_ref = _state.sent(), errors = _ref.errors, values = _ref.values;
|
|
2525
|
+
_formState.errors = errors;
|
|
2526
|
+
fieldValues = values;
|
|
2527
|
+
return [
|
|
2528
|
+
3,
|
|
2529
|
+
4
|
|
2530
|
+
];
|
|
2531
|
+
case 2:
|
|
2532
|
+
return [
|
|
2533
|
+
4,
|
|
2534
|
+
executeBuiltInValidation(_fields)
|
|
2535
|
+
];
|
|
2536
|
+
case 3:
|
|
2537
|
+
_state.sent();
|
|
2538
|
+
_state.label = 4;
|
|
2539
|
+
case 4:
|
|
2540
|
+
if (_names.disabled.size) {
|
|
2541
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
2542
|
+
try {
|
|
2543
|
+
for(_iterator = _names.disabled[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
2544
|
+
name = _step.value;
|
|
2545
|
+
set(fieldValues, name, undefined);
|
|
2546
|
+
}
|
|
2547
|
+
} catch (err) {
|
|
2548
|
+
_didIteratorError = true;
|
|
2549
|
+
_iteratorError = err;
|
|
2550
|
+
} finally{
|
|
2551
|
+
try {
|
|
2552
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
2553
|
+
_iterator.return();
|
|
2554
|
+
}
|
|
2555
|
+
} finally{
|
|
2556
|
+
if (_didIteratorError) {
|
|
2557
|
+
throw _iteratorError;
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
unset(_formState.errors, "root");
|
|
2563
|
+
if (!isEmptyObject(_formState.errors)) return [
|
|
2564
|
+
3,
|
|
2565
|
+
9
|
|
2566
|
+
];
|
|
2567
|
+
_subjects.state.next({
|
|
2568
|
+
errors: {}
|
|
2569
|
+
});
|
|
2570
|
+
_state.label = 5;
|
|
2571
|
+
case 5:
|
|
2572
|
+
_state.trys.push([
|
|
2573
|
+
5,
|
|
2574
|
+
7,
|
|
2575
|
+
,
|
|
2576
|
+
8
|
|
2577
|
+
]);
|
|
2578
|
+
return [
|
|
2579
|
+
4,
|
|
2580
|
+
onValid(fieldValues, e)
|
|
2581
|
+
];
|
|
2582
|
+
case 6:
|
|
2583
|
+
_state.sent();
|
|
2584
|
+
return [
|
|
2585
|
+
3,
|
|
2586
|
+
8
|
|
2587
|
+
];
|
|
2588
|
+
case 7:
|
|
2589
|
+
error = _state.sent();
|
|
2590
|
+
onValidError = error;
|
|
2591
|
+
return [
|
|
2592
|
+
3,
|
|
2593
|
+
8
|
|
2594
|
+
];
|
|
2595
|
+
case 8:
|
|
2596
|
+
return [
|
|
2597
|
+
3,
|
|
2598
|
+
12
|
|
2599
|
+
];
|
|
2600
|
+
case 9:
|
|
2601
|
+
if (!onInvalid) return [
|
|
2602
|
+
3,
|
|
2603
|
+
11
|
|
2604
|
+
];
|
|
2605
|
+
return [
|
|
2606
|
+
4,
|
|
2607
|
+
onInvalid(_object_spread({}, _formState.errors), e)
|
|
2608
|
+
];
|
|
2609
|
+
case 10:
|
|
2610
|
+
_state.sent();
|
|
2611
|
+
_state.label = 11;
|
|
2612
|
+
case 11:
|
|
2613
|
+
_focusError();
|
|
2614
|
+
setTimeout(_focusError);
|
|
2615
|
+
_state.label = 12;
|
|
2616
|
+
case 12:
|
|
2617
|
+
_subjects.state.next({
|
|
2618
|
+
isSubmitted: true,
|
|
2619
|
+
isSubmitting: false,
|
|
2620
|
+
isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
|
|
2621
|
+
submitCount: _formState.submitCount + 1,
|
|
2622
|
+
errors: _formState.errors
|
|
2623
|
+
});
|
|
2624
|
+
if (onValidError) {
|
|
2625
|
+
throw onValidError;
|
|
2626
|
+
}
|
|
2627
|
+
return [
|
|
2628
|
+
2
|
|
2629
|
+
];
|
|
2630
|
+
}
|
|
2631
|
+
});
|
|
2632
|
+
});
|
|
2633
|
+
return function(e) {
|
|
2634
|
+
return _ref.apply(this, arguments);
|
|
2635
|
+
};
|
|
2636
|
+
}();
|
|
2637
|
+
};
|
|
2638
|
+
var resetField = function(name) {
|
|
2639
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
2640
|
+
if (get(_fields, name)) {
|
|
2641
|
+
if (isUndefined(options.defaultValue)) {
|
|
2642
|
+
setValue(name, cloneObject(get(_defaultValues, name)));
|
|
2643
|
+
} else {
|
|
2644
|
+
setValue(name, options.defaultValue);
|
|
2645
|
+
set(_defaultValues, name, cloneObject(options.defaultValue));
|
|
2646
|
+
}
|
|
2647
|
+
if (!options.keepTouched) {
|
|
2648
|
+
unset(_formState.touchedFields, name);
|
|
2649
|
+
}
|
|
2650
|
+
if (!options.keepDirty) {
|
|
2651
|
+
unset(_formState.dirtyFields, name);
|
|
2652
|
+
_formState.isDirty = options.defaultValue ? _getDirty(name, cloneObject(get(_defaultValues, name))) : _getDirty();
|
|
2653
|
+
}
|
|
2654
|
+
if (!options.keepError) {
|
|
2655
|
+
unset(_formState.errors, name);
|
|
2656
|
+
_proxyFormState.isValid && _setValid();
|
|
2657
|
+
}
|
|
2658
|
+
_subjects.state.next(_object_spread({}, _formState));
|
|
2659
|
+
}
|
|
2660
|
+
};
|
|
2661
|
+
var _reset = function(formValues) {
|
|
2662
|
+
var keepStateOptions = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
2663
|
+
var updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
|
|
2664
|
+
var cloneUpdatedValues = cloneObject(updatedValues);
|
|
2665
|
+
var isEmptyResetValues = isEmptyObject(formValues);
|
|
2666
|
+
var values = isEmptyResetValues ? _defaultValues : cloneUpdatedValues;
|
|
2667
|
+
if (!keepStateOptions.keepDefaultValues) {
|
|
2668
|
+
_defaultValues = updatedValues;
|
|
2669
|
+
}
|
|
2670
|
+
if (!keepStateOptions.keepValues) {
|
|
2671
|
+
if (keepStateOptions.keepDirtyValues) {
|
|
2672
|
+
var fieldsToCheck = new Set(_to_consumable_array(_names.mount).concat(_to_consumable_array(Object.keys(getDirtyFields(_defaultValues, _formValues)))));
|
|
2673
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
2674
|
+
try {
|
|
2675
|
+
for(var _iterator = Array.from(fieldsToCheck)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
2676
|
+
var fieldName = _step.value;
|
|
2677
|
+
get(_formState.dirtyFields, fieldName) ? set(values, fieldName, get(_formValues, fieldName)) : setValue(fieldName, get(values, fieldName));
|
|
2678
|
+
}
|
|
2679
|
+
} catch (err) {
|
|
2680
|
+
_didIteratorError = true;
|
|
2681
|
+
_iteratorError = err;
|
|
2682
|
+
} finally{
|
|
2683
|
+
try {
|
|
2684
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
2685
|
+
_iterator.return();
|
|
2686
|
+
}
|
|
2687
|
+
} finally{
|
|
2688
|
+
if (_didIteratorError) {
|
|
2689
|
+
throw _iteratorError;
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
} else {
|
|
2694
|
+
if (isWeb && isUndefined(formValues)) {
|
|
2695
|
+
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
|
|
2696
|
+
try {
|
|
2697
|
+
for(var _iterator1 = _names.mount[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
2698
|
+
var name = _step1.value;
|
|
2699
|
+
var field = get(_fields, name);
|
|
2700
|
+
if (field && field._f) {
|
|
2701
|
+
var fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
|
|
2702
|
+
if (isHTMLElement(fieldReference)) {
|
|
2703
|
+
var form = fieldReference.closest("form");
|
|
2704
|
+
if (form) {
|
|
2705
|
+
form.reset();
|
|
2706
|
+
break;
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2709
|
+
}
|
|
2710
|
+
}
|
|
2711
|
+
} catch (err) {
|
|
2712
|
+
_didIteratorError1 = true;
|
|
2713
|
+
_iteratorError1 = err;
|
|
2714
|
+
} finally{
|
|
2715
|
+
try {
|
|
2716
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
2717
|
+
_iterator1.return();
|
|
2718
|
+
}
|
|
2719
|
+
} finally{
|
|
2720
|
+
if (_didIteratorError1) {
|
|
2721
|
+
throw _iteratorError1;
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
}
|
|
2726
|
+
var _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
|
|
2727
|
+
try {
|
|
2728
|
+
for(var _iterator2 = _names.mount[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true){
|
|
2729
|
+
var fieldName1 = _step2.value;
|
|
2730
|
+
setValue(fieldName1, get(values, fieldName1));
|
|
2731
|
+
}
|
|
2732
|
+
} catch (err) {
|
|
2733
|
+
_didIteratorError2 = true;
|
|
2734
|
+
_iteratorError2 = err;
|
|
2735
|
+
} finally{
|
|
2736
|
+
try {
|
|
2737
|
+
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
|
|
2738
|
+
_iterator2.return();
|
|
2739
|
+
}
|
|
2740
|
+
} finally{
|
|
2741
|
+
if (_didIteratorError2) {
|
|
2742
|
+
throw _iteratorError2;
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
2746
|
+
}
|
|
2747
|
+
_formValues = cloneObject(values);
|
|
2748
|
+
_subjects.array.next({
|
|
2749
|
+
values: _object_spread({}, values)
|
|
2750
|
+
});
|
|
2751
|
+
_subjects.state.next({
|
|
2752
|
+
values: _object_spread({}, values)
|
|
2753
|
+
});
|
|
2754
|
+
}
|
|
2755
|
+
_names = {
|
|
2756
|
+
mount: keepStateOptions.keepDirtyValues ? _names.mount : new Set(),
|
|
2757
|
+
unMount: new Set(),
|
|
2758
|
+
array: new Set(),
|
|
2759
|
+
disabled: new Set(),
|
|
2760
|
+
watch: new Set(),
|
|
2761
|
+
watchAll: false,
|
|
2762
|
+
focus: ""
|
|
2763
|
+
};
|
|
2764
|
+
_state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid || !!keepStateOptions.keepDirtyValues;
|
|
2765
|
+
_state.watch = !!_options.shouldUnregister;
|
|
2766
|
+
_subjects.state.next({
|
|
2767
|
+
submitCount: keepStateOptions.keepSubmitCount ? _formState.submitCount : 0,
|
|
2768
|
+
isDirty: isEmptyResetValues ? false : keepStateOptions.keepDirty ? _formState.isDirty : !!(keepStateOptions.keepDefaultValues && !deepEqual(formValues, _defaultValues)),
|
|
2769
|
+
isSubmitted: keepStateOptions.keepIsSubmitted ? _formState.isSubmitted : false,
|
|
2770
|
+
dirtyFields: isEmptyResetValues ? {} : keepStateOptions.keepDirtyValues ? keepStateOptions.keepDefaultValues && _formValues ? getDirtyFields(_defaultValues, _formValues) : _formState.dirtyFields : keepStateOptions.keepDefaultValues && formValues ? getDirtyFields(_defaultValues, formValues) : keepStateOptions.keepDirty ? _formState.dirtyFields : {},
|
|
2771
|
+
touchedFields: keepStateOptions.keepTouched ? _formState.touchedFields : {},
|
|
2772
|
+
errors: keepStateOptions.keepErrors ? _formState.errors : {},
|
|
2773
|
+
isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful ? _formState.isSubmitSuccessful : false,
|
|
2774
|
+
isSubmitting: false
|
|
2775
|
+
});
|
|
2776
|
+
};
|
|
2777
|
+
var reset = function(formValues, keepStateOptions) {
|
|
2778
|
+
return _reset(isFunction(formValues) ? formValues(_formValues) : formValues, keepStateOptions);
|
|
2779
|
+
};
|
|
2780
|
+
var setFocus = function(name) {
|
|
2781
|
+
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
2782
|
+
var field = get(_fields, name);
|
|
2783
|
+
var fieldReference = field && field._f;
|
|
2784
|
+
if (fieldReference) {
|
|
2785
|
+
var fieldRef = fieldReference.refs ? fieldReference.refs[0] : fieldReference.ref;
|
|
2786
|
+
if (fieldRef.focus) {
|
|
2787
|
+
fieldRef.focus();
|
|
2788
|
+
options.shouldSelect && isFunction(fieldRef.select) && fieldRef.select();
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
2791
|
+
};
|
|
2792
|
+
var _setFormState = function(updatedFormState) {
|
|
2793
|
+
_formState = _object_spread({}, _formState, updatedFormState);
|
|
2794
|
+
};
|
|
2795
|
+
var _resetDefaultValues = function() {
|
|
2796
|
+
return isFunction(_options.defaultValues) && _options.defaultValues().then(function(values) {
|
|
2797
|
+
reset(values, _options.resetOptions);
|
|
2798
|
+
_subjects.state.next({
|
|
2799
|
+
isLoading: false
|
|
2800
|
+
});
|
|
2801
|
+
});
|
|
2802
|
+
};
|
|
2803
|
+
var methods = {
|
|
2804
|
+
control: {
|
|
2805
|
+
register: register,
|
|
2806
|
+
unregister: unregister,
|
|
2807
|
+
getFieldState: getFieldState,
|
|
2808
|
+
handleSubmit: handleSubmit,
|
|
2809
|
+
setError: setError,
|
|
2810
|
+
_subscribe: _subscribe,
|
|
2811
|
+
_runSchema: _runSchema,
|
|
2812
|
+
_getWatch: _getWatch,
|
|
2813
|
+
_getDirty: _getDirty,
|
|
2814
|
+
_setValid: _setValid,
|
|
2815
|
+
_setFieldArray: _setFieldArray,
|
|
2816
|
+
_setDisabledField: _setDisabledField,
|
|
2817
|
+
_setErrors: _setErrors,
|
|
2818
|
+
_getFieldArray: _getFieldArray,
|
|
2819
|
+
_reset: _reset,
|
|
2820
|
+
_resetDefaultValues: _resetDefaultValues,
|
|
2821
|
+
_removeUnmounted: _removeUnmounted,
|
|
2822
|
+
_disableForm: _disableForm,
|
|
2823
|
+
_subjects: _subjects,
|
|
2824
|
+
_proxyFormState: _proxyFormState,
|
|
2825
|
+
get _fields () {
|
|
2826
|
+
return _fields;
|
|
2827
|
+
},
|
|
2828
|
+
get _formValues () {
|
|
2829
|
+
return _formValues;
|
|
2830
|
+
},
|
|
2831
|
+
get _state () {
|
|
2832
|
+
return _state;
|
|
2833
|
+
},
|
|
2834
|
+
set _state (value){
|
|
2835
|
+
_state = value;
|
|
2836
|
+
},
|
|
2837
|
+
get _defaultValues () {
|
|
2838
|
+
return _defaultValues;
|
|
2839
|
+
},
|
|
2840
|
+
get _names () {
|
|
2841
|
+
return _names;
|
|
2842
|
+
},
|
|
2843
|
+
set _names (value){
|
|
2844
|
+
_names = value;
|
|
2845
|
+
},
|
|
2846
|
+
get _formState () {
|
|
2847
|
+
return _formState;
|
|
2848
|
+
},
|
|
2849
|
+
get _options () {
|
|
2850
|
+
return _options;
|
|
2851
|
+
},
|
|
2852
|
+
set _options (value){
|
|
2853
|
+
_options = _object_spread({}, _options, value);
|
|
2854
|
+
}
|
|
2855
|
+
},
|
|
2856
|
+
subscribe: subscribe,
|
|
2857
|
+
trigger: trigger,
|
|
2858
|
+
register: register,
|
|
2859
|
+
handleSubmit: handleSubmit,
|
|
2860
|
+
watch: watch,
|
|
2861
|
+
setValue: setValue,
|
|
2862
|
+
getValues: getValues,
|
|
2863
|
+
reset: reset,
|
|
2864
|
+
resetField: resetField,
|
|
2865
|
+
clearErrors: clearErrors,
|
|
2866
|
+
unregister: unregister,
|
|
2867
|
+
setError: setError,
|
|
2868
|
+
setFocus: setFocus,
|
|
2869
|
+
getFieldState: getFieldState
|
|
2870
|
+
};
|
|
2871
|
+
return _object_spread_props(_object_spread({}, methods), {
|
|
2872
|
+
formControl: methods
|
|
2873
|
+
});
|
|
2874
|
+
}
|
|
2875
|
+
var generateId = function() {
|
|
2876
|
+
var d = typeof performance === "undefined" ? Date.now() : performance.now() * 1000;
|
|
2877
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
|
2878
|
+
var r = (Math.random() * 16 + d) % 16 | 0;
|
|
2879
|
+
return (c == "x" ? r : r & 0x3 | 0x8).toString(16);
|
|
2880
|
+
});
|
|
2881
|
+
};
|
|
2882
|
+
var getFocusFieldName = function(name, index) {
|
|
2883
|
+
var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
2884
|
+
return options.shouldFocus || isUndefined(options.shouldFocus) ? options.focusName || "".concat(name, ".").concat(isUndefined(options.focusIndex) ? index : options.focusIndex, ".") : "";
|
|
2885
|
+
};
|
|
2886
|
+
var appendAt = function(data, value1) {
|
|
2887
|
+
return _to_consumable_array(data).concat(_to_consumable_array(convertToArrayPayload(value1)));
|
|
2888
|
+
};
|
|
2889
|
+
var fillEmptyArray = function(value1) {
|
|
2890
|
+
return Array.isArray(value1) ? value1.map(function() {
|
|
2891
|
+
return undefined;
|
|
2892
|
+
}) : undefined;
|
|
2893
|
+
};
|
|
2894
|
+
function insert(data, index, value1) {
|
|
2895
|
+
return _to_consumable_array(data.slice(0, index)).concat(_to_consumable_array(convertToArrayPayload(value1)), _to_consumable_array(data.slice(index)));
|
|
2896
|
+
}
|
|
2897
|
+
var moveArrayAt = function(data, from, to) {
|
|
2898
|
+
if (!Array.isArray(data)) {
|
|
2899
|
+
return [];
|
|
2900
|
+
}
|
|
2901
|
+
if (isUndefined(data[to])) {
|
|
2902
|
+
data[to] = undefined;
|
|
2903
|
+
}
|
|
2904
|
+
data.splice(to, 0, data.splice(from, 1)[0]);
|
|
2905
|
+
return data;
|
|
2906
|
+
};
|
|
2907
|
+
var prependAt = function(data, value1) {
|
|
2908
|
+
return _to_consumable_array(convertToArrayPayload(value1)).concat(_to_consumable_array(convertToArrayPayload(data)));
|
|
2909
|
+
};
|
|
2910
|
+
function removeAtIndexes(data, indexes) {
|
|
2911
|
+
var i = 0;
|
|
2912
|
+
var temp = _to_consumable_array(data);
|
|
2913
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
2914
|
+
try {
|
|
2915
|
+
for(var _iterator = indexes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
2916
|
+
var index = _step.value;
|
|
2917
|
+
temp.splice(index - i, 1);
|
|
2918
|
+
i++;
|
|
2919
|
+
}
|
|
2920
|
+
} catch (err) {
|
|
2921
|
+
_didIteratorError = true;
|
|
2922
|
+
_iteratorError = err;
|
|
2923
|
+
} finally{
|
|
2924
|
+
try {
|
|
2925
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
2926
|
+
_iterator.return();
|
|
2927
|
+
}
|
|
2928
|
+
} finally{
|
|
2929
|
+
if (_didIteratorError) {
|
|
2930
|
+
throw _iteratorError;
|
|
2931
|
+
}
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
return compact(temp).length ? temp : [];
|
|
2935
|
+
}
|
|
2936
|
+
var removeArrayAt = function(data, index) {
|
|
2937
|
+
return isUndefined(index) ? [] : removeAtIndexes(data, convertToArrayPayload(index).sort(function(a, b) {
|
|
2938
|
+
return a - b;
|
|
2939
|
+
}));
|
|
2940
|
+
};
|
|
2941
|
+
var swapArrayAt = function(data, indexA, indexB) {
|
|
2942
|
+
var ref;
|
|
2943
|
+
ref = [
|
|
2944
|
+
data[indexB],
|
|
2945
|
+
data[indexA]
|
|
2946
|
+
], data[indexA] = ref[0], data[indexB] = ref[1];
|
|
2947
|
+
};
|
|
2948
|
+
var updateAt = function(fieldValues, index, value1) {
|
|
2949
|
+
fieldValues[index] = value1;
|
|
2950
|
+
return fieldValues;
|
|
2951
|
+
};
|
|
2952
|
+
/**
|
|
2953
|
+
* A custom hook that exposes convenient methods to perform operations with a list of dynamic inputs that need to be appended, updated, removed etc. • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn) • [Video](https://youtu.be/4MrbfGSFY2A)
|
|
2954
|
+
*
|
|
2955
|
+
* @remarks
|
|
2956
|
+
* [API](https://react-hook-form.com/docs/usefieldarray) • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn)
|
|
2957
|
+
*
|
|
2958
|
+
* @param props - useFieldArray props
|
|
2959
|
+
*
|
|
2960
|
+
* @returns methods - functions to manipulate with the Field Arrays (dynamic inputs) {@link UseFieldArrayReturn}
|
|
2961
|
+
*
|
|
2962
|
+
* @example
|
|
2963
|
+
* ```tsx
|
|
2964
|
+
* function App() {
|
|
2965
|
+
* const { register, control, handleSubmit, reset, trigger, setError } = useForm({
|
|
2966
|
+
* defaultValues: {
|
|
2967
|
+
* test: []
|
|
2968
|
+
* }
|
|
2969
|
+
* });
|
|
2970
|
+
* const { fields, append } = useFieldArray({
|
|
2971
|
+
* control,
|
|
2972
|
+
* name: "test"
|
|
2973
|
+
* });
|
|
2974
|
+
*
|
|
2975
|
+
* return (
|
|
2976
|
+
* <form onSubmit={handleSubmit(data => console.log(data))}>
|
|
2977
|
+
* {fields.map((item, index) => (
|
|
2978
|
+
* <input key={item.id} {...register(`test.${index}.firstName`)} />
|
|
2979
|
+
* ))}
|
|
2980
|
+
* <button type="button" onClick={() => append({ firstName: "bill" })}>
|
|
2981
|
+
* append
|
|
2982
|
+
* </button>
|
|
2983
|
+
* <input type="submit" />
|
|
2984
|
+
* </form>
|
|
2985
|
+
* );
|
|
2986
|
+
* }
|
|
2987
|
+
* ```
|
|
2988
|
+
*/ function useFieldArray(props) {
|
|
2989
|
+
var methods = useFormContext();
|
|
2990
|
+
var _props_control = props.control, control = _props_control === void 0 ? methods.control : _props_control, name = props.name, _props_keyName = props.keyName, keyName = _props_keyName === void 0 ? "id" : _props_keyName, shouldUnregister = props.shouldUnregister, rules = props.rules;
|
|
2991
|
+
var _React__default_useState = _sliced_to_array(React__default.useState(control._getFieldArray(name)), 2), fields = _React__default_useState[0], setFields = _React__default_useState[1];
|
|
2992
|
+
var ids = React__default.useRef(control._getFieldArray(name).map(generateId));
|
|
2993
|
+
var _fieldIds = React__default.useRef(fields);
|
|
2994
|
+
var _name = React__default.useRef(name);
|
|
2995
|
+
var _actioned = React__default.useRef(false);
|
|
2996
|
+
_name.current = name;
|
|
2997
|
+
_fieldIds.current = fields;
|
|
2998
|
+
control._names.array.add(name);
|
|
2999
|
+
rules && control.register(name, rules);
|
|
3000
|
+
React__default.useEffect(function() {
|
|
3001
|
+
return control._subjects.array.subscribe({
|
|
3002
|
+
next: function(param) {
|
|
3003
|
+
var values = param.values, fieldArrayName = param.name;
|
|
3004
|
+
if (fieldArrayName === _name.current || !fieldArrayName) {
|
|
3005
|
+
var fieldValues = get(values, _name.current);
|
|
3006
|
+
if (Array.isArray(fieldValues)) {
|
|
3007
|
+
setFields(fieldValues);
|
|
3008
|
+
ids.current = fieldValues.map(generateId);
|
|
3009
|
+
}
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
}).unsubscribe;
|
|
3013
|
+
}, [
|
|
3014
|
+
control
|
|
3015
|
+
]);
|
|
3016
|
+
var updateValues = React__default.useCallback(function(updatedFieldArrayValues) {
|
|
3017
|
+
_actioned.current = true;
|
|
3018
|
+
control._setFieldArray(name, updatedFieldArrayValues);
|
|
3019
|
+
}, [
|
|
3020
|
+
control,
|
|
3021
|
+
name
|
|
3022
|
+
]);
|
|
3023
|
+
var append = function(value1, options) {
|
|
3024
|
+
var appendValue = convertToArrayPayload(cloneObject(value1));
|
|
3025
|
+
var updatedFieldArrayValues = appendAt(control._getFieldArray(name), appendValue);
|
|
3026
|
+
control._names.focus = getFocusFieldName(name, updatedFieldArrayValues.length - 1, options);
|
|
3027
|
+
ids.current = appendAt(ids.current, appendValue.map(generateId));
|
|
3028
|
+
updateValues(updatedFieldArrayValues);
|
|
3029
|
+
setFields(updatedFieldArrayValues);
|
|
3030
|
+
control._setFieldArray(name, updatedFieldArrayValues, appendAt, {
|
|
3031
|
+
argA: fillEmptyArray(value1)
|
|
3032
|
+
});
|
|
3033
|
+
};
|
|
3034
|
+
var prepend = function(value1, options) {
|
|
3035
|
+
var prependValue = convertToArrayPayload(cloneObject(value1));
|
|
3036
|
+
var updatedFieldArrayValues = prependAt(control._getFieldArray(name), prependValue);
|
|
3037
|
+
control._names.focus = getFocusFieldName(name, 0, options);
|
|
3038
|
+
ids.current = prependAt(ids.current, prependValue.map(generateId));
|
|
3039
|
+
updateValues(updatedFieldArrayValues);
|
|
3040
|
+
setFields(updatedFieldArrayValues);
|
|
3041
|
+
control._setFieldArray(name, updatedFieldArrayValues, prependAt, {
|
|
3042
|
+
argA: fillEmptyArray(value1)
|
|
3043
|
+
});
|
|
3044
|
+
};
|
|
3045
|
+
var remove = function(index) {
|
|
3046
|
+
var updatedFieldArrayValues = removeArrayAt(control._getFieldArray(name), index);
|
|
3047
|
+
ids.current = removeArrayAt(ids.current, index);
|
|
3048
|
+
updateValues(updatedFieldArrayValues);
|
|
3049
|
+
setFields(updatedFieldArrayValues);
|
|
3050
|
+
!Array.isArray(get(control._fields, name)) && set(control._fields, name, undefined);
|
|
3051
|
+
control._setFieldArray(name, updatedFieldArrayValues, removeArrayAt, {
|
|
3052
|
+
argA: index
|
|
3053
|
+
});
|
|
3054
|
+
};
|
|
3055
|
+
var insert$1 = function(index, value1, options) {
|
|
3056
|
+
var insertValue = convertToArrayPayload(cloneObject(value1));
|
|
3057
|
+
var updatedFieldArrayValues = insert(control._getFieldArray(name), index, insertValue);
|
|
3058
|
+
control._names.focus = getFocusFieldName(name, index, options);
|
|
3059
|
+
ids.current = insert(ids.current, index, insertValue.map(generateId));
|
|
3060
|
+
updateValues(updatedFieldArrayValues);
|
|
3061
|
+
setFields(updatedFieldArrayValues);
|
|
3062
|
+
control._setFieldArray(name, updatedFieldArrayValues, insert, {
|
|
3063
|
+
argA: index,
|
|
3064
|
+
argB: fillEmptyArray(value1)
|
|
3065
|
+
});
|
|
3066
|
+
};
|
|
3067
|
+
var swap = function(indexA, indexB) {
|
|
3068
|
+
var updatedFieldArrayValues = control._getFieldArray(name);
|
|
3069
|
+
swapArrayAt(updatedFieldArrayValues, indexA, indexB);
|
|
3070
|
+
swapArrayAt(ids.current, indexA, indexB);
|
|
3071
|
+
updateValues(updatedFieldArrayValues);
|
|
3072
|
+
setFields(updatedFieldArrayValues);
|
|
3073
|
+
control._setFieldArray(name, updatedFieldArrayValues, swapArrayAt, {
|
|
3074
|
+
argA: indexA,
|
|
3075
|
+
argB: indexB
|
|
3076
|
+
}, false);
|
|
3077
|
+
};
|
|
3078
|
+
var move = function(from, to) {
|
|
3079
|
+
var updatedFieldArrayValues = control._getFieldArray(name);
|
|
3080
|
+
moveArrayAt(updatedFieldArrayValues, from, to);
|
|
3081
|
+
moveArrayAt(ids.current, from, to);
|
|
3082
|
+
updateValues(updatedFieldArrayValues);
|
|
3083
|
+
setFields(updatedFieldArrayValues);
|
|
3084
|
+
control._setFieldArray(name, updatedFieldArrayValues, moveArrayAt, {
|
|
3085
|
+
argA: from,
|
|
3086
|
+
argB: to
|
|
3087
|
+
}, false);
|
|
3088
|
+
};
|
|
3089
|
+
var update = function(index, value1) {
|
|
3090
|
+
var updateValue = cloneObject(value1);
|
|
3091
|
+
var updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, updateValue);
|
|
3092
|
+
ids.current = _to_consumable_array(updatedFieldArrayValues).map(function(item, i) {
|
|
3093
|
+
return !item || i === index ? generateId() : ids.current[i];
|
|
3094
|
+
});
|
|
3095
|
+
updateValues(updatedFieldArrayValues);
|
|
3096
|
+
setFields(_to_consumable_array(updatedFieldArrayValues));
|
|
3097
|
+
control._setFieldArray(name, updatedFieldArrayValues, updateAt, {
|
|
3098
|
+
argA: index,
|
|
3099
|
+
argB: updateValue
|
|
3100
|
+
}, true, false);
|
|
3101
|
+
};
|
|
3102
|
+
var replace = function(value1) {
|
|
3103
|
+
var updatedFieldArrayValues = convertToArrayPayload(cloneObject(value1));
|
|
3104
|
+
ids.current = updatedFieldArrayValues.map(generateId);
|
|
3105
|
+
updateValues(_to_consumable_array(updatedFieldArrayValues));
|
|
3106
|
+
setFields(_to_consumable_array(updatedFieldArrayValues));
|
|
3107
|
+
control._setFieldArray(name, _to_consumable_array(updatedFieldArrayValues), function(data) {
|
|
3108
|
+
return data;
|
|
3109
|
+
}, {}, true, false);
|
|
3110
|
+
};
|
|
3111
|
+
React__default.useEffect(function() {
|
|
3112
|
+
control._state.action = false;
|
|
3113
|
+
isWatched(name, control._names) && control._subjects.state.next(_object_spread({}, control._formState));
|
|
3114
|
+
if (_actioned.current && (!getValidationModes(control._options.mode).isOnSubmit || control._formState.isSubmitted) && !getValidationModes(control._options.reValidateMode).isOnSubmit) {
|
|
3115
|
+
if (control._options.resolver) {
|
|
3116
|
+
control._runSchema([
|
|
3117
|
+
name
|
|
3118
|
+
]).then(function(result) {
|
|
3119
|
+
var error = get(result.errors, name);
|
|
3120
|
+
var existingError = get(control._formState.errors, name);
|
|
3121
|
+
if (existingError ? !error && existingError.type || error && (existingError.type !== error.type || existingError.message !== error.message) : error && error.type) {
|
|
3122
|
+
error ? set(control._formState.errors, name, error) : unset(control._formState.errors, name);
|
|
3123
|
+
control._subjects.state.next({
|
|
3124
|
+
errors: control._formState.errors
|
|
3125
|
+
});
|
|
3126
|
+
}
|
|
3127
|
+
});
|
|
3128
|
+
} else {
|
|
3129
|
+
var field = get(control._fields, name);
|
|
3130
|
+
if (field && field._f && !(getValidationModes(control._options.reValidateMode).isOnSubmit && getValidationModes(control._options.mode).isOnSubmit)) {
|
|
3131
|
+
validateField(field, control._names.disabled, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then(function(error) {
|
|
3132
|
+
return !isEmptyObject(error) && control._subjects.state.next({
|
|
3133
|
+
errors: updateFieldArrayRootError(control._formState.errors, error, name)
|
|
3134
|
+
});
|
|
3135
|
+
});
|
|
3136
|
+
}
|
|
3137
|
+
}
|
|
3138
|
+
}
|
|
3139
|
+
control._subjects.state.next({
|
|
3140
|
+
name: name,
|
|
3141
|
+
values: cloneObject(control._formValues)
|
|
3142
|
+
});
|
|
3143
|
+
control._names.focus && iterateFieldsByAction(control._fields, function(ref, key) {
|
|
3144
|
+
if (control._names.focus && key.startsWith(control._names.focus) && ref.focus) {
|
|
3145
|
+
ref.focus();
|
|
3146
|
+
return 1;
|
|
3147
|
+
}
|
|
3148
|
+
return;
|
|
3149
|
+
});
|
|
3150
|
+
control._names.focus = "";
|
|
3151
|
+
control._setValid();
|
|
3152
|
+
_actioned.current = false;
|
|
3153
|
+
}, [
|
|
3154
|
+
fields,
|
|
3155
|
+
name,
|
|
3156
|
+
control
|
|
3157
|
+
]);
|
|
3158
|
+
React__default.useEffect(function() {
|
|
3159
|
+
!get(control._formValues, name) && control._setFieldArray(name);
|
|
3160
|
+
return function() {
|
|
3161
|
+
var updateMounted = function(name, value1) {
|
|
3162
|
+
var field = get(control._fields, name);
|
|
3163
|
+
if (field && field._f) {
|
|
3164
|
+
field._f.mount = value1;
|
|
3165
|
+
}
|
|
3166
|
+
};
|
|
3167
|
+
control._options.shouldUnregister || shouldUnregister ? control.unregister(name) : updateMounted(name, false);
|
|
3168
|
+
};
|
|
3169
|
+
}, [
|
|
3170
|
+
name,
|
|
3171
|
+
control,
|
|
3172
|
+
keyName,
|
|
3173
|
+
shouldUnregister
|
|
3174
|
+
]);
|
|
3175
|
+
return {
|
|
3176
|
+
swap: React__default.useCallback(swap, [
|
|
3177
|
+
updateValues,
|
|
3178
|
+
name,
|
|
3179
|
+
control
|
|
3180
|
+
]),
|
|
3181
|
+
move: React__default.useCallback(move, [
|
|
3182
|
+
updateValues,
|
|
3183
|
+
name,
|
|
3184
|
+
control
|
|
3185
|
+
]),
|
|
3186
|
+
prepend: React__default.useCallback(prepend, [
|
|
3187
|
+
updateValues,
|
|
3188
|
+
name,
|
|
3189
|
+
control
|
|
3190
|
+
]),
|
|
3191
|
+
append: React__default.useCallback(append, [
|
|
3192
|
+
updateValues,
|
|
3193
|
+
name,
|
|
3194
|
+
control
|
|
3195
|
+
]),
|
|
3196
|
+
remove: React__default.useCallback(remove, [
|
|
3197
|
+
updateValues,
|
|
3198
|
+
name,
|
|
3199
|
+
control
|
|
3200
|
+
]),
|
|
3201
|
+
insert: React__default.useCallback(insert$1, [
|
|
3202
|
+
updateValues,
|
|
3203
|
+
name,
|
|
3204
|
+
control
|
|
3205
|
+
]),
|
|
3206
|
+
update: React__default.useCallback(update, [
|
|
3207
|
+
updateValues,
|
|
3208
|
+
name,
|
|
3209
|
+
control
|
|
3210
|
+
]),
|
|
3211
|
+
replace: React__default.useCallback(replace, [
|
|
3212
|
+
updateValues,
|
|
3213
|
+
name,
|
|
3214
|
+
control
|
|
3215
|
+
]),
|
|
3216
|
+
fields: React__default.useMemo(function() {
|
|
3217
|
+
return fields.map(function(field, index) {
|
|
3218
|
+
return _object_spread_props(_object_spread({}, field), _define_property({}, keyName, ids.current[index] || generateId()));
|
|
3219
|
+
});
|
|
3220
|
+
}, [
|
|
3221
|
+
fields,
|
|
3222
|
+
keyName
|
|
3223
|
+
])
|
|
3224
|
+
};
|
|
3225
|
+
}
|
|
3226
|
+
/**
|
|
3227
|
+
* Custom hook to manage the entire form.
|
|
3228
|
+
*
|
|
3229
|
+
* @remarks
|
|
3230
|
+
* [API](https://react-hook-form.com/docs/useform) • [Demo](https://codesandbox.io/s/react-hook-form-get-started-ts-5ksmm) • [Video](https://www.youtube.com/watch?v=RkXv4AXXC_4)
|
|
3231
|
+
*
|
|
3232
|
+
* @param props - form configuration and validation parameters.
|
|
3233
|
+
*
|
|
3234
|
+
* @returns methods - individual functions to manage the form state. {@link UseFormReturn}
|
|
3235
|
+
*
|
|
3236
|
+
* @example
|
|
3237
|
+
* ```tsx
|
|
3238
|
+
* function App() {
|
|
3239
|
+
* const { register, handleSubmit, watch, formState: { errors } } = useForm();
|
|
3240
|
+
* const onSubmit = data => console.log(data);
|
|
3241
|
+
*
|
|
3242
|
+
* console.log(watch("example"));
|
|
3243
|
+
*
|
|
3244
|
+
* return (
|
|
3245
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
|
3246
|
+
* <input defaultValue="test" {...register("example")} />
|
|
3247
|
+
* <input {...register("exampleRequired", { required: true })} />
|
|
3248
|
+
* {errors.exampleRequired && <span>This field is required</span>}
|
|
3249
|
+
* <button>Submit</button>
|
|
3250
|
+
* </form>
|
|
3251
|
+
* );
|
|
3252
|
+
* }
|
|
3253
|
+
* ```
|
|
3254
|
+
*/ function useForm() {
|
|
3255
|
+
var props = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
3256
|
+
var _formControl = React__default.useRef(undefined);
|
|
3257
|
+
var _values = React__default.useRef(undefined);
|
|
3258
|
+
var _React__default_useState = _sliced_to_array(React__default.useState({
|
|
3259
|
+
isDirty: false,
|
|
3260
|
+
isValidating: false,
|
|
3261
|
+
isLoading: isFunction(props.defaultValues),
|
|
3262
|
+
isSubmitted: false,
|
|
3263
|
+
isSubmitting: false,
|
|
3264
|
+
isSubmitSuccessful: false,
|
|
3265
|
+
isValid: false,
|
|
3266
|
+
submitCount: 0,
|
|
3267
|
+
dirtyFields: {},
|
|
3268
|
+
touchedFields: {},
|
|
3269
|
+
validatingFields: {},
|
|
3270
|
+
errors: props.errors || {},
|
|
3271
|
+
disabled: props.disabled || false,
|
|
3272
|
+
isReady: false,
|
|
3273
|
+
defaultValues: isFunction(props.defaultValues) ? undefined : props.defaultValues
|
|
3274
|
+
}), 2), formState = _React__default_useState[0], updateFormState = _React__default_useState[1];
|
|
3275
|
+
if (!_formControl.current) {
|
|
3276
|
+
_formControl.current = _object_spread_props(_object_spread({}, props.formControl ? props.formControl : createFormControl(props)), {
|
|
3277
|
+
formState: formState
|
|
3278
|
+
});
|
|
3279
|
+
if (props.formControl && props.defaultValues && !isFunction(props.defaultValues)) {
|
|
3280
|
+
props.formControl.reset(props.defaultValues, props.resetOptions);
|
|
3281
|
+
}
|
|
3282
|
+
}
|
|
3283
|
+
var control = _formControl.current.control;
|
|
3284
|
+
control._options = props;
|
|
3285
|
+
useIsomorphicLayoutEffect(function() {
|
|
3286
|
+
var sub = control._subscribe({
|
|
3287
|
+
formState: control._proxyFormState,
|
|
3288
|
+
callback: function() {
|
|
3289
|
+
return updateFormState(_object_spread({}, control._formState));
|
|
3290
|
+
},
|
|
3291
|
+
reRenderRoot: true
|
|
3292
|
+
});
|
|
3293
|
+
updateFormState(function(data) {
|
|
3294
|
+
return _object_spread_props(_object_spread({}, data), {
|
|
3295
|
+
isReady: true
|
|
3296
|
+
});
|
|
3297
|
+
});
|
|
3298
|
+
control._formState.isReady = true;
|
|
3299
|
+
return sub;
|
|
3300
|
+
}, [
|
|
3301
|
+
control
|
|
3302
|
+
]);
|
|
3303
|
+
React__default.useEffect(function() {
|
|
3304
|
+
return control._disableForm(props.disabled);
|
|
3305
|
+
}, [
|
|
3306
|
+
control,
|
|
3307
|
+
props.disabled
|
|
3308
|
+
]);
|
|
3309
|
+
React__default.useEffect(function() {
|
|
3310
|
+
if (props.mode) {
|
|
3311
|
+
control._options.mode = props.mode;
|
|
3312
|
+
}
|
|
3313
|
+
if (props.reValidateMode) {
|
|
3314
|
+
control._options.reValidateMode = props.reValidateMode;
|
|
3315
|
+
}
|
|
3316
|
+
if (props.errors && !isEmptyObject(props.errors)) {
|
|
3317
|
+
control._setErrors(props.errors);
|
|
3318
|
+
}
|
|
3319
|
+
}, [
|
|
3320
|
+
control,
|
|
3321
|
+
props.errors,
|
|
3322
|
+
props.mode,
|
|
3323
|
+
props.reValidateMode
|
|
3324
|
+
]);
|
|
3325
|
+
React__default.useEffect(function() {
|
|
3326
|
+
props.shouldUnregister && control._subjects.state.next({
|
|
3327
|
+
values: control._getWatch()
|
|
3328
|
+
});
|
|
3329
|
+
}, [
|
|
3330
|
+
control,
|
|
3331
|
+
props.shouldUnregister
|
|
3332
|
+
]);
|
|
3333
|
+
React__default.useEffect(function() {
|
|
3334
|
+
if (control._proxyFormState.isDirty) {
|
|
3335
|
+
var isDirty = control._getDirty();
|
|
3336
|
+
if (isDirty !== formState.isDirty) {
|
|
3337
|
+
control._subjects.state.next({
|
|
3338
|
+
isDirty: isDirty
|
|
3339
|
+
});
|
|
3340
|
+
}
|
|
3341
|
+
}
|
|
3342
|
+
}, [
|
|
3343
|
+
control,
|
|
3344
|
+
formState.isDirty
|
|
3345
|
+
]);
|
|
3346
|
+
React__default.useEffect(function() {
|
|
3347
|
+
if (props.values && !deepEqual(props.values, _values.current)) {
|
|
3348
|
+
control._reset(props.values, control._options.resetOptions);
|
|
3349
|
+
_values.current = props.values;
|
|
3350
|
+
updateFormState(function(state) {
|
|
3351
|
+
return _object_spread({}, state);
|
|
3352
|
+
});
|
|
3353
|
+
} else {
|
|
3354
|
+
control._resetDefaultValues();
|
|
3355
|
+
}
|
|
3356
|
+
}, [
|
|
3357
|
+
control,
|
|
3358
|
+
props.values
|
|
3359
|
+
]);
|
|
3360
|
+
React__default.useEffect(function() {
|
|
3361
|
+
if (!control._state.mount) {
|
|
3362
|
+
control._setValid();
|
|
3363
|
+
control._state.mount = true;
|
|
3364
|
+
}
|
|
3365
|
+
if (control._state.watch) {
|
|
3366
|
+
control._state.watch = false;
|
|
3367
|
+
control._subjects.state.next(_object_spread({}, control._formState));
|
|
3368
|
+
}
|
|
3369
|
+
control._removeUnmounted();
|
|
3370
|
+
});
|
|
3371
|
+
_formControl.current.formState = getProxyFormState(formState, control);
|
|
3372
|
+
return _formControl.current;
|
|
3373
|
+
}
|
|
3374
|
+
|
|
3375
|
+
export { Controller as C, FormProvider as F, useFormContext as a, useController as b, useFieldArray as c, useFormState as d, useWatch as e, useForm as u };
|