react-hook-form 7.75.0 → 7.76.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.
@@ -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,EAYd,MAAM,UAAU,CAAC;AAqDlB,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,CAsoDA"}
1
+ {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAcV,WAAW,EAiBX,YAAY,EAIZ,aAAa,EAYd,MAAM,UAAU,CAAC;AAuDlB,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,CAmtDA"}
@@ -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,wBA8PE"}
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,wBA6PE"}
@@ -654,8 +654,7 @@ var validateField = async (field, disabledFieldNames, formValues, validateAllFie
654
654
  isUndefined(inputValue)) ||
655
655
  (isHTMLElement(ref) && ref.value === '') ||
656
656
  inputValue === '' ||
657
- (Array.isArray(inputValue) && !inputValue.length) ||
658
- (valueAsNumber && typeof inputValue === 'number' && isNaN(inputValue));
657
+ (Array.isArray(inputValue) && !inputValue.length);
659
658
  const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
660
659
  const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
661
660
  const message = exceedMax ? maxLengthMessage : minLengthMessage;
@@ -985,9 +984,28 @@ function createFormControl(props = {}) {
985
984
  isValid: false,
986
985
  });
987
986
  };
987
+ const hasExplicitNullIntermediate = (name) => {
988
+ const segments = isKey(name) ? [name] : stringToPath(name);
989
+ let formValues = _formValues;
990
+ let defaultValues = _defaultValues;
991
+ for (let i = 0; i < segments.length - 1; i++) {
992
+ const key = segments[i];
993
+ formValues = isNullOrUndefined(formValues) ? formValues : formValues[key];
994
+ defaultValues = isNullOrUndefined(defaultValues)
995
+ ? defaultValues
996
+ : defaultValues[key];
997
+ if (formValues === null && defaultValues !== null) {
998
+ return true;
999
+ }
1000
+ }
1001
+ return false;
1002
+ };
988
1003
  const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
989
1004
  const field = get(_fields, name);
990
1005
  if (field) {
1006
+ if (hasExplicitNullIntermediate(name)) {
1007
+ return;
1008
+ }
991
1009
  const wasUnsetInFormValues = isUndefined(get(_formValues, name));
992
1010
  const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
993
1011
  isUndefined(defaultValue) ||
@@ -1030,9 +1048,14 @@ function createFormControl(props = {}) {
1030
1048
  }
1031
1049
  const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
1032
1050
  isPreviousDirty = !!get(_formState.dirtyFields, name);
1033
- isCurrentFieldPristine
1034
- ? unset(_formState.dirtyFields, name)
1035
- : set(_formState.dirtyFields, name, true);
1051
+ if (isCurrentFieldPristine !== _formState.isDirty) {
1052
+ _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
1053
+ }
1054
+ else {
1055
+ isCurrentFieldPristine
1056
+ ? unset(_formState.dirtyFields, name)
1057
+ : set(_formState.dirtyFields, name, true);
1058
+ }
1036
1059
  output.dirtyFields = _formState.dirtyFields;
1037
1060
  shouldUpdateField =
1038
1061
  shouldUpdateField ||
@@ -1099,7 +1122,9 @@ function createFormControl(props = {}) {
1099
1122
  for (const name of names) {
1100
1123
  const error = get(errors, name);
1101
1124
  error
1102
- ? set(_formState.errors, name, error)
1125
+ ? _names.array.has(name) && isObject(error)
1126
+ ? updateFieldArrayRootError(_formState.errors, { [name]: error }, name)
1127
+ : set(_formState.errors, name, error)
1103
1128
  : unset(_formState.errors, name);
1104
1129
  }
1105
1130
  }
@@ -1121,8 +1146,8 @@ function createFormControl(props = {}) {
1121
1146
  const error = result[key];
1122
1147
  if (error) {
1123
1148
  setError(`${FORM_ERROR_TYPE}.${key}`, {
1124
- message: isString(result.message) ? result.message : '',
1125
- type: INPUT_VALIDATION_RULES.validate,
1149
+ message: isString(error.message) ? error.message : '',
1150
+ type: error.type || INPUT_VALIDATION_RULES.validate,
1126
1151
  });
1127
1152
  }
1128
1153
  }
@@ -1164,11 +1189,15 @@ function createFormControl(props = {}) {
1164
1189
  if (_f) {
1165
1190
  const isFieldArrayRoot = _names.array.has(_f.name);
1166
1191
  const isPromiseFunction = field._f && hasPromiseValidation(field._f);
1167
- if (isPromiseFunction && _proxyFormState.validatingFields) {
1192
+ const shouldTrackIsValidatingState = _proxyFormState.validatingFields ||
1193
+ _proxyFormState.isValidating ||
1194
+ _proxySubscribeFormState.validatingFields ||
1195
+ _proxySubscribeFormState.isValidating;
1196
+ if (isPromiseFunction && shouldTrackIsValidatingState) {
1168
1197
  _updateIsValidating([_f.name], true);
1169
1198
  }
1170
1199
  const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !onlyCheckValid, isFieldArrayRoot);
1171
- if (isPromiseFunction && _proxyFormState.validatingFields) {
1200
+ if (isPromiseFunction && shouldTrackIsValidatingState) {
1172
1201
  _updateIsValidating([_f.name]);
1173
1202
  }
1174
1203
  if (fieldError[_f.name]) {
@@ -1223,7 +1252,7 @@ function createFormControl(props = {}) {
1223
1252
  : defaultValue),
1224
1253
  }, isGlobal, defaultValue);
1225
1254
  const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
1226
- const setFieldValue = (name, value, options = {}) => {
1255
+ const setFieldValue = (name, value, options = {}, skipClone = false) => {
1227
1256
  const field = get(_fields, name);
1228
1257
  let fieldValue = value;
1229
1258
  if (field) {
@@ -1264,7 +1293,7 @@ function createFormControl(props = {}) {
1264
1293
  if (!fieldReference.ref.type) {
1265
1294
  _subjects.state.next({
1266
1295
  name,
1267
- values: cloneObject(_formValues),
1296
+ values: skipClone ? _formValues : cloneObject(_formValues),
1268
1297
  });
1269
1298
  }
1270
1299
  }
@@ -1274,7 +1303,7 @@ function createFormControl(props = {}) {
1274
1303
  updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
1275
1304
  options.shouldValidate && trigger(name);
1276
1305
  };
1277
- const setFieldValues = (name, value, options) => {
1306
+ const setFieldValues = (name, value, options, skipClone = false) => {
1278
1307
  for (const fieldKey in value) {
1279
1308
  if (!value.hasOwnProperty(fieldKey)) {
1280
1309
  return;
@@ -1286,21 +1315,23 @@ function createFormControl(props = {}) {
1286
1315
  isObject(fieldValue) ||
1287
1316
  (field && !field._f)) &&
1288
1317
  !isDateObject(fieldValue)
1289
- ? setFieldValues(fieldName, fieldValue, options)
1290
- : setFieldValue(fieldName, fieldValue, options);
1318
+ ? setFieldValues(fieldName, fieldValue, options, skipClone)
1319
+ : setFieldValue(fieldName, fieldValue, options, skipClone);
1291
1320
  }
1292
1321
  };
1293
- const setValue = (name, value, options = {}) => {
1322
+ const _setValue = (name, value, options, skipClone) => {
1294
1323
  const field = get(_fields, name);
1295
1324
  const isFieldArray = _names.array.has(name);
1296
- const cloneValue = cloneObject(value);
1325
+ const cloneValue = skipClone ? value : cloneObject(value);
1297
1326
  const previousValue = get(_formValues, name);
1298
1327
  const isValueUnchanged = deepEqual(previousValue, cloneValue);
1299
- set(_formValues, name, cloneValue);
1328
+ if (!isValueUnchanged) {
1329
+ set(_formValues, name, cloneValue);
1330
+ }
1300
1331
  if (isFieldArray) {
1301
1332
  _subjects.array.next({
1302
1333
  name,
1303
- values: cloneObject(_formValues),
1334
+ values: skipClone ? _formValues : cloneObject(_formValues),
1304
1335
  });
1305
1336
  if ((_proxyFormState.isDirty ||
1306
1337
  _proxyFormState.dirtyFields ||
@@ -1319,22 +1350,24 @@ function createFormControl(props = {}) {
1319
1350
  const isEmpty = (Array.isArray(cloneValue) && !cloneValue.length) ||
1320
1351
  isEmptyObject(cloneValue);
1321
1352
  if (!field || field._f || isNullOrUndefined(cloneValue) || isEmpty) {
1322
- setFieldValue(name, cloneValue, options);
1353
+ setFieldValue(name, cloneValue, options, skipClone);
1323
1354
  }
1324
1355
  else {
1325
- setFieldValues(name, cloneValue, options);
1356
+ setFieldValues(name, cloneValue, options, skipClone);
1326
1357
  }
1327
1358
  }
1328
1359
  if (!isValueUnchanged) {
1329
1360
  const watched = isWatched(name, _names);
1361
+ const values = skipClone ? _formValues : cloneObject(_formValues);
1330
1362
  _subjects.state.next({
1331
1363
  ...(watched && _formState),
1332
1364
  name: _state.mount || watched ? name : undefined,
1333
- values: cloneObject(_formValues),
1365
+ values,
1334
1366
  });
1335
1367
  }
1336
1368
  };
1337
- const setValues = (formValues) => {
1369
+ const setValue = (name, value, options = {}) => _setValue(name, value, options, false);
1370
+ const setValues = (formValues, options = {}) => {
1338
1371
  const updatedFormValues = isFunction(formValues)
1339
1372
  ? formValues(_formValues)
1340
1373
  : formValues;
@@ -1343,7 +1376,18 @@ function createFormControl(props = {}) {
1343
1376
  ..._formValues,
1344
1377
  ...updatedFormValues,
1345
1378
  };
1346
- _subjects.state.next({ ..._formState, values: _formValues });
1379
+ for (const fieldName of _names.mount) {
1380
+ _setValue(fieldName, get(updatedFormValues, fieldName), options, true);
1381
+ }
1382
+ _subjects.state.next({
1383
+ ..._formState,
1384
+ name: undefined,
1385
+ type: undefined,
1386
+ values: _formValues,
1387
+ });
1388
+ if (options.shouldValidate) {
1389
+ _setValid();
1390
+ }
1347
1391
  }
1348
1392
  };
1349
1393
  const onChange = async (event) => {
@@ -1720,6 +1764,7 @@ function createFormControl(props = {}) {
1720
1764
  };
1721
1765
  };
1722
1766
  const _focusError = () => _options.shouldFocusError &&
1767
+ !_options.shouldUseNativeValidation &&
1723
1768
  iterateFieldsByAction(_fields, _focusInput, _names.mount);
1724
1769
  const _disableForm = (disabled) => {
1725
1770
  if (isBoolean(disabled)) {