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