@vuehookform/core 0.6.0 → 0.7.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.
@@ -816,7 +816,7 @@ function shouldValidateOnBlur(mode, hasSubmitted, reValidateMode) {
816
816
  return mode === "onBlur" || mode === "onTouched" || hasSubmitted && (reValidateMode === "onBlur" || reValidateMode === "onTouched");
817
817
  }
818
818
  var validationRequestCounter = 0;
819
- function createFieldRegistration(ctx, validate) {
819
+ function createFieldRegistration(ctx, validate, onChangeHelpers) {
820
820
  function register(name, registerOptions) {
821
821
  if (__DEV__) {
822
822
  const syntaxError = validatePathSyntax(name);
@@ -862,6 +862,11 @@ function createFieldRegistration(ctx, validate) {
862
862
  set(ctx.formData, name, value);
863
863
  updateFieldDirtyState(ctx.dirtyFields, ctx.defaultValues, ctx.defaultValueHashes, name, value);
864
864
  const fieldOpts = ctx.fieldOptions.get(name);
865
+ if (fieldOpts?.onChange && onChangeHelpers?.setValue) try {
866
+ fieldOpts.onChange(value, { setValue: onChangeHelpers.setValue });
867
+ } catch (err) {
868
+ if (__DEV__) console.error(`[vue-hook-form] Error in onChange callback for field '${name}':`, err);
869
+ }
865
870
  if (shouldValidateOnChange(ctx.options.mode ?? "onSubmit", ctx.touchedFields.value[name] === true, ctx.submitCount.value > 0, ctx.options.reValidateMode)) {
866
871
  const validationDebounceMs = ctx.options.validationDebounce || 0;
867
872
  if (validationDebounceMs > 0) {
@@ -1421,7 +1426,8 @@ function useForm(options) {
1421
1426
  ctx.cleanup();
1422
1427
  });
1423
1428
  const { validate, clearAllPendingErrors } = createValidation(ctx);
1424
- const { register, unregister } = createFieldRegistration(ctx, validate);
1429
+ const onChangeHelpers = { setValue: null };
1430
+ const { register, unregister } = createFieldRegistration(ctx, validate, onChangeHelpers);
1425
1431
  function setFocus(name, focusOptions) {
1426
1432
  if (__DEV__) {
1427
1433
  const syntaxError = validatePathSyntax(name);
@@ -1512,6 +1518,12 @@ function useForm(options) {
1512
1518
  },
1513
1519
  get disabled() {
1514
1520
  return ctx.isDisabled.value;
1521
+ },
1522
+ get isPristine() {
1523
+ return !isDirtyComputed.value;
1524
+ },
1525
+ get canSubmit() {
1526
+ return isValidComputed.value && !ctx.isSubmitting.value && !ctx.isLoading.value && !ctx.isDisabled.value;
1515
1527
  }
1516
1528
  });
1517
1529
  const formState = computed(() => formStateInternal);
@@ -1561,6 +1573,7 @@ function useForm(options) {
1561
1573
  }
1562
1574
  if (setValueOptions?.shouldValidate) validate(name);
1563
1575
  }
1576
+ onChangeHelpers.setValue = setValue;
1564
1577
  function reset(values, resetOptions) {
1565
1578
  const opts = resetOptions || {};
1566
1579
  ctx.validationCache.clear();
@@ -1826,25 +1839,28 @@ function useFormContext() {
1826
1839
  function useWatch(options = {}) {
1827
1840
  const { control, name, defaultValue } = options;
1828
1841
  const form = control ?? useFormContext();
1829
- return computed(() => {
1830
- if (name === void 0) return form.getValues();
1831
- if (Array.isArray(name)) {
1832
- const result = {};
1833
- for (const fieldName of name) result[fieldName] = get(form.getValues(), fieldName) ?? defaultValue;
1842
+ if (name === void 0) return form.watch();
1843
+ if (Array.isArray(name)) {
1844
+ const watched$1 = form.watch(name);
1845
+ if (defaultValue === void 0) return watched$1;
1846
+ return computed(() => {
1847
+ const result = { ...watched$1.value };
1848
+ for (const fieldName of name) if (result[fieldName] === void 0) result[fieldName] = defaultValue;
1834
1849
  return result;
1835
- }
1836
- return get(form.getValues(), name) ?? defaultValue;
1837
- });
1850
+ });
1851
+ }
1852
+ const watched = form.watch(name);
1853
+ if (defaultValue === void 0) return watched;
1854
+ return computed(() => watched.value ?? defaultValue);
1838
1855
  }
1839
1856
  function useController(options) {
1840
1857
  const { name, control, defaultValue } = options;
1841
1858
  const form = control ?? useFormContext();
1842
1859
  const elementRef = ref(null);
1843
1860
  if (defaultValue !== void 0 && form.getValues(name) === void 0) form.setValue(name, defaultValue);
1861
+ const watchedValue = form.watch(name);
1844
1862
  const value = computed({
1845
- get: () => {
1846
- return form.getValues(name) ?? defaultValue;
1847
- },
1863
+ get: () => watchedValue.value ?? defaultValue,
1848
1864
  set: (newValue) => {
1849
1865
  form.setValue(name, newValue);
1850
1866
  }
@@ -1903,9 +1919,22 @@ function useFormState(options = {}) {
1903
1919
  return { [name]: fullState[name] };
1904
1920
  });
1905
1921
  }
1922
+ function useFieldError(options) {
1923
+ const form = options.control ?? useFormContext();
1924
+ return computed(() => {
1925
+ const error = get(form.formState.value.errors, options.name);
1926
+ if (!error) return void 0;
1927
+ if (typeof error === "string") return error;
1928
+ if (Array.isArray(error)) {
1929
+ if (__DEV__) console.warn(`[vue-hook-form] useFieldError('${options.name}') resolved to an array of per-item errors.\nuseFieldError only returns scalar error messages. Use formState.value.errors['${options.name}'] directly for item-level errors.`);
1930
+ return;
1931
+ }
1932
+ if (typeof error === "object" && "message" in error) return error.message;
1933
+ });
1934
+ }
1906
1935
  function isFieldError(error) {
1907
1936
  return typeof error === "object" && error !== null && "type" in error && "message" in error && typeof error.type === "string" && typeof error.message === "string";
1908
1937
  }
1909
- export { FormContextKey, clearPathCache, get, isFieldError, provideForm, set, unset, useController, useForm, useFormContext, useFormState, useWatch };
1938
+ export { FormContextKey, clearPathCache, get, isFieldError, provideForm, set, unset, useController, useFieldError, useForm, useFormContext, useFormState, useWatch };
1910
1939
 
1911
1940
  //# sourceMappingURL=vuehookform.js.map