react-hook-form 7.43.4 → 7.43.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/__tests__/form.test.d.ts +2 -2
- package/dist/form.d.ts +60 -79
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.mjs +89 -75
- package/dist/index.esm.mjs.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/logic/createFormControl.d.ts.map +1 -1
- package/dist/types/fieldArray.d.ts +1 -1
- package/dist/types/path/eager.d.ts +2 -2
- package/dist/types/validator.d.ts +1 -1
- package/dist/types/validator.d.ts.map +1 -1
- package/dist/useController.d.ts.map +1 -1
- package/dist/useSubscribe.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.mjs
CHANGED
@@ -22,6 +22,42 @@ var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/))
|
|
22
22
|
|
23
23
|
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
24
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
|
+
if (data instanceof Date) {
|
38
|
+
copy = new Date(data);
|
39
|
+
}
|
40
|
+
else if (data instanceof Set) {
|
41
|
+
copy = new Set(data);
|
42
|
+
}
|
43
|
+
else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
|
44
|
+
(isArray || isObject(data))) {
|
45
|
+
copy = isArray ? [] : {};
|
46
|
+
if (!Array.isArray(data) && !isPlainObject(data)) {
|
47
|
+
copy = data;
|
48
|
+
}
|
49
|
+
else {
|
50
|
+
for (const key in data) {
|
51
|
+
copy[key] = cloneObject(data[key]);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
else {
|
56
|
+
return data;
|
57
|
+
}
|
58
|
+
return copy;
|
59
|
+
}
|
60
|
+
|
25
61
|
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
26
62
|
|
27
63
|
var isUndefined = (val) => val === undefined;
|
@@ -173,6 +209,7 @@ function useSubscribe(props) {
|
|
173
209
|
_props.current = props;
|
174
210
|
React.useEffect(() => {
|
175
211
|
const subscription = !props.disabled &&
|
212
|
+
_props.current.subject &&
|
176
213
|
_props.current.subject.subscribe({
|
177
214
|
next: _props.current.next,
|
178
215
|
});
|
@@ -269,42 +306,6 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
|
|
269
306
|
return formValues;
|
270
307
|
};
|
271
308
|
|
272
|
-
var isPlainObject = (tempObject) => {
|
273
|
-
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
274
|
-
return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
|
275
|
-
};
|
276
|
-
|
277
|
-
var isWeb = typeof window !== 'undefined' &&
|
278
|
-
typeof window.HTMLElement !== 'undefined' &&
|
279
|
-
typeof document !== 'undefined';
|
280
|
-
|
281
|
-
function cloneObject(data) {
|
282
|
-
let copy;
|
283
|
-
const isArray = Array.isArray(data);
|
284
|
-
if (data instanceof Date) {
|
285
|
-
copy = new Date(data);
|
286
|
-
}
|
287
|
-
else if (data instanceof Set) {
|
288
|
-
copy = new Set(data);
|
289
|
-
}
|
290
|
-
else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
|
291
|
-
(isArray || isObject(data))) {
|
292
|
-
copy = isArray ? [] : {};
|
293
|
-
if (!Array.isArray(data) && !isPlainObject(data)) {
|
294
|
-
copy = data;
|
295
|
-
}
|
296
|
-
else {
|
297
|
-
for (const key in data) {
|
298
|
-
copy[key] = cloneObject(data[key]);
|
299
|
-
}
|
300
|
-
}
|
301
|
-
}
|
302
|
-
else {
|
303
|
-
return data;
|
304
|
-
}
|
305
|
-
return copy;
|
306
|
-
}
|
307
|
-
|
308
309
|
/**
|
309
310
|
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
310
311
|
*
|
@@ -340,6 +341,33 @@ function useWatch(props) {
|
|
340
341
|
return value;
|
341
342
|
}
|
342
343
|
|
344
|
+
var isKey = (value) => /^\w*$/.test(value);
|
345
|
+
|
346
|
+
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
347
|
+
|
348
|
+
function set(object, path, value) {
|
349
|
+
let index = -1;
|
350
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
351
|
+
const length = tempPath.length;
|
352
|
+
const lastIndex = length - 1;
|
353
|
+
while (++index < length) {
|
354
|
+
const key = tempPath[index];
|
355
|
+
let newValue = value;
|
356
|
+
if (index !== lastIndex) {
|
357
|
+
const objValue = object[key];
|
358
|
+
newValue =
|
359
|
+
isObject(objValue) || Array.isArray(objValue)
|
360
|
+
? objValue
|
361
|
+
: !isNaN(+tempPath[index + 1])
|
362
|
+
? []
|
363
|
+
: {};
|
364
|
+
}
|
365
|
+
object[key] = newValue;
|
366
|
+
object = object[key];
|
367
|
+
}
|
368
|
+
return object;
|
369
|
+
}
|
370
|
+
|
343
371
|
/**
|
344
372
|
* Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
|
345
373
|
*
|
@@ -383,6 +411,7 @@ function useController(props) {
|
|
383
411
|
value,
|
384
412
|
}));
|
385
413
|
React.useEffect(() => {
|
414
|
+
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
386
415
|
const updateMounted = (name, value) => {
|
387
416
|
const field = get(control._fields, name);
|
388
417
|
if (field) {
|
@@ -390,8 +419,14 @@ function useController(props) {
|
|
390
419
|
}
|
391
420
|
};
|
392
421
|
updateMounted(name, true);
|
422
|
+
if (_shouldUnregisterField) {
|
423
|
+
const value = cloneObject(get(control._options.defaultValues, name));
|
424
|
+
set(control._defaultValues, name, value);
|
425
|
+
if (isUndefined(get(control._formValues, name))) {
|
426
|
+
set(control._formValues, name, value);
|
427
|
+
}
|
428
|
+
}
|
393
429
|
return () => {
|
394
|
-
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
395
430
|
(isArrayField
|
396
431
|
? _shouldUnregisterField && !control._state.action
|
397
432
|
: _shouldUnregisterField)
|
@@ -505,33 +540,6 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
|
|
505
540
|
}
|
506
541
|
: {};
|
507
542
|
|
508
|
-
var isKey = (value) => /^\w*$/.test(value);
|
509
|
-
|
510
|
-
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
|
511
|
-
|
512
|
-
function set(object, path, value) {
|
513
|
-
let index = -1;
|
514
|
-
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
515
|
-
const length = tempPath.length;
|
516
|
-
const lastIndex = length - 1;
|
517
|
-
while (++index < length) {
|
518
|
-
const key = tempPath[index];
|
519
|
-
let newValue = value;
|
520
|
-
if (index !== lastIndex) {
|
521
|
-
const objValue = object[key];
|
522
|
-
newValue =
|
523
|
-
isObject(objValue) || Array.isArray(objValue)
|
524
|
-
? objValue
|
525
|
-
: !isNaN(+tempPath[index + 1])
|
526
|
-
? []
|
527
|
-
: {};
|
528
|
-
}
|
529
|
-
object[key] = newValue;
|
530
|
-
object = object[key];
|
531
|
-
}
|
532
|
-
return object;
|
533
|
-
}
|
534
|
-
|
535
543
|
const focusFieldBy = (fields, callback, fieldsNames) => {
|
536
544
|
for (const key of fieldsNames || Object.keys(fields)) {
|
537
545
|
const field = get(fields, key);
|
@@ -1390,7 +1398,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1390
1398
|
let _formState = {
|
1391
1399
|
submitCount: 0,
|
1392
1400
|
isDirty: false,
|
1393
|
-
isLoading:
|
1401
|
+
isLoading: isFunction(_options.defaultValues),
|
1394
1402
|
isValidating: false,
|
1395
1403
|
isSubmitted: false,
|
1396
1404
|
isSubmitting: false,
|
@@ -1604,7 +1612,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1604
1612
|
const { _f, ...fieldValue } = field;
|
1605
1613
|
if (_f) {
|
1606
1614
|
const isFieldArrayRoot = _names.array.has(_f.name);
|
1607
|
-
const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, isFieldArrayRoot);
|
1615
|
+
const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
1608
1616
|
if (fieldError[_f.name]) {
|
1609
1617
|
context.valid = false;
|
1610
1618
|
if (shouldOnlyCheckValid) {
|
@@ -1741,6 +1749,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1741
1749
|
const onChange = async (event) => {
|
1742
1750
|
const target = event.target;
|
1743
1751
|
let name = target.name;
|
1752
|
+
let isFieldValueUpdated = true;
|
1744
1753
|
const field = get(_fields, name);
|
1745
1754
|
const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
|
1746
1755
|
if (field) {
|
@@ -1787,16 +1796,21 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1787
1796
|
}
|
1788
1797
|
else {
|
1789
1798
|
error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1799
|
+
isFieldValueUpdated = fieldValue === get(_formValues, name, fieldValue);
|
1800
|
+
if (isFieldValueUpdated) {
|
1801
|
+
if (error) {
|
1802
|
+
isValid = false;
|
1803
|
+
}
|
1804
|
+
else if (_proxyFormState.isValid) {
|
1805
|
+
isValid = await executeBuiltInValidation(_fields, true);
|
1806
|
+
}
|
1795
1807
|
}
|
1796
1808
|
}
|
1797
|
-
|
1798
|
-
|
1799
|
-
|
1809
|
+
if (isFieldValueUpdated) {
|
1810
|
+
field._f.deps &&
|
1811
|
+
trigger(field._f.deps);
|
1812
|
+
shouldRenderByError(name, isValid, error, fieldState);
|
1813
|
+
}
|
1800
1814
|
}
|
1801
1815
|
};
|
1802
1816
|
const trigger = async (name, options = {}) => {
|
@@ -2265,7 +2279,7 @@ function useForm(props = {}) {
|
|
2265
2279
|
const [formState, updateFormState] = React.useState({
|
2266
2280
|
isDirty: false,
|
2267
2281
|
isValidating: false,
|
2268
|
-
isLoading:
|
2282
|
+
isLoading: isFunction(props.defaultValues),
|
2269
2283
|
isSubmitted: false,
|
2270
2284
|
isSubmitting: false,
|
2271
2285
|
isSubmitSuccessful: false,
|