@form-observer/vue 0.9.1 → 1.0.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.
@@ -38,15 +38,15 @@ function createFormValidityObserver(type, options) {
38
38
  /* -------------------- Enhancements -------------------- */
39
39
  // Add automatic setup/teardown
40
40
  observer.autoObserve = function autoObserve(novalidate = true) {
41
- /** @type {HTMLFormElement | null} */
41
+ /** @type {Element | import("vue").ComponentPublicInstance | null} */
42
42
  let form;
43
43
 
44
44
  /** @param {typeof form} vueRef @returns {void} */
45
45
  return (vueRef) => {
46
46
  if (vueRef) {
47
47
  form = vueRef;
48
- observer.observe(form);
49
- if (novalidate) form.setAttribute("novalidate", "");
48
+ observer.observe(/** @type {HTMLFormElement} */ (form));
49
+ if (novalidate) /** @type {HTMLFormElement} */ (form).setAttribute("novalidate", "");
50
50
  return;
51
51
  }
52
52
 
@@ -55,6 +55,16 @@ function createFormValidityObserver(type, options) {
55
55
  };
56
56
  };
57
57
 
58
+ observer.vAutoObserve = {
59
+ mounted(form, binding) {
60
+ observer.observe(form);
61
+ if (binding.value !== false) form.setAttribute("novalidate", "");
62
+ },
63
+ beforeUnmount(form) {
64
+ observer.unobserve(form);
65
+ },
66
+ };
67
+
58
68
  // Enhanced `configure` method
59
69
  observer.configure = function configure(name, errorMessages) {
60
70
  const keys = /** @type {Array<keyof import("./types.d.cts").VueValidationErrors<M, any, R>>} */ (
@@ -9,4 +9,4 @@
9
9
  * @param {import("./index.d.cts").FormValidityObserverOptions<M, E, R>} [options]
10
10
  * @returns {import("./types.d.cts").VueFormValidityObserver<M, R>}
11
11
  */
12
- export default function createFormValidityObserver<T extends keyof DocumentEventMap | null, M = string, E extends import("@form-observer/core/types").ValidatableField = import("@form-observer/core/types").ValidatableField, R extends boolean = false>(type: T, options?: import("@form-observer/core/FormValidityObserver").FormValidityObserverOptions<M, E, R> | undefined): import("./types.d.cts").VueFormValidityObserver<M, R>;
12
+ export default function createFormValidityObserver<T extends import("./index.d.cts").EventType | null, M = string, E extends import("./index.d.cts").ValidatableField = import("@form-observer/core/types").ValidatableField, R extends boolean = false>(type: T, options?: import("./index.d.cts").FormValidityObserverOptions<M, E, R>): import("./types.d.cts").VueFormValidityObserver<M, R>;
@@ -9,4 +9,4 @@
9
9
  * @param {import("./index.d.ts").FormValidityObserverOptions<M, E, R>} [options]
10
10
  * @returns {import("./types.d.ts").VueFormValidityObserver<M, R>}
11
11
  */
12
- export default function createFormValidityObserver<T extends keyof DocumentEventMap | null, M = string, E extends import("@form-observer/core/types").ValidatableField = import("@form-observer/core/types").ValidatableField, R extends boolean = false>(type: T, options?: import("@form-observer/core/FormValidityObserver").FormValidityObserverOptions<M, E, R> | undefined): import("./types.d.ts").VueFormValidityObserver<M, R>;
12
+ export default function createFormValidityObserver<T extends import("./index.d.ts").EventType | null, M = string, E extends import("./index.d.ts").ValidatableField = import("@form-observer/core/types").ValidatableField, R extends boolean = false>(type: T, options?: import("./index.d.ts").FormValidityObserverOptions<M, E, R>): import("./types.d.ts").VueFormValidityObserver<M, R>;
@@ -34,15 +34,15 @@ export default function createFormValidityObserver(type, options) {
34
34
  /* -------------------- Enhancements -------------------- */
35
35
  // Add automatic setup/teardown
36
36
  observer.autoObserve = function autoObserve(novalidate = true) {
37
- /** @type {HTMLFormElement | null} */
37
+ /** @type {Element | import("vue").ComponentPublicInstance | null} */
38
38
  let form;
39
39
 
40
40
  /** @param {typeof form} vueRef @returns {void} */
41
41
  return (vueRef) => {
42
42
  if (vueRef) {
43
43
  form = vueRef;
44
- observer.observe(form);
45
- if (novalidate) form.setAttribute("novalidate", "");
44
+ observer.observe(/** @type {HTMLFormElement} */ (form));
45
+ if (novalidate) /** @type {HTMLFormElement} */ (form).setAttribute("novalidate", "");
46
46
  return;
47
47
  }
48
48
 
@@ -51,6 +51,16 @@ export default function createFormValidityObserver(type, options) {
51
51
  };
52
52
  };
53
53
 
54
+ observer.vAutoObserve = {
55
+ mounted(form, binding) {
56
+ observer.observe(form);
57
+ if (binding.value !== false) form.setAttribute("novalidate", "");
58
+ },
59
+ beforeUnmount(form) {
60
+ observer.unobserve(form);
61
+ },
62
+ };
63
+
54
64
  // Enhanced `configure` method
55
65
  observer.configure = function configure(name, errorMessages) {
56
66
  const keys = /** @type {Array<keyof import("./types.d.ts").VueValidationErrors<M, any, R>>} */ (
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@form-observer/vue",
3
3
  "type": "module",
4
- "version": "0.9.1",
4
+ "version": "1.0.0",
5
5
  "sideEffects": false,
6
6
  "description": "Convenience functions for the `@form-observer/core` package, designed for Vue apps",
7
7
  "exports": {
@@ -29,11 +29,11 @@
29
29
  }
30
30
  },
31
31
  "peerDependencies": {
32
- "@form-observer/core": "^0.9.1"
32
+ "@form-observer/core": "^1.0.0"
33
33
  },
34
34
  "devDependencies": {
35
- "@testing-library/vue": "^8.0.3",
36
- "@vitejs/plugin-vue": "^5.0.4",
35
+ "@testing-library/vue": "^8.1.0",
36
+ "@vitejs/plugin-vue": "^6.0.3",
37
37
  "vue": "^3.3.6"
38
38
  },
39
39
  "author": "Isaiah Thomason",
package/types.d.cts CHANGED
@@ -1,8 +1,10 @@
1
1
  import type { ErrorMessage, ValidationErrors, ValidatableField, FormValidityObserver } from "@form-observer/core";
2
- import type { InputHTMLAttributes } from "vue";
2
+ import type { Directive, ComponentPublicInstance, InputHTMLAttributes } from "vue";
3
3
 
4
- export interface VueFormValidityObserver<M = string, R extends boolean = false>
5
- extends Omit<FormValidityObserver<M, R>, "configure"> {
4
+ export interface VueFormValidityObserver<M = string, R extends boolean = false> extends Omit<
5
+ FormValidityObserver<M, R>,
6
+ "configure"
7
+ > {
6
8
  /**
7
9
  * An enhanced version of {@link FormValidityObserver.configure} for `Vue`. In addition to configuring a field's
8
10
  * error messages, it generates the props that should be applied to the field based on the provided arguments.
@@ -27,18 +29,39 @@ export interface VueFormValidityObserver<M = string, R extends boolean = false>
27
29
  /**
28
30
  * Creates a Vue function `ref` used to automatically setup and cleanup a form's observer.
29
31
  *
30
- * **Note**: If you use this `ref`, you should **not** call `observe`, `unobserve`, or `disconnect` directly.
32
+ * **Note**: If you use this `ref`, you should **not** call `observe`, `unobserve` or `disconnect` directly,
33
+ * nor should you use {@link vAutoObserve `v-auto-observe`}
31
34
  *
32
35
  * @param novalidate Indicates that the
33
36
  * {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#novalidate novalidate} attribute should
34
37
  * be applied to the `form` element when JavaScript is enabled. Defaults to `true`.
35
38
  *
36
39
  * @example
37
- * <form :ref="autoObserve()">
40
+ * <form v-auto-observe>
38
41
  * <input name="first-name" type="textbox" required />
39
42
  * </form>
43
+ *
44
+ * // Or
45
+ * <form v-auto-observe="true">
46
+ * <input name="email" type="email" required />
47
+ * </form>
48
+ */
49
+ autoObserve(novalidate?: boolean): (formRef: Element | ComponentPublicInstance | null) => void;
50
+
51
+ /**
52
+ * A custom Vue [`directive`](https://vuejs.org/guide/reusability/custom-directives) used to automatically
53
+ * setup and cleanup a form's observer.
54
+ *
55
+ * Accepts a `boolean` value which determines if the
56
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#novalidate novalidate} attribute
57
+ * should be applied to the `form` element when JavaScript is enabled. (Defaults to `true`.)
58
+ *
59
+ * **Note**: If you use this `directive`, you should **not** call `observe`, `unobserve` or `disconnect` directly,
60
+ * nor should you use {@link autoObserve `autoObserve`}.
61
+ *
62
+ * @example
40
63
  */
41
- autoObserve(novalidate?: boolean): (formRef: HTMLFormElement | null) => void;
64
+ vAutoObserve: Directive<HTMLFormElement, boolean>;
42
65
  }
43
66
 
44
67
  export type VueFieldProps = Pick<
@@ -50,8 +73,11 @@ export type VueFieldProps = Pick<
50
73
  * An augmetation of {@link ValidationErrors} for `Vue`. Represents the constraints that should be applied
51
74
  * to a form field, and the error messages that should be displayed when those constraints are broken.
52
75
  */
53
- export interface VueValidationErrors<M, E extends ValidatableField = ValidatableField, R extends boolean = false>
54
- extends Pick<ValidationErrors<M, E, R>, "badinput" | "validate"> {
76
+ export interface VueValidationErrors<
77
+ M,
78
+ E extends ValidatableField = ValidatableField,
79
+ R extends boolean = false,
80
+ > extends Pick<ValidationErrors<M, E, R>, "badinput" | "validate"> {
55
81
  // Standard HTML Attributes
56
82
  required?: VueErrorDetails<M, InputHTMLAttributes["required"], E, R> | ErrorMessage<R extends true ? M : string, E>;
57
83
  minlength?: VueErrorDetails<M, InputHTMLAttributes["minlength"], E, R>;
package/types.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import type { ErrorMessage, ValidationErrors, ValidatableField, FormValidityObserver } from "@form-observer/core";
2
- import type { InputHTMLAttributes } from "vue";
2
+ import type { Directive, ComponentPublicInstance, InputHTMLAttributes } from "vue";
3
3
 
4
- export interface VueFormValidityObserver<M = string, R extends boolean = false>
5
- extends Omit<FormValidityObserver<M, R>, "configure"> {
4
+ export interface VueFormValidityObserver<M = string, R extends boolean = false> extends Omit<
5
+ FormValidityObserver<M, R>,
6
+ "configure"
7
+ > {
6
8
  /**
7
9
  * An enhanced version of {@link FormValidityObserver.configure} for `Vue`. In addition to configuring a field's
8
10
  * error messages, it generates the props that should be applied to the field based on the provided arguments.
@@ -27,18 +29,39 @@ export interface VueFormValidityObserver<M = string, R extends boolean = false>
27
29
  /**
28
30
  * Creates a Vue function `ref` used to automatically setup and cleanup a form's observer.
29
31
  *
30
- * **Note**: If you use this `ref`, you should **not** call `observe`, `unobserve`, or `disconnect` directly.
32
+ * **Note**: If you use this `ref`, you should **not** call `observe`, `unobserve` or `disconnect` directly,
33
+ * nor should you use {@link vAutoObserve `v-auto-observe`}
31
34
  *
32
35
  * @param novalidate Indicates that the
33
36
  * {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#novalidate novalidate} attribute should
34
37
  * be applied to the `form` element when JavaScript is enabled. Defaults to `true`.
35
38
  *
36
39
  * @example
37
- * <form :ref="autoObserve()">
40
+ * <form v-auto-observe>
38
41
  * <input name="first-name" type="textbox" required />
39
42
  * </form>
43
+ *
44
+ * // Or
45
+ * <form v-auto-observe="true">
46
+ * <input name="email" type="email" required />
47
+ * </form>
48
+ */
49
+ autoObserve(novalidate?: boolean): (formRef: Element | ComponentPublicInstance | null) => void;
50
+
51
+ /**
52
+ * A custom Vue [`directive`](https://vuejs.org/guide/reusability/custom-directives) used to automatically
53
+ * setup and cleanup a form's observer.
54
+ *
55
+ * Accepts a `boolean` value which determines if the
56
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#novalidate novalidate} attribute
57
+ * should be applied to the `form` element when JavaScript is enabled. (Defaults to `true`.)
58
+ *
59
+ * **Note**: If you use this `directive`, you should **not** call `observe`, `unobserve` or `disconnect` directly,
60
+ * nor should you use {@link autoObserve `autoObserve`}.
61
+ *
62
+ * @example
40
63
  */
41
- autoObserve(novalidate?: boolean): (formRef: HTMLFormElement | null) => void;
64
+ vAutoObserve: Directive<HTMLFormElement, boolean>;
42
65
  }
43
66
 
44
67
  export type VueFieldProps = Pick<
@@ -50,8 +73,11 @@ export type VueFieldProps = Pick<
50
73
  * An augmetation of {@link ValidationErrors} for `Vue`. Represents the constraints that should be applied
51
74
  * to a form field, and the error messages that should be displayed when those constraints are broken.
52
75
  */
53
- export interface VueValidationErrors<M, E extends ValidatableField = ValidatableField, R extends boolean = false>
54
- extends Pick<ValidationErrors<M, E, R>, "badinput" | "validate"> {
76
+ export interface VueValidationErrors<
77
+ M,
78
+ E extends ValidatableField = ValidatableField,
79
+ R extends boolean = false,
80
+ > extends Pick<ValidationErrors<M, E, R>, "badinput" | "validate"> {
55
81
  // Standard HTML Attributes
56
82
  required?: VueErrorDetails<M, InputHTMLAttributes["required"], E, R> | ErrorMessage<R extends true ? M : string, E>;
57
83
  minlength?: VueErrorDetails<M, InputHTMLAttributes["minlength"], E, R>;