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.
package/dist/index.esm.js CHANGED
@@ -832,13 +832,14 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
832
832
  }
833
833
  else if (!isPrimitive(value)) {
834
834
  setInternalValues(name, value, config);
835
- if (fieldArrayNamesRef.current.has(name) && config.shouldDirty) {
835
+ if (fieldArrayNamesRef.current.has(name)) {
836
836
  fieldArrayDefaultValuesRef.current[name] = value;
837
837
  resetFieldArrayFunctionRef.current[name]({
838
838
  [name]: value,
839
839
  });
840
- if (readFormStateRef.current.isDirty ||
841
- readFormStateRef.current.dirtyFields) {
840
+ if ((readFormStateRef.current.isDirty ||
841
+ readFormStateRef.current.dirtyFields) &&
842
+ config.shouldDirty) {
842
843
  set(formStateRef.current.dirtyFields, name, setFieldArrayDirtyFields(value, get(defaultValuesRef.current, name, []), get(formStateRef.current.dirtyFields, name, [])));
843
844
  updateFormState({
844
845
  isDirty: !deepEqual(Object.assign(Object.assign({}, getValues()), { [name]: value }), defaultValuesRef.current),
@@ -948,6 +949,22 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
948
949
  });
949
950
  }, [isValidateAllFieldCriteria]);
950
951
  const removeFieldEventListener = useCallback((field, forceDelete) => findRemovedFieldAndRemoveListener(fieldsRef, handleChangeRef.current, field, shallowFieldsStateRef, shouldUnregister, forceDelete), [shouldUnregister]);
952
+ const updateWatchedValue = (name) => {
953
+ if (isWatchAllRef.current) {
954
+ updateFormState();
955
+ }
956
+ else if (watchFieldsRef) {
957
+ let shouldRenderUseWatch = true;
958
+ for (const watchField of watchFieldsRef.current) {
959
+ if (watchField.startsWith(name)) {
960
+ updateFormState();
961
+ shouldRenderUseWatch = false;
962
+ break;
963
+ }
964
+ }
965
+ shouldRenderUseWatch && renderWatchedInputs(name);
966
+ }
967
+ };
951
968
  const removeFieldEventListenerAndRef = useCallback((field, forceDelete) => {
952
969
  if (field) {
953
970
  removeFieldEventListener(field, forceDelete);
@@ -963,6 +980,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
963
980
  dirtyFields: formStateRef.current.dirtyFields,
964
981
  });
965
982
  resolverRef.current && validateResolver();
983
+ updateWatchedValue(field.ref.name);
966
984
  }
967
985
  }
968
986
  }, [validateResolver, removeFieldEventListener]);
@@ -1118,16 +1136,14 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1118
1136
  }
1119
1137
  let fieldErrors = {};
1120
1138
  let fieldValues = setFieldArrayDefaultValues(getFieldsValues(fieldsRef, shallowFieldsStateRef, true));
1121
- if (readFormStateRef.current.isSubmitting) {
1139
+ readFormStateRef.current.isSubmitting &&
1122
1140
  updateFormState({
1123
1141
  isSubmitting: true,
1124
1142
  });
1125
- }
1126
1143
  try {
1127
1144
  if (resolverRef.current) {
1128
1145
  const { errors, values } = await resolverRef.current(fieldValues, contextRef.current, isValidateAllFieldCriteria);
1129
- formStateRef.current.errors = errors;
1130
- fieldErrors = errors;
1146
+ formStateRef.current.errors = fieldErrors = errors;
1131
1147
  fieldValues = values;
1132
1148
  }
1133
1149
  else {
@@ -1211,13 +1227,11 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1211
1227
  }
1212
1228
  fieldsRef.current = {};
1213
1229
  defaultValuesRef.current = cloneObject(values || defaultValuesRef.current);
1214
- if (values) {
1215
- renderWatchedInputs('');
1216
- }
1230
+ values && renderWatchedInputs('');
1231
+ Object.values(resetFieldArrayFunctionRef.current).forEach((resetFieldArray) => isFunction(resetFieldArray) && resetFieldArray());
1217
1232
  shallowFieldsStateRef.current = shouldUnregister
1218
1233
  ? {}
1219
1234
  : cloneObject(values) || {};
1220
- Object.values(resetFieldArrayFunctionRef.current).forEach((resetFieldArray) => isFunction(resetFieldArray) && resetFieldArray());
1221
1235
  resetRefs(omitResetState);
1222
1236
  };
1223
1237
  useEffect(() => {
@@ -1249,15 +1263,13 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1249
1263
  register: useCallback(register, [defaultValuesRef.current]),
1250
1264
  unregister: useCallback(unregister, []),
1251
1265
  };
1252
- const control = Object.assign({ renderWatchedInputs,
1266
+ const control = Object.assign({ updateWatchedValue,
1253
1267
  shouldUnregister,
1254
1268
  removeFieldEventListener,
1255
1269
  watchInternal, mode: modeRef.current, reValidateMode: {
1256
1270
  isReValidateOnBlur,
1257
1271
  isReValidateOnChange,
1258
1272
  }, fieldsRef,
1259
- isWatchAllRef,
1260
- watchFieldsRef,
1261
1273
  resetFieldArrayFunctionRef,
1262
1274
  useWatchFieldsRef,
1263
1275
  useWatchRenderFunctionsRef,
@@ -1405,7 +1417,7 @@ const useFieldArray = ({ control, name, keyName = 'id', }) => {
1405
1417
  }
1406
1418
  }
1407
1419
  const focusIndexRef = useRef(-1);
1408
- const { isWatchAllRef, resetFieldArrayFunctionRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, removeFieldEventListener, formStateRef, shallowFieldsStateRef, updateFormState, readFormStateRef, watchFieldsRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, validateResolver, renderWatchedInputs, getValues, shouldUnregister, } = control || methods.control;
1420
+ const { updateWatchedValue, resetFieldArrayFunctionRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, removeFieldEventListener, formStateRef, shallowFieldsStateRef, updateFormState, readFormStateRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, validateResolver, getValues, shouldUnregister, } = control || methods.control;
1409
1421
  const fieldArrayParentName = getFieldArrayParentName(name);
1410
1422
  const memoizedDefaultValues = useRef([
1411
1423
  ...(get(fieldArrayDefaultValuesRef.current, fieldArrayParentName)
@@ -1605,20 +1617,7 @@ const useFieldArray = ({ control, name, keyName = 'id', }) => {
1605
1617
  defaultValues.pop();
1606
1618
  set(fieldArrayDefaultValuesRef.current, name, defaultValues);
1607
1619
  }
1608
- if (isWatchAllRef.current) {
1609
- updateFormState();
1610
- }
1611
- else if (watchFieldsRef) {
1612
- let shouldRenderUseWatch = true;
1613
- for (const watchField of watchFieldsRef.current) {
1614
- if (watchField.startsWith(name)) {
1615
- updateFormState();
1616
- shouldRenderUseWatch = false;
1617
- break;
1618
- }
1619
- }
1620
- shouldRenderUseWatch && renderWatchedInputs(name);
1621
- }
1620
+ updateWatchedValue(name);
1622
1621
  if (focusIndexRef.current > -1) {
1623
1622
  for (const key in fieldsRef.current) {
1624
1623
  const field = fieldsRef.current[key];
@@ -1670,7 +1669,7 @@ function useWatch({ control, name, defaultValue, }) {
1670
1669
  : defaultValuesRef.current
1671
1670
  : defaultValue);
1672
1671
  const idRef = useRef();
1673
- const defaultValueRef = useRef(value);
1672
+ const defaultValueRef = useRef(defaultValue);
1674
1673
  const updateWatchValue = useCallback(() => {
1675
1674
  const value = watchInternal(name, defaultValueRef.current, idRef.current);
1676
1675
  setValue(isObject(value)