react-hook-form 7.44.0-next.1 → 7.44.0-rc.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.
@@ -23,6 +23,42 @@ var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/))
23
23
 
24
24
  var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
25
25
 
26
+ var isPlainObject = (tempObject) => {
27
+ const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
28
+ return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
29
+ };
30
+
31
+ var isWeb = typeof window !== 'undefined' &&
32
+ typeof window.HTMLElement !== 'undefined' &&
33
+ typeof document !== 'undefined';
34
+
35
+ function cloneObject(data) {
36
+ let copy;
37
+ const isArray = Array.isArray(data);
38
+ if (data instanceof Date) {
39
+ copy = new Date(data);
40
+ }
41
+ else if (data instanceof Set) {
42
+ copy = new Set(data);
43
+ }
44
+ else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
45
+ (isArray || isObject(data))) {
46
+ copy = isArray ? [] : {};
47
+ if (!Array.isArray(data) && !isPlainObject(data)) {
48
+ copy = data;
49
+ }
50
+ else {
51
+ for (const key in data) {
52
+ copy[key] = cloneObject(data[key]);
53
+ }
54
+ }
55
+ }
56
+ else {
57
+ return data;
58
+ }
59
+ return copy;
60
+ }
61
+
26
62
  var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
27
63
 
28
64
  var isUndefined = (val) => val === undefined;
@@ -174,6 +210,7 @@ function useSubscribe(props) {
174
210
  _props.current = props;
175
211
  React__default.useEffect(() => {
176
212
  const subscription = !props.disabled &&
213
+ _props.current.subject &&
177
214
  _props.current.subject.subscribe({
178
215
  next: _props.current.next,
179
216
  });
@@ -270,42 +307,6 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
270
307
  return formValues;
271
308
  };
272
309
 
273
- var isPlainObject = (tempObject) => {
274
- const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
275
- return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
276
- };
277
-
278
- var isWeb = typeof window !== 'undefined' &&
279
- typeof window.HTMLElement !== 'undefined' &&
280
- typeof document !== 'undefined';
281
-
282
- function cloneObject(data) {
283
- let copy;
284
- const isArray = Array.isArray(data);
285
- if (data instanceof Date) {
286
- copy = new Date(data);
287
- }
288
- else if (data instanceof Set) {
289
- copy = new Set(data);
290
- }
291
- else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
292
- (isArray || isObject(data))) {
293
- copy = isArray ? [] : {};
294
- if (!Array.isArray(data) && !isPlainObject(data)) {
295
- copy = data;
296
- }
297
- else {
298
- for (const key in data) {
299
- copy[key] = cloneObject(data[key]);
300
- }
301
- }
302
- }
303
- else {
304
- return data;
305
- }
306
- return copy;
307
- }
308
-
309
310
  /**
310
311
  * Custom hook to subscribe to field change and isolate re-rendering at the component level.
311
312
  *
@@ -329,7 +330,7 @@ function useWatch(props) {
329
330
  _name.current = name;
330
331
  useSubscribe({
331
332
  disabled,
332
- subject: control._subjects.watch,
333
+ subject: control._subjects.values,
333
334
  next: (formState) => {
334
335
  if (shouldSubscribeByName(_name.current, formState.name, exact)) {
335
336
  updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
@@ -341,6 +342,33 @@ function useWatch(props) {
341
342
  return value;
342
343
  }
343
344
 
345
+ var isKey = (value) => /^\w*$/.test(value);
346
+
347
+ var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
348
+
349
+ function set(object, path, value) {
350
+ let index = -1;
351
+ const tempPath = isKey(path) ? [path] : stringToPath(path);
352
+ const length = tempPath.length;
353
+ const lastIndex = length - 1;
354
+ while (++index < length) {
355
+ const key = tempPath[index];
356
+ let newValue = value;
357
+ if (index !== lastIndex) {
358
+ const objValue = object[key];
359
+ newValue =
360
+ isObject(objValue) || Array.isArray(objValue)
361
+ ? objValue
362
+ : !isNaN(+tempPath[index + 1])
363
+ ? []
364
+ : {};
365
+ }
366
+ object[key] = newValue;
367
+ object = object[key];
368
+ }
369
+ return object;
370
+ }
371
+
344
372
  /**
345
373
  * Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
346
374
  *
@@ -384,6 +412,7 @@ function useController(props) {
384
412
  value,
385
413
  }));
386
414
  React__default.useEffect(() => {
415
+ const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
387
416
  const updateMounted = (name, value) => {
388
417
  const field = get(control._fields, name);
389
418
  if (field) {
@@ -391,10 +420,12 @@ function useController(props) {
391
420
  }
392
421
  };
393
422
  updateMounted(name, true);
423
+ if (_shouldUnregisterField) {
424
+ set(control._defaultValues, name, cloneObject(get(control._options.defaultValues, name)));
425
+ }
394
426
  return () => {
395
- const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
396
427
  (isArrayField
397
- ? _shouldUnregisterField && !control._stateFlags.action
428
+ ? _shouldUnregisterField && !control._state.action
398
429
  : _shouldUnregisterField)
399
430
  ? control.unregister(name)
400
431
  : updateMounted(name, false);
@@ -523,36 +554,33 @@ function Form(props) {
523
554
  const methods = useFormContext();
524
555
  const [mounted, setMounted] = React.useState(false);
525
556
  const { control = methods.control, onSubmit, children, action, method = POST_REQUEST, headers, encType, onError, render, onSuccess, validateStatus, fetcher, ...rest } = props;
526
- const isPostRequest = method === POST_REQUEST;
527
557
  const submit = async (event) => {
528
558
  let serverError = false;
529
559
  await control.handleSubmit(async (values) => {
560
+ const formData = new FormData();
561
+ const formDataJson = JSON.stringify(values);
562
+ control._names.mount.forEach((name) => formData.append(name, get(values, name)));
530
563
  onSubmit && onSubmit(values);
531
564
  if (action) {
532
- const formData = new FormData();
533
- let shouldStringifySubmissionData = false;
534
- if (headers) {
535
- shouldStringifySubmissionData =
536
- headers['Content-Type'].includes('json');
537
- }
538
- else {
539
- control._names.mount.forEach((name) => formData.append(name, get(values, name)));
540
- }
541
- if (!fetcher) {
542
- try {
565
+ try {
566
+ if (fetcher) {
567
+ await fetcher(action, {
568
+ method,
569
+ values,
570
+ event,
571
+ formData,
572
+ formDataJson,
573
+ });
574
+ }
575
+ else {
576
+ const shouldStringifySubmissionData = headers && headers['Content-Type'].includes('json');
543
577
  const response = await fetch(action, {
544
578
  method,
545
579
  headers: {
546
580
  ...headers,
547
581
  ...(encType ? { 'Content-Type': encType } : {}),
548
582
  },
549
- ...(isPostRequest
550
- ? {
551
- body: shouldStringifySubmissionData
552
- ? JSON.stringify(values)
553
- : formData,
554
- }
555
- : {}),
583
+ body: shouldStringifySubmissionData ? formDataJson : formData,
556
584
  });
557
585
  if (response &&
558
586
  (validateStatus
@@ -565,17 +593,10 @@ function Form(props) {
565
593
  onSuccess && onSuccess({ response });
566
594
  }
567
595
  }
568
- catch (error) {
569
- serverError = true;
570
- onError && onError({ error });
571
- }
572
596
  }
573
- else {
574
- await fetcher(action, {
575
- method,
576
- values,
577
- event,
578
- });
597
+ catch (error) {
598
+ serverError = true;
599
+ onError && onError({ error });
579
600
  }
580
601
  }
581
602
  })(event);
@@ -603,33 +624,6 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
603
624
  }
604
625
  : {};
605
626
 
606
- var isKey = (value) => /^\w*$/.test(value);
607
-
608
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
609
-
610
- function set(object, path, value) {
611
- let index = -1;
612
- const tempPath = isKey(path) ? [path] : stringToPath(path);
613
- const length = tempPath.length;
614
- const lastIndex = length - 1;
615
- while (++index < length) {
616
- const key = tempPath[index];
617
- let newValue = value;
618
- if (index !== lastIndex) {
619
- const objValue = object[key];
620
- newValue =
621
- isObject(objValue) || Array.isArray(objValue)
622
- ? objValue
623
- : !isNaN(+tempPath[index + 1])
624
- ? []
625
- : {};
626
- }
627
- object[key] = newValue;
628
- object = object[key];
629
- }
630
- return object;
631
- }
632
-
633
627
  const focusFieldBy = (fields, callback, fieldsNames) => {
634
628
  for (const key of fieldsNames || Object.keys(fields)) {
635
629
  const field = get(fields, key);
@@ -1180,8 +1174,11 @@ function useFieldArray(props) {
1180
1174
  control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
1181
1175
  };
1182
1176
  React__default.useEffect(() => {
1183
- control._stateFlags.action = false;
1184
- isWatched(name, control._names) && control._subjects.state.next({});
1177
+ control._state.action = false;
1178
+ isWatched(name, control._names) &&
1179
+ control._subjects.state.next({
1180
+ ...control._formState,
1181
+ });
1185
1182
  if (_actioned.current &&
1186
1183
  (!getValidationModes(control._options.mode).isOnSubmit ||
1187
1184
  control._formState.isSubmitted)) {
@@ -1209,9 +1206,9 @@ function useFieldArray(props) {
1209
1206
  }
1210
1207
  }
1211
1208
  }
1212
- control._subjects.watch.next({
1209
+ control._subjects.values.next({
1213
1210
  name,
1214
- values: control._formValues,
1211
+ values: { ...control._formValues },
1215
1212
  });
1216
1213
  control._names.focus &&
1217
1214
  focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus || ''));
@@ -1245,7 +1242,7 @@ function createSubject() {
1245
1242
  let _observers = [];
1246
1243
  const next = (value) => {
1247
1244
  for (const observer of _observers) {
1248
- observer.next(value);
1245
+ observer.next && observer.next(value);
1249
1246
  }
1250
1247
  };
1251
1248
  const subscribe = (observer) => {
@@ -1350,9 +1347,7 @@ function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues
1350
1347
  }
1351
1348
  }
1352
1349
  else {
1353
- deepEqual(data[key], formValues[key])
1354
- ? delete dirtyFieldsFromValues[key]
1355
- : (dirtyFieldsFromValues[key] = true);
1350
+ dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
1356
1351
  }
1357
1352
  }
1358
1353
  }
@@ -1484,7 +1479,6 @@ function createFormControl(props = {}, flushRootRender) {
1484
1479
  ...defaultOptions,
1485
1480
  ...props,
1486
1481
  };
1487
- const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
1488
1482
  let _formState = {
1489
1483
  submitCount: 0,
1490
1484
  isDirty: false,
@@ -1505,7 +1499,7 @@ function createFormControl(props = {}, flushRootRender) {
1505
1499
  let _formValues = _options.shouldUnregister
1506
1500
  ? {}
1507
1501
  : cloneObject(_defaultValues);
1508
- let _stateFlags = {
1502
+ let _state = {
1509
1503
  action: false,
1510
1504
  mount: false,
1511
1505
  watch: false,
@@ -1527,16 +1521,17 @@ function createFormControl(props = {}, flushRootRender) {
1527
1521
  errors: false,
1528
1522
  };
1529
1523
  const _subjects = {
1530
- watch: createSubject(),
1524
+ values: createSubject(),
1531
1525
  array: createSubject(),
1532
1526
  state: createSubject(),
1533
1527
  };
1528
+ const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
1534
1529
  const validationModeBeforeSubmit = getValidationModes(_options.mode);
1535
1530
  const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
1536
1531
  const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
1537
1532
  const debounce = (callback) => (wait) => {
1538
1533
  clearTimeout(timer);
1539
- timer = window.setTimeout(callback, wait);
1534
+ timer = setTimeout(callback, wait);
1540
1535
  };
1541
1536
  const _updateValid = async (shouldUpdateValid) => {
1542
1537
  if (_proxyFormState.isValid || shouldUpdateValid) {
@@ -1556,7 +1551,7 @@ function createFormControl(props = {}, flushRootRender) {
1556
1551
  });
1557
1552
  const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
1558
1553
  if (args && method) {
1559
- _stateFlags.action = true;
1554
+ _state.action = true;
1560
1555
  if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
1561
1556
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
1562
1557
  shouldSetValues && set(_fields, name, fieldValues);
@@ -1603,7 +1598,7 @@ function createFormControl(props = {}, flushRootRender) {
1603
1598
  shouldSkipSetValueAs
1604
1599
  ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
1605
1600
  : setFieldValue(name, defaultValue);
1606
- _stateFlags.mount && _updateValid();
1601
+ _state.mount && _updateValid();
1607
1602
  }
1608
1603
  };
1609
1604
  const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
@@ -1676,7 +1671,7 @@ function createFormControl(props = {}, flushRootRender) {
1676
1671
  }
1677
1672
  _updateIsValidating(false);
1678
1673
  };
1679
- const _executeSchema = async (name) => await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1674
+ const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
1680
1675
  const executeSchemaAndUpdateState = async (names) => {
1681
1676
  const { errors } = await _executeSchema();
1682
1677
  if (names) {
@@ -1701,7 +1696,7 @@ function createFormControl(props = {}, flushRootRender) {
1701
1696
  const { _f, ...fieldValue } = field;
1702
1697
  if (_f) {
1703
1698
  const isFieldArrayRoot = _names.array.has(_f.name);
1704
- const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, isFieldArrayRoot);
1699
+ const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
1705
1700
  if (fieldError[_f.name]) {
1706
1701
  context.valid = false;
1707
1702
  if (shouldOnlyCheckValid) {
@@ -1735,7 +1730,7 @@ function createFormControl(props = {}, flushRootRender) {
1735
1730
  const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
1736
1731
  !deepEqual(getValues(), _defaultValues));
1737
1732
  const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
1738
- ...(_stateFlags.mount
1733
+ ...(_state.mount
1739
1734
  ? _formValues
1740
1735
  : isUndefined(defaultValue)
1741
1736
  ? _defaultValues
@@ -1743,7 +1738,7 @@ function createFormControl(props = {}, flushRootRender) {
1743
1738
  ? { [names]: defaultValue }
1744
1739
  : defaultValue),
1745
1740
  }, isGlobal, defaultValue);
1746
- const _getFieldArray = (name) => compact(get(_stateFlags.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
1741
+ const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
1747
1742
  const setFieldValue = (name, value, options = {}) => {
1748
1743
  const field = get(_fields, name);
1749
1744
  let fieldValue = value;
@@ -1779,8 +1774,9 @@ function createFormControl(props = {}, flushRootRender) {
1779
1774
  else {
1780
1775
  fieldReference.ref.value = fieldValue;
1781
1776
  if (!fieldReference.ref.type) {
1782
- _subjects.watch.next({
1777
+ _subjects.values.next({
1783
1778
  name,
1779
+ values: { ..._formValues },
1784
1780
  });
1785
1781
  }
1786
1782
  }
@@ -1811,7 +1807,7 @@ function createFormControl(props = {}, flushRootRender) {
1811
1807
  if (isFieldArray) {
1812
1808
  _subjects.array.next({
1813
1809
  name,
1814
- values: _formValues,
1810
+ values: { ..._formValues },
1815
1811
  });
1816
1812
  if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
1817
1813
  options.shouldDirty) {
@@ -1827,15 +1823,17 @@ function createFormControl(props = {}, flushRootRender) {
1827
1823
  ? setValues(name, cloneValue, options)
1828
1824
  : setFieldValue(name, cloneValue, options);
1829
1825
  }
1830
- isWatched(name, _names) && _subjects.state.next({});
1831
- _subjects.watch.next({
1826
+ isWatched(name, _names) && _subjects.state.next({ ..._formState });
1827
+ _subjects.values.next({
1832
1828
  name,
1829
+ values: { ..._formValues },
1833
1830
  });
1834
- !_stateFlags.mount && flushRootRender();
1831
+ !_state.mount && flushRootRender();
1835
1832
  };
1836
1833
  const onChange = async (event) => {
1837
1834
  const target = event.target;
1838
1835
  let name = target.name;
1836
+ let isFieldValueUpdated = true;
1839
1837
  const field = get(_fields, name);
1840
1838
  const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
1841
1839
  if (field) {
@@ -1860,16 +1858,17 @@ function createFormControl(props = {}, flushRootRender) {
1860
1858
  const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
1861
1859
  const shouldRender = !isEmptyObject(fieldState) || watched;
1862
1860
  !isBlurEvent &&
1863
- _subjects.watch.next({
1861
+ _subjects.values.next({
1864
1862
  name,
1865
1863
  type: event.type,
1864
+ values: { ..._formValues },
1866
1865
  });
1867
1866
  if (shouldSkipValidation) {
1868
1867
  _proxyFormState.isValid && _updateValid();
1869
1868
  return (shouldRender &&
1870
1869
  _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
1871
1870
  }
1872
- !isBlurEvent && watched && _subjects.state.next({});
1871
+ !isBlurEvent && watched && _subjects.state.next({ ..._formState });
1873
1872
  _updateIsValidating(true);
1874
1873
  if (_options.resolver) {
1875
1874
  const { errors } = await _executeSchema([name]);
@@ -1881,16 +1880,21 @@ function createFormControl(props = {}, flushRootRender) {
1881
1880
  }
1882
1881
  else {
1883
1882
  error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1884
- if (error) {
1885
- isValid = false;
1886
- }
1887
- else if (_proxyFormState.isValid) {
1888
- isValid = await executeBuiltInValidation(_fields, true);
1883
+ isFieldValueUpdated = fieldValue === get(_formValues, name, fieldValue);
1884
+ if (isFieldValueUpdated) {
1885
+ if (error) {
1886
+ isValid = false;
1887
+ }
1888
+ else if (_proxyFormState.isValid) {
1889
+ isValid = await executeBuiltInValidation(_fields, true);
1890
+ }
1889
1891
  }
1890
1892
  }
1891
- field._f.deps &&
1892
- trigger(field._f.deps);
1893
- shouldRenderByError(name, isValid, error, fieldState);
1893
+ if (isFieldValueUpdated) {
1894
+ field._f.deps &&
1895
+ trigger(field._f.deps);
1896
+ shouldRenderByError(name, isValid, error, fieldState);
1897
+ }
1894
1898
  }
1895
1899
  };
1896
1900
  const trigger = async (name, options = {}) => {
@@ -1932,7 +1936,7 @@ function createFormControl(props = {}, flushRootRender) {
1932
1936
  const getValues = (fieldNames) => {
1933
1937
  const values = {
1934
1938
  ..._defaultValues,
1935
- ...(_stateFlags.mount ? _formValues : {}),
1939
+ ...(_state.mount ? _formValues : {}),
1936
1940
  };
1937
1941
  return isUndefined(fieldNames)
1938
1942
  ? values
@@ -1967,7 +1971,7 @@ function createFormControl(props = {}, flushRootRender) {
1967
1971
  options && options.shouldFocus && ref && ref.focus && ref.focus();
1968
1972
  };
1969
1973
  const watch = (name, defaultValue) => isFunction(name)
1970
- ? _subjects.watch.subscribe({
1974
+ ? _subjects.values.subscribe({
1971
1975
  next: (payload) => name(_getWatch(undefined, defaultValue), payload),
1972
1976
  })
1973
1977
  : _getWatch(name, defaultValue, true);
@@ -1988,7 +1992,9 @@ function createFormControl(props = {}, flushRootRender) {
1988
1992
  unset(_defaultValues, fieldName);
1989
1993
  }
1990
1994
  }
1991
- _subjects.watch.next({});
1995
+ _subjects.values.next({
1996
+ values: { ..._formValues },
1997
+ });
1992
1998
  _subjects.state.next({
1993
1999
  ..._formState,
1994
2000
  ...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
@@ -2068,7 +2074,7 @@ function createFormControl(props = {}, flushRootRender) {
2068
2074
  field._f.mount = false;
2069
2075
  }
2070
2076
  (_options.shouldUnregister || options.shouldUnregister) &&
2071
- !(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
2077
+ !(isNameInFieldArray(_names.array, name) && _state.action) &&
2072
2078
  _names.unMount.add(name);
2073
2079
  }
2074
2080
  },
@@ -2105,6 +2111,7 @@ function createFormControl(props = {}, flushRootRender) {
2105
2111
  await onInvalid({ ..._formState.errors }, e);
2106
2112
  }
2107
2113
  _focusError();
2114
+ setTimeout(_focusError);
2108
2115
  }
2109
2116
  _subjects.state.next({
2110
2117
  isSubmitted: true,
@@ -2182,10 +2189,10 @@ function createFormControl(props = {}, flushRootRender) {
2182
2189
  : {}
2183
2190
  : cloneUpdatedValues;
2184
2191
  _subjects.array.next({
2185
- values,
2192
+ values: { ...values },
2186
2193
  });
2187
- _subjects.watch.next({
2188
- values,
2194
+ _subjects.values.next({
2195
+ values: { ...values },
2189
2196
  });
2190
2197
  }
2191
2198
  _names = {
@@ -2196,22 +2203,21 @@ function createFormControl(props = {}, flushRootRender) {
2196
2203
  watchAll: false,
2197
2204
  focus: '',
2198
2205
  };
2199
- !_stateFlags.mount && flushRootRender();
2200
- _stateFlags.mount =
2201
- !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
2202
- _stateFlags.watch = !!props.shouldUnregister;
2206
+ !_state.mount && flushRootRender();
2207
+ _state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
2208
+ _state.watch = !!props.shouldUnregister;
2203
2209
  _subjects.state.next({
2204
2210
  submitCount: keepStateOptions.keepSubmitCount
2205
2211
  ? _formState.submitCount
2206
2212
  : 0,
2207
- isDirty: keepStateOptions.keepDirty || keepStateOptions.keepDirtyValues
2213
+ isDirty: keepStateOptions.keepDirty
2208
2214
  ? _formState.isDirty
2209
2215
  : !!(keepStateOptions.keepDefaultValues &&
2210
2216
  !deepEqual(formValues, _defaultValues)),
2211
2217
  isSubmitted: keepStateOptions.keepIsSubmitted
2212
2218
  ? _formState.isSubmitted
2213
2219
  : false,
2214
- dirtyFields: keepStateOptions.keepDirty || keepStateOptions.keepDirtyValues
2220
+ dirtyFields: keepStateOptions.keepDirtyValues
2215
2221
  ? _formState.dirtyFields
2216
2222
  : keepStateOptions.keepDefaultValues && formValues
2217
2223
  ? getDirtyFields(_defaultValues, formValues)
@@ -2262,7 +2268,6 @@ function createFormControl(props = {}, flushRootRender) {
2262
2268
  handleSubmit,
2263
2269
  setError,
2264
2270
  _executeSchema,
2265
- _focusError,
2266
2271
  _getWatch,
2267
2272
  _getDirty,
2268
2273
  _updateValid,
@@ -2279,11 +2284,11 @@ function createFormControl(props = {}, flushRootRender) {
2279
2284
  get _formValues() {
2280
2285
  return _formValues;
2281
2286
  },
2282
- get _stateFlags() {
2283
- return _stateFlags;
2287
+ get _state() {
2288
+ return _state;
2284
2289
  },
2285
- set _stateFlags(value) {
2286
- _stateFlags = value;
2290
+ set _state(value) {
2291
+ _state = value;
2287
2292
  },
2288
2293
  get _defaultValues() {
2289
2294
  return _defaultValues;
@@ -2389,25 +2394,22 @@ function useForm(props = {}) {
2389
2394
  }
2390
2395
  },
2391
2396
  });
2392
- React__default.useEffect(() => {
2393
- if (!control._stateFlags.mount) {
2394
- control._updateValid();
2395
- control._stateFlags.mount = true;
2396
- }
2397
- if (control._stateFlags.watch) {
2398
- control._stateFlags.watch = false;
2399
- control._subjects.state.next({});
2400
- }
2401
- control._removeUnmounted();
2402
- });
2403
2397
  React__default.useEffect(() => {
2404
2398
  if (props.values && !deepEqual(props.values, control._defaultValues)) {
2405
2399
  control._reset(props.values, control._options.resetOptions);
2406
2400
  }
2407
2401
  }, [props.values, control]);
2408
2402
  React__default.useEffect(() => {
2409
- formState.submitCount && control._focusError();
2410
- }, [control, formState.submitCount]);
2403
+ if (!control._state.mount) {
2404
+ control._updateValid();
2405
+ control._state.mount = true;
2406
+ }
2407
+ if (control._state.watch) {
2408
+ control._state.watch = false;
2409
+ control._subjects.state.next({ ...control._formState });
2410
+ }
2411
+ control._removeUnmounted();
2412
+ });
2411
2413
  _formControl.current.formState = getProxyFormState(formState, control);
2412
2414
  return _formControl.current;
2413
2415
  }