solid-hook-form 1.3.4 → 1.3.6
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/form_context.d.ts +1 -1
- package/dist/main.js +31 -24
- package/dist/main.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/form_context.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const FormContext: import('solid-js').Context<
|
|
1
|
+
export declare const FormContext: import('solid-js').Context<unknown>;
|
package/dist/main.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { createSignal as p, createContext as q,
|
|
2
|
-
const
|
|
1
|
+
import { createSignal as p, createContext as q, createComponent as M, useContext as S } from "solid-js";
|
|
2
|
+
const H = (e) => {
|
|
3
3
|
const n = e.target;
|
|
4
4
|
return n instanceof HTMLSelectElement ? n.value : n instanceof HTMLInputElement && n.type === "checkbox" ? n.checked : n.value;
|
|
5
5
|
}, y = (e, n) => {
|
|
@@ -12,20 +12,20 @@ const S = (e) => {
|
|
|
12
12
|
return;
|
|
13
13
|
}
|
|
14
14
|
e.value = n;
|
|
15
|
-
},
|
|
15
|
+
}, m = (e) => e instanceof RegExp || typeof e == "string" || typeof e == "number" ? e : e.value, g = (e) => typeof e == "string" ? e : typeof e.message == "string" ? e.message : "", N = (e, n, r = {}) => {
|
|
16
16
|
const i = e[n];
|
|
17
17
|
if (r.required && !i)
|
|
18
|
-
return { type: "required", message:
|
|
19
|
-
if (r.min && Number(i) < Number(
|
|
20
|
-
return { type: "min", message:
|
|
21
|
-
if (r.max && Number(i) > Number(
|
|
22
|
-
return { type: "max", message:
|
|
23
|
-
if (r.minLength && i.length <
|
|
24
|
-
return { type: "minLength", message:
|
|
25
|
-
if (r.maxLength && i.length >
|
|
26
|
-
return { type: "maxLength", message:
|
|
27
|
-
if (r.pattern && !
|
|
28
|
-
return { type: "pattern", message:
|
|
18
|
+
return { type: "required", message: g(r.required) };
|
|
19
|
+
if (r.min && Number(i) < Number(m(r.min)))
|
|
20
|
+
return { type: "min", message: g(r.min) };
|
|
21
|
+
if (r.max && Number(i) > Number(m(r.max)))
|
|
22
|
+
return { type: "max", message: g(r.max) };
|
|
23
|
+
if (r.minLength && i.length < m(r.minLength))
|
|
24
|
+
return { type: "minLength", message: g(r.minLength) };
|
|
25
|
+
if (r.maxLength && i.length > m(r.maxLength))
|
|
26
|
+
return { type: "maxLength", message: g(r.maxLength) };
|
|
27
|
+
if (r.pattern && !m(r.pattern).test(i))
|
|
28
|
+
return { type: "pattern", message: g(r.pattern) };
|
|
29
29
|
if (r.validate) {
|
|
30
30
|
const a = r.validate(i, e);
|
|
31
31
|
if (a === !1)
|
|
@@ -33,7 +33,7 @@ const S = (e) => {
|
|
|
33
33
|
if (typeof a == "string")
|
|
34
34
|
return { type: "validate", message: a };
|
|
35
35
|
}
|
|
36
|
-
},
|
|
36
|
+
}, T = () => {
|
|
37
37
|
const [e, n] = p({});
|
|
38
38
|
return {
|
|
39
39
|
errors: e,
|
|
@@ -50,24 +50,24 @@ const S = (e) => {
|
|
|
50
50
|
n({});
|
|
51
51
|
}
|
|
52
52
|
};
|
|
53
|
-
},
|
|
53
|
+
}, O = ({
|
|
54
54
|
defaultValues: e,
|
|
55
55
|
mode: n = "onInput"
|
|
56
56
|
}) => {
|
|
57
|
-
const r = {}, i = {}, [a, c] = p(e), { errors: f, appendError: u, removeError: F, resetErrors: b } =
|
|
57
|
+
const r = {}, i = {}, [a, c] = p(e), { errors: f, appendError: u, removeError: F, resetErrors: b } = T(), [h, l] = p(!0), C = (t, o) => {
|
|
58
58
|
const s = r[t];
|
|
59
59
|
s && (o.ref = s), u(t, o);
|
|
60
60
|
}, V = (t) => {
|
|
61
61
|
F(t);
|
|
62
62
|
}, v = (t) => {
|
|
63
|
-
const o = i[t], s =
|
|
63
|
+
const o = i[t], s = N(a(), t, o);
|
|
64
64
|
s ? C(t, s) : V(t), l(!Object.keys(f()).length);
|
|
65
65
|
}, I = () => {
|
|
66
66
|
Object.keys(a()).forEach((t) => {
|
|
67
67
|
v(t);
|
|
68
68
|
});
|
|
69
69
|
}, x = (t, o) => {
|
|
70
|
-
const s =
|
|
70
|
+
const s = H(t);
|
|
71
71
|
c((d) => ({ ...d, [o]: s })), v(o);
|
|
72
72
|
}, k = (t, o = {}) => (i[t] = {
|
|
73
73
|
required: o.required,
|
|
@@ -111,14 +111,21 @@ const S = (e) => {
|
|
|
111
111
|
})), b(), l(!0);
|
|
112
112
|
}
|
|
113
113
|
};
|
|
114
|
-
}, L = q(
|
|
115
|
-
|
|
114
|
+
}, L = q(), A = (e) => M(L.Provider, {
|
|
115
|
+
get value() {
|
|
116
|
+
return e.form;
|
|
117
|
+
},
|
|
118
|
+
get children() {
|
|
119
|
+
return e.children;
|
|
120
|
+
}
|
|
121
|
+
}), D = () => {
|
|
122
|
+
const e = S(L);
|
|
116
123
|
if (!e)
|
|
117
124
|
throw new Error("useFormContext: cannot find a FormProvider");
|
|
118
125
|
return e;
|
|
119
126
|
};
|
|
120
127
|
export {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
128
|
+
A as FormProvider,
|
|
129
|
+
O as useForm,
|
|
130
|
+
D as useFormContext
|
|
124
131
|
};
|
package/dist/main.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(c
|
|
1
|
+
(function(f,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("solid-js")):typeof define=="function"&&define.amd?define(["exports","solid-js"],c):(f=typeof globalThis<"u"?globalThis:f||self,c(f["solid-hook-form"]={},f.solid))})(this,function(f,c){"use strict";const b=e=>{const n=e.target;return n instanceof HTMLSelectElement?n.value:n instanceof HTMLInputElement&&n.type==="checkbox"?n.checked:n.value},l=(e,n)=>{if(e instanceof HTMLSelectElement){e.value=n;return}if(e instanceof HTMLInputElement&&e.type==="checkbox"){e.checked=n;return}e.value=n},p=e=>e instanceof RegExp||typeof e=="string"||typeof e=="number"?e:e.value,m=e=>typeof e=="string"?e:typeof e.message=="string"?e.message:"",C=(e,n,r={})=>{const s=e[n];if(r.required&&!s)return{type:"required",message:m(r.required)};if(r.min&&Number(s)<Number(p(r.min)))return{type:"min",message:m(r.min)};if(r.max&&Number(s)>Number(p(r.max)))return{type:"max",message:m(r.max)};if(r.minLength&&s.length<p(r.minLength))return{type:"minLength",message:m(r.minLength)};if(r.maxLength&&s.length>p(r.maxLength))return{type:"maxLength",message:m(r.maxLength)};if(r.pattern&&!p(r.pattern).test(s))return{type:"pattern",message:m(r.pattern)};if(r.validate){const a=r.validate(s,e);if(a===!1)return{type:"validate"};if(typeof a=="string")return{type:"validate",message:a}}},S=()=>{const[e,n]=c.createSignal({});return{errors:e,appendError:(u,g)=>{n(d=>({...d,[u]:g}))},removeError:u=>{n(g=>{const d={...g};return delete d[u],d})},resetErrors:()=>{n({})}}},V=({defaultValues:e,mode:n="onInput"})=>{const r={},s={},[a,u]=c.createSignal(e),{errors:g,appendError:d,removeError:T,resetErrors:j}=S(),[x,y]=c.createSignal(!0),q=(t,o)=>{const i=r[t];i&&(o.ref=i),d(t,o)},M=t=>{T(t)},E=t=>{const o=s[t],i=C(a(),t,o);i?q(t,i):M(t),y(!Object.keys(g()).length)},P=()=>{Object.keys(a()).forEach(t=>{E(t)})},F=(t,o)=>{const i=b(t);u(h=>({...h,[o]:i})),E(o)},H=(t,o={})=>(s[t]={required:o.required,min:o.min,max:o.max,minLength:o.minLength,maxLength:o.maxLength,pattern:o.pattern,validate:o.validate},{name:t,onInput(i){n==="onInput"&&F(i,t)},onChange(i){(n==="onChange"||n==="onInput")&&F(i,t)},ref(i){r[t]||(r[t]=i,i&&l(i,a()[t]))}}),L=t=>t?a()[t]:a();return{values:a,errors:g,isValid:x,register:H,getValues:L,setValue:(t,o)=>{u(h=>({...h,[t]:o}));const i=r[t];i&&l(i,o)},onSubmit:t=>o=>{o.preventDefault(),P(),x()&&t(L())},reset:(t={})=>{u(()=>({...e,...t})),j(),y(!0)}}},v=c.createContext(),k=e=>c.createComponent(v.Provider,{get value(){return e.form},get children(){return e.children}}),I=()=>{const e=c.useContext(v);if(!e)throw new Error("useFormContext: cannot find a FormProvider");return e};f.FormProvider=k,f.useForm=V,f.useFormContext=I,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
|