msw-dev-tool 3.1.2 → 3.2.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/dist/cjs/lib/handlerStore.js +38 -15
- package/dist/cjs/lib/handlerStore.js.map +1 -1
- package/dist/cjs/lib/types/core.js +19 -0
- package/dist/cjs/lib/types/core.js.map +1 -0
- package/dist/cjs/lib/types/http.js +71 -0
- package/dist/cjs/lib/types/http.js.map +1 -0
- package/dist/cjs/lib/types/utils.js +10 -0
- package/dist/cjs/lib/types/utils.js.map +1 -0
- package/dist/cjs/lib/{util.js → utils/index.js} +11 -10
- package/dist/cjs/lib/utils/index.js.map +1 -0
- package/dist/cjs/msw-dev-tool.css +1 -1
- package/dist/cjs/node_modules/@hookform/resolvers/dist/resolvers.js +9 -0
- package/dist/cjs/node_modules/@hookform/resolvers/dist/resolvers.js.map +1 -0
- package/dist/cjs/node_modules/@hookform/resolvers/zod/dist/zod.js +9 -0
- package/dist/cjs/node_modules/@hookform/resolvers/zod/dist/zod.js.map +1 -0
- package/dist/cjs/node_modules/@radix-ui/react-icons/dist/react-icons.esm.js +23 -0
- package/dist/cjs/node_modules/@radix-ui/react-icons/dist/react-icons.esm.js.map +1 -1
- package/dist/cjs/node_modules/@radix-ui/react-select/dist/index.js +81 -81
- package/dist/cjs/node_modules/react-hook-form/dist/index.esm.js +1930 -0
- package/dist/cjs/node_modules/react-hook-form/dist/index.esm.js.map +1 -0
- package/dist/cjs/schema/handler.js +68 -0
- package/dist/cjs/schema/handler.js.map +1 -0
- package/dist/cjs/ui/DevToolContent/CloseButton.js +13 -0
- package/dist/cjs/ui/DevToolContent/CloseButton.js.map +1 -0
- package/dist/cjs/ui/DevToolContent/Form/FormFieldBase.js +3 -3
- package/dist/cjs/ui/DevToolContent/Form/FormFieldBase.js.map +1 -1
- package/dist/cjs/ui/DevToolContent/Form/Select.js +4 -3
- package/dist/cjs/ui/DevToolContent/Form/Select.js.map +1 -1
- package/dist/cjs/ui/DevToolContent/Form/SelectFormField.js +9 -3
- package/dist/cjs/ui/DevToolContent/Form/SelectFormField.js.map +1 -1
- package/dist/cjs/ui/DevToolContent/HandlerTable/BehaviorSelect.js +4 -3
- package/dist/cjs/ui/DevToolContent/HandlerTable/BehaviorSelect.js.map +1 -1
- package/dist/cjs/ui/DevToolContent/ToolButtonGroup/HandlerForm.js +42 -39
- package/dist/cjs/ui/DevToolContent/ToolButtonGroup/HandlerForm.js.map +1 -1
- package/dist/cjs/ui/DevToolContent/ToolButtonGroup/index.js +3 -8
- package/dist/cjs/ui/DevToolContent/ToolButtonGroup/index.js.map +1 -1
- package/dist/cjs/ui/DevToolContent/ToolButtonGroup/util.js +25 -0
- package/dist/cjs/ui/DevToolContent/ToolButtonGroup/util.js.map +1 -1
- package/dist/cjs/ui/MSWDevTool.js +4 -9
- package/dist/cjs/ui/MSWDevTool.js.map +1 -1
- package/dist/esm/lib/handlerStore.js +31 -8
- package/dist/esm/lib/handlerStore.js.map +1 -1
- package/dist/esm/lib/types/core.js +16 -0
- package/dist/esm/lib/types/core.js.map +1 -0
- package/dist/esm/lib/types/http.js +64 -0
- package/dist/esm/lib/types/http.js.map +1 -0
- package/dist/esm/lib/types/utils.js +8 -0
- package/dist/esm/lib/types/utils.js.map +1 -0
- package/dist/esm/lib/{util.js → utils/index.js} +5 -4
- package/dist/esm/lib/utils/index.js.map +1 -0
- package/dist/esm/msw-dev-tool.css +1 -1
- package/dist/esm/node_modules/@hookform/resolvers/dist/resolvers.js +6 -0
- package/dist/esm/node_modules/@hookform/resolvers/dist/resolvers.js.map +1 -0
- package/dist/esm/node_modules/@hookform/resolvers/zod/dist/zod.js +7 -0
- package/dist/esm/node_modules/@hookform/resolvers/zod/dist/zod.js.map +1 -0
- package/dist/esm/node_modules/@radix-ui/react-icons/dist/react-icons.esm.js +23 -1
- package/dist/esm/node_modules/@radix-ui/react-icons/dist/react-icons.esm.js.map +1 -1
- package/dist/esm/node_modules/@radix-ui/react-select/dist/index.js +2 -2
- package/dist/esm/node_modules/react-hook-form/dist/index.esm.js +1924 -0
- package/dist/esm/node_modules/react-hook-form/dist/index.esm.js.map +1 -0
- package/dist/esm/schema/handler.js +66 -0
- package/dist/esm/schema/handler.js.map +1 -0
- package/dist/esm/ui/DevToolContent/CloseButton.js +11 -0
- package/dist/esm/ui/DevToolContent/CloseButton.js.map +1 -0
- package/dist/esm/ui/DevToolContent/Form/FormFieldBase.js +3 -3
- package/dist/esm/ui/DevToolContent/Form/FormFieldBase.js.map +1 -1
- package/dist/esm/ui/DevToolContent/Form/Select.js +7 -6
- package/dist/esm/ui/DevToolContent/Form/Select.js.map +1 -1
- package/dist/esm/ui/DevToolContent/Form/SelectFormField.js +10 -4
- package/dist/esm/ui/DevToolContent/Form/SelectFormField.js.map +1 -1
- package/dist/esm/ui/DevToolContent/HandlerTable/BehaviorSelect.js +2 -1
- package/dist/esm/ui/DevToolContent/HandlerTable/BehaviorSelect.js.map +1 -1
- package/dist/esm/ui/DevToolContent/ToolButtonGroup/HandlerForm.js +43 -40
- package/dist/esm/ui/DevToolContent/ToolButtonGroup/HandlerForm.js.map +1 -1
- package/dist/esm/ui/DevToolContent/ToolButtonGroup/index.js +3 -8
- package/dist/esm/ui/DevToolContent/ToolButtonGroup/index.js.map +1 -1
- package/dist/esm/ui/DevToolContent/ToolButtonGroup/util.js +22 -1
- package/dist/esm/ui/DevToolContent/ToolButtonGroup/util.js.map +1 -1
- package/dist/esm/ui/MSWDevTool.js +4 -9
- package/dist/esm/ui/MSWDevTool.js.map +1 -1
- package/dist/types/index.d.ts +21 -20
- package/package.json +6 -1
- package/dist/cjs/lib/type.js +0 -57
- package/dist/cjs/lib/type.js.map +0 -1
- package/dist/cjs/lib/util.js.map +0 -1
- package/dist/cjs/ui/DevToolContent/ToolButtonGroup/schema.js +0 -31
- package/dist/cjs/ui/DevToolContent/ToolButtonGroup/schema.js.map +0 -1
- package/dist/esm/lib/type.js +0 -52
- package/dist/esm/lib/type.js.map +0 -1
- package/dist/esm/lib/util.js.map +0 -1
- package/dist/esm/ui/DevToolContent/ToolButtonGroup/schema.js +0 -29
- package/dist/esm/ui/DevToolContent/ToolButtonGroup/schema.js.map +0 -1
@@ -0,0 +1,1924 @@
|
|
1
|
+
import React__default from 'react';
|
2
|
+
|
3
|
+
var isCheckBoxInput = (element) => element.type === 'checkbox';
|
4
|
+
|
5
|
+
var isDateObject = (value) => value instanceof Date;
|
6
|
+
|
7
|
+
var isNullOrUndefined = (value) => value == null;
|
8
|
+
|
9
|
+
const isObjectType = (value) => typeof value === 'object';
|
10
|
+
var isObject = (value) => !isNullOrUndefined(value) &&
|
11
|
+
!Array.isArray(value) &&
|
12
|
+
isObjectType(value) &&
|
13
|
+
!isDateObject(value);
|
14
|
+
|
15
|
+
var getEventValue = (event) => isObject(event) && event.target
|
16
|
+
? isCheckBoxInput(event.target)
|
17
|
+
? event.target.checked
|
18
|
+
: event.target.value
|
19
|
+
: event;
|
20
|
+
|
21
|
+
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
22
|
+
|
23
|
+
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
24
|
+
|
25
|
+
var isPlainObject = (tempObject) => {
|
26
|
+
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
27
|
+
return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
|
28
|
+
};
|
29
|
+
|
30
|
+
var isWeb = typeof window !== 'undefined' &&
|
31
|
+
typeof window.HTMLElement !== 'undefined' &&
|
32
|
+
typeof document !== 'undefined';
|
33
|
+
|
34
|
+
function cloneObject(data) {
|
35
|
+
let copy;
|
36
|
+
const isArray = Array.isArray(data);
|
37
|
+
const isFileListInstance = typeof FileList !== 'undefined' ? data instanceof FileList : false;
|
38
|
+
if (data instanceof Date) {
|
39
|
+
copy = new Date(data);
|
40
|
+
}
|
41
|
+
else if (data instanceof Set) {
|
42
|
+
copy = new Set(data);
|
43
|
+
}
|
44
|
+
else if (!(isWeb && (data instanceof Blob || isFileListInstance)) &&
|
45
|
+
(isArray || isObject(data))) {
|
46
|
+
copy = isArray ? [] : {};
|
47
|
+
if (!isArray && !isPlainObject(data)) {
|
48
|
+
copy = data;
|
49
|
+
}
|
50
|
+
else {
|
51
|
+
for (const key in data) {
|
52
|
+
if (data.hasOwnProperty(key)) {
|
53
|
+
copy[key] = cloneObject(data[key]);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
else {
|
59
|
+
return data;
|
60
|
+
}
|
61
|
+
return copy;
|
62
|
+
}
|
63
|
+
|
64
|
+
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
65
|
+
|
66
|
+
var isUndefined = (val) => val === undefined;
|
67
|
+
|
68
|
+
var get = (object, path, defaultValue) => {
|
69
|
+
if (!path || !isObject(object)) {
|
70
|
+
return defaultValue;
|
71
|
+
}
|
72
|
+
const result = compact(path.split(/[,[\].]+?/)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], object);
|
73
|
+
return isUndefined(result) || result === object
|
74
|
+
? isUndefined(object[path])
|
75
|
+
? defaultValue
|
76
|
+
: object[path]
|
77
|
+
: result;
|
78
|
+
};
|
79
|
+
|
80
|
+
var isBoolean = (value) => typeof value === 'boolean';
|
81
|
+
|
82
|
+
var isKey = (value) => /^\w*$/.test(value);
|
83
|
+
|
84
|
+
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
85
|
+
|
86
|
+
var set = (object, path, value) => {
|
87
|
+
let index = -1;
|
88
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
89
|
+
const length = tempPath.length;
|
90
|
+
const lastIndex = length - 1;
|
91
|
+
while (++index < length) {
|
92
|
+
const key = tempPath[index];
|
93
|
+
let newValue = value;
|
94
|
+
if (index !== lastIndex) {
|
95
|
+
const objValue = object[key];
|
96
|
+
newValue =
|
97
|
+
isObject(objValue) || Array.isArray(objValue)
|
98
|
+
? objValue
|
99
|
+
: !isNaN(+tempPath[index + 1])
|
100
|
+
? []
|
101
|
+
: {};
|
102
|
+
}
|
103
|
+
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
104
|
+
return;
|
105
|
+
}
|
106
|
+
object[key] = newValue;
|
107
|
+
object = object[key];
|
108
|
+
}
|
109
|
+
};
|
110
|
+
|
111
|
+
const EVENTS = {
|
112
|
+
BLUR: 'blur',
|
113
|
+
FOCUS_OUT: 'focusout',
|
114
|
+
CHANGE: 'change',
|
115
|
+
};
|
116
|
+
const VALIDATION_MODE = {
|
117
|
+
onBlur: 'onBlur',
|
118
|
+
onChange: 'onChange',
|
119
|
+
onSubmit: 'onSubmit',
|
120
|
+
onTouched: 'onTouched',
|
121
|
+
all: 'all',
|
122
|
+
};
|
123
|
+
const INPUT_VALIDATION_RULES = {
|
124
|
+
max: 'max',
|
125
|
+
min: 'min',
|
126
|
+
maxLength: 'maxLength',
|
127
|
+
minLength: 'minLength',
|
128
|
+
pattern: 'pattern',
|
129
|
+
required: 'required',
|
130
|
+
validate: 'validate',
|
131
|
+
};
|
132
|
+
|
133
|
+
React__default.createContext(null);
|
134
|
+
|
135
|
+
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
136
|
+
const result = {
|
137
|
+
defaultValues: control._defaultValues,
|
138
|
+
};
|
139
|
+
for (const key in formState) {
|
140
|
+
Object.defineProperty(result, key, {
|
141
|
+
get: () => {
|
142
|
+
const _key = key;
|
143
|
+
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
144
|
+
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
145
|
+
}
|
146
|
+
localProxyFormState && (localProxyFormState[_key] = true);
|
147
|
+
return formState[_key];
|
148
|
+
},
|
149
|
+
});
|
150
|
+
}
|
151
|
+
return result;
|
152
|
+
};
|
153
|
+
|
154
|
+
var isString = (value) => typeof value === 'string';
|
155
|
+
|
156
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
|
157
|
+
if (isString(names)) {
|
158
|
+
isGlobal && _names.watch.add(names);
|
159
|
+
return get(formValues, names, defaultValue);
|
160
|
+
}
|
161
|
+
if (Array.isArray(names)) {
|
162
|
+
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
163
|
+
}
|
164
|
+
isGlobal && (_names.watchAll = true);
|
165
|
+
return formValues;
|
166
|
+
};
|
167
|
+
|
168
|
+
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
169
|
+
? {
|
170
|
+
...errors[name],
|
171
|
+
types: {
|
172
|
+
...(errors[name] && errors[name].types ? errors[name].types : {}),
|
173
|
+
[type]: message || true,
|
174
|
+
},
|
175
|
+
}
|
176
|
+
: {};
|
177
|
+
|
178
|
+
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
179
|
+
|
180
|
+
var createSubject = () => {
|
181
|
+
let _observers = [];
|
182
|
+
const next = (value) => {
|
183
|
+
for (const observer of _observers) {
|
184
|
+
observer.next && observer.next(value);
|
185
|
+
}
|
186
|
+
};
|
187
|
+
const subscribe = (observer) => {
|
188
|
+
_observers.push(observer);
|
189
|
+
return {
|
190
|
+
unsubscribe: () => {
|
191
|
+
_observers = _observers.filter((o) => o !== observer);
|
192
|
+
},
|
193
|
+
};
|
194
|
+
};
|
195
|
+
const unsubscribe = () => {
|
196
|
+
_observers = [];
|
197
|
+
};
|
198
|
+
return {
|
199
|
+
get observers() {
|
200
|
+
return _observers;
|
201
|
+
},
|
202
|
+
next,
|
203
|
+
subscribe,
|
204
|
+
unsubscribe,
|
205
|
+
};
|
206
|
+
};
|
207
|
+
|
208
|
+
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
209
|
+
|
210
|
+
function deepEqual(object1, object2) {
|
211
|
+
if (isPrimitive(object1) || isPrimitive(object2)) {
|
212
|
+
return object1 === object2;
|
213
|
+
}
|
214
|
+
if (isDateObject(object1) && isDateObject(object2)) {
|
215
|
+
return object1.getTime() === object2.getTime();
|
216
|
+
}
|
217
|
+
const keys1 = Object.keys(object1);
|
218
|
+
const keys2 = Object.keys(object2);
|
219
|
+
if (keys1.length !== keys2.length) {
|
220
|
+
return false;
|
221
|
+
}
|
222
|
+
for (const key of keys1) {
|
223
|
+
const val1 = object1[key];
|
224
|
+
if (!keys2.includes(key)) {
|
225
|
+
return false;
|
226
|
+
}
|
227
|
+
if (key !== 'ref') {
|
228
|
+
const val2 = object2[key];
|
229
|
+
if ((isDateObject(val1) && isDateObject(val2)) ||
|
230
|
+
(isObject(val1) && isObject(val2)) ||
|
231
|
+
(Array.isArray(val1) && Array.isArray(val2))
|
232
|
+
? !deepEqual(val1, val2)
|
233
|
+
: val1 !== val2) {
|
234
|
+
return false;
|
235
|
+
}
|
236
|
+
}
|
237
|
+
}
|
238
|
+
return true;
|
239
|
+
}
|
240
|
+
|
241
|
+
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
242
|
+
|
243
|
+
var isFileInput = (element) => element.type === 'file';
|
244
|
+
|
245
|
+
var isFunction = (value) => typeof value === 'function';
|
246
|
+
|
247
|
+
var isHTMLElement = (value) => {
|
248
|
+
if (!isWeb) {
|
249
|
+
return false;
|
250
|
+
}
|
251
|
+
const owner = value ? value.ownerDocument : 0;
|
252
|
+
return (value instanceof
|
253
|
+
(owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
|
254
|
+
};
|
255
|
+
|
256
|
+
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
257
|
+
|
258
|
+
var isRadioInput = (element) => element.type === 'radio';
|
259
|
+
|
260
|
+
var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
261
|
+
|
262
|
+
var live = (ref) => isHTMLElement(ref) && ref.isConnected;
|
263
|
+
|
264
|
+
function baseGet(object, updatePath) {
|
265
|
+
const length = updatePath.slice(0, -1).length;
|
266
|
+
let index = 0;
|
267
|
+
while (index < length) {
|
268
|
+
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
269
|
+
}
|
270
|
+
return object;
|
271
|
+
}
|
272
|
+
function isEmptyArray(obj) {
|
273
|
+
for (const key in obj) {
|
274
|
+
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
275
|
+
return false;
|
276
|
+
}
|
277
|
+
}
|
278
|
+
return true;
|
279
|
+
}
|
280
|
+
function unset(object, path) {
|
281
|
+
const paths = Array.isArray(path)
|
282
|
+
? path
|
283
|
+
: isKey(path)
|
284
|
+
? [path]
|
285
|
+
: stringToPath(path);
|
286
|
+
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
287
|
+
const index = paths.length - 1;
|
288
|
+
const key = paths[index];
|
289
|
+
if (childObject) {
|
290
|
+
delete childObject[key];
|
291
|
+
}
|
292
|
+
if (index !== 0 &&
|
293
|
+
((isObject(childObject) && isEmptyObject(childObject)) ||
|
294
|
+
(Array.isArray(childObject) && isEmptyArray(childObject)))) {
|
295
|
+
unset(object, paths.slice(0, -1));
|
296
|
+
}
|
297
|
+
return object;
|
298
|
+
}
|
299
|
+
|
300
|
+
var objectHasFunction = (data) => {
|
301
|
+
for (const key in data) {
|
302
|
+
if (isFunction(data[key])) {
|
303
|
+
return true;
|
304
|
+
}
|
305
|
+
}
|
306
|
+
return false;
|
307
|
+
};
|
308
|
+
|
309
|
+
function markFieldsDirty(data, fields = {}) {
|
310
|
+
const isParentNodeArray = Array.isArray(data);
|
311
|
+
if (isObject(data) || isParentNodeArray) {
|
312
|
+
for (const key in data) {
|
313
|
+
if (Array.isArray(data[key]) ||
|
314
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
315
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
316
|
+
markFieldsDirty(data[key], fields[key]);
|
317
|
+
}
|
318
|
+
else if (!isNullOrUndefined(data[key])) {
|
319
|
+
fields[key] = true;
|
320
|
+
}
|
321
|
+
}
|
322
|
+
}
|
323
|
+
return fields;
|
324
|
+
}
|
325
|
+
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
326
|
+
const isParentNodeArray = Array.isArray(data);
|
327
|
+
if (isObject(data) || isParentNodeArray) {
|
328
|
+
for (const key in data) {
|
329
|
+
if (Array.isArray(data[key]) ||
|
330
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
331
|
+
if (isUndefined(formValues) ||
|
332
|
+
isPrimitive(dirtyFieldsFromValues[key])) {
|
333
|
+
dirtyFieldsFromValues[key] = Array.isArray(data[key])
|
334
|
+
? markFieldsDirty(data[key], [])
|
335
|
+
: { ...markFieldsDirty(data[key]) };
|
336
|
+
}
|
337
|
+
else {
|
338
|
+
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
339
|
+
}
|
340
|
+
}
|
341
|
+
else {
|
342
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
343
|
+
}
|
344
|
+
}
|
345
|
+
}
|
346
|
+
return dirtyFieldsFromValues;
|
347
|
+
}
|
348
|
+
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
349
|
+
|
350
|
+
const defaultResult = {
|
351
|
+
value: false,
|
352
|
+
isValid: false,
|
353
|
+
};
|
354
|
+
const validResult = { value: true, isValid: true };
|
355
|
+
var getCheckboxValue = (options) => {
|
356
|
+
if (Array.isArray(options)) {
|
357
|
+
if (options.length > 1) {
|
358
|
+
const values = options
|
359
|
+
.filter((option) => option && option.checked && !option.disabled)
|
360
|
+
.map((option) => option.value);
|
361
|
+
return { value: values, isValid: !!values.length };
|
362
|
+
}
|
363
|
+
return options[0].checked && !options[0].disabled
|
364
|
+
? // @ts-expect-error expected to work in the browser
|
365
|
+
options[0].attributes && !isUndefined(options[0].attributes.value)
|
366
|
+
? isUndefined(options[0].value) || options[0].value === ''
|
367
|
+
? validResult
|
368
|
+
: { value: options[0].value, isValid: true }
|
369
|
+
: validResult
|
370
|
+
: defaultResult;
|
371
|
+
}
|
372
|
+
return defaultResult;
|
373
|
+
};
|
374
|
+
|
375
|
+
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value)
|
376
|
+
? value
|
377
|
+
: valueAsNumber
|
378
|
+
? value === ''
|
379
|
+
? NaN
|
380
|
+
: value
|
381
|
+
? +value
|
382
|
+
: value
|
383
|
+
: valueAsDate && isString(value)
|
384
|
+
? new Date(value)
|
385
|
+
: setValueAs
|
386
|
+
? setValueAs(value)
|
387
|
+
: value;
|
388
|
+
|
389
|
+
const defaultReturn = {
|
390
|
+
isValid: false,
|
391
|
+
value: null,
|
392
|
+
};
|
393
|
+
var getRadioValue = (options) => Array.isArray(options)
|
394
|
+
? options.reduce((previous, option) => option && option.checked && !option.disabled
|
395
|
+
? {
|
396
|
+
isValid: true,
|
397
|
+
value: option.value,
|
398
|
+
}
|
399
|
+
: previous, defaultReturn)
|
400
|
+
: defaultReturn;
|
401
|
+
|
402
|
+
function getFieldValue(_f) {
|
403
|
+
const ref = _f.ref;
|
404
|
+
if (isFileInput(ref)) {
|
405
|
+
return ref.files;
|
406
|
+
}
|
407
|
+
if (isRadioInput(ref)) {
|
408
|
+
return getRadioValue(_f.refs).value;
|
409
|
+
}
|
410
|
+
if (isMultipleSelect(ref)) {
|
411
|
+
return [...ref.selectedOptions].map(({ value }) => value);
|
412
|
+
}
|
413
|
+
if (isCheckBoxInput(ref)) {
|
414
|
+
return getCheckboxValue(_f.refs).value;
|
415
|
+
}
|
416
|
+
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
417
|
+
}
|
418
|
+
|
419
|
+
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
|
420
|
+
const fields = {};
|
421
|
+
for (const name of fieldsNames) {
|
422
|
+
const field = get(_fields, name);
|
423
|
+
field && set(fields, name, field._f);
|
424
|
+
}
|
425
|
+
return {
|
426
|
+
criteriaMode,
|
427
|
+
names: [...fieldsNames],
|
428
|
+
fields,
|
429
|
+
shouldUseNativeValidation,
|
430
|
+
};
|
431
|
+
};
|
432
|
+
|
433
|
+
var isRegex = (value) => value instanceof RegExp;
|
434
|
+
|
435
|
+
var getRuleValue = (rule) => isUndefined(rule)
|
436
|
+
? rule
|
437
|
+
: isRegex(rule)
|
438
|
+
? rule.source
|
439
|
+
: isObject(rule)
|
440
|
+
? isRegex(rule.value)
|
441
|
+
? rule.value.source
|
442
|
+
: rule.value
|
443
|
+
: rule;
|
444
|
+
|
445
|
+
var getValidationModes = (mode) => ({
|
446
|
+
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
447
|
+
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
448
|
+
isOnChange: mode === VALIDATION_MODE.onChange,
|
449
|
+
isOnAll: mode === VALIDATION_MODE.all,
|
450
|
+
isOnTouch: mode === VALIDATION_MODE.onTouched,
|
451
|
+
});
|
452
|
+
|
453
|
+
const ASYNC_FUNCTION = 'AsyncFunction';
|
454
|
+
var hasPromiseValidation = (fieldReference) => !!fieldReference &&
|
455
|
+
!!fieldReference.validate &&
|
456
|
+
!!((isFunction(fieldReference.validate) &&
|
457
|
+
fieldReference.validate.constructor.name === ASYNC_FUNCTION) ||
|
458
|
+
(isObject(fieldReference.validate) &&
|
459
|
+
Object.values(fieldReference.validate).find((validateFunction) => validateFunction.constructor.name === ASYNC_FUNCTION)));
|
460
|
+
|
461
|
+
var hasValidation = (options) => options.mount &&
|
462
|
+
(options.required ||
|
463
|
+
options.min ||
|
464
|
+
options.max ||
|
465
|
+
options.maxLength ||
|
466
|
+
options.minLength ||
|
467
|
+
options.pattern ||
|
468
|
+
options.validate);
|
469
|
+
|
470
|
+
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
471
|
+
(_names.watchAll ||
|
472
|
+
_names.watch.has(name) ||
|
473
|
+
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
474
|
+
/^\.\w+/.test(name.slice(watchName.length))));
|
475
|
+
|
476
|
+
const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
|
477
|
+
for (const key of fieldsNames || Object.keys(fields)) {
|
478
|
+
const field = get(fields, key);
|
479
|
+
if (field) {
|
480
|
+
const { _f, ...currentField } = field;
|
481
|
+
if (_f) {
|
482
|
+
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
|
483
|
+
return true;
|
484
|
+
}
|
485
|
+
else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
|
486
|
+
return true;
|
487
|
+
}
|
488
|
+
else {
|
489
|
+
if (iterateFieldsByAction(currentField, action)) {
|
490
|
+
break;
|
491
|
+
}
|
492
|
+
}
|
493
|
+
}
|
494
|
+
else if (isObject(currentField)) {
|
495
|
+
if (iterateFieldsByAction(currentField, action)) {
|
496
|
+
break;
|
497
|
+
}
|
498
|
+
}
|
499
|
+
}
|
500
|
+
}
|
501
|
+
return;
|
502
|
+
};
|
503
|
+
|
504
|
+
function schemaErrorLookup(errors, _fields, name) {
|
505
|
+
const error = get(errors, name);
|
506
|
+
if (error || isKey(name)) {
|
507
|
+
return {
|
508
|
+
error,
|
509
|
+
name,
|
510
|
+
};
|
511
|
+
}
|
512
|
+
const names = name.split('.');
|
513
|
+
while (names.length) {
|
514
|
+
const fieldName = names.join('.');
|
515
|
+
const field = get(_fields, fieldName);
|
516
|
+
const foundError = get(errors, fieldName);
|
517
|
+
if (field && !Array.isArray(field) && name !== fieldName) {
|
518
|
+
return { name };
|
519
|
+
}
|
520
|
+
if (foundError && foundError.type) {
|
521
|
+
return {
|
522
|
+
name: fieldName,
|
523
|
+
error: foundError,
|
524
|
+
};
|
525
|
+
}
|
526
|
+
names.pop();
|
527
|
+
}
|
528
|
+
return {
|
529
|
+
name,
|
530
|
+
};
|
531
|
+
}
|
532
|
+
|
533
|
+
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
534
|
+
updateFormState(formStateData);
|
535
|
+
const { name, ...formState } = formStateData;
|
536
|
+
return (isEmptyObject(formState) ||
|
537
|
+
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
538
|
+
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
539
|
+
(!isRoot || VALIDATION_MODE.all)));
|
540
|
+
};
|
541
|
+
|
542
|
+
var shouldSubscribeByName = (name, signalName, exact) => !name ||
|
543
|
+
!signalName ||
|
544
|
+
name === signalName ||
|
545
|
+
convertToArrayPayload(name).some((currentName) => currentName &&
|
546
|
+
(exact
|
547
|
+
? currentName === signalName
|
548
|
+
: currentName.startsWith(signalName) ||
|
549
|
+
signalName.startsWith(currentName)));
|
550
|
+
|
551
|
+
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
552
|
+
if (mode.isOnAll) {
|
553
|
+
return false;
|
554
|
+
}
|
555
|
+
else if (!isSubmitted && mode.isOnTouch) {
|
556
|
+
return !(isTouched || isBlurEvent);
|
557
|
+
}
|
558
|
+
else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
559
|
+
return !isBlurEvent;
|
560
|
+
}
|
561
|
+
else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
562
|
+
return isBlurEvent;
|
563
|
+
}
|
564
|
+
return true;
|
565
|
+
};
|
566
|
+
|
567
|
+
var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
|
568
|
+
|
569
|
+
var updateFieldArrayRootError = (errors, error, name) => {
|
570
|
+
const fieldArrayErrors = convertToArrayPayload(get(errors, name));
|
571
|
+
set(fieldArrayErrors, 'root', error[name]);
|
572
|
+
set(errors, name, fieldArrayErrors);
|
573
|
+
return errors;
|
574
|
+
};
|
575
|
+
|
576
|
+
var isMessage = (value) => isString(value);
|
577
|
+
|
578
|
+
function getValidateError(result, ref, type = 'validate') {
|
579
|
+
if (isMessage(result) ||
|
580
|
+
(Array.isArray(result) && result.every(isMessage)) ||
|
581
|
+
(isBoolean(result) && !result)) {
|
582
|
+
return {
|
583
|
+
type,
|
584
|
+
message: isMessage(result) ? result : '',
|
585
|
+
ref,
|
586
|
+
};
|
587
|
+
}
|
588
|
+
}
|
589
|
+
|
590
|
+
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData)
|
591
|
+
? validationData
|
592
|
+
: {
|
593
|
+
value: validationData,
|
594
|
+
message: '',
|
595
|
+
};
|
596
|
+
|
597
|
+
var validateField = async (field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
598
|
+
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, } = field._f;
|
599
|
+
const inputValue = get(formValues, name);
|
600
|
+
if (!mount || disabledFieldNames.has(name)) {
|
601
|
+
return {};
|
602
|
+
}
|
603
|
+
const inputRef = refs ? refs[0] : ref;
|
604
|
+
const setCustomValidity = (message) => {
|
605
|
+
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
606
|
+
inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
|
607
|
+
inputRef.reportValidity();
|
608
|
+
}
|
609
|
+
};
|
610
|
+
const error = {};
|
611
|
+
const isRadio = isRadioInput(ref);
|
612
|
+
const isCheckBox = isCheckBoxInput(ref);
|
613
|
+
const isRadioOrCheckbox = isRadio || isCheckBox;
|
614
|
+
const isEmpty = ((valueAsNumber || isFileInput(ref)) &&
|
615
|
+
isUndefined(ref.value) &&
|
616
|
+
isUndefined(inputValue)) ||
|
617
|
+
(isHTMLElement(ref) && ref.value === '') ||
|
618
|
+
inputValue === '' ||
|
619
|
+
(Array.isArray(inputValue) && !inputValue.length);
|
620
|
+
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
621
|
+
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
622
|
+
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
623
|
+
error[name] = {
|
624
|
+
type: exceedMax ? maxType : minType,
|
625
|
+
message,
|
626
|
+
ref,
|
627
|
+
...appendErrorsCurry(exceedMax ? maxType : minType, message),
|
628
|
+
};
|
629
|
+
};
|
630
|
+
if (isFieldArray
|
631
|
+
? !Array.isArray(inputValue) || !inputValue.length
|
632
|
+
: required &&
|
633
|
+
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
634
|
+
(isBoolean(inputValue) && !inputValue) ||
|
635
|
+
(isCheckBox && !getCheckboxValue(refs).isValid) ||
|
636
|
+
(isRadio && !getRadioValue(refs).isValid))) {
|
637
|
+
const { value, message } = isMessage(required)
|
638
|
+
? { value: !!required, message: required }
|
639
|
+
: getValueAndMessage(required);
|
640
|
+
if (value) {
|
641
|
+
error[name] = {
|
642
|
+
type: INPUT_VALIDATION_RULES.required,
|
643
|
+
message,
|
644
|
+
ref: inputRef,
|
645
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
|
646
|
+
};
|
647
|
+
if (!validateAllFieldCriteria) {
|
648
|
+
setCustomValidity(message);
|
649
|
+
return error;
|
650
|
+
}
|
651
|
+
}
|
652
|
+
}
|
653
|
+
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
654
|
+
let exceedMax;
|
655
|
+
let exceedMin;
|
656
|
+
const maxOutput = getValueAndMessage(max);
|
657
|
+
const minOutput = getValueAndMessage(min);
|
658
|
+
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
659
|
+
const valueNumber = ref.valueAsNumber ||
|
660
|
+
(inputValue ? +inputValue : inputValue);
|
661
|
+
if (!isNullOrUndefined(maxOutput.value)) {
|
662
|
+
exceedMax = valueNumber > maxOutput.value;
|
663
|
+
}
|
664
|
+
if (!isNullOrUndefined(minOutput.value)) {
|
665
|
+
exceedMin = valueNumber < minOutput.value;
|
666
|
+
}
|
667
|
+
}
|
668
|
+
else {
|
669
|
+
const valueDate = ref.valueAsDate || new Date(inputValue);
|
670
|
+
const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
|
671
|
+
const isTime = ref.type == 'time';
|
672
|
+
const isWeek = ref.type == 'week';
|
673
|
+
if (isString(maxOutput.value) && inputValue) {
|
674
|
+
exceedMax = isTime
|
675
|
+
? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
|
676
|
+
: isWeek
|
677
|
+
? inputValue > maxOutput.value
|
678
|
+
: valueDate > new Date(maxOutput.value);
|
679
|
+
}
|
680
|
+
if (isString(minOutput.value) && inputValue) {
|
681
|
+
exceedMin = isTime
|
682
|
+
? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
|
683
|
+
: isWeek
|
684
|
+
? inputValue < minOutput.value
|
685
|
+
: valueDate < new Date(minOutput.value);
|
686
|
+
}
|
687
|
+
}
|
688
|
+
if (exceedMax || exceedMin) {
|
689
|
+
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
690
|
+
if (!validateAllFieldCriteria) {
|
691
|
+
setCustomValidity(error[name].message);
|
692
|
+
return error;
|
693
|
+
}
|
694
|
+
}
|
695
|
+
}
|
696
|
+
if ((maxLength || minLength) &&
|
697
|
+
!isEmpty &&
|
698
|
+
(isString(inputValue) || (isFieldArray && Array.isArray(inputValue)))) {
|
699
|
+
const maxLengthOutput = getValueAndMessage(maxLength);
|
700
|
+
const minLengthOutput = getValueAndMessage(minLength);
|
701
|
+
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
|
702
|
+
inputValue.length > +maxLengthOutput.value;
|
703
|
+
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
|
704
|
+
inputValue.length < +minLengthOutput.value;
|
705
|
+
if (exceedMax || exceedMin) {
|
706
|
+
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
707
|
+
if (!validateAllFieldCriteria) {
|
708
|
+
setCustomValidity(error[name].message);
|
709
|
+
return error;
|
710
|
+
}
|
711
|
+
}
|
712
|
+
}
|
713
|
+
if (pattern && !isEmpty && isString(inputValue)) {
|
714
|
+
const { value: patternValue, message } = getValueAndMessage(pattern);
|
715
|
+
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
716
|
+
error[name] = {
|
717
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
718
|
+
message,
|
719
|
+
ref,
|
720
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
|
721
|
+
};
|
722
|
+
if (!validateAllFieldCriteria) {
|
723
|
+
setCustomValidity(message);
|
724
|
+
return error;
|
725
|
+
}
|
726
|
+
}
|
727
|
+
}
|
728
|
+
if (validate) {
|
729
|
+
if (isFunction(validate)) {
|
730
|
+
const result = await validate(inputValue, formValues);
|
731
|
+
const validateError = getValidateError(result, inputRef);
|
732
|
+
if (validateError) {
|
733
|
+
error[name] = {
|
734
|
+
...validateError,
|
735
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
|
736
|
+
};
|
737
|
+
if (!validateAllFieldCriteria) {
|
738
|
+
setCustomValidity(validateError.message);
|
739
|
+
return error;
|
740
|
+
}
|
741
|
+
}
|
742
|
+
}
|
743
|
+
else if (isObject(validate)) {
|
744
|
+
let validationResult = {};
|
745
|
+
for (const key in validate) {
|
746
|
+
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
747
|
+
break;
|
748
|
+
}
|
749
|
+
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
750
|
+
if (validateError) {
|
751
|
+
validationResult = {
|
752
|
+
...validateError,
|
753
|
+
...appendErrorsCurry(key, validateError.message),
|
754
|
+
};
|
755
|
+
setCustomValidity(validateError.message);
|
756
|
+
if (validateAllFieldCriteria) {
|
757
|
+
error[name] = validationResult;
|
758
|
+
}
|
759
|
+
}
|
760
|
+
}
|
761
|
+
if (!isEmptyObject(validationResult)) {
|
762
|
+
error[name] = {
|
763
|
+
ref: inputRef,
|
764
|
+
...validationResult,
|
765
|
+
};
|
766
|
+
if (!validateAllFieldCriteria) {
|
767
|
+
return error;
|
768
|
+
}
|
769
|
+
}
|
770
|
+
}
|
771
|
+
}
|
772
|
+
setCustomValidity(true);
|
773
|
+
return error;
|
774
|
+
};
|
775
|
+
|
776
|
+
const defaultOptions = {
|
777
|
+
mode: VALIDATION_MODE.onSubmit,
|
778
|
+
reValidateMode: VALIDATION_MODE.onChange,
|
779
|
+
shouldFocusError: true,
|
780
|
+
};
|
781
|
+
function createFormControl(props = {}) {
|
782
|
+
let _options = {
|
783
|
+
...defaultOptions,
|
784
|
+
...props,
|
785
|
+
};
|
786
|
+
let _formState = {
|
787
|
+
submitCount: 0,
|
788
|
+
isDirty: false,
|
789
|
+
isLoading: isFunction(_options.defaultValues),
|
790
|
+
isValidating: false,
|
791
|
+
isSubmitted: false,
|
792
|
+
isSubmitting: false,
|
793
|
+
isSubmitSuccessful: false,
|
794
|
+
isValid: false,
|
795
|
+
touchedFields: {},
|
796
|
+
dirtyFields: {},
|
797
|
+
validatingFields: {},
|
798
|
+
errors: _options.errors || {},
|
799
|
+
disabled: _options.disabled || false,
|
800
|
+
};
|
801
|
+
const _fields = {};
|
802
|
+
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
|
803
|
+
? cloneObject(_options.values || _options.defaultValues) || {}
|
804
|
+
: {};
|
805
|
+
let _formValues = _options.shouldUnregister
|
806
|
+
? {}
|
807
|
+
: cloneObject(_defaultValues);
|
808
|
+
let _state = {
|
809
|
+
action: false,
|
810
|
+
mount: false,
|
811
|
+
watch: false,
|
812
|
+
};
|
813
|
+
let _names = {
|
814
|
+
mount: new Set(),
|
815
|
+
disabled: new Set(),
|
816
|
+
unMount: new Set(),
|
817
|
+
array: new Set(),
|
818
|
+
watch: new Set(),
|
819
|
+
};
|
820
|
+
let delayErrorCallback;
|
821
|
+
let timer = 0;
|
822
|
+
const _proxyFormState = {
|
823
|
+
isDirty: false,
|
824
|
+
dirtyFields: false,
|
825
|
+
validatingFields: false,
|
826
|
+
touchedFields: false,
|
827
|
+
isValidating: false,
|
828
|
+
isValid: false,
|
829
|
+
errors: false,
|
830
|
+
};
|
831
|
+
let _proxySubscribeFormState = {
|
832
|
+
..._proxyFormState,
|
833
|
+
};
|
834
|
+
const _subjects = {
|
835
|
+
array: createSubject(),
|
836
|
+
state: createSubject(),
|
837
|
+
};
|
838
|
+
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
839
|
+
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
840
|
+
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
841
|
+
const debounce = (callback) => (wait) => {
|
842
|
+
clearTimeout(timer);
|
843
|
+
timer = setTimeout(callback, wait);
|
844
|
+
};
|
845
|
+
const _setValid = async (shouldUpdateValid) => {
|
846
|
+
if (!_options.disabled &&
|
847
|
+
(_proxyFormState.isValid ||
|
848
|
+
_proxySubscribeFormState.isValid ||
|
849
|
+
shouldUpdateValid)) {
|
850
|
+
const isValid = _options.resolver
|
851
|
+
? isEmptyObject((await _runSchema()).errors)
|
852
|
+
: await executeBuiltInValidation(_fields, true);
|
853
|
+
if (isValid !== _formState.isValid) {
|
854
|
+
_subjects.state.next({
|
855
|
+
isValid,
|
856
|
+
});
|
857
|
+
}
|
858
|
+
}
|
859
|
+
};
|
860
|
+
const _updateIsValidating = (names, isValidating) => {
|
861
|
+
if (!_options.disabled &&
|
862
|
+
(_proxyFormState.isValidating ||
|
863
|
+
_proxyFormState.validatingFields ||
|
864
|
+
_proxySubscribeFormState.isValidating ||
|
865
|
+
_proxySubscribeFormState.validatingFields)) {
|
866
|
+
(names || Array.from(_names.mount)).forEach((name) => {
|
867
|
+
if (name) {
|
868
|
+
isValidating
|
869
|
+
? set(_formState.validatingFields, name, isValidating)
|
870
|
+
: unset(_formState.validatingFields, name);
|
871
|
+
}
|
872
|
+
});
|
873
|
+
_subjects.state.next({
|
874
|
+
validatingFields: _formState.validatingFields,
|
875
|
+
isValidating: !isEmptyObject(_formState.validatingFields),
|
876
|
+
});
|
877
|
+
}
|
878
|
+
};
|
879
|
+
const _setFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
880
|
+
if (args && method && !_options.disabled) {
|
881
|
+
_state.action = true;
|
882
|
+
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
883
|
+
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
884
|
+
shouldSetValues && set(_fields, name, fieldValues);
|
885
|
+
}
|
886
|
+
if (shouldUpdateFieldsAndState &&
|
887
|
+
Array.isArray(get(_formState.errors, name))) {
|
888
|
+
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
889
|
+
shouldSetValues && set(_formState.errors, name, errors);
|
890
|
+
unsetEmptyArray(_formState.errors, name);
|
891
|
+
}
|
892
|
+
if ((_proxyFormState.touchedFields ||
|
893
|
+
_proxySubscribeFormState.touchedFields) &&
|
894
|
+
shouldUpdateFieldsAndState &&
|
895
|
+
Array.isArray(get(_formState.touchedFields, name))) {
|
896
|
+
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
897
|
+
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
898
|
+
}
|
899
|
+
if (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) {
|
900
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
901
|
+
}
|
902
|
+
_subjects.state.next({
|
903
|
+
name,
|
904
|
+
isDirty: _getDirty(name, values),
|
905
|
+
dirtyFields: _formState.dirtyFields,
|
906
|
+
errors: _formState.errors,
|
907
|
+
isValid: _formState.isValid,
|
908
|
+
});
|
909
|
+
}
|
910
|
+
else {
|
911
|
+
set(_formValues, name, values);
|
912
|
+
}
|
913
|
+
};
|
914
|
+
const updateErrors = (name, error) => {
|
915
|
+
set(_formState.errors, name, error);
|
916
|
+
_subjects.state.next({
|
917
|
+
errors: _formState.errors,
|
918
|
+
});
|
919
|
+
};
|
920
|
+
const _setErrors = (errors) => {
|
921
|
+
_formState.errors = errors;
|
922
|
+
_subjects.state.next({
|
923
|
+
errors: _formState.errors,
|
924
|
+
isValid: false,
|
925
|
+
});
|
926
|
+
};
|
927
|
+
const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
|
928
|
+
const field = get(_fields, name);
|
929
|
+
if (field) {
|
930
|
+
const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
|
931
|
+
isUndefined(defaultValue) ||
|
932
|
+
(ref && ref.defaultChecked) ||
|
933
|
+
shouldSkipSetValueAs
|
934
|
+
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
|
935
|
+
: setFieldValue(name, defaultValue);
|
936
|
+
_state.mount && _setValid();
|
937
|
+
}
|
938
|
+
};
|
939
|
+
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
940
|
+
let shouldUpdateField = false;
|
941
|
+
let isPreviousDirty = false;
|
942
|
+
const output = {
|
943
|
+
name,
|
944
|
+
};
|
945
|
+
if (!_options.disabled) {
|
946
|
+
if (!isBlurEvent || shouldDirty) {
|
947
|
+
if (_proxyFormState.isDirty || _proxySubscribeFormState.isDirty) {
|
948
|
+
isPreviousDirty = _formState.isDirty;
|
949
|
+
_formState.isDirty = output.isDirty = _getDirty();
|
950
|
+
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
951
|
+
}
|
952
|
+
const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
|
953
|
+
isPreviousDirty = !!get(_formState.dirtyFields, name);
|
954
|
+
isCurrentFieldPristine
|
955
|
+
? unset(_formState.dirtyFields, name)
|
956
|
+
: set(_formState.dirtyFields, name, true);
|
957
|
+
output.dirtyFields = _formState.dirtyFields;
|
958
|
+
shouldUpdateField =
|
959
|
+
shouldUpdateField ||
|
960
|
+
((_proxyFormState.dirtyFields ||
|
961
|
+
_proxySubscribeFormState.dirtyFields) &&
|
962
|
+
isPreviousDirty !== !isCurrentFieldPristine);
|
963
|
+
}
|
964
|
+
if (isBlurEvent) {
|
965
|
+
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
966
|
+
if (!isPreviousFieldTouched) {
|
967
|
+
set(_formState.touchedFields, name, isBlurEvent);
|
968
|
+
output.touchedFields = _formState.touchedFields;
|
969
|
+
shouldUpdateField =
|
970
|
+
shouldUpdateField ||
|
971
|
+
((_proxyFormState.touchedFields ||
|
972
|
+
_proxySubscribeFormState.touchedFields) &&
|
973
|
+
isPreviousFieldTouched !== isBlurEvent);
|
974
|
+
}
|
975
|
+
}
|
976
|
+
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
977
|
+
}
|
978
|
+
return shouldUpdateField ? output : {};
|
979
|
+
};
|
980
|
+
const shouldRenderByError = (name, isValid, error, fieldState) => {
|
981
|
+
const previousFieldError = get(_formState.errors, name);
|
982
|
+
const shouldUpdateValid = (_proxyFormState.isValid || _proxySubscribeFormState.isValid) &&
|
983
|
+
isBoolean(isValid) &&
|
984
|
+
_formState.isValid !== isValid;
|
985
|
+
if (_options.delayError && error) {
|
986
|
+
delayErrorCallback = debounce(() => updateErrors(name, error));
|
987
|
+
delayErrorCallback(_options.delayError);
|
988
|
+
}
|
989
|
+
else {
|
990
|
+
clearTimeout(timer);
|
991
|
+
delayErrorCallback = null;
|
992
|
+
error
|
993
|
+
? set(_formState.errors, name, error)
|
994
|
+
: unset(_formState.errors, name);
|
995
|
+
}
|
996
|
+
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) ||
|
997
|
+
!isEmptyObject(fieldState) ||
|
998
|
+
shouldUpdateValid) {
|
999
|
+
const updatedFormState = {
|
1000
|
+
...fieldState,
|
1001
|
+
...(shouldUpdateValid && isBoolean(isValid) ? { isValid } : {}),
|
1002
|
+
errors: _formState.errors,
|
1003
|
+
name,
|
1004
|
+
};
|
1005
|
+
_formState = {
|
1006
|
+
..._formState,
|
1007
|
+
...updatedFormState,
|
1008
|
+
};
|
1009
|
+
_subjects.state.next(updatedFormState);
|
1010
|
+
}
|
1011
|
+
};
|
1012
|
+
const _runSchema = async (name) => {
|
1013
|
+
_updateIsValidating(name, true);
|
1014
|
+
const result = await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
1015
|
+
_updateIsValidating(name);
|
1016
|
+
return result;
|
1017
|
+
};
|
1018
|
+
const executeSchemaAndUpdateState = async (names) => {
|
1019
|
+
const { errors } = await _runSchema(names);
|
1020
|
+
if (names) {
|
1021
|
+
for (const name of names) {
|
1022
|
+
const error = get(errors, name);
|
1023
|
+
error
|
1024
|
+
? set(_formState.errors, name, error)
|
1025
|
+
: unset(_formState.errors, name);
|
1026
|
+
}
|
1027
|
+
}
|
1028
|
+
else {
|
1029
|
+
_formState.errors = errors;
|
1030
|
+
}
|
1031
|
+
return errors;
|
1032
|
+
};
|
1033
|
+
const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
|
1034
|
+
valid: true,
|
1035
|
+
}) => {
|
1036
|
+
for (const name in fields) {
|
1037
|
+
const field = fields[name];
|
1038
|
+
if (field) {
|
1039
|
+
const { _f, ...fieldValue } = field;
|
1040
|
+
if (_f) {
|
1041
|
+
const isFieldArrayRoot = _names.array.has(_f.name);
|
1042
|
+
const isPromiseFunction = field._f && hasPromiseValidation(field._f);
|
1043
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
1044
|
+
_updateIsValidating([name], true);
|
1045
|
+
}
|
1046
|
+
const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
1047
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
1048
|
+
_updateIsValidating([name]);
|
1049
|
+
}
|
1050
|
+
if (fieldError[_f.name]) {
|
1051
|
+
context.valid = false;
|
1052
|
+
if (shouldOnlyCheckValid) {
|
1053
|
+
break;
|
1054
|
+
}
|
1055
|
+
}
|
1056
|
+
!shouldOnlyCheckValid &&
|
1057
|
+
(get(fieldError, _f.name)
|
1058
|
+
? isFieldArrayRoot
|
1059
|
+
? updateFieldArrayRootError(_formState.errors, fieldError, _f.name)
|
1060
|
+
: set(_formState.errors, _f.name, fieldError[_f.name])
|
1061
|
+
: unset(_formState.errors, _f.name));
|
1062
|
+
}
|
1063
|
+
!isEmptyObject(fieldValue) &&
|
1064
|
+
(await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
|
1065
|
+
}
|
1066
|
+
}
|
1067
|
+
return context.valid;
|
1068
|
+
};
|
1069
|
+
const _removeUnmounted = () => {
|
1070
|
+
for (const name of _names.unMount) {
|
1071
|
+
const field = get(_fields, name);
|
1072
|
+
field &&
|
1073
|
+
(field._f.refs
|
1074
|
+
? field._f.refs.every((ref) => !live(ref))
|
1075
|
+
: !live(field._f.ref)) &&
|
1076
|
+
unregister(name);
|
1077
|
+
}
|
1078
|
+
_names.unMount = new Set();
|
1079
|
+
};
|
1080
|
+
const _getDirty = (name, data) => !_options.disabled &&
|
1081
|
+
(name && data && set(_formValues, name, data),
|
1082
|
+
!deepEqual(getValues(), _defaultValues));
|
1083
|
+
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
1084
|
+
...(_state.mount
|
1085
|
+
? _formValues
|
1086
|
+
: isUndefined(defaultValue)
|
1087
|
+
? _defaultValues
|
1088
|
+
: isString(names)
|
1089
|
+
? { [names]: defaultValue }
|
1090
|
+
: defaultValue),
|
1091
|
+
}, isGlobal, defaultValue);
|
1092
|
+
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
|
1093
|
+
const setFieldValue = (name, value, options = {}) => {
|
1094
|
+
const field = get(_fields, name);
|
1095
|
+
let fieldValue = value;
|
1096
|
+
if (field) {
|
1097
|
+
const fieldReference = field._f;
|
1098
|
+
if (fieldReference) {
|
1099
|
+
!fieldReference.disabled &&
|
1100
|
+
set(_formValues, name, getFieldValueAs(value, fieldReference));
|
1101
|
+
fieldValue =
|
1102
|
+
isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
|
1103
|
+
? ''
|
1104
|
+
: value;
|
1105
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
1106
|
+
[...fieldReference.ref.options].forEach((optionRef) => (optionRef.selected = fieldValue.includes(optionRef.value)));
|
1107
|
+
}
|
1108
|
+
else if (fieldReference.refs) {
|
1109
|
+
if (isCheckBoxInput(fieldReference.ref)) {
|
1110
|
+
fieldReference.refs.length > 1
|
1111
|
+
? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !checkboxRef.disabled) &&
|
1112
|
+
(checkboxRef.checked = Array.isArray(fieldValue)
|
1113
|
+
? !!fieldValue.find((data) => data === checkboxRef.value)
|
1114
|
+
: fieldValue === checkboxRef.value))
|
1115
|
+
: fieldReference.refs[0] &&
|
1116
|
+
(fieldReference.refs[0].checked = !!fieldValue);
|
1117
|
+
}
|
1118
|
+
else {
|
1119
|
+
fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
|
1120
|
+
}
|
1121
|
+
}
|
1122
|
+
else if (isFileInput(fieldReference.ref)) {
|
1123
|
+
fieldReference.ref.value = '';
|
1124
|
+
}
|
1125
|
+
else {
|
1126
|
+
fieldReference.ref.value = fieldValue;
|
1127
|
+
if (!fieldReference.ref.type) {
|
1128
|
+
_subjects.state.next({
|
1129
|
+
name,
|
1130
|
+
values: cloneObject(_formValues),
|
1131
|
+
});
|
1132
|
+
}
|
1133
|
+
}
|
1134
|
+
}
|
1135
|
+
}
|
1136
|
+
(options.shouldDirty || options.shouldTouch) &&
|
1137
|
+
updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
1138
|
+
options.shouldValidate && trigger(name);
|
1139
|
+
};
|
1140
|
+
const setValues = (name, value, options) => {
|
1141
|
+
for (const fieldKey in value) {
|
1142
|
+
const fieldValue = value[fieldKey];
|
1143
|
+
const fieldName = `${name}.${fieldKey}`;
|
1144
|
+
const field = get(_fields, fieldName);
|
1145
|
+
(_names.array.has(name) ||
|
1146
|
+
isObject(fieldValue) ||
|
1147
|
+
(field && !field._f)) &&
|
1148
|
+
!isDateObject(fieldValue)
|
1149
|
+
? setValues(fieldName, fieldValue, options)
|
1150
|
+
: setFieldValue(fieldName, fieldValue, options);
|
1151
|
+
}
|
1152
|
+
};
|
1153
|
+
const setValue = (name, value, options = {}) => {
|
1154
|
+
const field = get(_fields, name);
|
1155
|
+
const isFieldArray = _names.array.has(name);
|
1156
|
+
const cloneValue = cloneObject(value);
|
1157
|
+
set(_formValues, name, cloneValue);
|
1158
|
+
if (isFieldArray) {
|
1159
|
+
_subjects.array.next({
|
1160
|
+
name,
|
1161
|
+
values: cloneObject(_formValues),
|
1162
|
+
});
|
1163
|
+
if ((_proxyFormState.isDirty ||
|
1164
|
+
_proxyFormState.dirtyFields ||
|
1165
|
+
_proxySubscribeFormState.isDirty ||
|
1166
|
+
_proxySubscribeFormState.dirtyFields) &&
|
1167
|
+
options.shouldDirty) {
|
1168
|
+
_subjects.state.next({
|
1169
|
+
name,
|
1170
|
+
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
1171
|
+
isDirty: _getDirty(name, cloneValue),
|
1172
|
+
});
|
1173
|
+
}
|
1174
|
+
}
|
1175
|
+
else {
|
1176
|
+
field && !field._f && !isNullOrUndefined(cloneValue)
|
1177
|
+
? setValues(name, cloneValue, options)
|
1178
|
+
: setFieldValue(name, cloneValue, options);
|
1179
|
+
}
|
1180
|
+
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
1181
|
+
_subjects.state.next({
|
1182
|
+
name: _state.mount ? name : undefined,
|
1183
|
+
values: cloneObject(_formValues),
|
1184
|
+
});
|
1185
|
+
};
|
1186
|
+
const onChange = async (event) => {
|
1187
|
+
_state.mount = true;
|
1188
|
+
const target = event.target;
|
1189
|
+
let name = target.name;
|
1190
|
+
let isFieldValueUpdated = true;
|
1191
|
+
const field = get(_fields, name);
|
1192
|
+
const _updateIsFieldValueUpdated = (fieldValue) => {
|
1193
|
+
isFieldValueUpdated =
|
1194
|
+
Number.isNaN(fieldValue) ||
|
1195
|
+
(isDateObject(fieldValue) && isNaN(fieldValue.getTime())) ||
|
1196
|
+
deepEqual(fieldValue, get(_formValues, name, fieldValue));
|
1197
|
+
};
|
1198
|
+
if (field) {
|
1199
|
+
let error;
|
1200
|
+
let isValid;
|
1201
|
+
const fieldValue = target.type
|
1202
|
+
? getFieldValue(field._f)
|
1203
|
+
: getEventValue(event);
|
1204
|
+
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
1205
|
+
const shouldSkipValidation = (!hasValidation(field._f) &&
|
1206
|
+
!_options.resolver &&
|
1207
|
+
!get(_formState.errors, name) &&
|
1208
|
+
!field._f.deps) ||
|
1209
|
+
skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
1210
|
+
const watched = isWatched(name, _names, isBlurEvent);
|
1211
|
+
set(_formValues, name, fieldValue);
|
1212
|
+
if (isBlurEvent) {
|
1213
|
+
field._f.onBlur && field._f.onBlur(event);
|
1214
|
+
delayErrorCallback && delayErrorCallback(0);
|
1215
|
+
}
|
1216
|
+
else if (field._f.onChange) {
|
1217
|
+
field._f.onChange(event);
|
1218
|
+
}
|
1219
|
+
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent);
|
1220
|
+
const shouldRender = !isEmptyObject(fieldState) || watched;
|
1221
|
+
!isBlurEvent &&
|
1222
|
+
_subjects.state.next({
|
1223
|
+
name,
|
1224
|
+
type: event.type,
|
1225
|
+
values: cloneObject(_formValues),
|
1226
|
+
});
|
1227
|
+
if (shouldSkipValidation) {
|
1228
|
+
if (_proxyFormState.isValid || _proxySubscribeFormState.isValid) {
|
1229
|
+
if (_options.mode === 'onBlur') {
|
1230
|
+
if (isBlurEvent) {
|
1231
|
+
_setValid();
|
1232
|
+
}
|
1233
|
+
}
|
1234
|
+
else if (!isBlurEvent) {
|
1235
|
+
_setValid();
|
1236
|
+
}
|
1237
|
+
}
|
1238
|
+
return (shouldRender &&
|
1239
|
+
_subjects.state.next({ name, ...(watched ? {} : fieldState) }));
|
1240
|
+
}
|
1241
|
+
!isBlurEvent && watched && _subjects.state.next({ ..._formState });
|
1242
|
+
if (_options.resolver) {
|
1243
|
+
const { errors } = await _runSchema([name]);
|
1244
|
+
_updateIsFieldValueUpdated(fieldValue);
|
1245
|
+
if (isFieldValueUpdated) {
|
1246
|
+
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
1247
|
+
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
1248
|
+
error = errorLookupResult.error;
|
1249
|
+
name = errorLookupResult.name;
|
1250
|
+
isValid = isEmptyObject(errors);
|
1251
|
+
}
|
1252
|
+
}
|
1253
|
+
else {
|
1254
|
+
_updateIsValidating([name], true);
|
1255
|
+
error = (await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
1256
|
+
_updateIsValidating([name]);
|
1257
|
+
_updateIsFieldValueUpdated(fieldValue);
|
1258
|
+
if (isFieldValueUpdated) {
|
1259
|
+
if (error) {
|
1260
|
+
isValid = false;
|
1261
|
+
}
|
1262
|
+
else if (_proxyFormState.isValid ||
|
1263
|
+
_proxySubscribeFormState.isValid) {
|
1264
|
+
isValid = await executeBuiltInValidation(_fields, true);
|
1265
|
+
}
|
1266
|
+
}
|
1267
|
+
}
|
1268
|
+
if (isFieldValueUpdated) {
|
1269
|
+
field._f.deps &&
|
1270
|
+
trigger(field._f.deps);
|
1271
|
+
shouldRenderByError(name, isValid, error, fieldState);
|
1272
|
+
}
|
1273
|
+
}
|
1274
|
+
};
|
1275
|
+
const _focusInput = (ref, key) => {
|
1276
|
+
if (get(_formState.errors, key) && ref.focus) {
|
1277
|
+
ref.focus();
|
1278
|
+
return 1;
|
1279
|
+
}
|
1280
|
+
return;
|
1281
|
+
};
|
1282
|
+
const trigger = async (name, options = {}) => {
|
1283
|
+
let isValid;
|
1284
|
+
let validationResult;
|
1285
|
+
const fieldNames = convertToArrayPayload(name);
|
1286
|
+
if (_options.resolver) {
|
1287
|
+
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
1288
|
+
isValid = isEmptyObject(errors);
|
1289
|
+
validationResult = name
|
1290
|
+
? !fieldNames.some((name) => get(errors, name))
|
1291
|
+
: isValid;
|
1292
|
+
}
|
1293
|
+
else if (name) {
|
1294
|
+
validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
|
1295
|
+
const field = get(_fields, fieldName);
|
1296
|
+
return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
|
1297
|
+
}))).every(Boolean);
|
1298
|
+
!(!validationResult && !_formState.isValid) && _setValid();
|
1299
|
+
}
|
1300
|
+
else {
|
1301
|
+
validationResult = isValid = await executeBuiltInValidation(_fields);
|
1302
|
+
}
|
1303
|
+
_subjects.state.next({
|
1304
|
+
...(!isString(name) ||
|
1305
|
+
((_proxyFormState.isValid || _proxySubscribeFormState.isValid) &&
|
1306
|
+
isValid !== _formState.isValid)
|
1307
|
+
? {}
|
1308
|
+
: { name }),
|
1309
|
+
...(_options.resolver || !name ? { isValid } : {}),
|
1310
|
+
errors: _formState.errors,
|
1311
|
+
});
|
1312
|
+
options.shouldFocus &&
|
1313
|
+
!validationResult &&
|
1314
|
+
iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
|
1315
|
+
return validationResult;
|
1316
|
+
};
|
1317
|
+
const getValues = (fieldNames) => {
|
1318
|
+
const values = {
|
1319
|
+
...(_state.mount ? _formValues : _defaultValues),
|
1320
|
+
};
|
1321
|
+
return isUndefined(fieldNames)
|
1322
|
+
? values
|
1323
|
+
: isString(fieldNames)
|
1324
|
+
? get(values, fieldNames)
|
1325
|
+
: fieldNames.map((name) => get(values, name));
|
1326
|
+
};
|
1327
|
+
const getFieldState = (name, formState) => ({
|
1328
|
+
invalid: !!get((formState || _formState).errors, name),
|
1329
|
+
isDirty: !!get((formState || _formState).dirtyFields, name),
|
1330
|
+
error: get((formState || _formState).errors, name),
|
1331
|
+
isValidating: !!get(_formState.validatingFields, name),
|
1332
|
+
isTouched: !!get((formState || _formState).touchedFields, name),
|
1333
|
+
});
|
1334
|
+
const clearErrors = (name) => {
|
1335
|
+
name &&
|
1336
|
+
convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
|
1337
|
+
_subjects.state.next({
|
1338
|
+
errors: name ? _formState.errors : {},
|
1339
|
+
});
|
1340
|
+
};
|
1341
|
+
const setError = (name, error, options) => {
|
1342
|
+
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
|
1343
|
+
const currentError = get(_formState.errors, name) || {};
|
1344
|
+
// Don't override existing error messages elsewhere in the object tree.
|
1345
|
+
const { ref: currentRef, message, type, ...restOfErrorTree } = currentError;
|
1346
|
+
set(_formState.errors, name, {
|
1347
|
+
...restOfErrorTree,
|
1348
|
+
...error,
|
1349
|
+
ref,
|
1350
|
+
});
|
1351
|
+
_subjects.state.next({
|
1352
|
+
name,
|
1353
|
+
errors: _formState.errors,
|
1354
|
+
isValid: false,
|
1355
|
+
});
|
1356
|
+
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
1357
|
+
};
|
1358
|
+
const watch = (name, defaultValue) => isFunction(name)
|
1359
|
+
? _subjects.state.subscribe({
|
1360
|
+
next: (payload) => name(_getWatch(undefined, defaultValue), payload),
|
1361
|
+
})
|
1362
|
+
: _getWatch(name, defaultValue, true);
|
1363
|
+
const _subscribe = (props) => _subjects.state.subscribe({
|
1364
|
+
next: (formState) => {
|
1365
|
+
if (shouldSubscribeByName(props.name, formState.name, props.exact) &&
|
1366
|
+
shouldRenderFormState(formState, props.formState || _proxyFormState, _setFormState, props.reRenderRoot)) {
|
1367
|
+
props.callback({
|
1368
|
+
values: { ..._formValues },
|
1369
|
+
..._formState,
|
1370
|
+
...formState,
|
1371
|
+
});
|
1372
|
+
}
|
1373
|
+
},
|
1374
|
+
}).unsubscribe;
|
1375
|
+
const subscribe = (props) => {
|
1376
|
+
_state.mount = true;
|
1377
|
+
_proxySubscribeFormState = {
|
1378
|
+
..._proxySubscribeFormState,
|
1379
|
+
...props.formState,
|
1380
|
+
};
|
1381
|
+
return _subscribe({
|
1382
|
+
...props,
|
1383
|
+
formState: _proxySubscribeFormState,
|
1384
|
+
});
|
1385
|
+
};
|
1386
|
+
const unregister = (name, options = {}) => {
|
1387
|
+
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
|
1388
|
+
_names.mount.delete(fieldName);
|
1389
|
+
_names.array.delete(fieldName);
|
1390
|
+
if (!options.keepValue) {
|
1391
|
+
unset(_fields, fieldName);
|
1392
|
+
unset(_formValues, fieldName);
|
1393
|
+
}
|
1394
|
+
!options.keepError && unset(_formState.errors, fieldName);
|
1395
|
+
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
1396
|
+
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
1397
|
+
!options.keepIsValidating &&
|
1398
|
+
unset(_formState.validatingFields, fieldName);
|
1399
|
+
!_options.shouldUnregister &&
|
1400
|
+
!options.keepDefaultValue &&
|
1401
|
+
unset(_defaultValues, fieldName);
|
1402
|
+
}
|
1403
|
+
_subjects.state.next({
|
1404
|
+
values: cloneObject(_formValues),
|
1405
|
+
});
|
1406
|
+
_subjects.state.next({
|
1407
|
+
..._formState,
|
1408
|
+
...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
|
1409
|
+
});
|
1410
|
+
!options.keepIsValid && _setValid();
|
1411
|
+
};
|
1412
|
+
const _setDisabledField = ({ disabled, name, }) => {
|
1413
|
+
if ((isBoolean(disabled) && _state.mount) ||
|
1414
|
+
!!disabled ||
|
1415
|
+
_names.disabled.has(name)) {
|
1416
|
+
disabled ? _names.disabled.add(name) : _names.disabled.delete(name);
|
1417
|
+
}
|
1418
|
+
};
|
1419
|
+
const register = (name, options = {}) => {
|
1420
|
+
let field = get(_fields, name);
|
1421
|
+
const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
|
1422
|
+
set(_fields, name, {
|
1423
|
+
...(field || {}),
|
1424
|
+
_f: {
|
1425
|
+
...(field && field._f ? field._f : { ref: { name } }),
|
1426
|
+
name,
|
1427
|
+
mount: true,
|
1428
|
+
...options,
|
1429
|
+
},
|
1430
|
+
});
|
1431
|
+
_names.mount.add(name);
|
1432
|
+
if (field) {
|
1433
|
+
_setDisabledField({
|
1434
|
+
disabled: isBoolean(options.disabled)
|
1435
|
+
? options.disabled
|
1436
|
+
: _options.disabled,
|
1437
|
+
name,
|
1438
|
+
});
|
1439
|
+
}
|
1440
|
+
else {
|
1441
|
+
updateValidAndValue(name, true, options.value);
|
1442
|
+
}
|
1443
|
+
return {
|
1444
|
+
...(disabledIsDefined
|
1445
|
+
? { disabled: options.disabled || _options.disabled }
|
1446
|
+
: {}),
|
1447
|
+
...(_options.progressive
|
1448
|
+
? {
|
1449
|
+
required: !!options.required,
|
1450
|
+
min: getRuleValue(options.min),
|
1451
|
+
max: getRuleValue(options.max),
|
1452
|
+
minLength: getRuleValue(options.minLength),
|
1453
|
+
maxLength: getRuleValue(options.maxLength),
|
1454
|
+
pattern: getRuleValue(options.pattern),
|
1455
|
+
}
|
1456
|
+
: {}),
|
1457
|
+
name,
|
1458
|
+
onChange,
|
1459
|
+
onBlur: onChange,
|
1460
|
+
ref: (ref) => {
|
1461
|
+
if (ref) {
|
1462
|
+
register(name, options);
|
1463
|
+
field = get(_fields, name);
|
1464
|
+
const fieldRef = isUndefined(ref.value)
|
1465
|
+
? ref.querySelectorAll
|
1466
|
+
? ref.querySelectorAll('input,select,textarea')[0] || ref
|
1467
|
+
: ref
|
1468
|
+
: ref;
|
1469
|
+
const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
|
1470
|
+
const refs = field._f.refs || [];
|
1471
|
+
if (radioOrCheckbox
|
1472
|
+
? refs.find((option) => option === fieldRef)
|
1473
|
+
: fieldRef === field._f.ref) {
|
1474
|
+
return;
|
1475
|
+
}
|
1476
|
+
set(_fields, name, {
|
1477
|
+
_f: {
|
1478
|
+
...field._f,
|
1479
|
+
...(radioOrCheckbox
|
1480
|
+
? {
|
1481
|
+
refs: [
|
1482
|
+
...refs.filter(live),
|
1483
|
+
fieldRef,
|
1484
|
+
...(Array.isArray(get(_defaultValues, name)) ? [{}] : []),
|
1485
|
+
],
|
1486
|
+
ref: { type: fieldRef.type, name },
|
1487
|
+
}
|
1488
|
+
: { ref: fieldRef }),
|
1489
|
+
},
|
1490
|
+
});
|
1491
|
+
updateValidAndValue(name, false, undefined, fieldRef);
|
1492
|
+
}
|
1493
|
+
else {
|
1494
|
+
field = get(_fields, name, {});
|
1495
|
+
if (field._f) {
|
1496
|
+
field._f.mount = false;
|
1497
|
+
}
|
1498
|
+
(_options.shouldUnregister || options.shouldUnregister) &&
|
1499
|
+
!(isNameInFieldArray(_names.array, name) && _state.action) &&
|
1500
|
+
_names.unMount.add(name);
|
1501
|
+
}
|
1502
|
+
},
|
1503
|
+
};
|
1504
|
+
};
|
1505
|
+
const _focusError = () => _options.shouldFocusError &&
|
1506
|
+
iterateFieldsByAction(_fields, _focusInput, _names.mount);
|
1507
|
+
const _disableForm = (disabled) => {
|
1508
|
+
if (isBoolean(disabled)) {
|
1509
|
+
_subjects.state.next({ disabled });
|
1510
|
+
iterateFieldsByAction(_fields, (ref, name) => {
|
1511
|
+
const currentField = get(_fields, name);
|
1512
|
+
if (currentField) {
|
1513
|
+
ref.disabled = currentField._f.disabled || disabled;
|
1514
|
+
if (Array.isArray(currentField._f.refs)) {
|
1515
|
+
currentField._f.refs.forEach((inputRef) => {
|
1516
|
+
inputRef.disabled = currentField._f.disabled || disabled;
|
1517
|
+
});
|
1518
|
+
}
|
1519
|
+
}
|
1520
|
+
}, 0, false);
|
1521
|
+
}
|
1522
|
+
};
|
1523
|
+
const handleSubmit = (onValid, onInvalid) => async (e) => {
|
1524
|
+
let onValidError = undefined;
|
1525
|
+
if (e) {
|
1526
|
+
e.preventDefault && e.preventDefault();
|
1527
|
+
e.persist &&
|
1528
|
+
e.persist();
|
1529
|
+
}
|
1530
|
+
let fieldValues = cloneObject(_formValues);
|
1531
|
+
_subjects.state.next({
|
1532
|
+
isSubmitting: true,
|
1533
|
+
});
|
1534
|
+
if (_options.resolver) {
|
1535
|
+
const { errors, values } = await _runSchema();
|
1536
|
+
_formState.errors = errors;
|
1537
|
+
fieldValues = values;
|
1538
|
+
}
|
1539
|
+
else {
|
1540
|
+
await executeBuiltInValidation(_fields);
|
1541
|
+
}
|
1542
|
+
if (_names.disabled.size) {
|
1543
|
+
for (const name of _names.disabled) {
|
1544
|
+
set(fieldValues, name, undefined);
|
1545
|
+
}
|
1546
|
+
}
|
1547
|
+
unset(_formState.errors, 'root');
|
1548
|
+
if (isEmptyObject(_formState.errors)) {
|
1549
|
+
_subjects.state.next({
|
1550
|
+
errors: {},
|
1551
|
+
});
|
1552
|
+
try {
|
1553
|
+
await onValid(fieldValues, e);
|
1554
|
+
}
|
1555
|
+
catch (error) {
|
1556
|
+
onValidError = error;
|
1557
|
+
}
|
1558
|
+
}
|
1559
|
+
else {
|
1560
|
+
if (onInvalid) {
|
1561
|
+
await onInvalid({ ..._formState.errors }, e);
|
1562
|
+
}
|
1563
|
+
_focusError();
|
1564
|
+
setTimeout(_focusError);
|
1565
|
+
}
|
1566
|
+
_subjects.state.next({
|
1567
|
+
isSubmitted: true,
|
1568
|
+
isSubmitting: false,
|
1569
|
+
isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
|
1570
|
+
submitCount: _formState.submitCount + 1,
|
1571
|
+
errors: _formState.errors,
|
1572
|
+
});
|
1573
|
+
if (onValidError) {
|
1574
|
+
throw onValidError;
|
1575
|
+
}
|
1576
|
+
};
|
1577
|
+
const resetField = (name, options = {}) => {
|
1578
|
+
if (get(_fields, name)) {
|
1579
|
+
if (isUndefined(options.defaultValue)) {
|
1580
|
+
setValue(name, cloneObject(get(_defaultValues, name)));
|
1581
|
+
}
|
1582
|
+
else {
|
1583
|
+
setValue(name, options.defaultValue);
|
1584
|
+
set(_defaultValues, name, cloneObject(options.defaultValue));
|
1585
|
+
}
|
1586
|
+
if (!options.keepTouched) {
|
1587
|
+
unset(_formState.touchedFields, name);
|
1588
|
+
}
|
1589
|
+
if (!options.keepDirty) {
|
1590
|
+
unset(_formState.dirtyFields, name);
|
1591
|
+
_formState.isDirty = options.defaultValue
|
1592
|
+
? _getDirty(name, cloneObject(get(_defaultValues, name)))
|
1593
|
+
: _getDirty();
|
1594
|
+
}
|
1595
|
+
if (!options.keepError) {
|
1596
|
+
unset(_formState.errors, name);
|
1597
|
+
_proxyFormState.isValid && _setValid();
|
1598
|
+
}
|
1599
|
+
_subjects.state.next({ ..._formState });
|
1600
|
+
}
|
1601
|
+
};
|
1602
|
+
const _reset = (formValues, keepStateOptions = {}) => {
|
1603
|
+
const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
|
1604
|
+
const cloneUpdatedValues = cloneObject(updatedValues);
|
1605
|
+
const isEmptyResetValues = isEmptyObject(formValues);
|
1606
|
+
const values = isEmptyResetValues ? _defaultValues : cloneUpdatedValues;
|
1607
|
+
if (!keepStateOptions.keepDefaultValues) {
|
1608
|
+
_defaultValues = updatedValues;
|
1609
|
+
}
|
1610
|
+
if (!keepStateOptions.keepValues) {
|
1611
|
+
if (keepStateOptions.keepDirtyValues) {
|
1612
|
+
const fieldsToCheck = new Set([
|
1613
|
+
..._names.mount,
|
1614
|
+
...Object.keys(getDirtyFields(_defaultValues, _formValues)),
|
1615
|
+
]);
|
1616
|
+
for (const fieldName of Array.from(fieldsToCheck)) {
|
1617
|
+
get(_formState.dirtyFields, fieldName)
|
1618
|
+
? set(values, fieldName, get(_formValues, fieldName))
|
1619
|
+
: setValue(fieldName, get(values, fieldName));
|
1620
|
+
}
|
1621
|
+
}
|
1622
|
+
else {
|
1623
|
+
if (isWeb && isUndefined(formValues)) {
|
1624
|
+
for (const name of _names.mount) {
|
1625
|
+
const field = get(_fields, name);
|
1626
|
+
if (field && field._f) {
|
1627
|
+
const fieldReference = Array.isArray(field._f.refs)
|
1628
|
+
? field._f.refs[0]
|
1629
|
+
: field._f.ref;
|
1630
|
+
if (isHTMLElement(fieldReference)) {
|
1631
|
+
const form = fieldReference.closest('form');
|
1632
|
+
if (form) {
|
1633
|
+
form.reset();
|
1634
|
+
break;
|
1635
|
+
}
|
1636
|
+
}
|
1637
|
+
}
|
1638
|
+
}
|
1639
|
+
}
|
1640
|
+
for (const fieldName of _names.mount) {
|
1641
|
+
setValue(fieldName, get(values, fieldName));
|
1642
|
+
}
|
1643
|
+
}
|
1644
|
+
_formValues = cloneObject(values);
|
1645
|
+
_subjects.array.next({
|
1646
|
+
values: { ...values },
|
1647
|
+
});
|
1648
|
+
_subjects.state.next({
|
1649
|
+
values: { ...values },
|
1650
|
+
});
|
1651
|
+
}
|
1652
|
+
_names = {
|
1653
|
+
mount: keepStateOptions.keepDirtyValues ? _names.mount : new Set(),
|
1654
|
+
unMount: new Set(),
|
1655
|
+
array: new Set(),
|
1656
|
+
disabled: new Set(),
|
1657
|
+
watch: new Set(),
|
1658
|
+
watchAll: false,
|
1659
|
+
focus: '',
|
1660
|
+
};
|
1661
|
+
_state.mount =
|
1662
|
+
!_proxyFormState.isValid ||
|
1663
|
+
!!keepStateOptions.keepIsValid ||
|
1664
|
+
!!keepStateOptions.keepDirtyValues;
|
1665
|
+
_state.watch = !!_options.shouldUnregister;
|
1666
|
+
_subjects.state.next({
|
1667
|
+
submitCount: keepStateOptions.keepSubmitCount
|
1668
|
+
? _formState.submitCount
|
1669
|
+
: 0,
|
1670
|
+
isDirty: isEmptyResetValues
|
1671
|
+
? false
|
1672
|
+
: keepStateOptions.keepDirty
|
1673
|
+
? _formState.isDirty
|
1674
|
+
: !!(keepStateOptions.keepDefaultValues &&
|
1675
|
+
!deepEqual(formValues, _defaultValues)),
|
1676
|
+
isSubmitted: keepStateOptions.keepIsSubmitted
|
1677
|
+
? _formState.isSubmitted
|
1678
|
+
: false,
|
1679
|
+
dirtyFields: isEmptyResetValues
|
1680
|
+
? {}
|
1681
|
+
: keepStateOptions.keepDirtyValues
|
1682
|
+
? keepStateOptions.keepDefaultValues && _formValues
|
1683
|
+
? getDirtyFields(_defaultValues, _formValues)
|
1684
|
+
: _formState.dirtyFields
|
1685
|
+
: keepStateOptions.keepDefaultValues && formValues
|
1686
|
+
? getDirtyFields(_defaultValues, formValues)
|
1687
|
+
: keepStateOptions.keepDirty
|
1688
|
+
? _formState.dirtyFields
|
1689
|
+
: {},
|
1690
|
+
touchedFields: keepStateOptions.keepTouched
|
1691
|
+
? _formState.touchedFields
|
1692
|
+
: {},
|
1693
|
+
errors: keepStateOptions.keepErrors ? _formState.errors : {},
|
1694
|
+
isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful
|
1695
|
+
? _formState.isSubmitSuccessful
|
1696
|
+
: false,
|
1697
|
+
isSubmitting: false,
|
1698
|
+
});
|
1699
|
+
};
|
1700
|
+
const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)
|
1701
|
+
? formValues(_formValues)
|
1702
|
+
: formValues, keepStateOptions);
|
1703
|
+
const setFocus = (name, options = {}) => {
|
1704
|
+
const field = get(_fields, name);
|
1705
|
+
const fieldReference = field && field._f;
|
1706
|
+
if (fieldReference) {
|
1707
|
+
const fieldRef = fieldReference.refs
|
1708
|
+
? fieldReference.refs[0]
|
1709
|
+
: fieldReference.ref;
|
1710
|
+
if (fieldRef.focus) {
|
1711
|
+
fieldRef.focus();
|
1712
|
+
options.shouldSelect &&
|
1713
|
+
isFunction(fieldRef.select) &&
|
1714
|
+
fieldRef.select();
|
1715
|
+
}
|
1716
|
+
}
|
1717
|
+
};
|
1718
|
+
const _setFormState = (updatedFormState) => {
|
1719
|
+
_formState = {
|
1720
|
+
..._formState,
|
1721
|
+
...updatedFormState,
|
1722
|
+
};
|
1723
|
+
};
|
1724
|
+
const _resetDefaultValues = () => isFunction(_options.defaultValues) &&
|
1725
|
+
_options.defaultValues().then((values) => {
|
1726
|
+
reset(values, _options.resetOptions);
|
1727
|
+
_subjects.state.next({
|
1728
|
+
isLoading: false,
|
1729
|
+
});
|
1730
|
+
});
|
1731
|
+
const methods = {
|
1732
|
+
control: {
|
1733
|
+
register,
|
1734
|
+
unregister,
|
1735
|
+
getFieldState,
|
1736
|
+
handleSubmit,
|
1737
|
+
setError,
|
1738
|
+
_subscribe,
|
1739
|
+
_runSchema,
|
1740
|
+
_getWatch,
|
1741
|
+
_getDirty,
|
1742
|
+
_setValid,
|
1743
|
+
_setFieldArray,
|
1744
|
+
_setDisabledField,
|
1745
|
+
_setErrors,
|
1746
|
+
_getFieldArray,
|
1747
|
+
_reset,
|
1748
|
+
_resetDefaultValues,
|
1749
|
+
_removeUnmounted,
|
1750
|
+
_disableForm,
|
1751
|
+
_subjects,
|
1752
|
+
_proxyFormState,
|
1753
|
+
get _fields() {
|
1754
|
+
return _fields;
|
1755
|
+
},
|
1756
|
+
get _formValues() {
|
1757
|
+
return _formValues;
|
1758
|
+
},
|
1759
|
+
get _state() {
|
1760
|
+
return _state;
|
1761
|
+
},
|
1762
|
+
set _state(value) {
|
1763
|
+
_state = value;
|
1764
|
+
},
|
1765
|
+
get _defaultValues() {
|
1766
|
+
return _defaultValues;
|
1767
|
+
},
|
1768
|
+
get _names() {
|
1769
|
+
return _names;
|
1770
|
+
},
|
1771
|
+
set _names(value) {
|
1772
|
+
_names = value;
|
1773
|
+
},
|
1774
|
+
get _formState() {
|
1775
|
+
return _formState;
|
1776
|
+
},
|
1777
|
+
get _options() {
|
1778
|
+
return _options;
|
1779
|
+
},
|
1780
|
+
set _options(value) {
|
1781
|
+
_options = {
|
1782
|
+
..._options,
|
1783
|
+
...value,
|
1784
|
+
};
|
1785
|
+
},
|
1786
|
+
},
|
1787
|
+
subscribe,
|
1788
|
+
trigger,
|
1789
|
+
register,
|
1790
|
+
handleSubmit,
|
1791
|
+
watch,
|
1792
|
+
setValue,
|
1793
|
+
getValues,
|
1794
|
+
reset,
|
1795
|
+
resetField,
|
1796
|
+
clearErrors,
|
1797
|
+
unregister,
|
1798
|
+
setError,
|
1799
|
+
setFocus,
|
1800
|
+
getFieldState,
|
1801
|
+
};
|
1802
|
+
return {
|
1803
|
+
...methods,
|
1804
|
+
formControl: methods,
|
1805
|
+
};
|
1806
|
+
}
|
1807
|
+
|
1808
|
+
/**
|
1809
|
+
* Custom hook to manage the entire form.
|
1810
|
+
*
|
1811
|
+
* @remarks
|
1812
|
+
* [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)
|
1813
|
+
*
|
1814
|
+
* @param props - form configuration and validation parameters.
|
1815
|
+
*
|
1816
|
+
* @returns methods - individual functions to manage the form state. {@link UseFormReturn}
|
1817
|
+
*
|
1818
|
+
* @example
|
1819
|
+
* ```tsx
|
1820
|
+
* function App() {
|
1821
|
+
* const { register, handleSubmit, watch, formState: { errors } } = useForm();
|
1822
|
+
* const onSubmit = data => console.log(data);
|
1823
|
+
*
|
1824
|
+
* console.log(watch("example"));
|
1825
|
+
*
|
1826
|
+
* return (
|
1827
|
+
* <form onSubmit={handleSubmit(onSubmit)}>
|
1828
|
+
* <input defaultValue="test" {...register("example")} />
|
1829
|
+
* <input {...register("exampleRequired", { required: true })} />
|
1830
|
+
* {errors.exampleRequired && <span>This field is required</span>}
|
1831
|
+
* <button>Submit</button>
|
1832
|
+
* </form>
|
1833
|
+
* );
|
1834
|
+
* }
|
1835
|
+
* ```
|
1836
|
+
*/
|
1837
|
+
function useForm(props = {}) {
|
1838
|
+
const _formControl = React__default.useRef(undefined);
|
1839
|
+
const _values = React__default.useRef(undefined);
|
1840
|
+
const [formState, updateFormState] = React__default.useState({
|
1841
|
+
isDirty: false,
|
1842
|
+
isValidating: false,
|
1843
|
+
isLoading: isFunction(props.defaultValues),
|
1844
|
+
isSubmitted: false,
|
1845
|
+
isSubmitting: false,
|
1846
|
+
isSubmitSuccessful: false,
|
1847
|
+
isValid: false,
|
1848
|
+
submitCount: 0,
|
1849
|
+
dirtyFields: {},
|
1850
|
+
touchedFields: {},
|
1851
|
+
validatingFields: {},
|
1852
|
+
errors: props.errors || {},
|
1853
|
+
disabled: props.disabled || false,
|
1854
|
+
defaultValues: isFunction(props.defaultValues)
|
1855
|
+
? undefined
|
1856
|
+
: props.defaultValues,
|
1857
|
+
});
|
1858
|
+
if (!_formControl.current) {
|
1859
|
+
_formControl.current = {
|
1860
|
+
...(props.formControl ? props.formControl : createFormControl(props)),
|
1861
|
+
formState,
|
1862
|
+
};
|
1863
|
+
if (props.formControl &&
|
1864
|
+
props.defaultValues &&
|
1865
|
+
!isFunction(props.defaultValues)) {
|
1866
|
+
props.formControl.reset(props.defaultValues, props.resetOptions);
|
1867
|
+
}
|
1868
|
+
}
|
1869
|
+
const control = _formControl.current.control;
|
1870
|
+
control._options = props;
|
1871
|
+
React__default.useLayoutEffect(() => control._subscribe({
|
1872
|
+
formState: control._proxyFormState,
|
1873
|
+
callback: () => updateFormState({ ...control._formState }),
|
1874
|
+
reRenderRoot: true,
|
1875
|
+
}), [control]);
|
1876
|
+
React__default.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
|
1877
|
+
React__default.useEffect(() => {
|
1878
|
+
if (control._proxyFormState.isDirty) {
|
1879
|
+
const isDirty = control._getDirty();
|
1880
|
+
if (isDirty !== formState.isDirty) {
|
1881
|
+
control._subjects.state.next({
|
1882
|
+
isDirty,
|
1883
|
+
});
|
1884
|
+
}
|
1885
|
+
}
|
1886
|
+
}, [control, formState.isDirty]);
|
1887
|
+
React__default.useEffect(() => {
|
1888
|
+
if (props.values && !deepEqual(props.values, _values.current)) {
|
1889
|
+
control._reset(props.values, control._options.resetOptions);
|
1890
|
+
_values.current = props.values;
|
1891
|
+
updateFormState((state) => ({ ...state }));
|
1892
|
+
}
|
1893
|
+
else {
|
1894
|
+
control._resetDefaultValues();
|
1895
|
+
}
|
1896
|
+
}, [props.values, control]);
|
1897
|
+
React__default.useEffect(() => {
|
1898
|
+
if (props.errors && !isEmptyObject(props.errors)) {
|
1899
|
+
control._setErrors(props.errors);
|
1900
|
+
}
|
1901
|
+
}, [props.errors, control]);
|
1902
|
+
React__default.useEffect(() => {
|
1903
|
+
if (!control._state.mount) {
|
1904
|
+
control._setValid();
|
1905
|
+
control._state.mount = true;
|
1906
|
+
}
|
1907
|
+
if (control._state.watch) {
|
1908
|
+
control._state.watch = false;
|
1909
|
+
control._subjects.state.next({ ...control._formState });
|
1910
|
+
}
|
1911
|
+
control._removeUnmounted();
|
1912
|
+
});
|
1913
|
+
React__default.useEffect(() => {
|
1914
|
+
props.shouldUnregister &&
|
1915
|
+
control._subjects.state.next({
|
1916
|
+
values: control._getWatch(),
|
1917
|
+
});
|
1918
|
+
}, [props.shouldUnregister, control]);
|
1919
|
+
_formControl.current.formState = getProxyFormState(formState, control);
|
1920
|
+
return _formControl.current;
|
1921
|
+
}
|
1922
|
+
|
1923
|
+
export { appendErrors, createFormControl, get, set, useForm };
|
1924
|
+
//# sourceMappingURL=index.esm.js.map
|