react-hook-form 7.76.0 → 7.76.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.
@@ -18,15 +18,9 @@ var getEventValue = (event) => isObject(event) && event.target
18
18
  : event.target.value
19
19
  : event;
20
20
 
21
- var getFieldArrayParentNames = (names, name) => {
22
- const parts = name.split('.');
23
- const matches = [];
24
- let prefix = parts[0];
25
- for (let i = 1; i < parts.length; prefix += '.' + parts[i++]) {
26
- !isNaN(+parts[i]) && names.has(prefix) && matches.push(prefix);
27
- }
28
- return matches;
29
- };
21
+ var isNameInFieldArray = (names, name) => name
22
+ .split('.')
23
+ .some((part, index, arr) => !isNaN(Number(part)) && names.has(arr.slice(0, index).join('.')));
30
24
 
31
25
  var isPlainObject = (tempObject) => {
32
26
  const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
@@ -402,8 +396,7 @@ function useWatch(props) {
402
396
  function useController(props) {
403
397
  const formControl = useFormControlContext();
404
398
  const { name, disabled, control = formControl, shouldUnregister, defaultValue, exact = true, } = props;
405
- const isArrayField = !!getFieldArrayParentNames(control._names.array, name)
406
- .length;
399
+ const isArrayField = isNameInFieldArray(control._names.array, name);
407
400
  const defaultValueMemo = React.useMemo(() => get(control._formValues, name, get(control._defaultValues, name, defaultValue)), [control, name, defaultValue]);
408
401
  const value = useWatch({
409
402
  control,
@@ -1273,8 +1266,7 @@ var validateField = async (field, disabledFieldNames, formValues, validateAllFie
1273
1266
  isUndefined(inputValue)) ||
1274
1267
  (isHTMLElement(ref) && ref.value === '') ||
1275
1268
  inputValue === '' ||
1276
- (Array.isArray(inputValue) && !inputValue.length) ||
1277
- (valueAsNumber && typeof inputValue === 'number' && isNaN(inputValue));
1269
+ (Array.isArray(inputValue) && !inputValue.length);
1278
1270
  const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
1279
1271
  const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
1280
1272
  const message = exceedMax ? maxLengthMessage : minLengthMessage;
@@ -1872,7 +1864,7 @@ function createFormControl(props = {}) {
1872
1864
  : defaultValue),
1873
1865
  }, isGlobal, defaultValue);
1874
1866
  const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
1875
- const setFieldValue = (name, value, options = {}) => {
1867
+ const setFieldValue = (name, value, options = {}, skipClone = false) => {
1876
1868
  const field = get(_fields, name);
1877
1869
  let fieldValue = value;
1878
1870
  if (field) {
@@ -1913,7 +1905,7 @@ function createFormControl(props = {}) {
1913
1905
  if (!fieldReference.ref.type) {
1914
1906
  _subjects.state.next({
1915
1907
  name,
1916
- values: cloneObject(_formValues),
1908
+ values: skipClone ? _formValues : cloneObject(_formValues),
1917
1909
  });
1918
1910
  }
1919
1911
  }
@@ -1923,7 +1915,7 @@ function createFormControl(props = {}) {
1923
1915
  updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
1924
1916
  options.shouldValidate && trigger(name);
1925
1917
  };
1926
- const setFieldValues = (name, value, options) => {
1918
+ const setFieldValues = (name, value, options, skipClone = false) => {
1927
1919
  for (const fieldKey in value) {
1928
1920
  if (!value.hasOwnProperty(fieldKey)) {
1929
1921
  return;
@@ -1935,14 +1927,14 @@ function createFormControl(props = {}) {
1935
1927
  isObject(fieldValue) ||
1936
1928
  (field && !field._f)) &&
1937
1929
  !isDateObject(fieldValue)
1938
- ? setFieldValues(fieldName, fieldValue, options)
1939
- : setFieldValue(fieldName, fieldValue, options);
1930
+ ? setFieldValues(fieldName, fieldValue, options, skipClone)
1931
+ : setFieldValue(fieldName, fieldValue, options, skipClone);
1940
1932
  }
1941
1933
  };
1942
- const setValue = (name, value, options = {}) => {
1934
+ const _setValue = (name, value, options, skipClone) => {
1943
1935
  const field = get(_fields, name);
1944
1936
  const isFieldArray = _names.array.has(name);
1945
- const cloneValue = cloneObject(value);
1937
+ const cloneValue = skipClone ? value : cloneObject(value);
1946
1938
  const previousValue = get(_formValues, name);
1947
1939
  const isValueUnchanged = deepEqual(previousValue, cloneValue);
1948
1940
  if (!isValueUnchanged) {
@@ -1951,7 +1943,7 @@ function createFormControl(props = {}) {
1951
1943
  if (isFieldArray) {
1952
1944
  _subjects.array.next({
1953
1945
  name,
1954
- values: cloneObject(_formValues),
1946
+ values: skipClone ? _formValues : cloneObject(_formValues),
1955
1947
  });
1956
1948
  if ((_proxyFormState.isDirty ||
1957
1949
  _proxyFormState.dirtyFields ||
@@ -1970,20 +1962,15 @@ function createFormControl(props = {}) {
1970
1962
  const isEmpty = (Array.isArray(cloneValue) && !cloneValue.length) ||
1971
1963
  isEmptyObject(cloneValue);
1972
1964
  if (!field || field._f || isNullOrUndefined(cloneValue) || isEmpty) {
1973
- setFieldValue(name, cloneValue, options);
1965
+ setFieldValue(name, cloneValue, options, skipClone);
1974
1966
  }
1975
1967
  else {
1976
- setFieldValues(name, cloneValue, options);
1968
+ setFieldValues(name, cloneValue, options, skipClone);
1977
1969
  }
1978
1970
  }
1979
1971
  if (!isValueUnchanged) {
1980
1972
  const watched = isWatched(name, _names);
1981
- const values = cloneObject(_formValues);
1982
- if (!isFieldArray) {
1983
- for (const arrayName of getFieldArrayParentNames(_names.array, name)) {
1984
- _subjects.array.next({ name: arrayName, values });
1985
- }
1986
- }
1973
+ const values = skipClone ? _formValues : cloneObject(_formValues);
1987
1974
  _subjects.state.next({
1988
1975
  ...(watched && _formState),
1989
1976
  name: _state.mount || watched ? name : undefined,
@@ -1991,7 +1978,8 @@ function createFormControl(props = {}) {
1991
1978
  });
1992
1979
  }
1993
1980
  };
1994
- const setValues = (formValues) => {
1981
+ const setValue = (name, value, options = {}) => _setValue(name, value, options, false);
1982
+ const setValues = (formValues, options = {}) => {
1995
1983
  const updatedFormValues = isFunction(formValues)
1996
1984
  ? formValues(_formValues)
1997
1985
  : formValues;
@@ -2001,9 +1989,17 @@ function createFormControl(props = {}) {
2001
1989
  ...updatedFormValues,
2002
1990
  };
2003
1991
  for (const fieldName of _names.mount) {
2004
- setValue(fieldName, get(updatedFormValues, fieldName));
1992
+ _setValue(fieldName, get(updatedFormValues, fieldName), options, true);
1993
+ }
1994
+ _subjects.state.next({
1995
+ ..._formState,
1996
+ name: undefined,
1997
+ type: undefined,
1998
+ values: _formValues,
1999
+ });
2000
+ if (options.shouldValidate) {
2001
+ _setValid();
2005
2002
  }
2006
- _subjects.state.next({ ..._formState, values: _formValues });
2007
2003
  }
2008
2004
  };
2009
2005
  const onChange = async (event) => {
@@ -2373,8 +2369,7 @@ function createFormControl(props = {}) {
2373
2369
  field._f.mount = false;
2374
2370
  }
2375
2371
  (_options.shouldUnregister || options.shouldUnregister) &&
2376
- !(getFieldArrayParentNames(_names.array, name).length &&
2377
- _state.action) &&
2372
+ !(isNameInFieldArray(_names.array, name) && _state.action) &&
2378
2373
  _names.unMount.add(name);
2379
2374
  }
2380
2375
  },