react-hook-form 7.88.0 → 7.89.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.
@@ -1 +1 @@
1
- {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAcV,WAAW,EAoBX,YAAY,EAKZ,aAAa,EAYd,MAAM,UAAU,CAAC;AAyElB,eAAO,MAAM,kBAAkB;;;;;;;;;;;;CAY9B,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,KAAK,GAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAM,GACnE,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,CAAC;CACH,CAijEA"}
1
+ {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAcV,WAAW,EAoBX,YAAY,EAKZ,aAAa,EAYd,MAAM,UAAU,CAAC;AAuElB,eAAO,MAAM,kBAAkB;;;;;;;;;;;;CAY9B,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,KAAK,GAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAM,GACnE,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,CAAC;CACH,CAyoEA"}
@@ -7,6 +7,7 @@ declare const _default: <TFieldValues extends FieldValues>(fieldsNames: Set<Inte
7
7
  name: InternalFieldName;
8
8
  refs?: HTMLInputElement[];
9
9
  mount?: boolean;
10
+ _c?: boolean;
10
11
  } & import("..").RegisterOptions>;
11
12
  shouldUseNativeValidation: boolean | undefined;
12
13
  };
@@ -1 +1 @@
1
- {"version":3,"file":"getResolverOptions.d.ts","sourceRoot":"","sources":["../../src/logic/getResolverOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EAEZ,SAAS,EACT,SAAS,EACT,WAAW,EACX,iBAAiB,EAClB,MAAM,UAAU,CAAC;yBAIF,YAAY,SAAS,WAAW,EAC9C,aAAa,GAAG,CAAC,iBAAiB,CAAC,GAAG,iBAAiB,EAAE,EACzD,SAAS,SAAS,EAClB,eAAe,YAAY,EAC3B,4BAA4B,OAAO,GAAG,SAAS;;WAYlB,SAAS,CAAC,YAAY,CAAC,EAAE;;;;;;;;;AAhBxD,wBAoBE"}
1
+ {"version":3,"file":"getResolverOptions.d.ts","sourceRoot":"","sources":["../../src/logic/getResolverOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EAEZ,SAAS,EACT,SAAS,EACT,WAAW,EACX,iBAAiB,EAClB,MAAM,UAAU,CAAC;yBAIF,YAAY,SAAS,WAAW,EAC9C,aAAa,GAAG,CAAC,iBAAiB,CAAC,GAAG,iBAAiB,EAAE,EACzD,SAAS,SAAS,EAClB,eAAe,YAAY,EAC3B,4BAA4B,OAAO,GAAG,SAAS;;WAYlB,SAAS,CAAC,YAAY,CAAC,EAAE;;;;;;;;;;AAhBxD,wBAoBE"}
@@ -1 +1 @@
1
- {"version":3,"file":"validateField.d.ts","sourceRoot":"","sources":["../../src/logic/validateField.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,KAAK,EAEL,WAAW,EACX,mBAAmB,EACnB,eAAe,EAKhB,MAAM,UAAU,CAAC;yBAsBI,CAAC,SAAS,WAAW,EACzC,OAAO,KAAK,EACZ,oBAAoB,eAAe,EACnC,YAAY,CAAC,EACb,0BAA0B,OAAO,EACjC,4BAA4B,OAAO,EACnC,eAAe,OAAO,KACrB,OAAO,CAAC,mBAAmB,CAAC;AAP/B,wBA2QE"}
1
+ {"version":3,"file":"validateField.d.ts","sourceRoot":"","sources":["../../src/logic/validateField.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,KAAK,EAEL,WAAW,EACX,mBAAmB,EACnB,eAAe,EAKhB,MAAM,UAAU,CAAC;yBAsBI,CAAC,SAAS,WAAW,EACzC,OAAO,KAAK,EACZ,oBAAoB,eAAe,EACnC,YAAY,CAAC,EACb,0BAA0B,OAAO,EACjC,4BAA4B,OAAO,EACnC,eAAe,OAAO,KACrB,OAAO,CAAC,mBAAmB,CAAC;AAP/B,wBAgRE"}
@@ -31,6 +31,16 @@ const INPUT_VALIDATION_RULES = {
31
31
  required: 'required',
32
32
  validate: 'validate',
33
33
  };
34
+ const REGISTER_VALIDATION_RULES = [
35
+ INPUT_VALIDATION_RULES.required,
36
+ INPUT_VALIDATION_RULES.min,
37
+ INPUT_VALIDATION_RULES.max,
38
+ INPUT_VALIDATION_RULES.minLength,
39
+ INPUT_VALIDATION_RULES.maxLength,
40
+ INPUT_VALIDATION_RULES.pattern,
41
+ INPUT_VALIDATION_RULES.validate,
42
+ ];
43
+ const FORM_ERROR_TYPE = 'form';
34
44
  const ROOT_ERROR_TYPE = 'root';
35
45
  const PROTOTYPE_KEYWORDS = ['__proto__', 'constructor', 'prototype'];
36
46
 
@@ -167,16 +177,15 @@ function deepEqual(object1, object2, visited = new WeakMap()) {
167
177
  }
168
178
 
169
179
  function extractFormValues(fieldsState, formValues) {
170
- const values = {};
180
+ const values = (Array.isArray(fieldsState) ? [] : {});
171
181
  for (const key in fieldsState) {
172
182
  if (fieldsState.hasOwnProperty(key)) {
173
183
  const fieldState = fieldsState[key];
174
184
  const fieldValue = formValues[key];
175
- if (fieldState && isObject(fieldState) && fieldValue) {
176
- const nestedFieldsState = extractFormValues(fieldState, fieldValue);
177
- if (isObject(nestedFieldsState)) {
178
- values[key] = nestedFieldsState;
179
- }
185
+ if (fieldState &&
186
+ (isObject(fieldState) || Array.isArray(fieldState)) &&
187
+ fieldValue) {
188
+ values[key] = extractFormValues(fieldState, fieldValue);
180
189
  }
181
190
  else if (fieldsState[key]) {
182
191
  values[key] = fieldValue;
@@ -742,7 +751,7 @@ var getValueAndMessage = (validationData) => isObject(validationData) && !isRege
742
751
  };
743
752
 
744
753
  var validateField = async (field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
745
- const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, } = field._f;
754
+ const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, _c, } = field._f;
746
755
  const inputValue = get(formValues, name);
747
756
  if (!mount || disabledFieldNames.has(name)) {
748
757
  return {};
@@ -752,7 +761,8 @@ var validateField = async (field, disabledFieldNames, formValues, validateAllFie
752
761
  if (shouldUseNativeValidation && inputRef.reportValidity) {
753
762
  const validityMessage = isBoolean(message) ? '' : message || '';
754
763
  if (refs) {
755
- refs.forEach((ref) => ref.setCustomValidity(validityMessage));
764
+ refs.forEach((ref) => isFunction(ref.setCustomValidity) &&
765
+ ref.setCustomValidity(validityMessage));
756
766
  }
757
767
  else {
758
768
  inputRef.setCustomValidity(validityMessage);
@@ -767,7 +777,7 @@ var validateField = async (field, disabledFieldNames, formValues, validateAllFie
767
777
  const isEmpty = ((valueAsNumber || isFileInput(ref)) &&
768
778
  isUndefined(ref.value) &&
769
779
  isUndefined(inputValue)) ||
770
- (isHTMLElement(ref) && ref.value === '') ||
780
+ (isHTMLElement(ref) && ref.value === '' && !_c) ||
771
781
  inputValue === '' ||
772
782
  (Array.isArray(inputValue) && !inputValue.length);
773
783
  const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
@@ -936,7 +946,6 @@ const defaultOptions = {
936
946
  reValidateMode: VALIDATION_MODE.onChange,
937
947
  shouldFocusError: true,
938
948
  };
939
- const FORM_ERROR_TYPE = 'form';
940
949
  const updateDirtyFields = (dirtyFields, nextDirtyFields) => {
941
950
  for (const key in dirtyFields) {
942
951
  if (!(key in nextDirtyFields)) {
@@ -1100,12 +1109,16 @@ function createFormControl(props = {}) {
1100
1109
  unsetEmptyArray(_formState.errors, name);
1101
1110
  }
1102
1111
  const touchedFieldsArray = get(_formState.touchedFields, name);
1103
- if (_isTracked('touchedFields') &&
1104
- shouldUpdateFieldsAndState &&
1105
- Array.isArray(touchedFieldsArray)) {
1112
+ const shouldUpdateTouchedFields = shouldUpdateFieldsAndState && Array.isArray(touchedFieldsArray);
1113
+ if (shouldUpdateTouchedFields) {
1106
1114
  const touchedFields = method(touchedFieldsArray, args.argA, args.argB);
1107
1115
  shouldSetValues && set(_formState.touchedFields, name, touchedFields);
1108
1116
  }
1117
+ const dirtyFieldsArray = get(_formState.dirtyFields, name);
1118
+ if (shouldUpdateFieldsAndState && Array.isArray(dirtyFieldsArray)) {
1119
+ const dirtyFields = method(dirtyFieldsArray, args.argA, args.argB) || dirtyFieldsArray;
1120
+ shouldSetValues && set(_formState.dirtyFields, name, dirtyFields);
1121
+ }
1109
1122
  if (_isTracked('dirtyFields')) {
1110
1123
  _updateDirtyFields();
1111
1124
  }
@@ -1113,6 +1126,9 @@ function createFormControl(props = {}) {
1113
1126
  name,
1114
1127
  isDirty: _getDirty(name, values),
1115
1128
  dirtyFields: _formState.dirtyFields,
1129
+ ...(shouldUpdateTouchedFields && {
1130
+ touchedFields: _formState.touchedFields,
1131
+ }),
1116
1132
  errors: _formState.errors,
1117
1133
  isValid: _formState.isValid,
1118
1134
  });
@@ -1130,11 +1146,13 @@ function createFormControl(props = {}) {
1130
1146
  };
1131
1147
  const _setErrors = (errors) => {
1132
1148
  Object.keys(delayErrorCallbacks).forEach(cancelDelayedError);
1149
+ const hasErrors = !isEmptyObject(errors);
1133
1150
  _formState.errors = errors;
1134
1151
  _subjects.state.next({
1135
1152
  errors: _formState.errors,
1136
- isValid: false,
1153
+ ...(hasErrors ? { isValid: false } : {}),
1137
1154
  });
1155
+ !hasErrors && _state.mount && _setValid();
1138
1156
  };
1139
1157
  const hasExplicitNullIntermediate = (name) => {
1140
1158
  const segments = isKey(name) ? [name] : stringToPath(name);
@@ -1202,7 +1220,13 @@ function createFormControl(props = {}) {
1202
1220
  ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
1203
1221
  : setFieldValue(name, defaultValue);
1204
1222
  if (_state.mount && !_state.action) {
1205
- _setValid();
1223
+ if (_options.resolver &&
1224
+ _isTracked('isValidating', 'validatingFields')) {
1225
+ Promise.resolve().then(() => _setValid());
1226
+ }
1227
+ else {
1228
+ _setValid();
1229
+ }
1206
1230
  if (wasUnsetInFormValues &&
1207
1231
  _formState.isDirty &&
1208
1232
  _isTracked('isDirty')) {
@@ -1310,7 +1334,7 @@ function createFormControl(props = {}) {
1310
1334
  if (names) {
1311
1335
  for (const name of names) {
1312
1336
  const error = get(errors, name);
1313
- cancelDelayedError(name);
1337
+ cancelDelayedErrorTree(name);
1314
1338
  const isFieldArrayRootError = _names.array.has(name) &&
1315
1339
  isObject(error) &&
1316
1340
  !Object.keys(error).some((key) => !Number.isNaN(Number(key)));
@@ -1334,34 +1358,43 @@ function createFormControl(props = {}) {
1334
1358
  return errors;
1335
1359
  };
1336
1360
  const validateForm = async ({ name, eventType, }) => {
1337
- if (props.validate) {
1338
- const result = await props.validate({
1361
+ if (_options.validate) {
1362
+ const resetCallId = _resetCallId;
1363
+ const result = await _options.validate({
1339
1364
  formValues: _formValues,
1340
1365
  formState: _formState,
1341
1366
  name,
1342
1367
  eventType,
1343
1368
  });
1369
+ if (resetCallId !== _resetCallId) {
1370
+ return true;
1371
+ }
1344
1372
  if (isObject(result)) {
1373
+ let isValid = true;
1374
+ clearErrors(FORM_ERROR_TYPE);
1345
1375
  for (const key in result) {
1346
1376
  const error = result[key];
1347
1377
  if (error) {
1378
+ isValid = false;
1348
1379
  setError(`${FORM_ERROR_TYPE}.${key}`, {
1349
1380
  message: isString(error.message) ? error.message : '',
1350
1381
  type: error.type || INPUT_VALIDATION_RULES.validate,
1351
1382
  });
1352
1383
  }
1353
1384
  }
1385
+ return isValid;
1354
1386
  }
1355
1387
  else if (isString(result) || !result) {
1356
1388
  setError(FORM_ERROR_TYPE, {
1357
1389
  message: result || '',
1358
1390
  type: INPUT_VALIDATION_RULES.validate,
1359
1391
  });
1392
+ return false;
1360
1393
  }
1361
1394
  else {
1362
1395
  clearErrors(FORM_ERROR_TYPE);
1396
+ return true;
1363
1397
  }
1364
- return result;
1365
1398
  }
1366
1399
  return true;
1367
1400
  };
@@ -1369,7 +1402,8 @@ function createFormControl(props = {}) {
1369
1402
  valid: true,
1370
1403
  runRootValidation: false,
1371
1404
  }, }) => {
1372
- if (props.validate) {
1405
+ const resetCallId = _resetCallId;
1406
+ if (_options.validate && !context.runRootValidation) {
1373
1407
  context.runRootValidation = true;
1374
1408
  const result = await validateForm({
1375
1409
  name,
@@ -1394,6 +1428,9 @@ function createFormControl(props = {}) {
1394
1428
  _updateIsValidating([_f.name], true);
1395
1429
  }
1396
1430
  const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !onlyCheckValid, isFieldArrayRoot);
1431
+ if (resetCallId !== _resetCallId) {
1432
+ return context.valid;
1433
+ }
1397
1434
  if (isPromiseFunction && shouldTrackIsValidatingState) {
1398
1435
  _updateIsValidating([_f.name]);
1399
1436
  }
@@ -1508,6 +1545,13 @@ function createFormControl(props = {}) {
1508
1545
  trigger(name, {
1509
1546
  delayError: options.delayError,
1510
1547
  });
1548
+ if (options.shouldValidate &&
1549
+ field &&
1550
+ field._f &&
1551
+ field._f.deps &&
1552
+ (!Array.isArray(field._f.deps) || field._f.deps.length > 0)) {
1553
+ trigger(field._f.deps);
1554
+ }
1511
1555
  };
1512
1556
  const setFieldValues = (name, value, options, skipClone = false, skipRender = false, skipValueRender = false) => {
1513
1557
  if (_names.array.has(name)) {
@@ -1555,6 +1599,10 @@ function createFormControl(props = {}) {
1555
1599
  });
1556
1600
  }
1557
1601
  }
1602
+ options.shouldValidate &&
1603
+ trigger(name, {
1604
+ delayError: options.delayError,
1605
+ });
1558
1606
  }
1559
1607
  else {
1560
1608
  const isEmpty = (Array.isArray(cloneValue) && !cloneValue.length) ||
@@ -1628,7 +1676,7 @@ function createFormControl(props = {}) {
1628
1676
  : getEventValue(event);
1629
1677
  const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
1630
1678
  const hasNoValidationEffect = !hasValidation(field._f) &&
1631
- !props.validate &&
1679
+ !_options.validate &&
1632
1680
  !_options.resolver &&
1633
1681
  !get(_formState.errors, name) &&
1634
1682
  !field._f.deps;
@@ -1671,7 +1719,7 @@ function createFormControl(props = {}) {
1671
1719
  return (shouldRender &&
1672
1720
  _subjects.state.next({ name, ...(watched ? {} : fieldState) }));
1673
1721
  }
1674
- if (!_options.resolver && props.validate) {
1722
+ if (!_options.resolver && _options.validate) {
1675
1723
  await validateForm({
1676
1724
  name: name,
1677
1725
  eventType: event.type,
@@ -1679,7 +1727,11 @@ function createFormControl(props = {}) {
1679
1727
  }
1680
1728
  !isBlurEvent && watched && _subjects.state.next({ ..._formState });
1681
1729
  if (_options.resolver) {
1730
+ const resetCallId = _resetCallId;
1682
1731
  const { errors } = await _runSchema([name]);
1732
+ if (resetCallId !== _resetCallId) {
1733
+ return;
1734
+ }
1683
1735
  _updateIsValidating([name]);
1684
1736
  _updateIsFieldValueUpdated(fieldValue);
1685
1737
  if (!isFieldValueUpdated) {
@@ -1693,8 +1745,12 @@ function createFormControl(props = {}) {
1693
1745
  isValid = isEmptyObject(errors);
1694
1746
  }
1695
1747
  else {
1748
+ const resetCallId = _resetCallId;
1696
1749
  _updateIsValidating([name], true);
1697
1750
  error = (await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1751
+ if (resetCallId !== _resetCallId) {
1752
+ return;
1753
+ }
1698
1754
  _updateIsValidating([name]);
1699
1755
  _updateIsFieldValueUpdated(fieldValue);
1700
1756
  if (isFieldValueUpdated) {
@@ -1708,6 +1764,9 @@ function createFormControl(props = {}) {
1708
1764
  name: name,
1709
1765
  eventType: event.type,
1710
1766
  });
1767
+ if (resetCallId !== _resetCallId) {
1768
+ return;
1769
+ }
1711
1770
  }
1712
1771
  }
1713
1772
  }
@@ -1841,7 +1900,7 @@ function createFormControl(props = {}) {
1841
1900
  }
1842
1901
  };
1843
1902
  const setError = (name, error, options) => {
1844
- cancelDelayedError(name);
1903
+ cancelDelayedErrorTree(name);
1845
1904
  const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
1846
1905
  const currentError = get(_formState.errors, name) || {};
1847
1906
  const { ref: currentRef, message, type, types, ...restOfErrorTree } = currentError;
@@ -1984,6 +2043,14 @@ function createFormControl(props = {}) {
1984
2043
  },
1985
2044
  });
1986
2045
  _names.mount.add(name);
2046
+ if (field && field._f) {
2047
+ const nextField = get(_fields, name);
2048
+ for (const rule of REGISTER_VALIDATION_RULES) {
2049
+ if (!(rule in options)) {
2050
+ delete nextField._f[rule];
2051
+ }
2052
+ }
2053
+ }
1987
2054
  if (field && !shouldRevalidateRemount) {
1988
2055
  _setDisabledField({
1989
2056
  disabled: isBoolean(options.disabled)
@@ -2105,10 +2172,14 @@ function createFormControl(props = {}) {
2105
2172
  fieldValues = cloneObject(values);
2106
2173
  }
2107
2174
  else {
2175
+ const resetCallId = _resetCallId;
2108
2176
  await executeBuiltInValidation({
2109
2177
  fields: _fields,
2110
2178
  eventType: EVENTS.SUBMIT,
2111
2179
  });
2180
+ if (resetCallId !== _resetCallId) {
2181
+ return;
2182
+ }
2112
2183
  unset(_formState.errors, ROOT_ERROR_TYPE);
2113
2184
  }
2114
2185
  if (_names.disabled.size) {
@@ -2166,7 +2237,7 @@ function createFormControl(props = {}) {
2166
2237
  : _getDirty();
2167
2238
  }
2168
2239
  if (!options.keepError) {
2169
- cancelDelayedError(name);
2240
+ cancelDelayedErrorTree(name);
2170
2241
  unset(_formState.errors, name);
2171
2242
  _setValid();
2172
2243
  }