react-hook-form 7.37.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.
@@ -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) {
@@ -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({
@@ -1113,7 +1126,7 @@ function useFieldArray(props) {
1113
1126
  values: control._formValues,
1114
1127
  });
1115
1128
  control._names.focus &&
1116
- focusFieldBy(control._fields, (key) => key.startsWith(control._names.focus));
1129
+ focusFieldBy(control._fields, (key) => !!key && key.startsWith(control._names.focus));
1117
1130
  control._names.focus = '';
1118
1131
  control._proxyFormState.isValid && control._updateValid();
1119
1132
  }, [fields, name, control]);
@@ -1816,13 +1829,13 @@ function createFormControl(props = {}) {
1816
1829
  (_proxyFormState.isValid && isValid !== _formState.isValid)
1817
1830
  ? {}
1818
1831
  : { name }),
1819
- ...(_options.resolver ? { isValid } : {}),
1832
+ ...(_options.resolver || !name ? { isValid } : {}),
1820
1833
  errors: _formState.errors,
1821
1834
  isValidating: false,
1822
1835
  });
1823
1836
  options.shouldFocus &&
1824
1837
  !validationResult &&
1825
- focusFieldBy(_fields, (key) => get(_formState.errors, key), name ? fieldNames : _names.mount);
1838
+ focusFieldBy(_fields, (key) => key && get(_formState.errors, key), name ? fieldNames : _names.mount);
1826
1839
  return validationResult;
1827
1840
  };
1828
1841
  const getValues = (fieldNames) => {
@@ -1973,6 +1986,8 @@ function createFormControl(props = {}) {
1973
1986
  },
1974
1987
  };
1975
1988
  };
1989
+ const _focusError = () => _options.shouldFocusError &&
1990
+ focusFieldBy(_fields, (key) => key && get(_formState.errors, key), _names.mount);
1976
1991
  const handleSubmit = (onValid, onInvalid) => async (e) => {
1977
1992
  if (e) {
1978
1993
  e.preventDefault && e.preventDefault();
@@ -2003,8 +2018,7 @@ function createFormControl(props = {}) {
2003
2018
  if (onInvalid) {
2004
2019
  await onInvalid({ ..._formState.errors }, e);
2005
2020
  }
2006
- _options.shouldFocusError &&
2007
- focusFieldBy(_fields, (key) => get(_formState.errors, key), _names.mount);
2021
+ _focusError();
2008
2022
  }
2009
2023
  }
2010
2024
  catch (err) {
@@ -2153,6 +2167,7 @@ function createFormControl(props = {}) {
2153
2167
  unregister,
2154
2168
  getFieldState,
2155
2169
  _executeSchema,
2170
+ _focusError,
2156
2171
  _getWatch,
2157
2172
  _getDirty,
2158
2173
  _updateValid,
@@ -2289,6 +2304,9 @@ function useForm(props = {}) {
2289
2304
  }
2290
2305
  control._removeUnmounted();
2291
2306
  });
2307
+ React.useEffect(() => {
2308
+ formState.submitCount && control._focusError();
2309
+ }, [control, formState.submitCount]);
2292
2310
  _formControl.current.formState = getProxyFormState(formState, control);
2293
2311
  return _formControl.current;
2294
2312
  }