react-hook-form 7.26.1 → 7.28.0-next.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.
@@ -733,13 +733,14 @@ function useFieldArray(props) {
733
733
  }, false);
734
734
  };
735
735
  const update = (index, value) => {
736
- const updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, value);
736
+ const updateValue = cloneObject(value);
737
+ const updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, updateValue);
737
738
  ids.current = [...updatedFieldArrayValues].map((item, i) => !item || i === index ? generateId() : ids.current[i]);
738
739
  updateValues(updatedFieldArrayValues);
739
740
  setFields([...updatedFieldArrayValues]);
740
741
  control._updateFieldArray(name, updatedFieldArrayValues, updateAt, {
741
742
  argA: index,
742
- argB: value,
743
+ argB: updateValue,
743
744
  }, true, false);
744
745
  };
745
746
  const replace = (value) => {
@@ -909,7 +910,9 @@ function unset(object, path) {
909
910
  if (currentPathsLength === index &&
910
911
  ((isObject(objectRef) && isEmptyObject(objectRef)) ||
911
912
  (Array.isArray(objectRef) &&
912
- !objectRef.filter((data) => (isObject(data) && !isEmptyObject(data)) || isBoolean(data)).length))) {
913
+ !objectRef.filter((data) => (isObject(data) && !isEmptyObject(data)) ||
914
+ isBoolean(data) ||
915
+ (Array.isArray(data) && data.length)).length))) {
913
916
  previousObjRef ? delete previousObjRef[item] : delete object[item];
914
917
  }
915
918
  previousObjRef = objectRef;
@@ -1342,21 +1345,22 @@ function createFormControl(props = {}) {
1342
1345
  }
1343
1346
  return isValid;
1344
1347
  };
1345
- const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndErrors = true) => {
1348
+ const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
1346
1349
  if (args && method) {
1347
1350
  _stateFlags.action = true;
1348
- if (shouldUpdateFieldsAndErrors && Array.isArray(get(_fields, name))) {
1351
+ if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
1349
1352
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
1350
1353
  shouldSetValues && set(_fields, name, fieldValues);
1351
1354
  }
1352
1355
  if (_proxyFormState.errors &&
1353
- shouldUpdateFieldsAndErrors &&
1356
+ shouldUpdateFieldsAndState &&
1354
1357
  Array.isArray(get(_formState.errors, name))) {
1355
1358
  const errors = method(get(_formState.errors, name), args.argA, args.argB);
1356
1359
  shouldSetValues && set(_formState.errors, name, errors);
1357
1360
  unsetEmptyArray(_formState.errors, name);
1358
1361
  }
1359
1362
  if (_proxyFormState.touchedFields &&
1363
+ shouldUpdateFieldsAndState &&
1360
1364
  Array.isArray(get(_formState.touchedFields, name))) {
1361
1365
  const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
1362
1366
  shouldSetValues && set(_formState.touchedFields, name, touchedFields);
@@ -1665,7 +1669,8 @@ function createFormControl(props = {}) {
1665
1669
  error = (await validateField(field, get(_formValues, name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1666
1670
  isValid = await _updateValid(true);
1667
1671
  }
1668
- field._f.deps && trigger(field._f.deps);
1672
+ field._f.deps &&
1673
+ trigger(field._f.deps);
1669
1674
  shouldRenderByError(false, name, isValid, error, fieldState);
1670
1675
  }
1671
1676
  };
@@ -1972,9 +1977,10 @@ function createFormControl(props = {}) {
1972
1977
  isSubmitSuccessful: false,
1973
1978
  });
1974
1979
  };
1975
- const setFocus = (name) => {
1980
+ const setFocus = (name, options = {}) => {
1976
1981
  const field = get(_fields, name)._f;
1977
- (field.ref.focus ? field.ref : field.refs[0]).focus();
1982
+ const fieldRef = field.refs ? field.refs[0] : field.ref;
1983
+ options.shouldSelect ? fieldRef.select() : fieldRef.focus();
1978
1984
  };
1979
1985
  return {
1980
1986
  control: {