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