react-hook-form 7.37.0-next.0 → 7.38.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.
@@ -395,9 +395,9 @@ function useController(props) {
395
395
  },
396
396
  type: EVENTS.BLUR,
397
397
  }), [name, control]),
398
- ref: React.useCallback((elm) => {
398
+ ref: (elm) => {
399
399
  const field = get(control._fields, name);
400
- if (elm && field && elm.focus) {
400
+ if (field && elm) {
401
401
  field._f.ref = {
402
402
  focus: () => elm.focus(),
403
403
  select: () => elm.select(),
@@ -405,7 +405,7 @@ function useController(props) {
405
405
  reportValidity: () => elm.reportValidity(),
406
406
  };
407
407
  }
408
- }, [name, control._fields]),
408
+ },
409
409
  },
410
410
  formState,
411
411
  fieldState: Object.defineProperties({}, {
@@ -694,11 +694,22 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
694
694
  }
695
695
  else {
696
696
  const valueDate = ref.valueAsDate || new Date(inputValue);
697
- if (isString(maxOutput.value)) {
698
- exceedMax = valueDate > new Date(maxOutput.value);
697
+ const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
698
+ const isTime = ref.type == 'time';
699
+ const isWeek = ref.type == 'week';
700
+ if (isString(maxOutput.value) && inputValue) {
701
+ exceedMax = isTime
702
+ ? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
703
+ : isWeek
704
+ ? inputValue > maxOutput.value
705
+ : valueDate > new Date(maxOutput.value);
699
706
  }
700
- if (isString(minOutput.value)) {
701
- exceedMin = valueDate < new Date(minOutput.value);
707
+ if (isString(minOutput.value) && inputValue) {
708
+ exceedMin = isTime
709
+ ? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
710
+ : isWeek
711
+ ? inputValue < minOutput.value
712
+ : valueDate < new Date(minOutput.value);
702
713
  }
703
714
  }
704
715
  if (exceedMax || exceedMin) {
@@ -793,10 +804,10 @@ function append(data, value) {
793
804
  return [...data, ...convertToArrayPayload(value)];
794
805
  }
795
806
 
796
- function isPlainObject(tempObject) {
807
+ var isPlainObject = (tempObject) => {
797
808
  const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
798
809
  return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
799
- }
810
+ };
800
811
 
801
812
  var isWeb = typeof window !== 'undefined' &&
802
813
  typeof window.HTMLElement !== 'undefined' &&
@@ -931,7 +942,7 @@ var updateAt = (fieldValues, index, value) => {
931
942
  };
932
943
 
933
944
  /**
934
- * A custom hook that exposes convenient methods to perform operations with a list of dynamic inputs that need to be appended, updated, removed etc.
945
+ * A custom hook that exposes convenient methods to perform operations with a list of dynamic inputs that need to be appended, updated, removed etc. • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn) • [Video](https://youtu.be/4MrbfGSFY2A)
935
946
  *
936
947
  * @remarks
937
948
  * [API](https://react-hook-form.com/api/usefieldarray) • [Demo](https://codesandbox.io/s/react-hook-form-usefieldarray-ssugn)
@@ -982,9 +993,11 @@ function useFieldArray(props) {
982
993
  control.register(name, props.rules);
983
994
  const callback = React.useCallback(({ values, name: fieldArrayName, }) => {
984
995
  if (fieldArrayName === _name.current || !fieldArrayName) {
985
- const fieldValues = get(values, _name.current, []);
986
- setFields(fieldValues);
987
- ids.current = fieldValues.map(generateId);
996
+ const fieldValues = get(values, _name.current);
997
+ if (Array.isArray(fieldValues)) {
998
+ setFields(fieldValues);
999
+ ids.current = fieldValues.map(generateId);
1000
+ }
988
1001
  }
989
1002
  }, []);
990
1003
  useSubscribe({
@@ -1081,7 +1094,9 @@ function useFieldArray(props) {
1081
1094
  React.useEffect(() => {
1082
1095
  control._stateFlags.action = false;
1083
1096
  isWatched(name, control._names) && control._subjects.state.next({});
1084
- if (_actioned.current) {
1097
+ if (_actioned.current &&
1098
+ (!getValidationModes(control._options.mode).isOnSubmit ||
1099
+ control._formState.isSubmitted)) {
1085
1100
  if (control._options.resolver) {
1086
1101
  control._executeSchema([name]).then((result) => {
1087
1102
  const error = get(result.errors, name);
@@ -1098,11 +1113,7 @@ function useFieldArray(props) {
1098
1113
  }
1099
1114
  else {
1100
1115
  const field = get(control._fields, name);
1101
- const validationModeBeforeSubmit = getValidationModes(control._options.mode);
1102
- if ((!validationModeBeforeSubmit.isOnSubmit ||
1103
- control._formState.isSubmitted) &&
1104
- field &&
1105
- field._f) {
1116
+ if (field && field._f) {
1106
1117
  validateField(field, get(control._formValues, name), control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
1107
1118
  control._subjects.state.next({
1108
1119
  errors: updateFieldArrayRootError(control._formState.errors, error, name),
@@ -1115,7 +1126,7 @@ function useFieldArray(props) {
1115
1126
  values: control._formValues,
1116
1127
  });
1117
1128
  control._names.focus &&
1118
- focusFieldBy(control._fields, (key) => key.startsWith(control._names.focus));
1129
+ focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus));
1119
1130
  control._names.focus = '';
1120
1131
  control._proxyFormState.isValid && control._updateValid();
1121
1132
  }, [fields, name, control]);
@@ -1655,7 +1666,7 @@ function createFormControl(props = {}) {
1655
1666
  ? ''
1656
1667
  : value;
1657
1668
  if (isMultipleSelect(fieldReference.ref)) {
1658
- [...fieldReference.ref.options].forEach((selectRef) => (selectRef.selected = fieldValue.includes(selectRef.value)));
1669
+ [...fieldReference.ref.options].forEach((optionRef) => (optionRef.selected = fieldValue.includes(optionRef.value)));
1659
1670
  }
1660
1671
  else if (fieldReference.refs) {
1661
1672
  if (isCheckBoxInput(fieldReference.ref)) {
@@ -1818,13 +1829,13 @@ function createFormControl(props = {}) {
1818
1829
  (_proxyFormState.isValid && isValid !== _formState.isValid)
1819
1830
  ? {}
1820
1831
  : { name }),
1821
- ...(_options.resolver ? { isValid } : {}),
1832
+ ...(_options.resolver || !name ? { isValid } : {}),
1822
1833
  errors: _formState.errors,
1823
1834
  isValidating: false,
1824
1835
  });
1825
1836
  options.shouldFocus &&
1826
1837
  !validationResult &&
1827
- focusFieldBy(_fields, (key) => get(_formState.errors, key), name ? fieldNames : _names.mount);
1838
+ focusFieldBy(_fields, (key) => key && get(_formState.errors, key), name ? fieldNames : _names.mount);
1828
1839
  return validationResult;
1829
1840
  };
1830
1841
  const getValues = (fieldNames) => {
@@ -1975,6 +1986,8 @@ function createFormControl(props = {}) {
1975
1986
  },
1976
1987
  };
1977
1988
  };
1989
+ const _focusError = () => _options.shouldFocusError &&
1990
+ focusFieldBy(_fields, (key) => key && get(_formState.errors, key), _names.mount);
1978
1991
  const handleSubmit = (onValid, onInvalid) => async (e) => {
1979
1992
  if (e) {
1980
1993
  e.preventDefault && e.preventDefault();
@@ -2005,8 +2018,7 @@ function createFormControl(props = {}) {
2005
2018
  if (onInvalid) {
2006
2019
  await onInvalid({ ..._formState.errors }, e);
2007
2020
  }
2008
- _options.shouldFocusError &&
2009
- focusFieldBy(_fields, (key) => get(_formState.errors, key), _names.mount);
2021
+ _focusError();
2010
2022
  }
2011
2023
  }
2012
2024
  catch (err) {
@@ -2155,6 +2167,7 @@ function createFormControl(props = {}) {
2155
2167
  unregister,
2156
2168
  getFieldState,
2157
2169
  _executeSchema,
2170
+ _focusError,
2158
2171
  _getWatch,
2159
2172
  _getDirty,
2160
2173
  _updateValid,
@@ -2291,6 +2304,9 @@ function useForm(props = {}) {
2291
2304
  }
2292
2305
  control._removeUnmounted();
2293
2306
  });
2307
+ React.useEffect(() => {
2308
+ formState.submitCount && control._focusError();
2309
+ }, [control, formState.submitCount]);
2294
2310
  _formControl.current.formState = getProxyFormState(formState, control);
2295
2311
  return _formControl.current;
2296
2312
  }