react-hook-form 7.84.0 → 7.85.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAcV,WAAW,EAkBX,YAAY,EAKZ,aAAa,EAYd,MAAM,UAAU,CAAC;AAwElB,eAAO,MAAM,kBAAkB;;;;;;;;;;;;CAY9B,CAAC;AAEF,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,CA83DA"}
1
+ {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAcV,WAAW,EAkBX,YAAY,EAKZ,aAAa,EAYd,MAAM,UAAU,CAAC;AAwElB,eAAO,MAAM,kBAAkB;;;;;;;;;;;;CAY9B,CAAC;AAEF,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,CAm9DA"}
@@ -1,3 +1,3 @@
1
- declare const _default: <T>(ref: T, name: string) => any;
1
+ declare const _default: <T>(ref: T, name: string) => void;
2
2
  export default _default;
3
3
  //# sourceMappingURL=unsetEmptyArray.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"unsetEmptyArray.d.ts","sourceRoot":"","sources":["../../src/logic/unsetEmptyArray.ts"],"names":[],"mappings":"yBAIgB,CAAC,EAAE,KAAK,CAAC,EAAE,MAAM,MAAM;AAAvC,wBACsD"}
1
+ {"version":3,"file":"unsetEmptyArray.d.ts","sourceRoot":"","sources":["../../src/logic/unsetEmptyArray.ts"],"names":[],"mappings":"yBAIgB,CAAC,EAAE,KAAK,CAAC,EAAE,MAAM,MAAM;AAAvC,wBAME"}
@@ -1 +1 @@
1
- {"version":3,"file":"validateField.d.ts","sourceRoot":"","sources":["../../src/logic/validateField.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,KAAK,EAEL,WAAW,EACX,mBAAmB,EACnB,eAAe,EAKhB,MAAM,UAAU,CAAC;yBAqBI,CAAC,SAAS,WAAW,EACzC,OAAO,KAAK,EACZ,oBAAoB,eAAe,EACnC,YAAY,CAAC,EACb,0BAA0B,OAAO,EACjC,4BAA4B,OAAO,EACnC,eAAe,OAAO,KACrB,OAAO,CAAC,mBAAmB,CAAC;AAP/B,wBAkQE"}
1
+ {"version":3,"file":"validateField.d.ts","sourceRoot":"","sources":["../../src/logic/validateField.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,KAAK,EAEL,WAAW,EACX,mBAAmB,EACnB,eAAe,EAKhB,MAAM,UAAU,CAAC;yBAsBI,CAAC,SAAS,WAAW,EACzC,OAAO,KAAK,EACZ,oBAAoB,eAAe,EACnC,YAAY,CAAC,EACb,0BAA0B,OAAO,EACjC,4BAA4B,OAAO,EACnC,eAAe,OAAO,KACrB,OAAO,CAAC,mBAAmB,CAAC;AAP/B,wBAsQE"}
@@ -688,7 +688,12 @@ var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode)
688
688
  return true;
689
689
  };
690
690
 
691
- var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
691
+ var unsetEmptyArray = (ref, name) => {
692
+ const array = get(ref, name);
693
+ !compact(array).length &&
694
+ !(array === null || array === void 0 ? void 0 : array.root) &&
695
+ unset(ref, name);
696
+ };
692
697
 
693
698
  var updateFieldArrayRootError = (errors, error, name) => {
694
699
  const existingErrors = get(errors, name);
@@ -784,7 +789,9 @@ var validateField = async (field, disabledFieldNames, formValues, validateAllFie
784
789
  let exceedMin;
785
790
  const maxOutput = getValueAndMessage(max);
786
791
  const minOutput = getValueAndMessage(min);
787
- if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
792
+ if (!isNullOrUndefined(inputValue) &&
793
+ !isDateObject(inputValue) &&
794
+ !isNaN(inputValue)) {
788
795
  const valueNumber = ref.valueAsNumber ||
789
796
  (inputValue ? +inputValue : inputValue);
790
797
  if (!isNullOrUndefined(maxOutput.value)) {
@@ -949,6 +956,7 @@ function createFormControl(props = {}) {
949
956
  : cloneObject(_defaultValues);
950
957
  let _state = {
951
958
  action: false,
959
+ actionArrayLengths: new Map(),
952
960
  mount: false,
953
961
  watch: false,
954
962
  keepIsValid: false,
@@ -1044,13 +1052,22 @@ function createFormControl(props = {}) {
1044
1052
  const _setFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
1045
1053
  if (args && method && !_options.disabled) {
1046
1054
  _state.action = true;
1055
+ if (!_state.actionArrayLengths.has(name)) {
1056
+ const preActionFields = get(_fields, name);
1057
+ _state.actionArrayLengths.set(name, Array.isArray(preActionFields) ? preActionFields.length : 0);
1058
+ }
1047
1059
  if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
1048
1060
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
1049
1061
  shouldSetValues && set(_fields, name, fieldValues);
1050
1062
  }
1051
1063
  if (shouldUpdateFieldsAndState &&
1052
1064
  Array.isArray(get(_formState.errors, name))) {
1053
- const errors = method(get(_formState.errors, name), args.argA, args.argB);
1065
+ const fieldArrayErrors = get(_formState.errors, name);
1066
+ const rootError = fieldArrayErrors.root;
1067
+ const errors = method(fieldArrayErrors, args.argA, args.argB) || fieldArrayErrors;
1068
+ if (rootError) {
1069
+ errors.root = rootError;
1070
+ }
1054
1071
  shouldSetValues && set(_formState.errors, name, errors);
1055
1072
  unsetEmptyArray(_formState.errors, name);
1056
1073
  }
@@ -1106,10 +1123,40 @@ function createFormControl(props = {}) {
1106
1123
  }
1107
1124
  return false;
1108
1125
  };
1126
+ const isStaleArrayIndex = (name) => {
1127
+ if (!_state.actionArrayLengths.size) {
1128
+ return false;
1129
+ }
1130
+ const segments = isKey(name) ? [name] : stringToPath(name);
1131
+ let node = _formValues;
1132
+ let path = '';
1133
+ let ownerDepth = -1;
1134
+ let ownerPreActionLength = 0;
1135
+ for (let i = 0; i < segments.length; i++) {
1136
+ if (isNullOrUndefined(node)) {
1137
+ return false;
1138
+ }
1139
+ const key = segments[i];
1140
+ path = path ? `${path}.${key}` : key;
1141
+ if (Array.isArray(node) && +key >= node.length) {
1142
+ return ownerDepth === -1
1143
+ ? false
1144
+ : i === ownerDepth
1145
+ ? +key < ownerPreActionLength
1146
+ : true;
1147
+ }
1148
+ if (_state.actionArrayLengths.has(path)) {
1149
+ ownerDepth = i + 1;
1150
+ ownerPreActionLength = _state.actionArrayLengths.get(path);
1151
+ }
1152
+ node = node[key];
1153
+ }
1154
+ return false;
1155
+ };
1109
1156
  const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
1110
1157
  const field = get(_fields, name);
1111
1158
  if (field) {
1112
- if (hasExplicitNullIntermediate(name)) {
1159
+ if (hasExplicitNullIntermediate(name) || isStaleArrayIndex(name)) {
1113
1160
  return;
1114
1161
  }
1115
1162
  const wasUnsetInFormValues = isUndefined(get(_formValues, name));
@@ -1357,14 +1404,12 @@ function createFormControl(props = {}) {
1357
1404
  const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
1358
1405
  ...(_state.mount
1359
1406
  ? _formValues
1360
- : isUndefined(defaultValue)
1407
+ : isUndefined(defaultValue) || isString(names)
1361
1408
  ? _defaultValues
1362
- : isString(names)
1363
- ? { [names]: defaultValue }
1364
- : defaultValue),
1409
+ : defaultValue),
1365
1410
  }, isGlobal, defaultValue);
1366
1411
  const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
1367
- const setFieldValue = (name, value, options = {}, skipClone = false, skipRender = false) => {
1412
+ const setFieldValue = (name, value, options = {}, skipClone = false, skipRender = false, skipValueRender = false) => {
1368
1413
  const field = get(_fields, name);
1369
1414
  let fieldValue = value;
1370
1415
  if (field) {
@@ -1402,7 +1447,7 @@ function createFormControl(props = {}) {
1402
1447
  }
1403
1448
  else {
1404
1449
  fieldReference.ref.value = fieldValue;
1405
- if (!fieldReference.ref.type && !skipRender) {
1450
+ if (!fieldReference.ref.type && !skipRender && !skipValueRender) {
1406
1451
  _subjects.state.next({
1407
1452
  name,
1408
1453
  values: skipClone ? _formValues : cloneObject(_formValues),
@@ -1418,7 +1463,7 @@ function createFormControl(props = {}) {
1418
1463
  delayError: options.delayError,
1419
1464
  });
1420
1465
  };
1421
- const setFieldValues = (name, value, options, skipClone = false, skipRender = false) => {
1466
+ const setFieldValues = (name, value, options, skipClone = false, skipRender = false, skipValueRender = false) => {
1422
1467
  if (_names.array.has(name)) {
1423
1468
  _subjects.array.next({
1424
1469
  name,
@@ -1436,8 +1481,8 @@ function createFormControl(props = {}) {
1436
1481
  isObject(fieldValue) ||
1437
1482
  (field && !field._f)) &&
1438
1483
  !isDateObject(fieldValue)
1439
- ? setFieldValues(fieldName, fieldValue, options, skipClone, skipRender)
1440
- : setFieldValue(fieldName, fieldValue, options, skipClone, skipRender);
1484
+ ? setFieldValues(fieldName, fieldValue, options, skipClone, skipRender, skipValueRender)
1485
+ : setFieldValue(fieldName, fieldValue, options, skipClone, skipRender, skipValueRender);
1441
1486
  }
1442
1487
  };
1443
1488
  const _setValue = (name, value, options, skipClone, skipStateEmit = false) => {
@@ -1472,11 +1517,12 @@ function createFormControl(props = {}) {
1472
1517
  else {
1473
1518
  const isEmpty = (Array.isArray(cloneValue) && !cloneValue.length) ||
1474
1519
  isEmptyObject(cloneValue);
1520
+ const skipValueRender = !isValueUnchanged && !skipStateEmit;
1475
1521
  if (!field || field._f || isNullOrUndefined(cloneValue) || isEmpty) {
1476
- setFieldValue(name, cloneValue, options, skipClone, skipStateEmit);
1522
+ setFieldValue(name, cloneValue, options, skipClone, skipStateEmit, skipValueRender);
1477
1523
  }
1478
1524
  else {
1479
- setFieldValues(name, cloneValue, options, skipClone, skipStateEmit);
1525
+ setFieldValues(name, cloneValue, options, skipClone, skipStateEmit, skipValueRender);
1480
1526
  }
1481
1527
  }
1482
1528
  if (!isValueUnchanged && !skipStateEmit) {
@@ -2138,6 +2184,7 @@ function createFormControl(props = {}) {
2138
2184
  _state.watch = !!_options.shouldUnregister;
2139
2185
  _state.keepIsValid = !!keepStateOptions.keepIsValid;
2140
2186
  _state.action = false;
2187
+ _state.actionArrayLengths.clear();
2141
2188
  if (!keepStateOptions.keepErrors) {
2142
2189
  _formState.errors = {};
2143
2190
  }
@@ -2210,6 +2257,7 @@ function createFormControl(props = {}) {
2210
2257
  ...formState,
2211
2258
  };
2212
2259
  };
2260
+ _subjects.state.subscribe({ next: _setFormState });
2213
2261
  const _resetDefaultValues = () => isFunction(_options.defaultValues) &&
2214
2262
  _options.defaultValues().then((values) => {
2215
2263
  reset(values, _options.resetOptions);