react-hook-form 7.49.2 → 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.
@@ -447,7 +447,7 @@ function useController(props) {
447
447
  field: {
448
448
  name,
449
449
  value,
450
- ...(isBoolean(disabled) || isBoolean(formState.disabled)
450
+ ...(isBoolean(disabled) || formState.disabled
451
451
  ? { disabled: formState.disabled || disabled }
452
452
  : {}),
453
453
  onChange: React.useCallback((event) => _registerProps.current.onChange({
@@ -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),
@@ -1532,11 +1535,11 @@ function createFormControl(props = {}, flushRootRender) {
1532
1535
  touchedFields: {},
1533
1536
  dirtyFields: {},
1534
1537
  errors: _options.errors || {},
1535
- disabled: false,
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
  ? {}
@@ -1567,7 +1570,6 @@ function createFormControl(props = {}, flushRootRender) {
1567
1570
  array: createSubject(),
1568
1571
  state: createSubject(),
1569
1572
  };
1570
- const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
1571
1573
  const validationModeBeforeSubmit = getValidationModes(_options.mode);
1572
1574
  const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
1573
1575
  const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
@@ -2175,6 +2177,7 @@ function createFormControl(props = {}, flushRootRender) {
2175
2177
  }
2176
2178
  };
2177
2179
  const handleSubmit = (onValid, onInvalid) => async (e) => {
2180
+ let onValidError = undefined;
2178
2181
  if (e) {
2179
2182
  e.preventDefault && e.preventDefault();
2180
2183
  e.persist && e.persist();
@@ -2196,7 +2199,12 @@ function createFormControl(props = {}, flushRootRender) {
2196
2199
  _subjects.state.next({
2197
2200
  errors: {},
2198
2201
  });
2199
- await onValid(fieldValues, e);
2202
+ try {
2203
+ await onValid(fieldValues, e);
2204
+ }
2205
+ catch (error) {
2206
+ onValidError = error;
2207
+ }
2200
2208
  }
2201
2209
  else {
2202
2210
  if (onInvalid) {
@@ -2208,19 +2216,22 @@ function createFormControl(props = {}, flushRootRender) {
2208
2216
  _subjects.state.next({
2209
2217
  isSubmitted: true,
2210
2218
  isSubmitting: false,
2211
- isSubmitSuccessful: isEmptyObject(_formState.errors),
2219
+ isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
2212
2220
  submitCount: _formState.submitCount + 1,
2213
2221
  errors: _formState.errors,
2214
2222
  });
2223
+ if (onValidError) {
2224
+ throw onValidError;
2225
+ }
2215
2226
  };
2216
2227
  const resetField = (name, options = {}) => {
2217
2228
  if (get(_fields, name)) {
2218
2229
  if (isUndefined(options.defaultValue)) {
2219
- setValue(name, get(_defaultValues, name));
2230
+ setValue(name, cloneObject(get(_defaultValues, name)));
2220
2231
  }
2221
2232
  else {
2222
2233
  setValue(name, options.defaultValue);
2223
- set(_defaultValues, name, options.defaultValue);
2234
+ set(_defaultValues, name, cloneObject(options.defaultValue));
2224
2235
  }
2225
2236
  if (!options.keepTouched) {
2226
2237
  unset(_formState.touchedFields, name);
@@ -2228,7 +2239,7 @@ function createFormControl(props = {}, flushRootRender) {
2228
2239
  if (!options.keepDirty) {
2229
2240
  unset(_formState.dirtyFields, name);
2230
2241
  _formState.isDirty = options.defaultValue
2231
- ? _getDirty(name, get(_defaultValues, name))
2242
+ ? _getDirty(name, cloneObject(get(_defaultValues, name)))
2232
2243
  : _getDirty();
2233
2244
  }
2234
2245
  if (!options.keepError) {
@@ -2248,7 +2259,7 @@ function createFormControl(props = {}, flushRootRender) {
2248
2259
  _defaultValues = updatedValues;
2249
2260
  }
2250
2261
  if (!keepStateOptions.keepValues) {
2251
- if (keepStateOptions.keepDirtyValues || shouldCaptureDirtyFields) {
2262
+ if (keepStateOptions.keepDirtyValues) {
2252
2263
  for (const fieldName of _names.mount) {
2253
2264
  get(_formState.dirtyFields, fieldName)
2254
2265
  ? set(values, fieldName, get(_formValues, fieldName))
@@ -2296,7 +2307,10 @@ function createFormControl(props = {}, flushRootRender) {
2296
2307
  focus: '',
2297
2308
  };
2298
2309
  !_state.mount && flushRootRender();
2299
- _state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
2310
+ _state.mount =
2311
+ !_proxyFormState.isValid ||
2312
+ !!keepStateOptions.keepIsValid ||
2313
+ !!keepStateOptions.keepDirtyValues;
2300
2314
  _state.watch = !!props.shouldUnregister;
2301
2315
  _subjects.state.next({
2302
2316
  submitCount: keepStateOptions.keepSubmitCount
@@ -2310,7 +2324,9 @@ function createFormControl(props = {}, flushRootRender) {
2310
2324
  ? _formState.isSubmitted
2311
2325
  : false,
2312
2326
  dirtyFields: keepStateOptions.keepDirtyValues
2313
- ? _formState.dirtyFields
2327
+ ? keepStateOptions.keepDefaultValues && _formValues
2328
+ ? getDirtyFields(_defaultValues, _formValues)
2329
+ : _formState.dirtyFields
2314
2330
  : keepStateOptions.keepDefaultValues && formValues
2315
2331
  ? getDirtyFields(_defaultValues, formValues)
2316
2332
  : {},
@@ -2472,7 +2488,7 @@ function useForm(props = {}) {
2472
2488
  dirtyFields: {},
2473
2489
  touchedFields: {},
2474
2490
  errors: props.errors || {},
2475
- disabled: false,
2491
+ disabled: props.disabled || false,
2476
2492
  defaultValues: isFunction(props.defaultValues)
2477
2493
  ? undefined
2478
2494
  : props.defaultValues,
@@ -2530,6 +2546,12 @@ function useForm(props = {}) {
2530
2546
  }
2531
2547
  control._removeUnmounted();
2532
2548
  });
2549
+ React.useEffect(() => {
2550
+ props.shouldUnregister &&
2551
+ control._subjects.values.next({
2552
+ values: control._getWatch(),
2553
+ });
2554
+ }, [props.shouldUnregister, control]);
2533
2555
  _formControl.current.formState = getProxyFormState(formState, control);
2534
2556
  return _formControl.current;
2535
2557
  }