solid-hook-form 1.2.1 → 1.3.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.
- package/dist/logic/get_value.d.ts +1 -3
- package/dist/logic/set_value.d.ts +1 -3
- package/dist/main.js +170 -174
- package/dist/main.umd.cjs +1 -1
- package/dist/types/form.d.ts +3 -4
- package/package.json +1 -1
- package/dist/test/input_text.test.d.ts +0 -1
- package/dist/types/event.d.ts +0 -7
package/dist/main.js
CHANGED
|
@@ -1,187 +1,187 @@
|
|
|
1
|
-
const
|
|
2
|
-
equals:
|
|
1
|
+
const J = (t, e) => t === e, y = {
|
|
2
|
+
equals: J
|
|
3
3
|
};
|
|
4
|
-
let
|
|
5
|
-
const
|
|
6
|
-
var
|
|
7
|
-
let
|
|
8
|
-
function
|
|
9
|
-
e = e ? Object.assign({},
|
|
4
|
+
let K = _;
|
|
5
|
+
const m = 1, w = 2, X = {};
|
|
6
|
+
var c = null;
|
|
7
|
+
let C = null, Y = null, l = null, a = null, g = null, E = 0;
|
|
8
|
+
function F(t, e) {
|
|
9
|
+
e = e ? Object.assign({}, y, e) : y;
|
|
10
10
|
const r = {
|
|
11
11
|
value: t,
|
|
12
12
|
observers: null,
|
|
13
13
|
observerSlots: null,
|
|
14
14
|
comparator: e.equals || void 0
|
|
15
|
-
},
|
|
16
|
-
return [
|
|
15
|
+
}, n = (s) => (typeof s == "function" && (s = s(r.value)), U(r, s));
|
|
16
|
+
return [R.bind(r), n];
|
|
17
17
|
}
|
|
18
|
-
function
|
|
19
|
-
const
|
|
20
|
-
|
|
18
|
+
function Z(t, e, r) {
|
|
19
|
+
const n = j(t, e, !1, m);
|
|
20
|
+
S(n);
|
|
21
21
|
}
|
|
22
22
|
function D(t, e, r) {
|
|
23
|
-
r = r ? Object.assign({},
|
|
24
|
-
const
|
|
25
|
-
return
|
|
23
|
+
r = r ? Object.assign({}, y, r) : y;
|
|
24
|
+
const n = j(t, e, !0, 0);
|
|
25
|
+
return n.observers = null, n.observerSlots = null, n.comparator = r.equals || void 0, S(n), R.bind(n);
|
|
26
26
|
}
|
|
27
|
-
function
|
|
28
|
-
if (
|
|
29
|
-
const e =
|
|
30
|
-
|
|
27
|
+
function P(t) {
|
|
28
|
+
if (l === null) return t();
|
|
29
|
+
const e = l;
|
|
30
|
+
l = null;
|
|
31
31
|
try {
|
|
32
32
|
return t();
|
|
33
33
|
} finally {
|
|
34
|
-
|
|
34
|
+
l = e;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
function
|
|
37
|
+
function $(t, e) {
|
|
38
38
|
const r = Symbol("context");
|
|
39
39
|
return {
|
|
40
40
|
id: r,
|
|
41
|
-
Provider:
|
|
41
|
+
Provider: ut(r),
|
|
42
42
|
defaultValue: t
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
|
-
function
|
|
45
|
+
function tt(t) {
|
|
46
46
|
let e;
|
|
47
|
-
return
|
|
47
|
+
return c && c.context && (e = c.context[t.id]) !== void 0 ? e : t.defaultValue;
|
|
48
48
|
}
|
|
49
|
-
function
|
|
50
|
-
const e = D(t), r = D(() =>
|
|
49
|
+
function et(t) {
|
|
50
|
+
const e = D(t), r = D(() => V(e()));
|
|
51
51
|
return r.toArray = () => {
|
|
52
|
-
const
|
|
53
|
-
return Array.isArray(
|
|
52
|
+
const n = r();
|
|
53
|
+
return Array.isArray(n) ? n : n != null ? [n] : [];
|
|
54
54
|
}, r;
|
|
55
55
|
}
|
|
56
|
-
function
|
|
56
|
+
function R() {
|
|
57
57
|
if (this.sources && this.state)
|
|
58
|
-
if (this.state ===
|
|
58
|
+
if (this.state === m) S(this);
|
|
59
59
|
else {
|
|
60
60
|
const t = a;
|
|
61
|
-
a = null,
|
|
61
|
+
a = null, A(() => x(this)), a = t;
|
|
62
62
|
}
|
|
63
|
-
if (
|
|
63
|
+
if (l) {
|
|
64
64
|
const t = this.observers ? this.observers.length : 0;
|
|
65
|
-
|
|
65
|
+
l.sources ? (l.sources.push(this), l.sourceSlots.push(t)) : (l.sources = [this], l.sourceSlots = [t]), this.observers ? (this.observers.push(l), this.observerSlots.push(l.sources.length - 1)) : (this.observers = [l], this.observerSlots = [l.sources.length - 1]);
|
|
66
66
|
}
|
|
67
67
|
return this.value;
|
|
68
68
|
}
|
|
69
|
-
function
|
|
70
|
-
let
|
|
71
|
-
return (!t.comparator || !t.comparator(
|
|
72
|
-
for (let
|
|
73
|
-
const i = t.observers[
|
|
74
|
-
p &&
|
|
69
|
+
function U(t, e, r) {
|
|
70
|
+
let n = t.value;
|
|
71
|
+
return (!t.comparator || !t.comparator(n, e)) && (t.value = e, t.observers && t.observers.length && A(() => {
|
|
72
|
+
for (let s = 0; s < t.observers.length; s += 1) {
|
|
73
|
+
const i = t.observers[s], p = C && C.running;
|
|
74
|
+
p && C.disposed.has(i), (p ? !i.tState : !i.state) && (i.pure ? a.push(i) : g.push(i), i.observers && G(i)), p || (i.state = m);
|
|
75
75
|
}
|
|
76
76
|
if (a.length > 1e6)
|
|
77
77
|
throw a = [], new Error();
|
|
78
78
|
})), e;
|
|
79
79
|
}
|
|
80
|
-
function
|
|
80
|
+
function S(t) {
|
|
81
81
|
if (!t.fn) return;
|
|
82
|
-
|
|
82
|
+
d(t);
|
|
83
83
|
const e = E;
|
|
84
|
-
|
|
84
|
+
rt(
|
|
85
85
|
t,
|
|
86
86
|
t.value,
|
|
87
87
|
e
|
|
88
88
|
);
|
|
89
89
|
}
|
|
90
|
-
function
|
|
91
|
-
let
|
|
92
|
-
const
|
|
93
|
-
|
|
90
|
+
function rt(t, e, r) {
|
|
91
|
+
let n;
|
|
92
|
+
const s = c, i = l;
|
|
93
|
+
l = c = t;
|
|
94
94
|
try {
|
|
95
|
-
|
|
95
|
+
n = t.fn(e);
|
|
96
96
|
} catch (p) {
|
|
97
|
-
return t.pure && (t.state =
|
|
97
|
+
return t.pure && (t.state = m, t.owned && t.owned.forEach(d), t.owned = null), t.updatedAt = r + 1, Q(p);
|
|
98
98
|
} finally {
|
|
99
|
-
|
|
99
|
+
l = i, c = s;
|
|
100
100
|
}
|
|
101
|
-
(!t.updatedAt || t.updatedAt <= r) && (t.updatedAt != null && "observers" in t ?
|
|
101
|
+
(!t.updatedAt || t.updatedAt <= r) && (t.updatedAt != null && "observers" in t ? U(t, n) : t.value = n, t.updatedAt = r);
|
|
102
102
|
}
|
|
103
|
-
function
|
|
103
|
+
function j(t, e, r, n = m, s) {
|
|
104
104
|
const i = {
|
|
105
105
|
fn: t,
|
|
106
|
-
state:
|
|
106
|
+
state: n,
|
|
107
107
|
updatedAt: null,
|
|
108
108
|
owned: null,
|
|
109
109
|
sources: null,
|
|
110
110
|
sourceSlots: null,
|
|
111
111
|
cleanups: null,
|
|
112
112
|
value: e,
|
|
113
|
-
owner:
|
|
114
|
-
context:
|
|
113
|
+
owner: c,
|
|
114
|
+
context: c ? c.context : null,
|
|
115
115
|
pure: r
|
|
116
116
|
};
|
|
117
|
-
return
|
|
117
|
+
return c === null || c !== X && (c.owned ? c.owned.push(i) : c.owned = [i]), i;
|
|
118
118
|
}
|
|
119
|
-
function
|
|
119
|
+
function H(t) {
|
|
120
120
|
if (t.state === 0) return;
|
|
121
|
-
if (t.state === w) return
|
|
122
|
-
if (t.suspense &&
|
|
121
|
+
if (t.state === w) return x(t);
|
|
122
|
+
if (t.suspense && P(t.suspense.inFallback)) return t.suspense.effects.push(t);
|
|
123
123
|
const e = [t];
|
|
124
124
|
for (; (t = t.owner) && (!t.updatedAt || t.updatedAt < E); )
|
|
125
125
|
t.state && e.push(t);
|
|
126
126
|
for (let r = e.length - 1; r >= 0; r--)
|
|
127
|
-
if (t = e[r], t.state ===
|
|
128
|
-
|
|
127
|
+
if (t = e[r], t.state === m)
|
|
128
|
+
S(t);
|
|
129
129
|
else if (t.state === w) {
|
|
130
|
-
const
|
|
131
|
-
a = null,
|
|
130
|
+
const n = a;
|
|
131
|
+
a = null, A(() => x(t, e[0])), a = n;
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
|
-
function
|
|
134
|
+
function A(t, e) {
|
|
135
135
|
if (a) return t();
|
|
136
136
|
let r = !1;
|
|
137
137
|
a = [], g ? r = !0 : g = [], E++;
|
|
138
138
|
try {
|
|
139
|
-
const
|
|
140
|
-
return nt(r),
|
|
141
|
-
} catch (
|
|
142
|
-
r || (g = null), a = null,
|
|
139
|
+
const n = t();
|
|
140
|
+
return nt(r), n;
|
|
141
|
+
} catch (n) {
|
|
142
|
+
r || (g = null), a = null, Q(n);
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
function nt(t) {
|
|
146
|
-
if (a && (
|
|
146
|
+
if (a && (_(a), a = null), t) return;
|
|
147
147
|
const e = g;
|
|
148
|
-
g = null, e.length &&
|
|
148
|
+
g = null, e.length && A(() => K(e));
|
|
149
149
|
}
|
|
150
|
-
function
|
|
151
|
-
for (let e = 0; e < t.length; e++)
|
|
150
|
+
function _(t) {
|
|
151
|
+
for (let e = 0; e < t.length; e++) H(t[e]);
|
|
152
152
|
}
|
|
153
|
-
function
|
|
153
|
+
function x(t, e) {
|
|
154
154
|
t.state = 0;
|
|
155
155
|
for (let r = 0; r < t.sources.length; r += 1) {
|
|
156
|
-
const
|
|
157
|
-
if (
|
|
158
|
-
const
|
|
159
|
-
|
|
156
|
+
const n = t.sources[r];
|
|
157
|
+
if (n.sources) {
|
|
158
|
+
const s = n.state;
|
|
159
|
+
s === m ? n !== e && (!n.updatedAt || n.updatedAt < E) && H(n) : s === w && x(n, e);
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
-
function
|
|
163
|
+
function G(t) {
|
|
164
164
|
for (let e = 0; e < t.observers.length; e += 1) {
|
|
165
165
|
const r = t.observers[e];
|
|
166
|
-
r.state || (r.state = w, r.pure ? a.push(r) : g.push(r), r.observers &&
|
|
166
|
+
r.state || (r.state = w, r.pure ? a.push(r) : g.push(r), r.observers && G(r));
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
|
-
function
|
|
169
|
+
function d(t) {
|
|
170
170
|
let e;
|
|
171
171
|
if (t.sources)
|
|
172
172
|
for (; t.sources.length; ) {
|
|
173
|
-
const r = t.sources.pop(),
|
|
174
|
-
if (
|
|
175
|
-
const i =
|
|
176
|
-
|
|
173
|
+
const r = t.sources.pop(), n = t.sourceSlots.pop(), s = r.observers;
|
|
174
|
+
if (s && s.length) {
|
|
175
|
+
const i = s.pop(), p = r.observerSlots.pop();
|
|
176
|
+
n < s.length && (i.sourceSlots[p] = n, s[n] = i, r.observerSlots[n] = p);
|
|
177
177
|
}
|
|
178
178
|
}
|
|
179
179
|
if (t.tOwned) {
|
|
180
|
-
for (e = t.tOwned.length - 1; e >= 0; e--)
|
|
180
|
+
for (e = t.tOwned.length - 1; e >= 0; e--) d(t.tOwned[e]);
|
|
181
181
|
delete t.tOwned;
|
|
182
182
|
}
|
|
183
183
|
if (t.owned) {
|
|
184
|
-
for (e = t.owned.length - 1; e >= 0; e--)
|
|
184
|
+
for (e = t.owned.length - 1; e >= 0; e--) d(t.owned[e]);
|
|
185
185
|
t.owned = null;
|
|
186
186
|
}
|
|
187
187
|
if (t.cleanups) {
|
|
@@ -190,143 +190,139 @@ function S(t) {
|
|
|
190
190
|
}
|
|
191
191
|
t.state = 0;
|
|
192
192
|
}
|
|
193
|
-
function
|
|
193
|
+
function st(t) {
|
|
194
194
|
return t instanceof Error ? t : new Error(typeof t == "string" ? t : "Unknown error", {
|
|
195
195
|
cause: t
|
|
196
196
|
});
|
|
197
197
|
}
|
|
198
|
-
function
|
|
199
|
-
throw
|
|
198
|
+
function Q(t, e = c) {
|
|
199
|
+
throw st(t);
|
|
200
200
|
}
|
|
201
|
-
function
|
|
202
|
-
if (typeof t == "function" && !t.length) return
|
|
201
|
+
function V(t) {
|
|
202
|
+
if (typeof t == "function" && !t.length) return V(t());
|
|
203
203
|
if (Array.isArray(t)) {
|
|
204
204
|
const e = [];
|
|
205
205
|
for (let r = 0; r < t.length; r++) {
|
|
206
|
-
const
|
|
207
|
-
Array.isArray(
|
|
206
|
+
const n = V(t[r]);
|
|
207
|
+
Array.isArray(n) ? e.push.apply(e, n) : e.push(n);
|
|
208
208
|
}
|
|
209
209
|
return e;
|
|
210
210
|
}
|
|
211
211
|
return t;
|
|
212
212
|
}
|
|
213
|
-
function
|
|
214
|
-
return function(
|
|
215
|
-
let
|
|
216
|
-
return
|
|
217
|
-
() =>
|
|
218
|
-
...
|
|
219
|
-
[t]:
|
|
220
|
-
},
|
|
213
|
+
function ut(t, e) {
|
|
214
|
+
return function(n) {
|
|
215
|
+
let s;
|
|
216
|
+
return Z(
|
|
217
|
+
() => s = P(() => (c.context = {
|
|
218
|
+
...c.context,
|
|
219
|
+
[t]: n.value
|
|
220
|
+
}, et(() => n.children))),
|
|
221
221
|
void 0
|
|
222
|
-
),
|
|
222
|
+
), s;
|
|
223
223
|
};
|
|
224
224
|
}
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
return t.checked;
|
|
231
|
-
}
|
|
232
|
-
}, ot = (t) => (P[t.type] || P.text)(t), R = {
|
|
233
|
-
text(t, e) {
|
|
225
|
+
const it = (t) => {
|
|
226
|
+
const e = t.target;
|
|
227
|
+
return e instanceof HTMLSelectElement ? e.value : e instanceof HTMLInputElement && e.type === "checkbox" ? e.checked : e.value;
|
|
228
|
+
}, M = (t, e) => {
|
|
229
|
+
if (t instanceof HTMLSelectElement) {
|
|
234
230
|
t.value = e;
|
|
235
|
-
|
|
236
|
-
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (t instanceof HTMLInputElement && t.type === "checkbox") {
|
|
237
234
|
t.checked = e;
|
|
235
|
+
return;
|
|
238
236
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
237
|
+
t.value = e;
|
|
238
|
+
}, b = (t) => t instanceof RegExp || typeof t == "string" || typeof t == "number" ? t : t.value, v = (t) => typeof t == "string" ? t : typeof t.message == "string" ? t.message : "", ot = (t, e, r = {}) => {
|
|
239
|
+
const n = t[e];
|
|
240
|
+
if (r.required && !n)
|
|
241
|
+
return { type: "required", message: v(r.required) };
|
|
242
|
+
if (r.min && Number(n) < Number(b(r.min)))
|
|
243
|
+
return { type: "min", message: v(r.min) };
|
|
244
|
+
if (r.max && Number(n) > Number(b(r.max)))
|
|
245
|
+
return { type: "max", message: v(r.max) };
|
|
246
|
+
if (r.minLength && n.length < b(r.minLength))
|
|
247
|
+
return { type: "minLength", message: v(r.minLength) };
|
|
248
|
+
if (r.maxLength && n.length > b(r.maxLength))
|
|
249
|
+
return { type: "maxLength", message: v(r.maxLength) };
|
|
250
|
+
if (r.pattern && !b(r.pattern).test(n))
|
|
251
|
+
return { type: "pattern", message: v(r.pattern) };
|
|
253
252
|
if (r.validate) {
|
|
254
|
-
const
|
|
255
|
-
if (
|
|
253
|
+
const s = r.validate(n, t);
|
|
254
|
+
if (s === !1)
|
|
256
255
|
return { type: "validate" };
|
|
257
|
-
if (typeof
|
|
258
|
-
return { type: "validate", message:
|
|
256
|
+
if (typeof s == "string")
|
|
257
|
+
return { type: "validate", message: s };
|
|
259
258
|
}
|
|
260
|
-
},
|
|
259
|
+
}, ft = ({
|
|
261
260
|
defaultValues: t,
|
|
262
261
|
mode: e = "onInput"
|
|
263
262
|
}) => {
|
|
264
|
-
const r = {},
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
const
|
|
268
|
-
return delete
|
|
269
|
-
}),
|
|
270
|
-
},
|
|
271
|
-
Object.keys(
|
|
272
|
-
|
|
263
|
+
const r = {}, n = {}, [s, i] = F(t), [p, L] = F({}), [k, O] = F(!0), I = (u) => {
|
|
264
|
+
const o = n[u], f = ot(s(), u, o);
|
|
265
|
+
L(f ? (h) => ({ ...h, [u]: f }) : (h) => {
|
|
266
|
+
const T = { ...h };
|
|
267
|
+
return delete T[u], T;
|
|
268
|
+
}), O(!Object.keys(p()).length);
|
|
269
|
+
}, z = () => {
|
|
270
|
+
Object.keys(s()).forEach((u) => {
|
|
271
|
+
I(u);
|
|
273
272
|
});
|
|
274
|
-
},
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
273
|
+
}, N = (u, o) => {
|
|
274
|
+
const f = it(u);
|
|
275
|
+
i((h) => ({ ...h, [o]: f })), I(o);
|
|
276
|
+
}, B = (u, o = {}) => (n[u] = {
|
|
277
|
+
required: o.required,
|
|
278
|
+
min: o.min,
|
|
279
|
+
max: o.max,
|
|
280
|
+
minLength: o.minLength,
|
|
281
|
+
maxLength: o.maxLength,
|
|
282
|
+
pattern: o.pattern,
|
|
283
|
+
validate: o.validate
|
|
282
284
|
}, {
|
|
283
285
|
name: u,
|
|
284
286
|
// value: values()[name],
|
|
285
287
|
onInput(f) {
|
|
286
|
-
|
|
287
|
-
const h = f.target.value;
|
|
288
|
-
i((m) => ({ ...m, [u]: h })), L(u);
|
|
289
|
-
}
|
|
288
|
+
e === "onInput" && N(f, u);
|
|
290
289
|
},
|
|
291
290
|
onChange(f) {
|
|
292
|
-
|
|
293
|
-
const h = ot(f.target);
|
|
294
|
-
i((m) => ({ ...m, [u]: h })), L(u);
|
|
295
|
-
}
|
|
291
|
+
(e === "onChange" || e === "onInput") && N(f, u);
|
|
296
292
|
},
|
|
297
293
|
ref(f) {
|
|
298
|
-
r[u] || (r[u] = f, f &&
|
|
294
|
+
r[u] || (r[u] = f, f && M(f, s()[u]));
|
|
299
295
|
}
|
|
300
|
-
}),
|
|
296
|
+
}), q = (u) => u ? s()[u] : s();
|
|
301
297
|
return {
|
|
302
|
-
values:
|
|
298
|
+
values: s,
|
|
303
299
|
errors: p,
|
|
304
|
-
isValid:
|
|
305
|
-
register:
|
|
306
|
-
getValues:
|
|
307
|
-
setValue: (u,
|
|
308
|
-
i((h) => ({ ...h, [u]:
|
|
300
|
+
isValid: k,
|
|
301
|
+
register: B,
|
|
302
|
+
getValues: q,
|
|
303
|
+
setValue: (u, o) => {
|
|
304
|
+
i((h) => ({ ...h, [u]: o }));
|
|
309
305
|
const f = r[u];
|
|
310
|
-
f &&
|
|
306
|
+
f && M(f, o);
|
|
311
307
|
},
|
|
312
|
-
onSubmit: (u) => (
|
|
313
|
-
|
|
308
|
+
onSubmit: (u) => (o) => {
|
|
309
|
+
o.preventDefault(), z(), k() && u(q());
|
|
314
310
|
},
|
|
315
311
|
reset: (u = {}) => {
|
|
316
312
|
i(() => ({
|
|
317
313
|
...t,
|
|
318
314
|
...u
|
|
319
|
-
})),
|
|
315
|
+
})), L({}), O(!0);
|
|
320
316
|
}
|
|
321
317
|
};
|
|
322
|
-
},
|
|
323
|
-
const t =
|
|
318
|
+
}, W = $({}), pt = (t) => /* @__PURE__ */ React.createElement(W.Provider, { value: t.form }, t.children), ht = () => {
|
|
319
|
+
const t = tt(W);
|
|
324
320
|
if (!t)
|
|
325
321
|
throw new Error("useFormContext: cannot find a FormProvider");
|
|
326
322
|
return t;
|
|
327
323
|
};
|
|
328
324
|
export {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
325
|
+
pt as FormProvider,
|
|
326
|
+
ft as useForm,
|
|
327
|
+
ht as useFormContext
|
|
332
328
|
};
|
package/dist/main.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(h,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(h=typeof globalThis<"u"?globalThis:h||self,y(h["solid-hook-form"]={}))})(this,function(h){"use strict";const w={equals:(t,e)=>t===e};let J=j;const m=1,x=2,K={};var o=null;let C=null,X=null,l=null,a=null,v=null,S=0;function k(t,e){e=e?Object.assign({},w,e):w;const n={value:t,observers:null,observerSlots:null,comparator:e.equals||void 0},r=s=>(typeof s=="function"&&(s=s(n.value)),N(n,s));return[q.bind(n),r]}function Y(t,e,n){const r=P(t,e,!1,m);E(r)}function T(t,e,n){n=n?Object.assign({},w,n):w;const r=P(t,e,!0,0);return r.observers=null,r.observerSlots=null,r.comparator=n.equals||void 0,E(r),q.bind(r)}function I(t){if(l===null)return t();const e=l;l=null;try{return t()}finally{l=e}}function Z(t,e){const n=Symbol("context");return{id:n,Provider:st(n),defaultValue:t}}function $(t){let e;return o&&o.context&&(e=o.context[t.id])!==void 0?e:t.defaultValue}function tt(t){const e=T(t),n=T(()=>O(e()));return n.toArray=()=>{const r=n();return Array.isArray(r)?r:r!=null?[r]:[]},n}function q(){if(this.sources&&this.state)if(this.state===m)E(this);else{const t=a;a=null,A(()=>L(this)),a=t}if(l){const t=this.observers?this.observers.length:0;l.sources?(l.sources.push(this),l.sourceSlots.push(t)):(l.sources=[this],l.sourceSlots=[t]),this.observers?(this.observers.push(l),this.observerSlots.push(l.sources.length-1)):(this.observers=[l],this.observerSlots=[l.sources.length-1])}return this.value}function N(t,e,n){let r=t.value;return(!t.comparator||!t.comparator(r,e))&&(t.value=e,t.observers&&t.observers.length&&A(()=>{for(let s=0;s<t.observers.length;s+=1){const u=t.observers[s],p=C&&C.running;p&&C.disposed.has(u),(p?!u.tState:!u.state)&&(u.pure?a.push(u):v.push(u),u.observers&&D(u)),p||(u.state=m)}if(a.length>1e6)throw a=[],new Error})),e}function E(t){if(!t.fn)return;F(t);const e=S;et(t,t.value,e)}function et(t,e,n){let r;const s=o,u=l;l=o=t;try{r=t.fn(e)}catch(p){return t.pure&&(t.state=m,t.owned&&t.owned.forEach(F),t.owned=null),t.updatedAt=n+1,R(p)}finally{l=u,o=s}(!t.updatedAt||t.updatedAt<=n)&&(t.updatedAt!=null&&"observers"in t?N(t,r):t.value=r,t.updatedAt=n)}function P(t,e,n,r=m,s){const u={fn:t,state:r,updatedAt:null,owned:null,sources:null,sourceSlots:null,cleanups:null,value:e,owner:o,context:o?o.context:null,pure:n};return o===null||o!==K&&(o.owned?o.owned.push(u):o.owned=[u]),u}function M(t){if(t.state===0)return;if(t.state===x)return L(t);if(t.suspense&&I(t.suspense.inFallback))return t.suspense.effects.push(t);const e=[t];for(;(t=t.owner)&&(!t.updatedAt||t.updatedAt<S);)t.state&&e.push(t);for(let n=e.length-1;n>=0;n--)if(t=e[n],t.state===m)E(t);else if(t.state===x){const r=a;a=null,A(()=>L(t,e[0])),a=r}}function A(t,e){if(a)return t();let n=!1;a=[],v?n=!0:v=[],S++;try{const r=t();return nt(n),r}catch(r){n||(v=null),a=null,R(r)}}function nt(t){if(a&&(j(a),a=null),t)return;const e=v;v=null,e.length&&A(()=>J(e))}function j(t){for(let e=0;e<t.length;e++)M(t[e])}function L(t,e){t.state=0;for(let n=0;n<t.sources.length;n+=1){const r=t.sources[n];if(r.sources){const s=r.state;s===m?r!==e&&(!r.updatedAt||r.updatedAt<S)&&M(r):s===x&&L(r,e)}}}function D(t){for(let e=0;e<t.observers.length;e+=1){const n=t.observers[e];n.state||(n.state=x,n.pure?a.push(n):v.push(n),n.observers&&D(n))}}function F(t){let e;if(t.sources)for(;t.sources.length;){const n=t.sources.pop(),r=t.sourceSlots.pop(),s=n.observers;if(s&&s.length){const u=s.pop(),p=n.observerSlots.pop();r<s.length&&(u.sourceSlots[p]=r,s[r]=u,n.observerSlots[r]=p)}}if(t.tOwned){for(e=t.tOwned.length-1;e>=0;e--)F(t.tOwned[e]);delete t.tOwned}if(t.owned){for(e=t.owned.length-1;e>=0;e--)F(t.owned[e]);t.owned=null}if(t.cleanups){for(e=t.cleanups.length-1;e>=0;e--)t.cleanups[e]();t.cleanups=null}t.state=0}function rt(t){return t instanceof Error?t:new Error(typeof t=="string"?t:"Unknown error",{cause:t})}function R(t,e=o){throw rt(t)}function O(t){if(typeof t=="function"&&!t.length)return O(t());if(Array.isArray(t)){const e=[];for(let n=0;n<t.length;n++){const r=O(t[n]);Array.isArray(r)?e.push.apply(e,r):e.push(r)}return e}return t}function st(t,e){return function(r){let s;return Y(()=>s=I(()=>(o.context={...o.context,[t]:r.value},tt(()=>r.children))),void 0),s}}const it=t=>{const e=t.target;return e instanceof HTMLSelectElement?e.value:e instanceof HTMLInputElement&&e.type==="checkbox"?e.checked:e.value},U=(t,e)=>{if(t instanceof HTMLSelectElement){t.value=e;return}if(t instanceof HTMLInputElement&&t.type==="checkbox"){t.checked=e;return}t.value=e},d=t=>t instanceof RegExp||typeof t=="string"||typeof t=="number"?t:t.value,b=t=>typeof t=="string"?t:typeof t.message=="string"?t.message:"",ut=(t,e,n={})=>{const r=t[e];if(n.required&&!r)return{type:"required",message:b(n.required)};if(n.min&&Number(r)<Number(d(n.min)))return{type:"min",message:b(n.min)};if(n.max&&Number(r)>Number(d(n.max)))return{type:"max",message:b(n.max)};if(n.minLength&&r.length<d(n.minLength))return{type:"minLength",message:b(n.minLength)};if(n.maxLength&&r.length>d(n.maxLength))return{type:"maxLength",message:b(n.maxLength)};if(n.pattern&&!d(n.pattern).test(r))return{type:"pattern",message:b(n.pattern)};if(n.validate){const s=n.validate(r,t);if(s===!1)return{type:"validate"};if(typeof s=="string")return{type:"validate",message:s}}},ot=({defaultValues:t,mode:e="onInput"})=>{const n={},r={},[s,u]=k(t),[p,V]=k({}),[_,G]=k(!0),Q=i=>{const c=r[i],f=ut(s(),i,c);V(f?g=>({...g,[i]:f}):g=>{const B={...g};return delete B[i],B}),G(!Object.keys(p()).length)},at=()=>{Object.keys(s()).forEach(i=>{Q(i)})},W=(i,c)=>{const f=it(i);u(g=>({...g,[c]:f})),Q(c)},ft=(i,c={})=>(r[i]={required:c.required,min:c.min,max:c.max,minLength:c.minLength,maxLength:c.maxLength,pattern:c.pattern,validate:c.validate},{name:i,onInput(f){e==="onInput"&&W(f,i)},onChange(f){(e==="onChange"||e==="onInput")&&W(f,i)},ref(f){n[i]||(n[i]=f,f&&U(f,s()[i]))}}),z=i=>i?s()[i]:s();return{values:s,errors:p,isValid:_,register:ft,getValues:z,setValue:(i,c)=>{u(g=>({...g,[i]:c}));const f=n[i];f&&U(f,c)},onSubmit:i=>c=>{c.preventDefault(),at(),_()&&i(z())},reset:(i={})=>{u(()=>({...t,...i})),V({}),G(!0)}}},H=Z({}),lt=t=>React.createElement(H.Provider,{value:t.form},t.children),ct=()=>{const t=$(H);if(!t)throw new Error("useFormContext: cannot find a FormProvider");return t};h.FormProvider=lt,h.useForm=ot,h.useFormContext=ct,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|
package/dist/types/form.d.ts
CHANGED
|
@@ -2,13 +2,12 @@ import { Accessor } from 'solid-js';
|
|
|
2
2
|
import { FieldPath, FieldPathValue, Path } from './path';
|
|
3
3
|
import { Rules } from './validate';
|
|
4
4
|
import { FieldErrors } from './errors';
|
|
5
|
-
import { InputEvent, ChangeEvent } from './event';
|
|
6
5
|
export type FormValues = Record<string, any>;
|
|
7
6
|
type RegisterReturn<F extends FormValues> = {
|
|
8
7
|
name: Path<F>;
|
|
9
|
-
ref(ref:
|
|
10
|
-
onInput(event:
|
|
11
|
-
onChange(event:
|
|
8
|
+
ref(ref: HTMLElement | null): void;
|
|
9
|
+
onInput(event: Event): void;
|
|
10
|
+
onChange(event: Event): void;
|
|
12
11
|
};
|
|
13
12
|
export type Register<T extends FormValues> = (name: Path<T>, options?: Rules<T, Path<T>>) => RegisterReturn<T>;
|
|
14
13
|
export type GetValues<F extends FormValues> = {
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|