react-hook-form 6.9.4 → 6.9.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.
@@ -836,13 +836,14 @@
836
836
  }
837
837
  else if (!isPrimitive(value)) {
838
838
  setInternalValues(name, value, config);
839
- if (fieldArrayNamesRef.current.has(name) && config.shouldDirty) {
839
+ if (fieldArrayNamesRef.current.has(name)) {
840
840
  fieldArrayDefaultValuesRef.current[name] = value;
841
841
  resetFieldArrayFunctionRef.current[name]({
842
842
  [name]: value,
843
843
  });
844
- if (readFormStateRef.current.isDirty ||
845
- readFormStateRef.current.dirtyFields) {
844
+ if ((readFormStateRef.current.isDirty ||
845
+ readFormStateRef.current.dirtyFields) &&
846
+ config.shouldDirty) {
846
847
  set(formStateRef.current.dirtyFields, name, setFieldArrayDirtyFields(value, get(defaultValuesRef.current, name, []), get(formStateRef.current.dirtyFields, name, [])));
847
848
  updateFormState({
848
849
  isDirty: !deepEqual(Object.assign(Object.assign({}, getValues()), { [name]: value }), defaultValuesRef.current),
@@ -952,6 +953,22 @@
952
953
  });
953
954
  }, [isValidateAllFieldCriteria]);
954
955
  const removeFieldEventListener = React.useCallback((field, forceDelete) => findRemovedFieldAndRemoveListener(fieldsRef, handleChangeRef.current, field, shallowFieldsStateRef, shouldUnregister, forceDelete), [shouldUnregister]);
956
+ const updateWatchedValue = (name) => {
957
+ if (isWatchAllRef.current) {
958
+ updateFormState();
959
+ }
960
+ else if (watchFieldsRef) {
961
+ let shouldRenderUseWatch = true;
962
+ for (const watchField of watchFieldsRef.current) {
963
+ if (watchField.startsWith(name)) {
964
+ updateFormState();
965
+ shouldRenderUseWatch = false;
966
+ break;
967
+ }
968
+ }
969
+ shouldRenderUseWatch && renderWatchedInputs(name);
970
+ }
971
+ };
955
972
  const removeFieldEventListenerAndRef = React.useCallback((field, forceDelete) => {
956
973
  if (field) {
957
974
  removeFieldEventListener(field, forceDelete);
@@ -967,6 +984,7 @@
967
984
  dirtyFields: formStateRef.current.dirtyFields,
968
985
  });
969
986
  resolverRef.current && validateResolver();
987
+ updateWatchedValue(field.ref.name);
970
988
  }
971
989
  }
972
990
  }, [validateResolver, removeFieldEventListener]);
@@ -1122,16 +1140,14 @@
1122
1140
  }
1123
1141
  let fieldErrors = {};
1124
1142
  let fieldValues = setFieldArrayDefaultValues(getFieldsValues(fieldsRef, shallowFieldsStateRef, true));
1125
- if (readFormStateRef.current.isSubmitting) {
1143
+ readFormStateRef.current.isSubmitting &&
1126
1144
  updateFormState({
1127
1145
  isSubmitting: true,
1128
1146
  });
1129
- }
1130
1147
  try {
1131
1148
  if (resolverRef.current) {
1132
1149
  const { errors, values } = await resolverRef.current(fieldValues, contextRef.current, isValidateAllFieldCriteria);
1133
- formStateRef.current.errors = errors;
1134
- fieldErrors = errors;
1150
+ formStateRef.current.errors = fieldErrors = errors;
1135
1151
  fieldValues = values;
1136
1152
  }
1137
1153
  else {
@@ -1215,13 +1231,11 @@
1215
1231
  }
1216
1232
  fieldsRef.current = {};
1217
1233
  defaultValuesRef.current = cloneObject(values || defaultValuesRef.current);
1218
- if (values) {
1219
- renderWatchedInputs('');
1220
- }
1234
+ values && renderWatchedInputs('');
1235
+ Object.values(resetFieldArrayFunctionRef.current).forEach((resetFieldArray) => isFunction(resetFieldArray) && resetFieldArray());
1221
1236
  shallowFieldsStateRef.current = shouldUnregister
1222
1237
  ? {}
1223
1238
  : cloneObject(values) || {};
1224
- Object.values(resetFieldArrayFunctionRef.current).forEach((resetFieldArray) => isFunction(resetFieldArray) && resetFieldArray());
1225
1239
  resetRefs(omitResetState);
1226
1240
  };
1227
1241
  React.useEffect(() => {
@@ -1252,15 +1266,13 @@
1252
1266
  register: React.useCallback(register, [defaultValuesRef.current]),
1253
1267
  unregister: React.useCallback(unregister, []),
1254
1268
  };
1255
- const control = Object.assign({ renderWatchedInputs,
1269
+ const control = Object.assign({ updateWatchedValue,
1256
1270
  shouldUnregister,
1257
1271
  removeFieldEventListener,
1258
1272
  watchInternal, mode: modeRef.current, reValidateMode: {
1259
1273
  isReValidateOnBlur,
1260
1274
  isReValidateOnChange,
1261
1275
  }, fieldsRef,
1262
- isWatchAllRef,
1263
- watchFieldsRef,
1264
1276
  resetFieldArrayFunctionRef,
1265
1277
  useWatchFieldsRef,
1266
1278
  useWatchRenderFunctionsRef,
@@ -1408,7 +1420,7 @@
1408
1420
  }
1409
1421
  }
1410
1422
  const focusIndexRef = React.useRef(-1);
1411
- const { isWatchAllRef, resetFieldArrayFunctionRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, removeFieldEventListener, formStateRef, shallowFieldsStateRef, updateFormState, readFormStateRef, watchFieldsRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, validateResolver, renderWatchedInputs, getValues, shouldUnregister, } = control || methods.control;
1423
+ const { updateWatchedValue, resetFieldArrayFunctionRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, removeFieldEventListener, formStateRef, shallowFieldsStateRef, updateFormState, readFormStateRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, validateResolver, getValues, shouldUnregister, } = control || methods.control;
1412
1424
  const fieldArrayParentName = getFieldArrayParentName(name);
1413
1425
  const memoizedDefaultValues = React.useRef([
1414
1426
  ...(get(fieldArrayDefaultValuesRef.current, fieldArrayParentName)
@@ -1608,20 +1620,7 @@
1608
1620
  defaultValues.pop();
1609
1621
  set(fieldArrayDefaultValuesRef.current, name, defaultValues);
1610
1622
  }
1611
- if (isWatchAllRef.current) {
1612
- updateFormState();
1613
- }
1614
- else if (watchFieldsRef) {
1615
- let shouldRenderUseWatch = true;
1616
- for (const watchField of watchFieldsRef.current) {
1617
- if (watchField.startsWith(name)) {
1618
- updateFormState();
1619
- shouldRenderUseWatch = false;
1620
- break;
1621
- }
1622
- }
1623
- shouldRenderUseWatch && renderWatchedInputs(name);
1624
- }
1623
+ updateWatchedValue(name);
1625
1624
  if (focusIndexRef.current > -1) {
1626
1625
  for (const key in fieldsRef.current) {
1627
1626
  const field = fieldsRef.current[key];
@@ -1673,7 +1672,7 @@
1673
1672
  : defaultValuesRef.current
1674
1673
  : defaultValue);
1675
1674
  const idRef = React.useRef();
1676
- const defaultValueRef = React.useRef(value);
1675
+ const defaultValueRef = React.useRef(defaultValue);
1677
1676
  const updateWatchValue = React.useCallback(() => {
1678
1677
  const value = watchInternal(name, defaultValueRef.current, idRef.current);
1679
1678
  setValue(isObject(value)