@chepchik/bind-form 0.1.0 → 0.1.1
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.
- package/dist/index.d.ts +29 -34
- package/dist/index.js +26 -27
- package/package.json +12 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,34 +1,29 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
/** Form values: field name -> string value taken from FormData. */
|
|
32
|
-
export declare type FormValues<TField extends string> = Record<TField, string>;
|
|
33
|
-
|
|
34
|
-
export { }
|
|
1
|
+
/** Validation rule for a single form field. */
|
|
2
|
+
export interface FieldRule {
|
|
3
|
+
/** Error message shown if the field is empty after trim(). */
|
|
4
|
+
required?: string;
|
|
5
|
+
/** Regex and error message checked only when the field is non-empty. */
|
|
6
|
+
pattern?: {
|
|
7
|
+
value: RegExp;
|
|
8
|
+
message: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/** Form values: field name -> string value taken from FormData. */
|
|
12
|
+
export type FormValues<TField extends string> = Record<TField, string>;
|
|
13
|
+
export interface BindFormOptions<TField extends string> {
|
|
14
|
+
/** Validation schema: keys must match the `name` attributes of the form fields. */
|
|
15
|
+
schema: Record<TField, FieldRule>;
|
|
16
|
+
/** Element used to display the validation error message (must support `hidden`). */
|
|
17
|
+
errorElement?: HTMLElement;
|
|
18
|
+
/** Called with the validated values after a successful submit. */
|
|
19
|
+
onSubmit: (values: FormValues<TField>, form: HTMLFormElement) => void | Promise<void>;
|
|
20
|
+
/** Reset the form after a successful onSubmit. */
|
|
21
|
+
resetOnSuccess?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Subscribes to a form's submit event: prevents the page reload, validates fields
|
|
25
|
+
* against the schema (required/pattern) and calls onSubmit with the collected values.
|
|
26
|
+
* Submitting via Enter works out of the box (default <form> behavior).
|
|
27
|
+
* Returns an unsubscribe function.
|
|
28
|
+
*/
|
|
29
|
+
export declare function bindForm<TField extends string>(form: HTMLFormElement, options: BindFormOptions<TField>): () => void;
|
package/dist/index.js
CHANGED
|
@@ -1,30 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
function e(e, r) {
|
|
3
|
+
let i = (i) => {
|
|
4
|
+
i.preventDefault();
|
|
5
|
+
let a = new FormData(e), o = {}, s;
|
|
6
|
+
for (let e of Object.keys(r.schema)) {
|
|
7
|
+
let t = String(a.get(e) ?? "").trim();
|
|
8
|
+
o[e] = t;
|
|
9
|
+
let n = r.schema[e];
|
|
10
|
+
!s && n.required && !t ? s = n.required : !s && t && n.pattern && !n.pattern.value.test(t) && (s = n.pattern.message);
|
|
11
|
+
}
|
|
12
|
+
if (s) {
|
|
13
|
+
t(r.errorElement, s);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
n(r.errorElement), Promise.resolve(r.onSubmit(o, e)).then(() => {
|
|
17
|
+
r.resetOnSuccess && e.reset();
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
return e.addEventListener("submit", i), () => e.removeEventListener("submit", i);
|
|
21
21
|
}
|
|
22
|
-
function
|
|
23
|
-
|
|
22
|
+
function t(e, t) {
|
|
23
|
+
e && (e.textContent = t, e.removeAttribute("hidden"));
|
|
24
24
|
}
|
|
25
|
-
function
|
|
26
|
-
|
|
25
|
+
function n(e) {
|
|
26
|
+
e && e.setAttribute("hidden", "");
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
};
|
|
28
|
+
//#endregion
|
|
29
|
+
export { e as bindForm };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chepchik/bind-form",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Tiny dependency-free form binding helper: submit handling, validation schema and error display.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -12,17 +12,23 @@
|
|
|
12
12
|
"import": "./dist/index.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
"files": [
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
16
18
|
"sideEffects": false,
|
|
17
19
|
"scripts": {
|
|
18
20
|
"build": "vite build",
|
|
19
21
|
"dev": "vite build --watch"
|
|
20
22
|
},
|
|
21
|
-
"keywords": [
|
|
23
|
+
"keywords": [
|
|
24
|
+
"form",
|
|
25
|
+
"validation",
|
|
26
|
+
"spa"
|
|
27
|
+
],
|
|
22
28
|
"license": "MIT",
|
|
23
29
|
"devDependencies": {
|
|
24
|
-
"typescript": "^
|
|
25
|
-
"vite": "^
|
|
26
|
-
"vite-plugin-dts": "^
|
|
30
|
+
"typescript": "^6.0.3",
|
|
31
|
+
"vite": "^8.0.16",
|
|
32
|
+
"vite-plugin-dts": "^5.0.2"
|
|
27
33
|
}
|
|
28
34
|
}
|