react-hook-form 7.53.0 → 7.53.1

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.
@@ -551,6 +551,22 @@ function useController(props) {
551
551
  */
552
552
  const Controller = (props) => props.render(useController(props));
553
553
 
554
+ const flatten = (obj) => {
555
+ const output = {};
556
+ for (const key of Object.keys(obj)) {
557
+ if (isObjectType(obj[key])) {
558
+ const nested = flatten(obj[key]);
559
+ for (const nestedKey of Object.keys(nested)) {
560
+ output[`${key}.${nestedKey}`] = nested[nestedKey];
561
+ }
562
+ }
563
+ else {
564
+ output[key] = obj[key];
565
+ }
566
+ }
567
+ return output;
568
+ };
569
+
554
570
  const POST_REQUEST = 'post';
555
571
  /**
556
572
  * Form component to manage submission.
@@ -588,8 +604,9 @@ function Form(props) {
588
604
  formDataJson = JSON.stringify(data);
589
605
  }
590
606
  catch (_a) { }
591
- for (const name of control._names.mount) {
592
- formData.append(name, get(data, name));
607
+ const flattenFormValues = flatten(control._formValues);
608
+ for (const key in flattenFormValues) {
609
+ formData.append(key, flattenFormValues[key]);
593
610
  }
594
611
  if (onSubmit) {
595
612
  await onSubmit({
@@ -1601,7 +1618,7 @@ function createFormControl(props = {}) {
1601
1618
  timer = setTimeout(callback, wait);
1602
1619
  };
1603
1620
  const _updateValid = async (shouldUpdateValid) => {
1604
- if (_proxyFormState.isValid || shouldUpdateValid) {
1621
+ if (!props.disabled && (_proxyFormState.isValid || shouldUpdateValid)) {
1605
1622
  const isValid = _options.resolver
1606
1623
  ? isEmptyObject((await _executeSchema()).errors)
1607
1624
  : await executeBuiltInValidation(_fields, true);
@@ -1613,7 +1630,8 @@ function createFormControl(props = {}) {
1613
1630
  }
1614
1631
  };
1615
1632
  const _updateIsValidating = (names, isValidating) => {
1616
- if (_proxyFormState.isValidating || _proxyFormState.validatingFields) {
1633
+ if (!props.disabled &&
1634
+ (_proxyFormState.isValidating || _proxyFormState.validatingFields)) {
1617
1635
  (names || Array.from(_names.mount)).forEach((name) => {
1618
1636
  if (name) {
1619
1637
  isValidating
@@ -1628,7 +1646,7 @@ function createFormControl(props = {}) {
1628
1646
  }
1629
1647
  };
1630
1648
  const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
1631
- if (args && method) {
1649
+ if (args && method && !props.disabled) {
1632
1650
  _state.action = true;
1633
1651
  if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
1634
1652
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
@@ -1692,38 +1710,40 @@ function createFormControl(props = {}) {
1692
1710
  const output = {
1693
1711
  name,
1694
1712
  };
1695
- const disabledField = !!(get(_fields, name) &&
1696
- get(_fields, name)._f &&
1697
- get(_fields, name)._f.disabled);
1698
- if (!isBlurEvent || shouldDirty) {
1699
- if (_proxyFormState.isDirty) {
1700
- isPreviousDirty = _formState.isDirty;
1701
- _formState.isDirty = output.isDirty = _getDirty();
1702
- shouldUpdateField = isPreviousDirty !== output.isDirty;
1703
- }
1704
- const isCurrentFieldPristine = disabledField || deepEqual(get(_defaultValues, name), fieldValue);
1705
- isPreviousDirty = !!(!disabledField && get(_formState.dirtyFields, name));
1706
- isCurrentFieldPristine || disabledField
1707
- ? unset(_formState.dirtyFields, name)
1708
- : set(_formState.dirtyFields, name, true);
1709
- output.dirtyFields = _formState.dirtyFields;
1710
- shouldUpdateField =
1711
- shouldUpdateField ||
1712
- (_proxyFormState.dirtyFields &&
1713
- isPreviousDirty !== !isCurrentFieldPristine);
1714
- }
1715
- if (isBlurEvent) {
1716
- const isPreviousFieldTouched = get(_formState.touchedFields, name);
1717
- if (!isPreviousFieldTouched) {
1718
- set(_formState.touchedFields, name, isBlurEvent);
1719
- output.touchedFields = _formState.touchedFields;
1713
+ if (!props.disabled) {
1714
+ const disabledField = !!(get(_fields, name) &&
1715
+ get(_fields, name)._f &&
1716
+ get(_fields, name)._f.disabled);
1717
+ if (!isBlurEvent || shouldDirty) {
1718
+ if (_proxyFormState.isDirty) {
1719
+ isPreviousDirty = _formState.isDirty;
1720
+ _formState.isDirty = output.isDirty = _getDirty();
1721
+ shouldUpdateField = isPreviousDirty !== output.isDirty;
1722
+ }
1723
+ const isCurrentFieldPristine = disabledField || deepEqual(get(_defaultValues, name), fieldValue);
1724
+ isPreviousDirty = !!(!disabledField && get(_formState.dirtyFields, name));
1725
+ isCurrentFieldPristine || disabledField
1726
+ ? unset(_formState.dirtyFields, name)
1727
+ : set(_formState.dirtyFields, name, true);
1728
+ output.dirtyFields = _formState.dirtyFields;
1720
1729
  shouldUpdateField =
1721
1730
  shouldUpdateField ||
1722
- (_proxyFormState.touchedFields &&
1723
- isPreviousFieldTouched !== isBlurEvent);
1731
+ (_proxyFormState.dirtyFields &&
1732
+ isPreviousDirty !== !isCurrentFieldPristine);
1724
1733
  }
1734
+ if (isBlurEvent) {
1735
+ const isPreviousFieldTouched = get(_formState.touchedFields, name);
1736
+ if (!isPreviousFieldTouched) {
1737
+ set(_formState.touchedFields, name, isBlurEvent);
1738
+ output.touchedFields = _formState.touchedFields;
1739
+ shouldUpdateField =
1740
+ shouldUpdateField ||
1741
+ (_proxyFormState.touchedFields &&
1742
+ isPreviousFieldTouched !== isBlurEvent);
1743
+ }
1744
+ }
1745
+ shouldUpdateField && shouldRender && _subjects.state.next(output);
1725
1746
  }
1726
- shouldUpdateField && shouldRender && _subjects.state.next(output);
1727
1747
  return shouldUpdateField ? output : {};
1728
1748
  };
1729
1749
  const shouldRenderByError = (name, isValid, error, fieldState) => {
@@ -1826,8 +1846,9 @@ function createFormControl(props = {}) {
1826
1846
  }
1827
1847
  _names.unMount = new Set();
1828
1848
  };
1829
- const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
1830
- !deepEqual(getValues(), _defaultValues));
1849
+ const _getDirty = (name, data) => !props.disabled &&
1850
+ (name && data && set(_formValues, name, data),
1851
+ !deepEqual(getValues(), _defaultValues));
1831
1852
  const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
1832
1853
  ...(_state.mount
1833
1854
  ? _formValues
@@ -1891,7 +1912,7 @@ function createFormControl(props = {}) {
1891
1912
  const fieldName = `${name}.${fieldKey}`;
1892
1913
  const field = get(_fields, fieldName);
1893
1914
  (_names.array.has(name) ||
1894
- !isPrimitive(fieldValue) ||
1915
+ isObject(fieldValue) ||
1895
1916
  (field && !field._f)) &&
1896
1917
  !isDateObject(fieldValue)
1897
1918
  ? setValues(fieldName, fieldValue, options)
@@ -1938,6 +1959,7 @@ function createFormControl(props = {}) {
1938
1959
  const _updateIsFieldValueUpdated = (fieldValue) => {
1939
1960
  isFieldValueUpdated =
1940
1961
  Number.isNaN(fieldValue) ||
1962
+ (isDateObject(fieldValue) && isNaN(fieldValue.getTime())) ||
1941
1963
  deepEqual(fieldValue, get(_formValues, name, fieldValue));
1942
1964
  };
1943
1965
  if (field) {
@@ -2327,7 +2349,11 @@ function createFormControl(props = {}) {
2327
2349
  }
2328
2350
  if (!keepStateOptions.keepValues) {
2329
2351
  if (keepStateOptions.keepDirtyValues) {
2330
- for (const fieldName of _names.mount) {
2352
+ const fieldsToCheck = new Set([
2353
+ ..._names.mount,
2354
+ ...Object.keys(getDirtyFields(_defaultValues, _formValues)),
2355
+ ]);
2356
+ for (const fieldName of Array.from(fieldsToCheck)) {
2331
2357
  get(_formState.dirtyFields, fieldName)
2332
2358
  ? set(values, fieldName, get(_formValues, fieldName))
2333
2359
  : setValue(fieldName, get(values, fieldName));
@@ -2625,6 +2651,11 @@ function useForm(props = {}) {
2625
2651
  values: control._getWatch(),
2626
2652
  });
2627
2653
  }, [props.shouldUnregister, control]);
2654
+ React.useEffect(() => {
2655
+ if (_formControl.current) {
2656
+ _formControl.current.watch = _formControl.current.watch.bind({});
2657
+ }
2658
+ }, [formState]);
2628
2659
  _formControl.current.formState = getProxyFormState(formState, control);
2629
2660
  return _formControl.current;
2630
2661
  }