react-hook-form 7.49.3 → 7.50.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.
@@ -1230,7 +1230,10 @@ function useFieldArray(props) {
1230
1230
  }
1231
1231
  else {
1232
1232
  const field = get(control._fields, name);
1233
- if (field && field._f) {
1233
+ if (field &&
1234
+ field._f &&
1235
+ !(getValidationModes(control._options.reValidateMode).isOnSubmit &&
1236
+ getValidationModes(control._options.mode).isOnSubmit)) {
1234
1237
  validateField(field, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
1235
1238
  control._subjects.state.next({
1236
1239
  errors: updateFieldArrayRootError(control._formState.errors, error, name),
@@ -1535,8 +1538,8 @@ function createFormControl(props = {}, flushRootRender) {
1535
1538
  disabled: _options.disabled || false,
1536
1539
  };
1537
1540
  let _fields = {};
1538
- let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
1539
- ? cloneObject(_options.defaultValues || _options.values) || {}
1541
+ let _defaultValues = isObject(_options.values) || isObject(_options.defaultValues)
1542
+ ? cloneObject(_options.values || _options.defaultValues) || {}
1540
1543
  : {};
1541
1544
  let _formValues = _options.shouldUnregister
1542
1545
  ? {}
@@ -2174,6 +2177,7 @@ function createFormControl(props = {}, flushRootRender) {
2174
2177
  }
2175
2178
  };
2176
2179
  const handleSubmit = (onValid, onInvalid) => async (e) => {
2180
+ let onValidError = undefined;
2177
2181
  if (e) {
2178
2182
  e.preventDefault && e.preventDefault();
2179
2183
  e.persist && e.persist();
@@ -2195,7 +2199,12 @@ function createFormControl(props = {}, flushRootRender) {
2195
2199
  _subjects.state.next({
2196
2200
  errors: {},
2197
2201
  });
2198
- await onValid(fieldValues, e);
2202
+ try {
2203
+ await onValid(fieldValues, e);
2204
+ }
2205
+ catch (error) {
2206
+ onValidError = error;
2207
+ }
2199
2208
  }
2200
2209
  else {
2201
2210
  if (onInvalid) {
@@ -2207,10 +2216,13 @@ function createFormControl(props = {}, flushRootRender) {
2207
2216
  _subjects.state.next({
2208
2217
  isSubmitted: true,
2209
2218
  isSubmitting: false,
2210
- isSubmitSuccessful: isEmptyObject(_formState.errors),
2219
+ isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
2211
2220
  submitCount: _formState.submitCount + 1,
2212
2221
  errors: _formState.errors,
2213
2222
  });
2223
+ if (onValidError) {
2224
+ throw onValidError;
2225
+ }
2214
2226
  };
2215
2227
  const resetField = (name, options = {}) => {
2216
2228
  if (get(_fields, name)) {
@@ -2295,7 +2307,10 @@ function createFormControl(props = {}, flushRootRender) {
2295
2307
  focus: '',
2296
2308
  };
2297
2309
  !_state.mount && flushRootRender();
2298
- _state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
2310
+ _state.mount =
2311
+ !_proxyFormState.isValid ||
2312
+ !!keepStateOptions.keepIsValid ||
2313
+ !!keepStateOptions.keepDirtyValues;
2299
2314
  _state.watch = !!props.shouldUnregister;
2300
2315
  _subjects.state.next({
2301
2316
  submitCount: keepStateOptions.keepSubmitCount
@@ -2309,7 +2324,9 @@ function createFormControl(props = {}, flushRootRender) {
2309
2324
  ? _formState.isSubmitted
2310
2325
  : false,
2311
2326
  dirtyFields: keepStateOptions.keepDirtyValues
2312
- ? _formState.dirtyFields
2327
+ ? keepStateOptions.keepDefaultValues && _formValues
2328
+ ? getDirtyFields(_defaultValues, _formValues)
2329
+ : _formState.dirtyFields
2313
2330
  : keepStateOptions.keepDefaultValues && formValues
2314
2331
  ? getDirtyFields(_defaultValues, formValues)
2315
2332
  : {},
@@ -2529,6 +2546,12 @@ function useForm(props = {}) {
2529
2546
  }
2530
2547
  control._removeUnmounted();
2531
2548
  });
2549
+ React.useEffect(() => {
2550
+ props.shouldUnregister &&
2551
+ control._subjects.values.next({
2552
+ values: control._getWatch(),
2553
+ });
2554
+ }, [props.shouldUnregister, control]);
2532
2555
  _formControl.current.formState = getProxyFormState(formState, control);
2533
2556
  return _formControl.current;
2534
2557
  }