react-hook-form 7.72.0 → 7.73.1
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 -19
- package/dist/__tests__/nested-null.test.d.ts +2 -0
- package/dist/__tests__/nested-null.test.d.ts.map +1 -0
- package/dist/form.d.ts.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.mjs +123 -84
- 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/logic/isNameInFieldArray.d.ts.map +1 -1
- package/dist/react-server.esm.mjs +64 -37
- package/dist/react-server.esm.mjs.map +1 -1
- package/dist/types/form.d.ts +1 -0
- package/dist/types/form.d.ts.map +1 -1
- package/dist/useFieldArray.d.ts.map +1 -1
- package/dist/useFormContext.d.ts.map +1 -1
- package/dist/utils/deepEqual.d.ts +1 -1
- package/dist/utils/deepEqual.d.ts.map +1 -1
- package/dist/utils/unset.d.ts.map +1 -1
- package/package.json +20 -20
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAaV,WAAW,EAiBX,YAAY,EAIZ,aAAa,EAWd,MAAM,UAAU,CAAC;AAsDlB,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,KAAK,GAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAM,GACnE,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,CAAC;CACH,
|
|
1
|
+
{"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAaV,WAAW,EAiBX,YAAY,EAIZ,aAAa,EAWd,MAAM,UAAU,CAAC;AAsDlB,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,KAAK,GAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAM,GACnE,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,CAAC;CACH,CAwmDA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isNameInFieldArray.d.ts","sourceRoot":"","sources":["../../src/logic/isNameInFieldArray.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"isNameInFieldArray.d.ts","sourceRoot":"","sources":["../../src/logic/isNameInFieldArray.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;yBAElC,OAAO,GAAG,CAAC,iBAAiB,CAAC,EAAE,MAAM,iBAAiB;AAAtE,wBAMM"}
|
|
@@ -108,7 +108,10 @@ var createSubject = () => {
|
|
|
108
108
|
|
|
109
109
|
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
110
110
|
|
|
111
|
-
function deepEqual(object1, object2,
|
|
111
|
+
function deepEqual(object1, object2, visited = new WeakSet()) {
|
|
112
|
+
if (object1 === object2) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
112
115
|
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
113
116
|
return Object.is(object1, object2);
|
|
114
117
|
}
|
|
@@ -120,22 +123,22 @@ function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
|
|
|
120
123
|
if (keys1.length !== keys2.length) {
|
|
121
124
|
return false;
|
|
122
125
|
}
|
|
123
|
-
if (
|
|
126
|
+
if (visited.has(object1) || visited.has(object2)) {
|
|
124
127
|
return true;
|
|
125
128
|
}
|
|
126
|
-
|
|
127
|
-
|
|
129
|
+
visited.add(object1);
|
|
130
|
+
visited.add(object2);
|
|
128
131
|
for (const key of keys1) {
|
|
129
132
|
const val1 = object1[key];
|
|
130
|
-
if (!
|
|
133
|
+
if (!(key in object2)) {
|
|
131
134
|
return false;
|
|
132
135
|
}
|
|
133
136
|
if (key !== 'ref') {
|
|
134
137
|
const val2 = object2[key];
|
|
135
138
|
if ((isDateObject(val1) && isDateObject(val2)) ||
|
|
136
|
-
(isObject(val1)
|
|
137
|
-
|
|
138
|
-
? !deepEqual(val1, val2,
|
|
139
|
+
((isObject(val1) || Array.isArray(val1)) &&
|
|
140
|
+
(isObject(val2) || Array.isArray(val2)))
|
|
141
|
+
? !deepEqual(val1, val2, visited)
|
|
139
142
|
: !Object.is(val1, val2)) {
|
|
140
143
|
return false;
|
|
141
144
|
}
|
|
@@ -174,7 +177,10 @@ var get = (object, path, defaultValue) => {
|
|
|
174
177
|
if (!path || !isObject(object)) {
|
|
175
178
|
return defaultValue;
|
|
176
179
|
}
|
|
177
|
-
const
|
|
180
|
+
const paths = isKey(path) ? [path] : stringToPath(path);
|
|
181
|
+
const result = paths.reduce((result, key) => {
|
|
182
|
+
return isNullOrUndefined(result) ? undefined : result[key];
|
|
183
|
+
}, object);
|
|
178
184
|
return isUndefined(result) || result === object
|
|
179
185
|
? isUndefined(object[path])
|
|
180
186
|
? defaultValue
|
|
@@ -253,6 +259,10 @@ function isEmptyArray(obj) {
|
|
|
253
259
|
return true;
|
|
254
260
|
}
|
|
255
261
|
function unset(object, path) {
|
|
262
|
+
if (isString(path) && Object.prototype.hasOwnProperty.call(object, path)) {
|
|
263
|
+
delete object[path];
|
|
264
|
+
return object;
|
|
265
|
+
}
|
|
256
266
|
const paths = Array.isArray(path)
|
|
257
267
|
? path
|
|
258
268
|
: isKey(path)
|
|
@@ -460,7 +470,9 @@ var hasValidation = (options) => options.mount &&
|
|
|
460
470
|
options.pattern ||
|
|
461
471
|
options.validate);
|
|
462
472
|
|
|
463
|
-
var isNameInFieldArray = (names, name) =>
|
|
473
|
+
var isNameInFieldArray = (names, name) => name
|
|
474
|
+
.split('.')
|
|
475
|
+
.some((part, index, arr) => !isNaN(Number(part)) && names.has(arr.slice(0, index).join('.')));
|
|
464
476
|
|
|
465
477
|
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
|
466
478
|
(_names.watchAll ||
|
|
@@ -817,6 +829,7 @@ function createFormControl(props = {}) {
|
|
|
817
829
|
unMount: new Set(),
|
|
818
830
|
array: new Set(),
|
|
819
831
|
watch: new Set(),
|
|
832
|
+
registerName: new Set(),
|
|
820
833
|
};
|
|
821
834
|
let delayErrorCallback;
|
|
822
835
|
let timer = 0;
|
|
@@ -890,6 +903,11 @@ function createFormControl(props = {}) {
|
|
|
890
903
|
});
|
|
891
904
|
}
|
|
892
905
|
};
|
|
906
|
+
const _updateDirtyFields = (name) => {
|
|
907
|
+
const fullDirtyFields = getDirtyFields(_defaultValues, _formValues);
|
|
908
|
+
const rootName = getNodeParentName(name);
|
|
909
|
+
set(_formState.dirtyFields, rootName, get(fullDirtyFields, rootName));
|
|
910
|
+
};
|
|
893
911
|
const _setFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
|
894
912
|
if (args && method && !_options.disabled) {
|
|
895
913
|
_state.action = true;
|
|
@@ -911,9 +929,7 @@ function createFormControl(props = {}) {
|
|
|
911
929
|
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
|
912
930
|
}
|
|
913
931
|
if (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) {
|
|
914
|
-
|
|
915
|
-
const rootName = getNodeParentName(name);
|
|
916
|
-
set(_formState.dirtyFields, rootName, get(fullDirtyFields, rootName));
|
|
932
|
+
_updateDirtyFields(name);
|
|
917
933
|
}
|
|
918
934
|
_subjects.state.next({
|
|
919
935
|
name,
|
|
@@ -1231,41 +1247,47 @@ function createFormControl(props = {}) {
|
|
|
1231
1247
|
const field = get(_fields, name);
|
|
1232
1248
|
const isFieldArray = _names.array.has(name);
|
|
1233
1249
|
const cloneValue = cloneObject(value);
|
|
1250
|
+
const previousValue = get(_formValues, name);
|
|
1251
|
+
const isValueUnchanged = deepEqual(previousValue, cloneValue);
|
|
1234
1252
|
set(_formValues, name, cloneValue);
|
|
1235
1253
|
if (isFieldArray) {
|
|
1236
1254
|
_subjects.array.next({
|
|
1237
1255
|
name,
|
|
1238
1256
|
values: cloneObject(_formValues),
|
|
1239
1257
|
});
|
|
1240
|
-
if (
|
|
1241
|
-
|
|
1242
|
-
_proxySubscribeFormState.isDirty ||
|
|
1243
|
-
_proxySubscribeFormState.dirtyFields) &&
|
|
1244
|
-
options.shouldDirty) {
|
|
1258
|
+
if (options.shouldDirty) {
|
|
1259
|
+
_updateDirtyFields(name);
|
|
1245
1260
|
_subjects.state.next({
|
|
1246
1261
|
name,
|
|
1247
|
-
dirtyFields:
|
|
1262
|
+
dirtyFields: _formState.dirtyFields,
|
|
1248
1263
|
isDirty: _getDirty(name, cloneValue),
|
|
1249
1264
|
});
|
|
1250
1265
|
}
|
|
1251
1266
|
}
|
|
1252
1267
|
else {
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
values: cloneObject(_formValues),
|
|
1262
|
-
});
|
|
1268
|
+
const isEmpty = (Array.isArray(cloneValue) && !cloneValue.length) ||
|
|
1269
|
+
isEmptyObject(cloneValue);
|
|
1270
|
+
if (!field || field._f || isNullOrUndefined(cloneValue) || isEmpty) {
|
|
1271
|
+
setFieldValue(name, cloneValue, options);
|
|
1272
|
+
}
|
|
1273
|
+
else {
|
|
1274
|
+
setValues(name, cloneValue, options);
|
|
1275
|
+
}
|
|
1263
1276
|
}
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1277
|
+
if (!isValueUnchanged) {
|
|
1278
|
+
if (isWatched(name, _names)) {
|
|
1279
|
+
_subjects.state.next({
|
|
1280
|
+
..._formState,
|
|
1281
|
+
name,
|
|
1282
|
+
values: cloneObject(_formValues),
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
else {
|
|
1286
|
+
_subjects.state.next({
|
|
1287
|
+
name: _state.mount ? name : undefined,
|
|
1288
|
+
values: cloneObject(_formValues),
|
|
1289
|
+
});
|
|
1290
|
+
}
|
|
1269
1291
|
}
|
|
1270
1292
|
};
|
|
1271
1293
|
const onChange = async (event) => {
|
|
@@ -1484,15 +1506,16 @@ function createFormControl(props = {}) {
|
|
|
1484
1506
|
const watch = (name, defaultValue) => isFunction(name)
|
|
1485
1507
|
? _subjects.state.subscribe({
|
|
1486
1508
|
next: (payload) => 'values' in payload &&
|
|
1487
|
-
name(_getWatch(undefined, defaultValue), payload),
|
|
1509
|
+
name(payload.values || _getWatch(undefined, defaultValue), payload),
|
|
1488
1510
|
})
|
|
1489
1511
|
: _getWatch(name, defaultValue, true);
|
|
1490
1512
|
const _subscribe = (props) => _subjects.state.subscribe({
|
|
1491
1513
|
next: (formState) => {
|
|
1492
1514
|
if (shouldSubscribeByName(props.name, formState.name, props.exact) &&
|
|
1493
1515
|
shouldRenderFormState(formState, props.formState || _proxyFormState, _setFormState, props.reRenderRoot)) {
|
|
1516
|
+
const snapshot = { ..._formValues };
|
|
1494
1517
|
props.callback({
|
|
1495
|
-
values:
|
|
1518
|
+
values: snapshot,
|
|
1496
1519
|
..._formState,
|
|
1497
1520
|
...formState,
|
|
1498
1521
|
defaultValues: _defaultValues,
|
|
@@ -1554,6 +1577,7 @@ function createFormControl(props = {}) {
|
|
|
1554
1577
|
const register = (name, options = {}) => {
|
|
1555
1578
|
let field = get(_fields, name);
|
|
1556
1579
|
const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
|
|
1580
|
+
const shouldRevalidateRemount = !_names.registerName.has(name) && field && field._f && !field._f.mount;
|
|
1557
1581
|
set(_fields, name, {
|
|
1558
1582
|
...(field || {}),
|
|
1559
1583
|
_f: {
|
|
@@ -1564,7 +1588,7 @@ function createFormControl(props = {}) {
|
|
|
1564
1588
|
},
|
|
1565
1589
|
});
|
|
1566
1590
|
_names.mount.add(name);
|
|
1567
|
-
if (field) {
|
|
1591
|
+
if (field && !shouldRevalidateRemount) {
|
|
1568
1592
|
_setDisabledField({
|
|
1569
1593
|
disabled: isBoolean(options.disabled)
|
|
1570
1594
|
? options.disabled
|
|
@@ -1594,7 +1618,9 @@ function createFormControl(props = {}) {
|
|
|
1594
1618
|
onBlur: onChange,
|
|
1595
1619
|
ref: (ref) => {
|
|
1596
1620
|
if (ref) {
|
|
1621
|
+
_names.registerName.add(name);
|
|
1597
1622
|
register(name, options);
|
|
1623
|
+
_names.registerName.delete(name);
|
|
1598
1624
|
field = get(_fields, name);
|
|
1599
1625
|
const fieldRef = isUndefined(ref.value)
|
|
1600
1626
|
? ref.querySelectorAll
|
|
@@ -1807,6 +1833,7 @@ function createFormControl(props = {}) {
|
|
|
1807
1833
|
mount: keepStateOptions.keepDirtyValues ? _names.mount : new Set(),
|
|
1808
1834
|
unMount: new Set(),
|
|
1809
1835
|
array: new Set(),
|
|
1836
|
+
registerName: new Set(),
|
|
1810
1837
|
disabled: new Set(),
|
|
1811
1838
|
watch: new Set(),
|
|
1812
1839
|
watchAll: false,
|