@fynixorg/ui 1.0.10 → 1.0.12
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/LICENSE +21 -0
- package/dist/README.md +36 -0
- package/dist/context/context.d.ts +19 -0
- package/dist/context/context.d.ts.map +1 -0
- package/dist/context/context.js +3 -11
- package/dist/context/context.js.map +3 -3
- package/dist/custom/button.d.ts +2 -0
- package/dist/custom/button.d.ts.map +1 -0
- package/dist/custom/button.js +2 -9
- package/dist/custom/button.js.map +3 -3
- package/dist/custom/index.d.ts +3 -0
- package/dist/custom/index.d.ts.map +1 -0
- package/dist/custom/index.js +2 -7
- package/dist/custom/index.js.map +3 -3
- package/dist/custom/path.d.ts +14 -0
- package/dist/custom/path.d.ts.map +1 -0
- package/dist/custom/path.js +17 -34
- package/dist/custom/path.js.map +3 -3
- package/dist/error/errorOverlay.d.ts +3 -0
- package/dist/error/errorOverlay.d.ts.map +1 -0
- package/dist/error/errorOverlay.js +82 -91
- package/dist/error/errorOverlay.js.map +3 -3
- package/dist/fynix/index.d.ts +5 -0
- package/dist/fynix/index.d.ts.map +1 -0
- package/dist/fynix/index.js +2 -7
- package/dist/fynix/index.js.map +3 -3
- package/dist/hooks/nixAsync.d.ts +14 -0
- package/dist/hooks/nixAsync.d.ts.map +1 -0
- package/dist/hooks/nixAsync.js +38 -43
- package/dist/hooks/nixAsync.js.map +3 -3
- package/dist/hooks/nixAsyncCache.d.ts +14 -0
- package/dist/hooks/nixAsyncCache.d.ts.map +1 -0
- package/dist/hooks/nixAsyncCache.js +57 -59
- package/dist/hooks/nixAsyncCache.js.map +3 -3
- package/dist/hooks/nixAsyncDebounce.d.ts +22 -0
- package/dist/hooks/nixAsyncDebounce.d.ts.map +1 -0
- package/dist/hooks/nixAsyncDebounce.js +74 -85
- package/dist/hooks/nixAsyncDebounce.js.map +3 -3
- package/dist/hooks/nixAsyncQuery.d.ts +16 -0
- package/dist/hooks/nixAsyncQuery.d.ts.map +1 -0
- package/dist/hooks/nixAsyncQuery.js +85 -79
- package/dist/hooks/nixAsyncQuery.js.map +3 -3
- package/dist/hooks/nixCallback.d.ts +2 -0
- package/dist/hooks/nixCallback.d.ts.map +1 -0
- package/dist/hooks/nixCallback.js +30 -40
- package/dist/hooks/nixCallback.js.map +3 -3
- package/dist/hooks/nixComputed.d.ts +16 -0
- package/dist/hooks/nixComputed.d.ts.map +1 -0
- package/dist/hooks/nixComputed.js +166 -198
- package/dist/hooks/nixComputed.js.map +4 -4
- package/dist/hooks/nixDebounce.d.ts +11 -0
- package/dist/hooks/nixDebounce.d.ts.map +1 -0
- package/dist/hooks/nixDebounce.js +53 -58
- package/dist/hooks/nixDebounce.js.map +3 -3
- package/dist/hooks/nixEffect.d.ts +4 -0
- package/dist/hooks/nixEffect.d.ts.map +1 -0
- package/dist/hooks/nixEffect.js +65 -75
- package/dist/hooks/nixEffect.js.map +3 -3
- package/dist/hooks/nixForm.d.ts +33 -0
- package/dist/hooks/nixForm.d.ts.map +1 -0
- package/dist/hooks/nixForm.js +110 -120
- package/dist/hooks/nixForm.js.map +3 -3
- package/dist/hooks/nixFormAsync.d.ts +42 -0
- package/dist/hooks/nixFormAsync.d.ts.map +1 -0
- package/dist/hooks/nixFormAsync.js +158 -167
- package/dist/hooks/nixFormAsync.js.map +3 -3
- package/dist/hooks/nixInterval.d.ts +2 -0
- package/dist/hooks/nixInterval.d.ts.map +1 -0
- package/dist/hooks/nixInterval.js +21 -27
- package/dist/hooks/nixInterval.js.map +3 -3
- package/dist/hooks/nixLazy.d.ts +8 -0
- package/dist/hooks/nixLazy.d.ts.map +1 -0
- package/dist/hooks/nixLazy.js +53 -58
- package/dist/hooks/nixLazy.js.map +3 -3
- package/dist/hooks/nixLazyAsync.d.ts +10 -0
- package/dist/hooks/nixLazyAsync.d.ts.map +1 -0
- package/dist/hooks/nixLazyAsync.js +65 -71
- package/dist/hooks/nixLazyAsync.js.map +3 -3
- package/dist/hooks/nixLazyFormAsync.d.ts +50 -0
- package/dist/hooks/nixLazyFormAsync.d.ts.map +1 -0
- package/dist/hooks/nixLazyFormAsync.js +209 -213
- package/dist/hooks/nixLazyFormAsync.js.map +3 -3
- package/dist/hooks/nixLocalStorage.d.ts +5 -0
- package/dist/hooks/nixLocalStorage.d.ts.map +1 -0
- package/dist/hooks/nixLocalStorage.js +21 -25
- package/dist/hooks/nixLocalStorage.js.map +3 -3
- package/dist/hooks/nixMemo.d.ts +2 -0
- package/dist/hooks/nixMemo.d.ts.map +1 -0
- package/dist/hooks/nixMemo.js +27 -31
- package/dist/hooks/nixMemo.js.map +3 -3
- package/dist/hooks/nixPrevious.d.ts +2 -0
- package/dist/hooks/nixPrevious.d.ts.map +1 -0
- package/dist/hooks/nixPrevious.js +13 -19
- package/dist/hooks/nixPrevious.js.map +3 -3
- package/dist/hooks/nixRef.d.ts +4 -0
- package/dist/hooks/nixRef.d.ts.map +1 -0
- package/dist/hooks/nixRef.js +14 -20
- package/dist/hooks/nixRef.js.map +3 -3
- package/dist/hooks/nixState.d.ts +15 -0
- package/dist/hooks/nixState.d.ts.map +1 -0
- package/dist/hooks/nixState.js +120 -173
- package/dist/hooks/nixState.js.map +3 -3
- package/dist/hooks/nixStore.d.ts +7 -0
- package/dist/hooks/nixStore.d.ts.map +1 -0
- package/dist/hooks/nixStore.js +48 -54
- package/dist/hooks/nixStore.js.map +3 -3
- package/dist/package.json +213 -0
- package/dist/plugins/vite-plugin-res.d.ts +41 -0
- package/dist/plugins/vite-plugin-res.d.ts.map +1 -0
- package/dist/plugins/vite-plugin-res.js +620 -33
- package/dist/plugins/vite-plugin-res.js.map +4 -4
- package/dist/router/router.d.ts +35 -0
- package/dist/router/router.d.ts.map +1 -0
- package/dist/router/router.js +520 -486
- package/dist/router/router.js.map +3 -3
- package/dist/runtime.d.ts +62 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +833 -820
- package/dist/runtime.js.map +4 -4
- package/package.json +226 -43
- package/types/fnx.d.ts +72 -0
- package/types/fynix-ui.d.ts +323 -0
- package/types/global.d.ts +46 -6
- package/types/index.d.ts +37 -0
- package/types/vite-env.d.ts +553 -0
- package/runtime.d.ts +0 -83
- package/types/jsx.d.ts +0 -692
package/dist/hooks/nixEffect.js
CHANGED
|
@@ -1,85 +1,75 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
1
|
import { activeContext } from "../context/context";
|
|
4
|
-
function nixEffect(effect, deps = []) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
const MAX_DEPS = 100;
|
|
17
|
-
if (deps.length > MAX_DEPS) {
|
|
18
|
-
console.warn(`[nixEffect] Dependency array too large (${deps.length}). Limited to ${MAX_DEPS}.`);
|
|
19
|
-
deps = deps.slice(0, MAX_DEPS);
|
|
20
|
-
}
|
|
21
|
-
const idx = ctx.hookIndex++;
|
|
22
|
-
const prev = ctx.hooks[idx];
|
|
23
|
-
const hasChanged = !prev || !shallowArrayEqual(prev.deps, deps);
|
|
24
|
-
if (hasChanged) {
|
|
25
|
-
if (prev?.cleanup) {
|
|
26
|
-
try {
|
|
27
|
-
if (typeof prev.cleanup === "function") {
|
|
28
|
-
prev.cleanup();
|
|
29
|
-
}
|
|
30
|
-
} catch (err) {
|
|
31
|
-
console.error("[nixEffect] Cleanup error:", err);
|
|
32
|
-
}
|
|
2
|
+
export function nixEffect(effect, deps = []) {
|
|
3
|
+
const ctx = activeContext;
|
|
4
|
+
if (!ctx)
|
|
5
|
+
throw new Error("nixEffect() called outside component");
|
|
6
|
+
if (typeof effect !== "function") {
|
|
7
|
+
console.error("[nixEffect] First argument must be a function");
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (!Array.isArray(deps)) {
|
|
11
|
+
console.error("[nixEffect] Second argument must be an array");
|
|
12
|
+
deps = [];
|
|
33
13
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
console.warn("[nixEffect] Effect should return undefined or a cleanup function");
|
|
39
|
-
cleanup = void 0;
|
|
40
|
-
}
|
|
41
|
-
} catch (err) {
|
|
42
|
-
console.error("[nixEffect] Effect error:", err);
|
|
43
|
-
cleanup = void 0;
|
|
14
|
+
const MAX_DEPS = 100;
|
|
15
|
+
if (deps.length > MAX_DEPS) {
|
|
16
|
+
console.warn(`[nixEffect] Dependency array too large (${deps.length}). Limited to ${MAX_DEPS}.`);
|
|
17
|
+
deps = deps.slice(0, MAX_DEPS);
|
|
44
18
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
19
|
+
const idx = ctx.hookIndex++;
|
|
20
|
+
const prev = ctx.hooks[idx];
|
|
21
|
+
const hasChanged = !prev || !shallowArrayEqual(prev.deps, deps);
|
|
22
|
+
if (hasChanged) {
|
|
23
|
+
if (prev?.cleanup) {
|
|
24
|
+
try {
|
|
25
|
+
if (typeof prev.cleanup === "function") {
|
|
26
|
+
prev.cleanup();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
console.error("[nixEffect] Cleanup error:", err);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
let cleanup;
|
|
34
|
+
try {
|
|
35
|
+
cleanup = effect();
|
|
36
|
+
if (cleanup !== undefined && typeof cleanup !== "function") {
|
|
37
|
+
console.warn("[nixEffect] Effect should return undefined or a cleanup function");
|
|
38
|
+
cleanup = undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
console.error("[nixEffect] Effect error:", err);
|
|
43
|
+
cleanup = undefined;
|
|
44
|
+
}
|
|
45
|
+
ctx.hooks[idx] = { deps, cleanup };
|
|
46
|
+
if (cleanup && typeof cleanup === "function") {
|
|
47
|
+
if (!ctx.cleanups)
|
|
48
|
+
ctx.cleanups = [];
|
|
49
|
+
ctx.cleanups.push(cleanup);
|
|
50
|
+
}
|
|
50
51
|
}
|
|
51
|
-
}
|
|
52
52
|
}
|
|
53
|
-
__name(nixEffect, "nixEffect");
|
|
54
53
|
function shallowArrayEqual(arr1, arr2) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
if (arr1.length !== arr2.length)
|
|
55
|
+
return false;
|
|
56
|
+
for (let i = 0; i < arr1.length; i++) {
|
|
57
|
+
if (!Object.is(arr1[i], arr2[i])) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
60
|
}
|
|
61
|
-
|
|
62
|
-
return true;
|
|
61
|
+
return true;
|
|
63
62
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return nixEffect(effect, []);
|
|
63
|
+
export function nixEffectOnce(effect) {
|
|
64
|
+
return nixEffect(effect, []);
|
|
67
65
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return nixEffect(effect, [ctx.version]);
|
|
66
|
+
export function nixEffectAlways(effect) {
|
|
67
|
+
const ctx = activeContext;
|
|
68
|
+
if (!ctx)
|
|
69
|
+
throw new Error("nixEffectAlways() called outside component");
|
|
70
|
+
if (typeof effect !== "function") {
|
|
71
|
+
console.error("[nixEffectAlways] Argument must be a function");
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
return nixEffect(effect, [ctx.version]);
|
|
78
75
|
}
|
|
79
|
-
__name(nixEffectAlways, "nixEffectAlways");
|
|
80
|
-
export {
|
|
81
|
-
nixEffect,
|
|
82
|
-
nixEffectAlways,
|
|
83
|
-
nixEffectOnce
|
|
84
|
-
};
|
|
85
|
-
//# sourceMappingURL=nixEffect.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../hooks/nixEffect.
|
|
4
|
-
"sourcesContent": ["/* ----------------------\r\n nixEffect - Side Effects Hook\r\n Memory Leaks & Security Issues Resolved\r\n---------------------- */\r\nimport { activeContext } from \"../context/context\";\r\n\r\n/**\r\n * Execute side effects in a component with automatic cleanup.\r\n * Similar to React's useEffect.\r\n
|
|
5
|
-
"mappings": ";;
|
|
3
|
+
"sources": ["../../hooks/nixEffect.ts"],
|
|
4
|
+
"sourcesContent": ["/* MIT License\r\n\r\n* Copyright (c) 2026 Resty Gonzales\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in all\r\ncopies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\n* SOFTWARE.\r\n */\r\n/* ----------------------\r\n nixEffect - Side Effects Hook\r\n Memory Leaks & Security Issues Resolved\r\n---------------------- */\r\nimport { activeContext } from \"../context/context\";\r\n\r\n/**\r\n * Execute side effects in a component with automatic cleanup.\r\n * Similar to React's useEffect.\r\n *\r\n * @param {() => (void | (() => void))} effect - Effect function, optionally returns cleanup function\r\n * @param {Array<any>} [deps=[]] - Dependency array. Effect re-runs when dependencies change.\r\n *\r\n * @example\r\n * // Run once on mount\r\n * nixEffect(() => {\r\n * console.log('Component mounted');\r\n * return () => console.log('Component unmounted');\r\n * }, []);\r\n *\r\n * @example\r\n * // Run when count changes\r\n * const count = nixState(0);\r\n * nixEffect(() => {\r\n * console.log('Count is:', count.value);\r\n * }, [count.value]);\r\n *\r\n * @example\r\n * // Timer with cleanup\r\n * nixEffect(() => {\r\n * const timer = setInterval(() => console.log('tick'), 1000);\r\n * return () => clearInterval(timer);\r\n * }, []);\r\n *\r\n * @example\r\n * // Event listener with cleanup\r\n * nixEffect(() => {\r\n * const handler = (e) => console.log('clicked', e);\r\n * document.addEventListener('click', handler);\r\n * return () => document.removeEventListener('click', handler);\r\n * }, []);\r\n *\r\n * @throws {Error} If called outside a component context\r\n */\r\nexport function nixEffect(\r\n effect: () => void | (() => void),\r\n deps: any[] = []\r\n): void {\r\n const ctx = activeContext as\r\n | (typeof activeContext & {\r\n hookIndex: number;\r\n hooks: Array<any>;\r\n cleanups?: Array<() => void>;\r\n })\r\n | undefined;\r\n if (!ctx) throw new Error(\"nixEffect() called outside component\");\r\n\r\n if (typeof effect !== \"function\") {\r\n console.error(\"[nixEffect] First argument must be a function\");\r\n return;\r\n }\r\n if (!Array.isArray(deps)) {\r\n console.error(\"[nixEffect] Second argument must be an array\");\r\n deps = [];\r\n }\r\n const MAX_DEPS = 100;\r\n if (deps.length > MAX_DEPS) {\r\n console.warn(\r\n `[nixEffect] Dependency array too large (${deps.length}). Limited to ${MAX_DEPS}.`\r\n );\r\n deps = deps.slice(0, MAX_DEPS);\r\n }\r\n const idx = ctx.hookIndex++;\r\n const prev = ctx.hooks[idx];\r\n const hasChanged = !prev || !shallowArrayEqual(prev.deps, deps);\r\n if (hasChanged) {\r\n if (prev?.cleanup) {\r\n try {\r\n if (typeof prev.cleanup === \"function\") {\r\n prev.cleanup();\r\n }\r\n } catch (err) {\r\n console.error(\"[nixEffect] Cleanup error:\", err);\r\n }\r\n }\r\n let cleanup: void | (() => void);\r\n try {\r\n cleanup = effect();\r\n if (cleanup !== undefined && typeof cleanup !== \"function\") {\r\n console.warn(\r\n \"[nixEffect] Effect should return undefined or a cleanup function\"\r\n );\r\n cleanup = undefined;\r\n }\r\n } catch (err) {\r\n console.error(\"[nixEffect] Effect error:\", err);\r\n cleanup = undefined;\r\n }\r\n ctx.hooks[idx] = { deps, cleanup };\r\n if (cleanup && typeof cleanup === \"function\") {\r\n if (!ctx.cleanups) ctx.cleanups = [];\r\n ctx.cleanups.push(cleanup);\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Shallow comparison of two arrays for dependency checking.\r\n * More reliable and secure than JSON.stringify.\r\n *\r\n * @param {Array<any>} arr1 - First array\r\n * @param {Array<any>} arr2 - Second array\r\n * @returns {boolean} True if arrays are shallowly equal\r\n */\r\nfunction shallowArrayEqual(arr1: any[], arr2: any[]): boolean {\r\n if (arr1.length !== arr2.length) return false;\r\n for (let i = 0; i < arr1.length; i++) {\r\n if (!Object.is(arr1[i], arr2[i])) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n}\r\n\r\n/**\r\n * Run an effect only once on component mount.\r\n * Convenience wrapper around nixEffect.\r\n *\r\n * @param {() => (void | (() => void))} effect - Effect function\r\n *\r\n * @example\r\n * nixEffectOnce(() => {\r\n * console.log('Mounted');\r\n * return () => console.log('Unmounted');\r\n * });\r\n */\r\nexport function nixEffectOnce(effect: () => void | (() => void)): void {\r\n return nixEffect(effect, []);\r\n}\r\n\r\n/**\r\n * Run an effect every time the component renders.\r\n * Use with caution - can cause performance issues.\r\n *\r\n * @param {() => (void | (() => void))} effect - Effect function\r\n *\r\n * @example\r\n * nixEffectAlways(() => {\r\n * console.log('Component rendered');\r\n * });\r\n */\r\nexport function nixEffectAlways(effect: () => void | (() => void)): void {\r\n const ctx = activeContext as\r\n | (typeof activeContext & { version: any })\r\n | undefined;\r\n if (!ctx) throw new Error(\"nixEffectAlways() called outside component\");\r\n if (typeof effect !== \"function\") {\r\n console.error(\"[nixEffectAlways] Argument must be a function\");\r\n return;\r\n }\r\n return nixEffect(effect, [ctx.version]);\r\n}\r\n"],
|
|
5
|
+
"mappings": ";;AA0BA,SAAS,qBAAqB;AAwCvB,SAAS,UACd,QACA,OAAc,CAAC,GACT;AACN,QAAM,MAAM;AAOZ,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,sCAAsC;AAEhE,MAAI,OAAO,WAAW,YAAY;AAChC,YAAQ,MAAM,+CAA+C;AAC7D;AAAA,EACF;AACA,MAAI,CAAC,MAAM,QAAQ,IAAI,GAAG;AACxB,YAAQ,MAAM,8CAA8C;AAC5D,WAAO,CAAC;AAAA,EACV;AACA,QAAM,WAAW;AACjB,MAAI,KAAK,SAAS,UAAU;AAC1B,YAAQ;AAAA,MACN,2CAA2C,KAAK,MAAM,iBAAiB,QAAQ;AAAA,IACjF;AACA,WAAO,KAAK,MAAM,GAAG,QAAQ;AAAA,EAC/B;AACA,QAAM,MAAM,IAAI;AAChB,QAAM,OAAO,IAAI,MAAM,GAAG;AAC1B,QAAM,aAAa,CAAC,QAAQ,CAAC,kBAAkB,KAAK,MAAM,IAAI;AAC9D,MAAI,YAAY;AACd,QAAI,MAAM,SAAS;AACjB,UAAI;AACF,YAAI,OAAO,KAAK,YAAY,YAAY;AACtC,eAAK,QAAQ;AAAA,QACf;AAAA,MACF,SAAS,KAAK;AACZ,gBAAQ,MAAM,8BAA8B,GAAG;AAAA,MACjD;AAAA,IACF;AACA,QAAI;AACJ,QAAI;AACF,gBAAU,OAAO;AACjB,UAAI,YAAY,UAAa,OAAO,YAAY,YAAY;AAC1D,gBAAQ;AAAA,UACN;AAAA,QACF;AACA,kBAAU;AAAA,MACZ;AAAA,IACF,SAAS,KAAK;AACZ,cAAQ,MAAM,6BAA6B,GAAG;AAC9C,gBAAU;AAAA,IACZ;AACA,QAAI,MAAM,GAAG,IAAI,EAAE,MAAM,QAAQ;AACjC,QAAI,WAAW,OAAO,YAAY,YAAY;AAC5C,UAAI,CAAC,IAAI,SAAU,KAAI,WAAW,CAAC;AACnC,UAAI,SAAS,KAAK,OAAO;AAAA,IAC3B;AAAA,EACF;AACF;AA5DgB;AAsEhB,SAAS,kBAAkB,MAAa,MAAsB;AAC5D,MAAI,KAAK,WAAW,KAAK,OAAQ,QAAO;AACxC,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,QAAI,CAAC,OAAO,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG;AAChC,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AARS;AAsBF,SAAS,cAAc,QAAyC;AACrE,SAAO,UAAU,QAAQ,CAAC,CAAC;AAC7B;AAFgB;AAeT,SAAS,gBAAgB,QAAyC;AACvE,QAAM,MAAM;AAGZ,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,4CAA4C;AACtE,MAAI,OAAO,WAAW,YAAY;AAChC,YAAQ,MAAM,+CAA+C;AAC7D;AAAA,EACF;AACA,SAAO,UAAU,QAAQ,CAAC,IAAI,OAAO,CAAC;AACxC;AAVgB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { nixComputed } from "./nixComputed.js";
|
|
2
|
+
import { nixState } from "./nixState.js";
|
|
3
|
+
type ValidationRule<T> = {
|
|
4
|
+
required?: boolean;
|
|
5
|
+
minLength?: number;
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
pattern?: RegExp;
|
|
8
|
+
custom?: (value: any, values: T) => boolean;
|
|
9
|
+
message?: string;
|
|
10
|
+
};
|
|
11
|
+
type ValidationRules<T> = {
|
|
12
|
+
[K in keyof T]?: ValidationRule<T>;
|
|
13
|
+
};
|
|
14
|
+
type FormState<T> = {
|
|
15
|
+
values: ReturnType<typeof nixState<T>>;
|
|
16
|
+
errors: ReturnType<typeof nixState<Partial<Record<keyof T, string>>>>;
|
|
17
|
+
touched: ReturnType<typeof nixState<Partial<Record<keyof T, boolean>>>>;
|
|
18
|
+
isSubmitting: ReturnType<typeof nixState<boolean>>;
|
|
19
|
+
isValid: ReturnType<typeof nixComputed<boolean>>;
|
|
20
|
+
handleChange: (fieldName: keyof T, value: any) => void;
|
|
21
|
+
handleBlur: (fieldName: keyof T) => void;
|
|
22
|
+
handleSubmit: (onSubmit: (values: T, signal: AbortSignal) => Promise<void>) => Promise<void>;
|
|
23
|
+
cancelSubmit: () => void;
|
|
24
|
+
reset: () => void;
|
|
25
|
+
getFieldProps: (fieldName: keyof T) => {
|
|
26
|
+
value: any;
|
|
27
|
+
"r-input": (e: any) => void;
|
|
28
|
+
"r-blur": () => void;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export declare function nixForm<T extends Record<string, any> = Record<string, any>>(initialValues?: T, validationRules?: ValidationRules<T>): FormState<T>;
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=nixForm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nixForm.d.ts","sourceRoot":"","sources":["../../hooks/nixForm.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,KAAK,cAAc,CAAC,CAAC,IAAI;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC;IAC5C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,KAAK,eAAe,CAAC,CAAC,IAAI;KACvB,CAAC,IAAI,MAA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,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzE,aAAa,GAAE,CAAW,EAC1B,eAAe,GAAE,eAAe,CAAC,CAAC,CAA4B,GAC7D,SAAS,CAAC,CAAC,CAAC,CA2Id"}
|
package/dist/hooks/nixForm.js
CHANGED
|
@@ -1,133 +1,123 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
import { nixState } from "./nixState.js";
|
|
4
1
|
import { nixComputed } from "./nixComputed.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
2
|
+
import { nixState } from "./nixState.js";
|
|
3
|
+
export function nixForm(initialValues = {}, validationRules = {}) {
|
|
4
|
+
const values = nixState({ ...initialValues });
|
|
5
|
+
const errors = nixState({});
|
|
6
|
+
const touched = nixState({});
|
|
7
|
+
const isSubmitting = nixState(false);
|
|
8
|
+
const isValid = nixComputed(() => Object.keys(errors.value).length === 0);
|
|
9
|
+
let abortController = null;
|
|
10
|
+
function validate(fieldName, value) {
|
|
11
|
+
const rules = validationRules[fieldName];
|
|
12
|
+
if (!rules)
|
|
13
|
+
return null;
|
|
14
|
+
if (rules.required && !value) {
|
|
15
|
+
return rules.message || `${String(fieldName)} is required`;
|
|
16
|
+
}
|
|
17
|
+
if (rules.minLength && value.length < rules.minLength) {
|
|
18
|
+
return (rules.message ||
|
|
19
|
+
`${String(fieldName)} must be at least ${rules.minLength} characters`);
|
|
20
|
+
}
|
|
21
|
+
if (rules.maxLength && value.length > rules.maxLength) {
|
|
22
|
+
return (rules.message ||
|
|
23
|
+
`${String(fieldName)} must be at most ${rules.maxLength} characters`);
|
|
24
|
+
}
|
|
25
|
+
if (rules.pattern && !rules.pattern.test(value)) {
|
|
26
|
+
return rules.message || `${String(fieldName)} is invalid`;
|
|
27
|
+
}
|
|
28
|
+
if (rules.custom && !rules.custom(value, values.value)) {
|
|
29
|
+
return rules.message || `${String(fieldName)} is invalid`;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
24
32
|
}
|
|
25
|
-
|
|
26
|
-
|
|
33
|
+
function handleChange(fieldName, value) {
|
|
34
|
+
values.value = { ...values.value, [fieldName]: value };
|
|
35
|
+
if (touched.value[fieldName]) {
|
|
36
|
+
const error = validate(fieldName, value);
|
|
37
|
+
if (error) {
|
|
38
|
+
errors.value = { ...errors.value, [fieldName]: error };
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const newErrors = { ...errors.value };
|
|
42
|
+
delete newErrors[fieldName];
|
|
43
|
+
errors.value = newErrors;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
27
46
|
}
|
|
28
|
-
|
|
29
|
-
|
|
47
|
+
function handleBlur(fieldName) {
|
|
48
|
+
touched.value = { ...touched.value, [fieldName]: true };
|
|
49
|
+
const error = validate(fieldName, values.value[fieldName]);
|
|
50
|
+
if (error) {
|
|
51
|
+
errors.value = { ...errors.value, [fieldName]: error };
|
|
52
|
+
}
|
|
30
53
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (error) {
|
|
39
|
-
errors.value = { ...errors.value, [fieldName]: error };
|
|
40
|
-
} else {
|
|
41
|
-
const newErrors = { ...errors.value };
|
|
42
|
-
delete newErrors[fieldName];
|
|
54
|
+
function validateAll() {
|
|
55
|
+
const newErrors = {};
|
|
56
|
+
Object.keys(validationRules).forEach((fieldName) => {
|
|
57
|
+
const error = validate(fieldName, values.value[fieldName]);
|
|
58
|
+
if (error)
|
|
59
|
+
newErrors[fieldName] = error;
|
|
60
|
+
});
|
|
43
61
|
errors.value = newErrors;
|
|
44
|
-
|
|
62
|
+
return Object.keys(newErrors).length === 0;
|
|
63
|
+
}
|
|
64
|
+
async function handleSubmit(onSubmit) {
|
|
65
|
+
const allTouched = Object.keys(validationRules).reduce((acc, key) => {
|
|
66
|
+
acc[key] = true;
|
|
67
|
+
return acc;
|
|
68
|
+
}, {});
|
|
69
|
+
touched.value = allTouched;
|
|
70
|
+
if (!validateAll())
|
|
71
|
+
return;
|
|
72
|
+
if (abortController)
|
|
73
|
+
abortController.abort();
|
|
74
|
+
abortController = new AbortController();
|
|
75
|
+
const signal = abortController.signal;
|
|
76
|
+
isSubmitting.value = true;
|
|
77
|
+
try {
|
|
78
|
+
await onSubmit(values.value, signal);
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
if (err.name !== "AbortError") {
|
|
82
|
+
console.error("[nixForm] Submission error:", err);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
if (!signal.aborted)
|
|
87
|
+
isSubmitting.value = false;
|
|
88
|
+
}
|
|
45
89
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (error) {
|
|
52
|
-
errors.value = { ...errors.value, [fieldName]: error };
|
|
90
|
+
function cancelSubmit() {
|
|
91
|
+
if (abortController) {
|
|
92
|
+
abortController.abort();
|
|
93
|
+
abortController = null;
|
|
94
|
+
}
|
|
53
95
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
Object.keys(validationRules).forEach((fieldName) => {
|
|
59
|
-
const error = validate(fieldName, values.value[fieldName]);
|
|
60
|
-
if (error)
|
|
61
|
-
newErrors[fieldName] = error;
|
|
62
|
-
});
|
|
63
|
-
errors.value = newErrors;
|
|
64
|
-
return Object.keys(newErrors).length === 0;
|
|
65
|
-
}
|
|
66
|
-
__name(validateAll, "validateAll");
|
|
67
|
-
async function handleSubmit(onSubmit) {
|
|
68
|
-
const allTouched = Object.keys(validationRules).reduce((acc, key) => {
|
|
69
|
-
acc[key] = true;
|
|
70
|
-
return acc;
|
|
71
|
-
}, {});
|
|
72
|
-
touched.value = allTouched;
|
|
73
|
-
if (!validateAll())
|
|
74
|
-
return;
|
|
75
|
-
if (abortController)
|
|
76
|
-
abortController.abort();
|
|
77
|
-
abortController = new AbortController();
|
|
78
|
-
const signal = abortController.signal;
|
|
79
|
-
isSubmitting.value = true;
|
|
80
|
-
try {
|
|
81
|
-
await onSubmit(values.value, signal);
|
|
82
|
-
} catch (err) {
|
|
83
|
-
if (err.name !== "AbortError") {
|
|
84
|
-
console.error("[nixForm] Submission error:", err);
|
|
85
|
-
}
|
|
86
|
-
} finally {
|
|
87
|
-
if (!signal.aborted)
|
|
96
|
+
function reset() {
|
|
97
|
+
values.value = { ...initialValues };
|
|
98
|
+
errors.value = {};
|
|
99
|
+
touched.value = {};
|
|
88
100
|
isSubmitting.value = false;
|
|
101
|
+
cancelSubmit();
|
|
89
102
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
103
|
+
function getFieldProps(fieldName) {
|
|
104
|
+
return {
|
|
105
|
+
value: values.value[fieldName] || "",
|
|
106
|
+
"r-input": (e) => handleChange(fieldName, e.target.value),
|
|
107
|
+
"r-blur": () => handleBlur(fieldName),
|
|
108
|
+
};
|
|
96
109
|
}
|
|
97
|
-
}
|
|
98
|
-
__name(cancelSubmit, "cancelSubmit");
|
|
99
|
-
function reset() {
|
|
100
|
-
values.value = { ...initialValues };
|
|
101
|
-
errors.value = {};
|
|
102
|
-
touched.value = {};
|
|
103
|
-
isSubmitting.value = false;
|
|
104
|
-
cancelSubmit();
|
|
105
|
-
}
|
|
106
|
-
__name(reset, "reset");
|
|
107
|
-
function getFieldProps(fieldName) {
|
|
108
110
|
return {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
values,
|
|
112
|
+
errors,
|
|
113
|
+
touched,
|
|
114
|
+
isSubmitting,
|
|
115
|
+
isValid,
|
|
116
|
+
handleChange,
|
|
117
|
+
handleBlur,
|
|
118
|
+
handleSubmit,
|
|
119
|
+
cancelSubmit,
|
|
120
|
+
reset,
|
|
121
|
+
getFieldProps,
|
|
112
122
|
};
|
|
113
|
-
}
|
|
114
|
-
__name(getFieldProps, "getFieldProps");
|
|
115
|
-
return {
|
|
116
|
-
values,
|
|
117
|
-
errors,
|
|
118
|
-
touched,
|
|
119
|
-
isSubmitting,
|
|
120
|
-
isValid,
|
|
121
|
-
handleChange,
|
|
122
|
-
handleBlur,
|
|
123
|
-
handleSubmit,
|
|
124
|
-
cancelSubmit,
|
|
125
|
-
reset,
|
|
126
|
-
getFieldProps
|
|
127
|
-
};
|
|
128
123
|
}
|
|
129
|
-
__name(nixForm, "nixForm");
|
|
130
|
-
export {
|
|
131
|
-
nixForm
|
|
132
|
-
};
|
|
133
|
-
//# sourceMappingURL=nixForm.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../hooks/nixForm.
|
|
4
|
-
"sourcesContent": ["// core/hooks/nixForm.js - Form handling utilities with memory-safe async support\r\nimport {
|
|
5
|
-
"mappings": ";;
|
|
3
|
+
"sources": ["../../hooks/nixForm.ts"],
|
|
4
|
+
"sourcesContent": ["/* MIT License\r\n\r\n* Copyright (c) 2026 Resty Gonzales\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in all\r\ncopies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\n* SOFTWARE.\r\n */\r\n// core/hooks/nixForm.js - Form handling utilities with memory-safe async support\r\nimport { nixComputed } from \"./nixComputed.js\";\r\nimport { nixState } from \"./nixState.js\";\r\n\r\ntype ValidationRule<T> = {\r\n required?: boolean;\r\n minLength?: number;\r\n maxLength?: number;\r\n pattern?: RegExp;\r\n custom?: (value: any, values: T) => boolean;\r\n message?: string;\r\n};\r\n\r\ntype ValidationRules<T> = {\r\n [K in keyof T]?: ValidationRule<T>;\r\n};\r\n\r\ntype FormState<T> = {\r\n values: ReturnType<typeof nixState<T>>;\r\n errors: ReturnType<typeof nixState<Partial<Record<keyof T, string>>>>;\r\n touched: ReturnType<typeof nixState<Partial<Record<keyof T, boolean>>>>;\r\n isSubmitting: ReturnType<typeof nixState<boolean>>;\r\n isValid: ReturnType<typeof nixComputed<boolean>>;\r\n handleChange: (fieldName: keyof T, value: any) => void;\r\n handleBlur: (fieldName: keyof T) => void;\r\n handleSubmit: (\r\n onSubmit: (values: T, signal: AbortSignal) => Promise<void>\r\n ) => Promise<void>;\r\n cancelSubmit: () => void;\r\n reset: () => void;\r\n getFieldProps: (fieldName: keyof T) => {\r\n value: any;\r\n \"r-input\": (e: any) => void;\r\n \"r-blur\": () => void;\r\n };\r\n};\r\n\r\n/**\r\n * Reactive form handler with validation, reactive state, and safe async submit.\r\n *\r\n * @param {Object} [initialValues={}] - Initial values for form fields.\r\n * @param {Object} [validationRules={}] - Validation rules for fields.\r\n * @returns {Object} Form utilities: values, errors, touched, isSubmitting, isValid,\r\n * handleChange, handleBlur, handleSubmit, reset, getFieldProps, cancelSubmit\r\n *\r\n * @example\r\n * const form = nixForm({ email: \"\" }, {\r\n * email: { required: true, pattern: /^\\S+@\\S+$/, message: \"Invalid email\" }\r\n * });\r\n * form.handleSubmit(async (values) => { await api.submit(values); });\r\n */\r\nexport function nixForm<T extends Record<string, any> = Record<string, any>>(\r\n initialValues: T = {} as T,\r\n validationRules: ValidationRules<T> = {} as ValidationRules<T>\r\n): FormState<T> {\r\n const values = nixState<T>({ ...initialValues });\r\n const errors = nixState<Partial<Record<keyof T, string>>>({});\r\n const touched = nixState<Partial<Record<keyof T, boolean>>>({});\r\n const isSubmitting = nixState<boolean>(false);\r\n\r\n const isValid = nixComputed<boolean>(\r\n () => Object.keys(errors.value).length === 0\r\n );\r\n\r\n let abortController: AbortController | null = null;\r\n\r\n function validate(fieldName: keyof T, value: any): string | null {\r\n const rules = validationRules[fieldName];\r\n if (!rules) return null;\r\n\r\n if (rules.required && !value) {\r\n return rules.message || `${String(fieldName)} is required`;\r\n }\r\n if (rules.minLength && value.length < rules.minLength) {\r\n return (\r\n rules.message ||\r\n `${String(fieldName)} must be at least ${rules.minLength} characters`\r\n );\r\n }\r\n if (rules.maxLength && value.length > rules.maxLength) {\r\n return (\r\n rules.message ||\r\n `${String(fieldName)} must be at most ${rules.maxLength} characters`\r\n );\r\n }\r\n if (rules.pattern && !rules.pattern.test(value)) {\r\n return rules.message || `${String(fieldName)} is invalid`;\r\n }\r\n if (rules.custom && !rules.custom(value, values.value)) {\r\n return rules.message || `${String(fieldName)} is invalid`;\r\n }\r\n return null;\r\n }\r\n\r\n function handleChange(fieldName: keyof T, value: any): void {\r\n values.value = { ...values.value, [fieldName]: value };\r\n\r\n if (touched.value[fieldName]) {\r\n const error = validate(fieldName, value);\r\n if (error) {\r\n errors.value = { ...errors.value, [fieldName]: error };\r\n } else {\r\n const newErrors = { ...errors.value };\r\n delete newErrors[fieldName];\r\n errors.value = newErrors;\r\n }\r\n }\r\n }\r\n\r\n function handleBlur(fieldName: keyof T): void {\r\n touched.value = { ...touched.value, [fieldName]: true };\r\n const error = validate(fieldName, values.value[fieldName]);\r\n if (error) {\r\n errors.value = { ...errors.value, [fieldName]: error };\r\n }\r\n }\r\n\r\n function validateAll(): boolean {\r\n const newErrors: Partial<Record<keyof T, string>> = {};\r\n (Object.keys(validationRules) as Array<keyof T>).forEach((fieldName) => {\r\n const error = validate(fieldName, values.value[fieldName]);\r\n if (error) newErrors[fieldName] = error;\r\n });\r\n errors.value = newErrors;\r\n return Object.keys(newErrors).length === 0;\r\n }\r\n\r\n async function handleSubmit(\r\n onSubmit: (values: T, signal: AbortSignal) => Promise<void>\r\n ): Promise<void> {\r\n const allTouched = (Object.keys(validationRules) as Array<keyof T>).reduce(\r\n (acc, key) => {\r\n acc[key] = true;\r\n return acc;\r\n },\r\n {} as Partial<Record<keyof T, boolean>>\r\n );\r\n touched.value = allTouched;\r\n\r\n if (!validateAll()) return;\r\n\r\n if (abortController) abortController.abort();\r\n abortController = new AbortController();\r\n const signal = abortController.signal;\r\n\r\n isSubmitting.value = true;\r\n try {\r\n await onSubmit(values.value, signal);\r\n } catch (err: any) {\r\n if (err.name !== \"AbortError\") {\r\n console.error(\"[nixForm] Submission error:\", err);\r\n }\r\n } finally {\r\n if (!signal.aborted) isSubmitting.value = false;\r\n }\r\n }\r\n\r\n function cancelSubmit(): void {\r\n if (abortController) {\r\n abortController.abort();\r\n abortController = null;\r\n }\r\n }\r\n\r\n function reset(): void {\r\n values.value = { ...initialValues };\r\n errors.value = {};\r\n touched.value = {};\r\n isSubmitting.value = false;\r\n cancelSubmit();\r\n }\r\n\r\n function getFieldProps(fieldName: keyof T) {\r\n return {\r\n value: values.value[fieldName] || \"\",\r\n \"r-input\": (e: any) => handleChange(fieldName, e.target.value),\r\n \"r-blur\": () => handleBlur(fieldName),\r\n };\r\n }\r\n\r\n return {\r\n values,\r\n errors,\r\n tou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IAAI,gBAAgB;AACtC,UAAM,SAAS,gBAAgB;AAE/B,iBAAa,QAAQ;AACrB,QAAI;AACF,YAAM,SAAS,OAAO,OAAO,MAAM;AAAA,IACrC,SAAS,KAAU;AACjB,UAAI,IAAI,SAAS,cAAc;AAC7B,gBAAQ,MAAM,+BAA+B,GAAG;AAAA,MAClD;AAAA,IACF,UAAE;AACA,UAAI,CAAC,OAAO,QAAS,cAAa,QAAQ;AAAA,IAC5C;AAAA,EACF;AA5Be;AA8Bf,WAAS,eAAqB;AAC5B,QAAI,iBAAiB;AACnB,sBAAgB,MAAM;AACtB,wBAAkB;AAAA,IACpB;AAAA,EACF;AALS;AAOT,WAAS,QAAc;AACrB,WAAO,QAAQ,EAAE,GAAG,cAAc;AAClC,WAAO,QAAQ,CAAC;AAChB,YAAQ,QAAQ,CAAC;AACjB,iBAAa,QAAQ;AACrB,iBAAa;AAAA,EACf;AANS;AAQT,WAAS,cAAc,WAAoB;AACzC,WAAO;AAAA,MACL,OAAO,OAAO,MAAM,SAAS,KAAK;AAAA,MAClC,WAAW,wBAAC,MAAW,aAAa,WAAW,EAAE,OAAO,KAAK,GAAlD;AAAA,MACX,UAAU,6BAAM,WAAW,SAAS,GAA1B;AAAA,IACZ;AAAA,EACF;AANS;AAQT,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AA9IgB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { nixComputed } from "./nixComputed.js";
|
|
2
|
+
import { nixState } from "./nixState.js";
|
|
3
|
+
type ValidationRule<T> = {
|
|
4
|
+
required?: boolean;
|
|
5
|
+
minLength?: number;
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
pattern?: RegExp;
|
|
8
|
+
custom?: (value: any, values: T) => boolean;
|
|
9
|
+
message?: string;
|
|
10
|
+
};
|
|
11
|
+
type ValidationRules<T> = {
|
|
12
|
+
[K in keyof T]?: ValidationRule<T>;
|
|
13
|
+
};
|
|
14
|
+
type FormAsyncState<T, D = any, E = any> = {
|
|
15
|
+
values: ReturnType<typeof nixState<T>>;
|
|
16
|
+
errors: ReturnType<typeof nixState<Partial<Record<keyof T, string>>>>;
|
|
17
|
+
touched: ReturnType<typeof nixState<Partial<Record<keyof T, boolean>>>>;
|
|
18
|
+
isSubmitting: ReturnType<typeof nixState<boolean>>;
|
|
19
|
+
isValid: ReturnType<typeof nixComputed<boolean>>;
|
|
20
|
+
data: ReturnType<typeof nixState<D | null>>;
|
|
21
|
+
error: ReturnType<typeof nixState<E | null>>;
|
|
22
|
+
loading: ReturnType<typeof nixState<boolean>>;
|
|
23
|
+
handleChange: (fieldName: keyof T, value: any) => void;
|
|
24
|
+
handleBlur: (fieldName: keyof T) => void;
|
|
25
|
+
handleSubmit: (onSubmit: (values: T, signal: AbortSignal) => Promise<D>) => void;
|
|
26
|
+
cancelSubmit: () => void;
|
|
27
|
+
reset: () => void;
|
|
28
|
+
getFieldProps: (fieldName: keyof T) => {
|
|
29
|
+
value: any;
|
|
30
|
+
"r-input": (e: any) => void;
|
|
31
|
+
"r-blur": () => void;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export declare function nixFormAsync<T extends Record<string, any> = Record<string, any>, D = any, E = any>(initialValues?: T, validationRules?: ValidationRules<T>, options?: {
|
|
35
|
+
delay?: number;
|
|
36
|
+
leading?: boolean;
|
|
37
|
+
trailing?: boolean;
|
|
38
|
+
maxWait?: number;
|
|
39
|
+
cache?: boolean;
|
|
40
|
+
}): FormAsyncState<T, D, E>;
|
|
41
|
+
export {};
|
|
42
|
+
//# sourceMappingURL=nixFormAsync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nixFormAsync.d.ts","sourceRoot":"","sources":["../../hooks/nixFormAsync.ts"],"names":[],"mappings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