react-hook-form 7.30.0 → 7.31.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.
@@ -557,11 +557,7 @@ function cloneObject(data) {
557
557
  else if (isArray || isObject(data)) {
558
558
  copy = isArray ? [] : {};
559
559
  for (const key in data) {
560
- if (isFunction(data[key])) {
561
- copy = data;
562
- break;
563
- }
564
- copy[key] = cloneObject(data[key]);
560
+ copy[key] = isFunction(data[key]) ? data[key] : cloneObject(data[key]);
565
561
  }
566
562
  }
567
563
  else {
@@ -612,6 +608,45 @@ var swapArrayAt = (data, indexA, indexB) => {
612
608
  data[indexA] = [data[indexB], (data[indexB] = data[indexA])][0];
613
609
  };
614
610
 
611
+ function baseGet(object, updatePath) {
612
+ const length = updatePath.slice(0, -1).length;
613
+ let index = 0;
614
+ while (index < length) {
615
+ object = isUndefined(object) ? index++ : object[updatePath[index++]];
616
+ }
617
+ return object;
618
+ }
619
+ function unset(object, path) {
620
+ const updatePath = isKey(path) ? [path] : stringToPath(path);
621
+ const childObject = updatePath.length == 1 ? object : baseGet(object, updatePath);
622
+ const key = updatePath[updatePath.length - 1];
623
+ let previousObjRef;
624
+ if (childObject) {
625
+ delete childObject[key];
626
+ }
627
+ for (let k = 0; k < updatePath.slice(0, -1).length; k++) {
628
+ let index = -1;
629
+ let objectRef;
630
+ const currentPaths = updatePath.slice(0, -(k + 1));
631
+ const currentPathsLength = currentPaths.length - 1;
632
+ if (k > 0) {
633
+ previousObjRef = object;
634
+ }
635
+ while (++index < currentPaths.length) {
636
+ const item = currentPaths[index];
637
+ objectRef = objectRef ? objectRef[item] : object[item];
638
+ if (currentPathsLength === index &&
639
+ ((isObject(objectRef) && isEmptyObject(objectRef)) ||
640
+ (Array.isArray(objectRef) &&
641
+ !objectRef.filter((data) => !isUndefined(data)).length))) {
642
+ previousObjRef ? delete previousObjRef[item] : delete object[item];
643
+ }
644
+ previousObjRef = objectRef;
645
+ }
646
+ }
647
+ return object;
648
+ }
649
+
615
650
  var updateAt = (fieldValues, index, value) => {
616
651
  fieldValues[index] = value;
617
652
  return fieldValues;
@@ -769,8 +804,11 @@ function useFieldArray(props) {
769
804
  if (_actioned.current) {
770
805
  control._executeSchema([name]).then((result) => {
771
806
  const error = get(result.errors, name);
772
- if (error && error.type && !get(control._formState.errors, name)) {
773
- set(control._formState.errors, name, error);
807
+ const existingError = get(control._formState.errors, name);
808
+ if (existingError ? !error && existingError.type : error && error.type) {
809
+ error
810
+ ? set(control._formState.errors, name, error)
811
+ : unset(control._formState.errors, name);
774
812
  control._subjects.state.next({
775
813
  errors: control._formState.errors,
776
814
  });
@@ -896,45 +934,6 @@ var isWeb = typeof window !== 'undefined' &&
896
934
 
897
935
  var live = (ref) => isHTMLElement(ref) && ref.isConnected;
898
936
 
899
- function baseGet(object, updatePath) {
900
- const length = updatePath.slice(0, -1).length;
901
- let index = 0;
902
- while (index < length) {
903
- object = isUndefined(object) ? index++ : object[updatePath[index++]];
904
- }
905
- return object;
906
- }
907
- function unset(object, path) {
908
- const updatePath = isKey(path) ? [path] : stringToPath(path);
909
- const childObject = updatePath.length == 1 ? object : baseGet(object, updatePath);
910
- const key = updatePath[updatePath.length - 1];
911
- let previousObjRef;
912
- if (childObject) {
913
- delete childObject[key];
914
- }
915
- for (let k = 0; k < updatePath.slice(0, -1).length; k++) {
916
- let index = -1;
917
- let objectRef;
918
- const currentPaths = updatePath.slice(0, -(k + 1));
919
- const currentPathsLength = currentPaths.length - 1;
920
- if (k > 0) {
921
- previousObjRef = object;
922
- }
923
- while (++index < currentPaths.length) {
924
- const item = currentPaths[index];
925
- objectRef = objectRef ? objectRef[item] : object[item];
926
- if (currentPathsLength === index &&
927
- ((isObject(objectRef) && isEmptyObject(objectRef)) ||
928
- (Array.isArray(objectRef) &&
929
- !objectRef.filter((data) => !isUndefined(data)).length))) {
930
- previousObjRef ? delete previousObjRef[item] : delete object[item];
931
- }
932
- previousObjRef = objectRef;
933
- }
934
- }
935
- return object;
936
- }
937
-
938
937
  function markFieldsDirty(data, fields = {}) {
939
938
  const isParentNodeArray = Array.isArray(data);
940
939
  if (isObject(data) || isParentNodeArray) {
@@ -1993,28 +1992,37 @@ function createFormControl(props = {}) {
1993
1992
  _defaultValues = updatedValues;
1994
1993
  }
1995
1994
  if (!keepStateOptions.keepValues) {
1996
- if (isWeb && isUndefined(formValues)) {
1997
- for (const name of _names.mount) {
1998
- const field = get(_fields, name);
1999
- if (field && field._f) {
2000
- const fieldReference = Array.isArray(field._f.refs)
2001
- ? field._f.refs[0]
2002
- : field._f.ref;
2003
- try {
2004
- isHTMLElement(fieldReference) &&
2005
- fieldReference.closest('form').reset();
2006
- break;
1995
+ if (keepStateOptions.keepDirtyValues) {
1996
+ for (const fieldName of _names.mount) {
1997
+ get(_formState.dirtyFields, fieldName)
1998
+ ? set(values, fieldName, get(_formValues, fieldName))
1999
+ : setValue(fieldName, get(values, fieldName));
2000
+ }
2001
+ }
2002
+ else {
2003
+ if (isWeb && isUndefined(formValues)) {
2004
+ for (const name of _names.mount) {
2005
+ const field = get(_fields, name);
2006
+ if (field && field._f) {
2007
+ const fieldReference = Array.isArray(field._f.refs)
2008
+ ? field._f.refs[0]
2009
+ : field._f.ref;
2010
+ try {
2011
+ isHTMLElement(fieldReference) &&
2012
+ fieldReference.closest('form').reset();
2013
+ break;
2014
+ }
2015
+ catch (_a) { }
2007
2016
  }
2008
- catch (_a) { }
2009
2017
  }
2010
2018
  }
2019
+ _fields = {};
2011
2020
  }
2012
2021
  _formValues = props.shouldUnregister
2013
2022
  ? keepStateOptions.keepDefaultValues
2014
2023
  ? cloneObject(_defaultValues)
2015
2024
  : {}
2016
2025
  : cloneUpdatedValues;
2017
- _fields = {};
2018
2026
  _subjects.array.next({
2019
2027
  values,
2020
2028
  });
@@ -2037,22 +2045,16 @@ function createFormControl(props = {}) {
2037
2045
  submitCount: keepStateOptions.keepSubmitCount
2038
2046
  ? _formState.submitCount
2039
2047
  : 0,
2040
- isDirty: keepStateOptions.keepDirty
2048
+ isDirty: keepStateOptions.keepDirty || keepStateOptions.keepDirtyValues
2041
2049
  ? _formState.isDirty
2042
- : keepStateOptions.keepDefaultValues
2043
- ? !deepEqual(formValues, _defaultValues)
2044
- : false,
2045
- isSubmitted: keepStateOptions.keepIsSubmitted
2046
- ? _formState.isSubmitted
2047
- : false,
2048
- dirtyFields: keepStateOptions.keepDirty
2050
+ : !!(keepStateOptions.keepDefaultValues &&
2051
+ !deepEqual(formValues, _defaultValues)),
2052
+ isSubmitted: !!keepStateOptions.keepIsSubmitted,
2053
+ dirtyFields: keepStateOptions.keepDirty || keepStateOptions.keepDirtyValues
2049
2054
  ? _formState.dirtyFields
2050
- : (keepStateOptions.keepDefaultValues && formValues
2051
- ? Object.entries(formValues).reduce((previous, [key, value]) => ({
2052
- ...previous,
2053
- [key]: value !== get(_defaultValues, key),
2054
- }), {})
2055
- : {}),
2055
+ : keepStateOptions.keepDefaultValues && formValues
2056
+ ? getDirtyFields(_defaultValues, formValues)
2057
+ : {},
2056
2058
  touchedFields: keepStateOptions.keepTouched
2057
2059
  ? _formState.touchedFields
2058
2060
  : {},