@vuehookform/core 0.4.7 → 0.6.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.
@@ -539,14 +539,15 @@ function clearFieldTouched(touchedFields, fieldName) {
539
539
  }
540
540
  function clearFieldErrors(errors, fieldName) {
541
541
  const currentErrors = errors.value;
542
- const keys = Object.keys(currentErrors);
543
- if (keys.length === 0) return;
542
+ if (Object.keys(currentErrors).length === 0) return;
543
+ const nestedError = get(currentErrors, fieldName);
544
544
  const prefix = `${fieldName}.`;
545
- const keysToDelete = [];
546
- for (const key of keys) if (key === fieldName || key.startsWith(prefix)) keysToDelete.push(key);
547
- if (keysToDelete.length === 0) return;
545
+ const flatKeysToDelete = [];
546
+ for (const key of Object.keys(currentErrors)) if (key === fieldName || key.startsWith(prefix)) flatKeysToDelete.push(key);
547
+ if (nestedError === void 0 && flatKeysToDelete.length === 0) return;
548
548
  const newErrors = { ...currentErrors };
549
- for (const key of keysToDelete) delete newErrors[key];
549
+ for (const key of flatKeysToDelete) delete newErrors[key];
550
+ if (nestedError !== void 0) unset(newErrors, fieldName);
550
551
  errors.value = newErrors;
551
552
  }
552
553
  function updateFieldDirtyState(dirtyFields, defaultValues, defaultValueHashes, fieldName, currentValue) {
@@ -590,7 +591,6 @@ function createFieldError(errors, criteriaMode = "firstError") {
590
591
  const firstError = errors[0];
591
592
  if (!firstError) return "";
592
593
  if (criteriaMode === "firstError") return firstError.message;
593
- if (errors.length === 1) return firstError.message;
594
594
  const types = {};
595
595
  for (const err of errors) {
596
596
  const existing = types[err.type];
@@ -843,13 +843,13 @@ function createFieldRegistration(ctx, validate) {
843
843
  const error = await fieldOpts.validate(value);
844
844
  if (requestId !== ctx.validationRequestIds.get(fieldName)) return;
845
845
  if (ctx.resetGeneration.value !== resetGenAtStart) return;
846
- if (error) ctx.errors.value = {
847
- ...ctx.errors.value,
848
- [fieldName]: error
849
- };
850
- else {
846
+ if (error) {
847
+ const newErrors = { ...ctx.errors.value };
848
+ set(newErrors, fieldName, error);
849
+ ctx.errors.value = newErrors;
850
+ } else {
851
851
  const newErrors = { ...ctx.errors.value };
852
- delete newErrors[fieldName];
852
+ unset(newErrors, fieldName);
853
853
  ctx.errors.value = newErrors;
854
854
  }
855
855
  };
@@ -1008,7 +1008,7 @@ function createFieldRegistration(ctx, validate) {
1008
1008
  unregister
1009
1009
  };
1010
1010
  }
1011
- function createFieldArrayManager(ctx, validate, setFocus) {
1011
+ function createFieldArrayManager(ctx, validate, setFocus, formMethods) {
1012
1012
  function fields(name, options) {
1013
1013
  if (__DEV__) {
1014
1014
  const syntaxError = validatePathSyntax(name);
@@ -1052,16 +1052,76 @@ function createFieldArrayManager(ctx, validate, setFocus) {
1052
1052
  if (item) indexCache.set(item.key, i);
1053
1053
  }
1054
1054
  };
1055
- const createItem = (key) => ({
1056
- key,
1057
- get index() {
1058
- return indexCache.get(key) ?? -1;
1059
- },
1060
- remove() {
1061
- const currentIndex = indexCache.get(key) ?? -1;
1062
- if (currentIndex !== -1) removeAt(currentIndex);
1063
- }
1064
- });
1055
+ const buildPath = (index, subPath) => {
1056
+ return subPath ? `${name}.${index}.${subPath}` : `${name}.${index}`;
1057
+ };
1058
+ const createScopedMethods = (indexGetter) => {
1059
+ return {
1060
+ register: (subPath, options$1) => formMethods.register(buildPath(indexGetter(), subPath), options$1),
1061
+ setValue: (subPath, value, options$1) => formMethods.setValue(buildPath(indexGetter(), subPath), value, options$1),
1062
+ getValue: (subPath) => formMethods.getValues(buildPath(indexGetter(), subPath)),
1063
+ watch: (subPath) => formMethods.watch(buildPath(indexGetter(), subPath)),
1064
+ getFieldState: (subPath) => formMethods.getFieldState(buildPath(indexGetter(), subPath)),
1065
+ trigger: (subPath) => {
1066
+ const index = indexGetter();
1067
+ if (!subPath) return formMethods.trigger(buildPath(index));
1068
+ if (Array.isArray(subPath)) return formMethods.trigger(subPath.map((p) => buildPath(index, p)));
1069
+ return formMethods.trigger(buildPath(index, subPath));
1070
+ },
1071
+ clearErrors: (subPath) => {
1072
+ const index = indexGetter();
1073
+ if (!subPath) {
1074
+ formMethods.clearErrors(buildPath(index));
1075
+ return;
1076
+ }
1077
+ if (Array.isArray(subPath)) {
1078
+ formMethods.clearErrors(subPath.map((p) => buildPath(index, p)));
1079
+ return;
1080
+ }
1081
+ formMethods.clearErrors(buildPath(index, subPath));
1082
+ },
1083
+ setError: (subPath, error) => formMethods.setError(buildPath(indexGetter(), subPath), error)
1084
+ };
1085
+ };
1086
+ const createItem = (key) => {
1087
+ const getIndex = () => indexCache.get(key) ?? -1;
1088
+ let scoped = null;
1089
+ const getScoped = () => scoped ??= createScopedMethods(getIndex);
1090
+ return {
1091
+ key,
1092
+ get index() {
1093
+ return getIndex();
1094
+ },
1095
+ remove() {
1096
+ const currentIndex = getIndex();
1097
+ if (currentIndex !== -1) removeAt(currentIndex);
1098
+ },
1099
+ get register() {
1100
+ return getScoped().register;
1101
+ },
1102
+ get setValue() {
1103
+ return getScoped().setValue;
1104
+ },
1105
+ get getValue() {
1106
+ return getScoped().getValue;
1107
+ },
1108
+ get watch() {
1109
+ return getScoped().watch;
1110
+ },
1111
+ get getFieldState() {
1112
+ return getScoped().getFieldState;
1113
+ },
1114
+ get trigger() {
1115
+ return getScoped().trigger;
1116
+ },
1117
+ get clearErrors() {
1118
+ return getScoped().clearErrors;
1119
+ },
1120
+ get setError() {
1121
+ return getScoped().setError;
1122
+ }
1123
+ };
1124
+ };
1065
1125
  if (fa.items.value.length === 0 && fa.values.length > 0) {
1066
1126
  fa.items.value = fa.values.map(() => createItem(generateId()));
1067
1127
  rebuildIndexCache();
@@ -1378,8 +1438,6 @@ function useForm(options) {
1378
1438
  el.focus();
1379
1439
  if (focusOptions?.shouldSelect && el instanceof HTMLInputElement && typeof el.select === "function") el.select();
1380
1440
  }
1381
- const setFocusWrapper = (name) => setFocus(name);
1382
- const { fields } = createFieldArrayManager(ctx, validate, setFocusWrapper);
1383
1441
  let lastSyncTime = 0;
1384
1442
  const SYNC_DEBOUNCE_MS = 16;
1385
1443
  function syncWithDebounce() {
@@ -1469,7 +1527,7 @@ function useForm(options) {
1469
1527
  try {
1470
1528
  syncWithDebounce();
1471
1529
  if (await validate()) {
1472
- await onValid(ctx.formData);
1530
+ await onValid(deepClone(ctx.formData));
1473
1531
  ctx.isSubmitSuccessful.value = true;
1474
1532
  } else {
1475
1533
  onInvalid?.(formState.value.errors);
@@ -1551,7 +1609,6 @@ function useForm(options) {
1551
1609
  }
1552
1610
  }
1553
1611
  const opts = resetFieldOptions || {};
1554
- ctx.resetGeneration.value++;
1555
1612
  ctx.validationCache.delete(`${name}:partial`);
1556
1613
  ctx.validationCache.delete(`${name}:full`);
1557
1614
  const errorTimer = ctx.errorDelayTimers.get(name);
@@ -1560,6 +1617,11 @@ function useForm(options) {
1560
1617
  ctx.errorDelayTimers.delete(name);
1561
1618
  }
1562
1619
  ctx.pendingErrors.delete(name);
1620
+ const schemaTimer = ctx.schemaValidationTimers.get(name);
1621
+ if (schemaTimer) {
1622
+ clearTimeout(schemaTimer);
1623
+ ctx.schemaValidationTimers.delete(name);
1624
+ }
1563
1625
  let defaultValue = opts.defaultValue;
1564
1626
  if (defaultValue === void 0) defaultValue = get(ctx.defaultValues, name);
1565
1627
  else {
@@ -1672,13 +1734,13 @@ function useForm(options) {
1672
1734
  }
1673
1735
  }
1674
1736
  syncWithDebounce();
1675
- if (nameOrNames === void 0) return { ...ctx.formData };
1737
+ if (nameOrNames === void 0) return deepClone(ctx.formData);
1676
1738
  if (Array.isArray(nameOrNames)) {
1677
1739
  const result = {};
1678
- for (const fieldName of nameOrNames) result[fieldName] = get(ctx.formData, fieldName);
1740
+ for (const fieldName of nameOrNames) result[fieldName] = deepClone(get(ctx.formData, fieldName));
1679
1741
  return result;
1680
1742
  }
1681
- return get(ctx.formData, nameOrNames);
1743
+ return deepClone(get(ctx.formData, nameOrNames));
1682
1744
  }
1683
1745
  function getFieldState(name) {
1684
1746
  if (__DEV__) {
@@ -1712,13 +1774,20 @@ function useForm(options) {
1712
1774
  }
1713
1775
  if (options$1?.markAsSubmitted) ctx.submitCount.value++;
1714
1776
  if (name === void 0) return await validate();
1715
- if (Array.isArray(name)) {
1716
- let allValid = true;
1717
- for (const fieldName of name) if (!await validate(fieldName)) allValid = false;
1718
- return allValid;
1719
- }
1777
+ if (Array.isArray(name)) return (await Promise.all(name.map((fieldName) => validate(fieldName)))).every((isValid) => isValid);
1720
1778
  return await validate(name);
1721
1779
  }
1780
+ const setFocusWrapper = (name) => setFocus(name);
1781
+ const { fields } = createFieldArrayManager(ctx, validate, setFocusWrapper, {
1782
+ register: (name, options$1) => register(name, options$1),
1783
+ setValue: (name, value, options$1) => setValue(name, value, options$1),
1784
+ getValues: (name) => getValues(name),
1785
+ watch: (name) => watch$1(name),
1786
+ getFieldState: (name) => getFieldState(name),
1787
+ trigger: (name) => trigger(name),
1788
+ clearErrors: (name) => clearErrors(name),
1789
+ setError: (name, error) => setError(name, error)
1790
+ });
1722
1791
  return {
1723
1792
  register,
1724
1793
  unregister,
@@ -1838,3 +1907,5 @@ function isFieldError(error) {
1838
1907
  return typeof error === "object" && error !== null && "type" in error && "message" in error && typeof error.type === "string" && typeof error.message === "string";
1839
1908
  }
1840
1909
  export { FormContextKey, clearPathCache, get, isFieldError, provideForm, set, unset, useController, useForm, useFormContext, useFormState, useWatch };
1910
+
1911
+ //# sourceMappingURL=vuehookform.js.map