react-hook-form 7.43.3 → 7.43.5
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/__tests__/form.test.d.ts +2 -0
- package/dist/__tests__/form.test.d.ts.map +1 -0
- package/dist/form.d.ts +79 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.mjs +96 -96
- 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 +0 -1
- package/dist/logic/createFormControl.d.ts.map +1 -1
- package/dist/types/form.d.ts +1 -3
- package/dist/types/form.d.ts.map +1 -1
- package/dist/useController.d.ts.map +1 -1
- package/dist/useSubscribe.d.ts.map +1 -1
- package/dist/utils/createSubject.d.ts.map +1 -1
- package/package.json +1 -2
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,10 +419,12 @@ function useController(props) {
|
|
390
419
|
}
|
391
420
|
};
|
392
421
|
updateMounted(name, true);
|
422
|
+
if (_shouldUnregisterField) {
|
423
|
+
set(control._defaultValues, name, cloneObject(get(control._options.defaultValues, name)));
|
424
|
+
}
|
393
425
|
return () => {
|
394
|
-
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
395
426
|
(isArrayField
|
396
|
-
? _shouldUnregisterField && !control.
|
427
|
+
? _shouldUnregisterField && !control._state.action
|
397
428
|
: _shouldUnregisterField)
|
398
429
|
? control.unregister(name)
|
399
430
|
: updateMounted(name, false);
|
@@ -505,33 +536,6 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
|
|
505
536
|
}
|
506
537
|
: {};
|
507
538
|
|
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
539
|
const focusFieldBy = (fields, callback, fieldsNames) => {
|
536
540
|
for (const key of fieldsNames || Object.keys(fields)) {
|
537
541
|
const field = get(fields, key);
|
@@ -1082,7 +1086,7 @@ function useFieldArray(props) {
|
|
1082
1086
|
control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
|
1083
1087
|
};
|
1084
1088
|
React.useEffect(() => {
|
1085
|
-
control.
|
1089
|
+
control._state.action = false;
|
1086
1090
|
isWatched(name, control._names) &&
|
1087
1091
|
control._subjects.state.next({
|
1088
1092
|
...control._formState,
|
@@ -1116,7 +1120,7 @@ function useFieldArray(props) {
|
|
1116
1120
|
}
|
1117
1121
|
control._subjects.values.next({
|
1118
1122
|
name,
|
1119
|
-
values: control._formValues,
|
1123
|
+
values: { ...control._formValues },
|
1120
1124
|
});
|
1121
1125
|
control._names.focus &&
|
1122
1126
|
focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
|
@@ -1149,11 +1153,8 @@ function useFieldArray(props) {
|
|
1149
1153
|
function createSubject() {
|
1150
1154
|
let _observers = [];
|
1151
1155
|
const next = (value) => {
|
1152
|
-
|
1153
|
-
|
1154
|
-
while (x < l) {
|
1155
|
-
_observers[x].next(value);
|
1156
|
-
++x;
|
1156
|
+
for (const observer of _observers) {
|
1157
|
+
observer.next && observer.next(value);
|
1157
1158
|
}
|
1158
1159
|
};
|
1159
1160
|
const subscribe = (observer) => {
|
@@ -1410,7 +1411,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1410
1411
|
let _formValues = _options.shouldUnregister
|
1411
1412
|
? {}
|
1412
1413
|
: cloneObject(_defaultValues);
|
1413
|
-
let
|
1414
|
+
let _state = {
|
1414
1415
|
action: false,
|
1415
1416
|
mount: false,
|
1416
1417
|
watch: false,
|
@@ -1423,7 +1424,6 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1423
1424
|
};
|
1424
1425
|
let delayErrorCallback;
|
1425
1426
|
let timer = 0;
|
1426
|
-
const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
|
1427
1427
|
const _proxyFormState = {
|
1428
1428
|
isDirty: false,
|
1429
1429
|
dirtyFields: false,
|
@@ -1437,6 +1437,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1437
1437
|
array: createSubject(),
|
1438
1438
|
state: createSubject(),
|
1439
1439
|
};
|
1440
|
+
const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
|
1440
1441
|
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
1441
1442
|
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
1442
1443
|
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
@@ -1462,7 +1463,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1462
1463
|
});
|
1463
1464
|
const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
1464
1465
|
if (args && method) {
|
1465
|
-
|
1466
|
+
_state.action = true;
|
1466
1467
|
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
1467
1468
|
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
1468
1469
|
shouldSetValues && set(_fields, name, fieldValues);
|
@@ -1509,7 +1510,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1509
1510
|
shouldSkipSetValueAs
|
1510
1511
|
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
|
1511
1512
|
: setFieldValue(name, defaultValue);
|
1512
|
-
|
1513
|
+
_state.mount && _updateValid();
|
1513
1514
|
}
|
1514
1515
|
};
|
1515
1516
|
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
@@ -1582,7 +1583,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1582
1583
|
}
|
1583
1584
|
_updateIsValidating(false);
|
1584
1585
|
};
|
1585
|
-
const _executeSchema = async (name) =>
|
1586
|
+
const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
1586
1587
|
const executeSchemaAndUpdateState = async (names) => {
|
1587
1588
|
const { errors } = await _executeSchema();
|
1588
1589
|
if (names) {
|
@@ -1641,7 +1642,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1641
1642
|
const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
|
1642
1643
|
!deepEqual(getValues(), _defaultValues));
|
1643
1644
|
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
1644
|
-
...(
|
1645
|
+
...(_state.mount
|
1645
1646
|
? _formValues
|
1646
1647
|
: isUndefined(defaultValue)
|
1647
1648
|
? _defaultValues
|
@@ -1649,7 +1650,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1649
1650
|
? { [names]: defaultValue }
|
1650
1651
|
: defaultValue),
|
1651
1652
|
}, isGlobal, defaultValue);
|
1652
|
-
const _getFieldArray = (name) => compact(get(
|
1653
|
+
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
|
1653
1654
|
const setFieldValue = (name, value, options = {}) => {
|
1654
1655
|
const field = get(_fields, name);
|
1655
1656
|
let fieldValue = value;
|
@@ -1739,7 +1740,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1739
1740
|
name,
|
1740
1741
|
values: { ..._formValues },
|
1741
1742
|
});
|
1742
|
-
!
|
1743
|
+
!_state.mount && flushRootRender();
|
1743
1744
|
};
|
1744
1745
|
const onChange = async (event) => {
|
1745
1746
|
const target = event.target;
|
@@ -1841,7 +1842,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1841
1842
|
const getValues = (fieldNames) => {
|
1842
1843
|
const values = {
|
1843
1844
|
..._defaultValues,
|
1844
|
-
...(
|
1845
|
+
...(_state.mount ? _formValues : {}),
|
1845
1846
|
};
|
1846
1847
|
return isUndefined(fieldNames)
|
1847
1848
|
? values
|
@@ -1979,7 +1980,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1979
1980
|
field._f.mount = false;
|
1980
1981
|
}
|
1981
1982
|
(_options.shouldUnregister || options.shouldUnregister) &&
|
1982
|
-
!(isNameInFieldArray(_names.array, name) &&
|
1983
|
+
!(isNameInFieldArray(_names.array, name) && _state.action) &&
|
1983
1984
|
_names.unMount.add(name);
|
1984
1985
|
}
|
1985
1986
|
},
|
@@ -2094,10 +2095,10 @@ function createFormControl(props = {}, flushRootRender) {
|
|
2094
2095
|
: {}
|
2095
2096
|
: cloneUpdatedValues;
|
2096
2097
|
_subjects.array.next({
|
2097
|
-
values,
|
2098
|
+
values: { ...values },
|
2098
2099
|
});
|
2099
2100
|
_subjects.values.next({
|
2100
|
-
values,
|
2101
|
+
values: { ...values },
|
2101
2102
|
});
|
2102
2103
|
}
|
2103
2104
|
_names = {
|
@@ -2108,10 +2109,9 @@ function createFormControl(props = {}, flushRootRender) {
|
|
2108
2109
|
watchAll: false,
|
2109
2110
|
focus: '',
|
2110
2111
|
};
|
2111
|
-
!
|
2112
|
-
|
2113
|
-
|
2114
|
-
_stateFlags.watch = !!props.shouldUnregister;
|
2112
|
+
!_state.mount && flushRootRender();
|
2113
|
+
_state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
|
2114
|
+
_state.watch = !!props.shouldUnregister;
|
2115
2115
|
_subjects.state.next({
|
2116
2116
|
submitCount: keepStateOptions.keepSubmitCount
|
2117
2117
|
? _formState.submitCount
|
@@ -2188,11 +2188,11 @@ function createFormControl(props = {}, flushRootRender) {
|
|
2188
2188
|
get _formValues() {
|
2189
2189
|
return _formValues;
|
2190
2190
|
},
|
2191
|
-
get
|
2192
|
-
return
|
2191
|
+
get _state() {
|
2192
|
+
return _state;
|
2193
2193
|
},
|
2194
|
-
set
|
2195
|
-
|
2194
|
+
set _state(value) {
|
2195
|
+
_state = value;
|
2196
2196
|
},
|
2197
2197
|
get _defaultValues() {
|
2198
2198
|
return _defaultValues;
|
@@ -2304,12 +2304,12 @@ function useForm(props = {}) {
|
|
2304
2304
|
}
|
2305
2305
|
}, [props.values, control]);
|
2306
2306
|
React.useEffect(() => {
|
2307
|
-
if (!control.
|
2307
|
+
if (!control._state.mount) {
|
2308
2308
|
control._updateValid();
|
2309
|
-
control.
|
2309
|
+
control._state.mount = true;
|
2310
2310
|
}
|
2311
|
-
if (control.
|
2312
|
-
control.
|
2311
|
+
if (control._state.watch) {
|
2312
|
+
control._state.watch = false;
|
2313
2313
|
control._subjects.state.next({ ...control._formState });
|
2314
2314
|
}
|
2315
2315
|
control._removeUnmounted();
|