react-hook-form 8.0.0-alpha.3 → 8.0.0-alpha.4

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,9 +18,9 @@ var getEventValue = (event) => isObject(event) && event.target
18
18
  : event.target.value
19
19
  : event;
20
20
 
21
- var getNodeParentName = (name) => name.substring(0, name.search(/.\d/)) || name;
21
+ var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
22
22
 
23
- var isNameInFieldArray = (names, name) => [...names].some((current) => getNodeParentName(name) === current);
23
+ var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
24
24
 
25
25
  var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
26
26
 
@@ -672,11 +672,13 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
672
672
  ...appendErrorsCurry(exceedMax ? maxType : minType, message),
673
673
  };
674
674
  };
675
- if (required &&
676
- ((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
677
- (isBoolean(inputValue) && !inputValue) ||
678
- (isCheckBox && !getCheckboxValue(refs).isValid) ||
679
- (isRadio && !getRadioValue(refs).isValid))) {
675
+ if (isFieldArray
676
+ ? Array.isArray(inputValue) && !inputValue.length
677
+ : required &&
678
+ ((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
679
+ (isBoolean(inputValue) && !inputValue) ||
680
+ (isCheckBox && !getCheckboxValue(refs).isValid) ||
681
+ (isRadio && !getRadioValue(refs).isValid))) {
680
682
  const { value, message } = isMessage(required)
681
683
  ? { value: !!required, message: required }
682
684
  : getValueAndMessage(required);
@@ -725,8 +727,8 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
725
727
  }
726
728
  }
727
729
  if ((maxLength || minLength) &&
728
- ((!isEmpty && isString(inputValue)) ||
729
- (isFieldArray && Array.isArray(inputValue)))) {
730
+ !isEmpty &&
731
+ (isString(inputValue) || (isFieldArray && Array.isArray(inputValue)))) {
730
732
  const maxLengthOutput = getValueAndMessage(maxLength);
731
733
  const minLengthOutput = getValueAndMessage(minLength);
732
734
  const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
@@ -936,10 +938,9 @@ function useFieldArray(props) {
936
938
  _name.current = name;
937
939
  _fieldIds.current = fields;
938
940
  control._names.array.add(name);
939
- if (props.rules) {
941
+ props.rules &&
940
942
  control.register(name, props.rules);
941
- }
942
- const callback = React.useCallback(({ values, name: fieldArrayName }) => {
943
+ const callback = React.useCallback(({ values, name: fieldArrayName, }) => {
943
944
  if (fieldArrayName === _name.current || !fieldArrayName) {
944
945
  const fieldValues = get(values, _name.current, []);
945
946
  setFields(fieldValues);
@@ -1059,13 +1060,10 @@ function useFieldArray(props) {
1059
1060
  control._formState.isSubmitted) &&
1060
1061
  field &&
1061
1062
  field._f) {
1062
- validateField(field, get(control._formValues, name), control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => {
1063
- if (!isEmptyObject(error)) {
1064
- control._subjects.state.next({
1065
- errors: updateFieldArrayRootError(control._formState.errors, error, name),
1066
- });
1067
- }
1068
- });
1063
+ validateField(field, get(control._formValues, name), control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
1064
+ control._subjects.state.next({
1065
+ errors: updateFieldArrayRootError(control._formState.errors, error, name),
1066
+ }));
1069
1067
  }
1070
1068
  }
1071
1069
  }
@@ -1096,7 +1094,7 @@ function useFieldArray(props) {
1096
1094
  replace: React.useCallback(replace, [updateValues, name, control]),
1097
1095
  fields: React.useMemo(() => fields.map((field, index) => ({
1098
1096
  ...field,
1099
- id: ids.current[index] || generateId(),
1097
+ key: ids.current[index] || generateId(),
1100
1098
  })), [fields]),
1101
1099
  };
1102
1100
  }
@@ -1605,19 +1603,12 @@ function createFormControl(props = {}) {
1605
1603
  break;
1606
1604
  }
1607
1605
  }
1608
- if (!shouldOnlyCheckValid) {
1609
- if (get(fieldError, _f.name)) {
1610
- if (isFieldArrayRoot) {
1611
- updateFieldArrayRootError(_formState.errors, fieldError, _f.name);
1612
- }
1613
- else {
1614
- set(_formState.errors, _f.name, fieldError[_f.name]);
1615
- }
1616
- }
1617
- else {
1618
- unset(_formState.errors, _f.name);
1619
- }
1620
- }
1606
+ !shouldOnlyCheckValid &&
1607
+ (get(fieldError, _f.name)
1608
+ ? isFieldArrayRoot
1609
+ ? updateFieldArrayRootError(_formState.errors, fieldError, _f.name)
1610
+ : set(_formState.errors, _f.name, fieldError[_f.name])
1611
+ : unset(_formState.errors, _f.name));
1621
1612
  }
1622
1613
  fieldValue &&
1623
1614
  (await executeBuildInValidation(fieldValue, shouldOnlyCheckValid, context));
@@ -1847,7 +1838,6 @@ function createFormControl(props = {}) {
1847
1838
  : fieldNames.map((name) => get(values, name));
1848
1839
  };
1849
1840
  const getFieldState = (name, formState) => ({
1850
- invalid: !!get((formState || _formState).errors, name),
1851
1841
  isDirty: !!get((formState || _formState).dirtyFields, name),
1852
1842
  isTouched: !!get((formState || _formState).touchedFields, name),
1853
1843
  error: get((formState || _formState).errors, name),