@teamnovu/kit-vue-forms 0.0.10 → 0.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.
@@ -7,4 +7,5 @@ export interface UseFieldOptions<T, K extends string> {
7
7
  path: K;
8
8
  errors?: MaybeRef<ValidationErrors>;
9
9
  }
10
+ export declare function getEmptyField(): FormField<unknown, string>;
10
11
  export declare function useField<T, K extends string>(options: UseFieldOptions<T, K>): FormField<T, K>;
@@ -13,7 +13,7 @@ export declare function useFieldRegistry<T extends FormDataDefault>(formState: F
13
13
  getField: <K extends Paths<T>>(path: K) => ResolvedFormField<T, K>;
14
14
  getFields: <TData extends T>() => FieldsTuple<TData>;
15
15
  registerField: <K extends Paths<T>>(field: ResolvedFormField<T, K>) => void;
16
- defineField: <K extends Paths<T>>(options: DefineFieldOptions<PickProps<T, K>, K>) => FormField<K extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_1 ? T_1 extends TRest ? T_1 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_2 ? T_2 extends TRest ? T_2 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_3 ? T_3 extends TRest ? T_3 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_4 ? T_4 extends TRest ? T_4 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_5 ? T_5 extends TRest ? T_5 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_6 ? T_6 extends TRest ? T_6 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_7 ? T_7 extends TRest ? T_7 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_8 ? T_8 extends TRest ? T_8 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_9 ? T_9 extends TRest ? T_9 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? TRest extends infer T_10 ? T_10 extends TRest ? T_10 extends `${infer TRoot}.${infer TRest}` ? TRoot extends string | number | symbol ? TRest extends string ? /*elided*/ any : never : TRoot extends `${number}` ? never : never : T_10 extends string | number | symbol ? Required<any>[T_10] : T_10 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_9 extends string | number | symbol ? Required<any>[T_9] : T_9 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_8 extends string | number | symbol ? Required<any>[T_8] : T_8 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_7 extends string | number | symbol ? Required<any>[T_7] : T_7 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_6 extends string | number | symbol ? Required<any>[T_6] : T_6 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_5 extends string | number | symbol ? Required<any>[T_5] : T_5 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_4 extends string | number | symbol ? Required<any>[T_4] : T_4 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_3 extends string | number | symbol ? Required<any>[T_3] : T_3 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_2 extends string | number | symbol ? Required<any>[T_2] : T_2 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : T_1 extends string | number | symbol ? Required<any>[T_1] : T_1 extends `${number}` ? never : never : never : never : never : TRoot extends `${number}` ? never : never : K extends string | number | symbol ? any : K extends `${number}` ? never : never, K>;
16
+ defineField: <K extends Paths<T>>(options: DefineFieldOptions<PickProps<T, K>, K>) => ResolvedFormField<T, K>;
17
17
  };
18
18
  export type FieldRegistry<T extends FormDataDefault> = ReturnType<typeof useFieldRegistry<T>>;
19
19
  export {};
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  var x = Object.defineProperty;
2
2
  var K = (e, r, t) => r in e ? x(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t;
3
3
  var S = (e, r, t) => K(e, typeof r != "symbol" ? r + "" : r, t);
4
- import { toValue as W, toRaw as I, computed as d, unref as c, reactive as m, watch as F, toRefs as O, toRef as D, ref as A, isRef as _, getCurrentScope as J, onBeforeUnmount as T, defineComponent as B, renderSlot as G, normalizeProps as L, guardReactiveProps as U } from "vue";
4
+ import { toValue as W, toRaw as I, computed as d, unref as u, reactive as m, watch as F, toRefs as O, toRef as D, ref as A, isRef as _, getCurrentScope as J, onBeforeUnmount as T, defineComponent as B, renderSlot as G, normalizeProps as L, guardReactiveProps as U } from "vue";
5
5
  import "zod";
6
6
  function y(e) {
7
7
  const r = W(e), t = I(r);
@@ -12,27 +12,27 @@ function N(e) {
12
12
  }
13
13
  function w(e, r) {
14
14
  return (Array.isArray(r) ? r : N(r)).reduce(
15
- (a, s) => a == null ? void 0 : a[s],
15
+ (s, a) => s == null ? void 0 : s[a],
16
16
  e
17
17
  );
18
18
  }
19
19
  function Z(e, r, t) {
20
- const a = Array.isArray(r) ? r : N(r);
21
- if (a.length === 0)
20
+ const s = Array.isArray(r) ? r : N(r);
21
+ if (s.length === 0)
22
22
  throw new Error("Path cannot be empty");
23
- const s = a.at(-1), n = a.slice(0, -1).reduce(
24
- (u, v) => u[v],
23
+ const a = s.at(-1), n = s.slice(0, -1).reduce(
24
+ (o, v) => o[v],
25
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
26
  e
27
27
  );
28
- n[s] = t;
28
+ n[a] = t;
29
29
  }
30
30
  const z = (e, r) => d({
31
31
  get() {
32
- return w(c(e), c(r));
32
+ return w(u(e), u(r));
33
33
  },
34
34
  set(t) {
35
- Z(c(e), c(r), t);
35
+ Z(u(e), u(r), t);
36
36
  }
37
37
  });
38
38
  function E(e, r) {
@@ -41,15 +41,15 @@ function E(e, r) {
41
41
  function k(e, r) {
42
42
  if (!r)
43
43
  return e;
44
- const t = `${r}.`, a = Object.fromEntries(
45
- Object.entries(e.propertyErrors).filter(([s]) => s.startsWith(t)).map(
46
- ([s, n]) => [s.slice(t.length), n]
44
+ const t = `${r}.`, s = Object.fromEntries(
45
+ Object.entries(e.propertyErrors).filter(([a]) => a.startsWith(t)).map(
46
+ ([a, n]) => [a.slice(t.length), n]
47
47
  )
48
48
  );
49
49
  return {
50
50
  general: e.general,
51
51
  // Keep general errors
52
- propertyErrors: a
52
+ propertyErrors: s
53
53
  };
54
54
  }
55
55
  function q(e) {
@@ -57,61 +57,64 @@ function q(e) {
57
57
  value: e.value,
58
58
  path: e.path,
59
59
  initialValue: d(() => Object.freeze(y(e.initialValue))),
60
- errors: c(e.errors) || [],
60
+ errors: u(e.errors) || [],
61
61
  touched: !1
62
62
  });
63
- F(() => c(e.errors), (f) => {
63
+ F(() => u(e.errors), (f) => {
64
64
  r.errors = f || [];
65
65
  });
66
- const t = d(() => JSON.stringify(r.value) !== JSON.stringify(r.initialValue)), a = (f) => {
66
+ const t = d(() => JSON.stringify(r.value) !== JSON.stringify(r.initialValue)), s = (f) => {
67
67
  r.value = f;
68
- }, s = () => {
68
+ }, a = () => {
69
69
  r.touched = !0;
70
70
  }, n = () => {
71
- }, u = () => {
71
+ }, o = () => {
72
72
  r.value = y(r.initialValue), r.touched = !1, r.errors = [];
73
73
  }, v = (f) => {
74
74
  r.errors = f;
75
- }, o = () => {
75
+ }, l = () => {
76
76
  r.errors = [];
77
- }, l = O(r);
77
+ }, c = O(r);
78
78
  return {
79
- data: l.value,
80
- path: l.path,
81
- initialValue: l.initialValue,
82
- errors: l.errors,
83
- touched: l.touched,
79
+ data: c.value,
80
+ path: c.path,
81
+ initialValue: c.initialValue,
82
+ errors: c.errors,
83
+ touched: c.touched,
84
84
  dirty: t,
85
- setData: a,
86
- onBlur: s,
85
+ setData: s,
86
+ onBlur: a,
87
87
  onFocus: n,
88
- reset: u,
88
+ reset: o,
89
89
  setErrors: v,
90
- clearErrors: o
90
+ clearErrors: l
91
91
  };
92
92
  }
93
93
  function H(e) {
94
- const r = {}, t = (u) => {
95
- const v = c(u.path);
96
- r[v] = u;
94
+ const r = {}, t = (o) => {
95
+ const v = u(o.path);
96
+ r[v] = o;
97
+ }, s = (o) => {
98
+ if (!r[o]) {
99
+ const v = q({
100
+ path: o,
101
+ value: z(D(e, "data"), o),
102
+ initialValue: d(() => w(e.initialData, o))
103
+ });
104
+ return t(v), v;
105
+ }
106
+ return r[o];
97
107
  };
98
108
  return {
99
109
  fields: r,
100
- getField: (u) => r[u] ?? {},
110
+ getField: s,
101
111
  getFields: () => Object.values(r),
102
112
  registerField: t,
103
- defineField: (u) => {
104
- const v = q({
105
- ...u,
106
- value: z(D(e, "data"), u.path),
107
- initialValue: d(() => w(e.initialData, c(u.path)))
108
- });
109
- return t(v), v;
110
- }
113
+ defineField: (o) => s(o.path)
111
114
  };
112
115
  }
113
116
  function Q(e) {
114
- const r = d(() => e.getFields().some((a) => c(a.dirty))), t = d(() => e.getFields().some((a) => c(a.touched)));
117
+ const r = d(() => e.getFields().some((s) => u(s.dirty))), t = d(() => e.getFields().some((s) => u(s.touched)));
115
118
  return {
116
119
  isDirty: r,
117
120
  isTouched: t
@@ -119,28 +122,28 @@ function Q(e) {
119
122
  }
120
123
  function X(e) {
121
124
  return e.filter(
122
- (r, t, a) => a.indexOf(r) === t
125
+ (r, t, s) => s.indexOf(r) === t
123
126
  );
124
127
  }
125
128
  function C(...e) {
126
129
  return e.slice(1).reduce((r, t) => {
127
130
  if (!r && !t)
128
131
  return;
129
- const a = ((t == null ? void 0 : t.length) ?? 0) > 0;
132
+ const s = ((t == null ? void 0 : t.length) ?? 0) > 0;
130
133
  if (!r && ((t == null ? void 0 : t.length) ?? 0) > 0)
131
134
  return t;
132
- if (!a)
135
+ if (!s)
133
136
  return r;
134
- const s = (r ?? []).concat(t);
135
- return X(s);
137
+ const a = (r ?? []).concat(t);
138
+ return X(a);
136
139
  }, e[0]);
137
140
  }
138
141
  function Y(...e) {
139
- return e.map((t) => Object.keys(t)).flat().reduce((t, a) => {
140
- const s = e.map((n) => n[a]).filter(Boolean);
142
+ return e.map((t) => Object.keys(t)).flat().reduce((t, s) => {
143
+ const a = e.map((n) => n[s]).filter(Boolean);
141
144
  return {
142
145
  ...t,
143
- [a]: C(...s)
146
+ [s]: C(...a)
144
147
  };
145
148
  }, {});
146
149
  }
@@ -152,24 +155,24 @@ function P(...e) {
152
155
  };
153
156
  const r = e[0];
154
157
  return e.length === 1 ? r : e.slice(1).reduce(
155
- (t, a) => ({
156
- general: C(t.general, a.general),
157
- propertyErrors: Y(t.propertyErrors ?? {}, a.propertyErrors ?? {})
158
+ (t, s) => ({
159
+ general: C(t.general, s.general),
160
+ propertyErrors: Y(t.propertyErrors ?? {}, s.propertyErrors ?? {})
158
161
  }),
159
162
  r
160
163
  );
161
164
  }
162
165
  function j(e) {
163
- var a;
164
- const r = (((a = e.general) == null ? void 0 : a.length) ?? 0) > 0, t = Object.entries(e.propertyErrors).filter(([, s]) => s == null ? void 0 : s.length).length > 0;
166
+ var s;
167
+ const r = (((s = e.general) == null ? void 0 : s.length) ?? 0) > 0, t = Object.entries(e.propertyErrors).filter(([, a]) => a == null ? void 0 : a.length).length > 0;
165
168
  return r || t;
166
169
  }
167
170
  function rr(e) {
168
- const r = e.issues.filter((a) => a.path.length === 0).map((a) => a.message), t = e.issues.filter((a) => a.path.length > 0).reduce((a, s) => {
169
- const n = s.path.join(".");
171
+ const r = e.issues.filter((s) => s.path.length === 0).map((s) => s.message), t = e.issues.filter((s) => s.path.length > 0).reduce((s, a) => {
172
+ const n = a.path.join(".");
170
173
  return {
171
- ...a,
172
- [n]: [...a[n] ?? [], s.message]
174
+ ...s,
175
+ [n]: [...s[n] ?? [], a.message]
173
176
  };
174
177
  }, {});
175
178
  return {
@@ -177,7 +180,7 @@ function rr(e) {
177
180
  propertyErrors: t
178
181
  };
179
182
  }
180
- const V = {
183
+ const p = {
181
184
  isValid: !0,
182
185
  errors: {
183
186
  general: [],
@@ -190,16 +193,16 @@ class er {
190
193
  }
191
194
  async validate(r) {
192
195
  if (!this.schema)
193
- return V;
196
+ return p;
194
197
  const t = await this.schema.safeParseAsync(r);
195
198
  if (t.success)
196
- return V;
197
- const a = rr(t.error);
199
+ return p;
200
+ const s = rr(t.error);
198
201
  return {
199
202
  isValid: !1,
200
203
  errors: {
201
- general: a.general ?? [],
202
- propertyErrors: a.propertyErrors ?? {}
204
+ general: s.general ?? [],
205
+ propertyErrors: s.propertyErrors ?? {}
203
206
  }
204
207
  };
205
208
  }
@@ -210,10 +213,10 @@ class tr {
210
213
  }
211
214
  async validate(r) {
212
215
  if (!this.validateFn)
213
- return V;
216
+ return p;
214
217
  try {
215
218
  const t = await this.validateFn(r);
216
- return t.isValid ? V : t;
219
+ return t.isValid ? p : t;
217
220
  } catch (t) {
218
221
  return {
219
222
  isValid: !1,
@@ -225,87 +228,87 @@ class tr {
225
228
  }
226
229
  }
227
230
  }
228
- class ar {
231
+ class sr {
229
232
  constructor(r, t) {
230
233
  S(this, "schemaValidator");
231
234
  S(this, "functionValidator");
232
235
  this.schema = r, this.validateFn = t, this.schemaValidator = new er(this.schema), this.functionValidator = new tr(this.validateFn);
233
236
  }
234
237
  async validate(r) {
235
- const [t, a] = await Promise.all([
238
+ const [t, s] = await Promise.all([
236
239
  this.schemaValidator.validate(r),
237
240
  this.functionValidator.validate(r)
238
241
  ]);
239
242
  return {
240
- isValid: t.isValid && a.isValid,
241
- errors: P(t.errors, a.errors)
243
+ isValid: t.isValid && s.isValid,
244
+ errors: P(t.errors, s.errors)
242
245
  };
243
246
  }
244
247
  }
245
248
  function b(e) {
246
- return d(() => new ar(
247
- c(e.schema),
248
- c(e.validateFn)
249
+ return d(() => new sr(
250
+ u(e.schema),
251
+ u(e.validateFn)
249
252
  ));
250
253
  }
251
- function sr(e, r) {
254
+ function ar(e, r) {
252
255
  const t = m({
253
256
  validators: A([b(r)]),
254
257
  isValidated: !1,
255
- errors: c(r.errors) ?? V.errors
258
+ errors: u(r.errors) ?? p.errors
256
259
  });
257
- F(() => c(r.errors), async () => {
258
- const o = await s();
259
- n(o.errors);
260
+ F(() => u(r.errors), async () => {
261
+ const l = await a();
262
+ n(l.errors);
260
263
  }, { immediate: !0 }), F(
261
264
  [() => t.validators],
262
- async (o) => {
265
+ async (l) => {
263
266
  if (t.isValidated)
264
- if (o) {
265
- const l = await s();
266
- t.errors = l.errors;
267
+ if (l) {
268
+ const c = await a();
269
+ t.errors = c.errors;
267
270
  } else
268
- t.errors = V.errors;
271
+ t.errors = p.errors;
269
272
  },
270
273
  { immediate: !0 }
271
274
  ), F(() => e.data, () => {
272
- t.isValidated && u();
275
+ t.isValidated && o();
273
276
  });
274
- const a = (o) => {
275
- const l = _(o) ? o : b(o);
276
- return t.validators.push(l), J() && T(() => {
277
+ const s = (l) => {
278
+ const c = _(l) ? l : b(l);
279
+ return t.validators.push(c), J() && T(() => {
277
280
  t.validators = t.validators.filter(
278
- (f) => f !== l
281
+ (f) => f !== c
279
282
  );
280
- }), l;
283
+ }), c;
281
284
  };
282
- async function s() {
283
- const o = await Promise.all(
284
- t.validators.filter((p) => c(p) !== void 0).map((p) => c(p).validate(e.data))
285
- ), l = o.every((p) => p.isValid);
286
- let { errors: f } = V;
287
- if (!l) {
288
- const p = o.map((R) => R.errors);
289
- f = P(...p);
285
+ async function a() {
286
+ const l = await Promise.all(
287
+ t.validators.filter((V) => u(V) !== void 0).map((V) => u(V).validate(e.data))
288
+ ), c = l.every((V) => V.isValid);
289
+ let { errors: f } = p;
290
+ if (!c) {
291
+ const V = l.map((R) => R.errors);
292
+ f = P(...V);
290
293
  }
291
294
  return {
292
295
  errors: f,
293
- isValid: l
296
+ isValid: c
294
297
  };
295
298
  }
296
- const n = (o) => {
297
- t.errors = P(c(r.errors) ?? V.errors, o);
298
- }, u = async () => {
299
- const o = await s();
300
- return n(o.errors), t.isValidated = !0, {
301
- isValid: !j(o.errors),
299
+ const n = (l) => {
300
+ t.errors = P(u(r.errors) ?? p.errors, l);
301
+ }, o = async () => {
302
+ const l = await a();
303
+ return n(l.errors), t.isValidated = !0, {
304
+ isValid: !j(l.errors),
302
305
  errors: t.errors
303
306
  };
304
307
  }, v = d(() => !j(t.errors));
305
308
  return {
306
309
  ...O(t),
307
- validateForm: u,
308
- defineValidator: a,
310
+ validateForm: o,
311
+ defineValidator: s,
309
312
  isValid: v
310
313
  };
311
314
  }
@@ -316,15 +319,15 @@ class ir {
316
319
  async validate(r) {
317
320
  const t = w(r, this.path);
318
321
  if (!this.validator)
319
- return V;
320
- const a = await this.validator.validate(t);
322
+ return p;
323
+ const s = await this.validator.validate(t);
321
324
  return {
322
- isValid: a.isValid,
325
+ isValid: s.isValid,
323
326
  errors: {
324
- general: a.errors.general || [],
325
- propertyErrors: a.errors.propertyErrors ? Object.fromEntries(
326
- Object.entries(a.errors.propertyErrors).map(([s, n]) => [
327
- E(this.path, s),
327
+ general: s.errors.general || [],
328
+ propertyErrors: s.errors.propertyErrors ? Object.fromEntries(
329
+ Object.entries(s.errors.propertyErrors).map(([a, n]) => [
330
+ E(this.path, a),
328
331
  n
329
332
  ])
330
333
  ) : {}
@@ -333,13 +336,13 @@ class ir {
333
336
  }
334
337
  }
335
338
  function nr(e, r, t) {
336
- const a = z(e.data, r), s = d(() => w(e.initialData.value, r)), n = (i) => ({
339
+ const s = z(e.data, r), a = d(() => w(e.initialData.value, r)), n = (i) => ({
337
340
  ...i,
338
- path: d(() => c(i.path).replace(r + ".", "")),
341
+ path: d(() => u(i.path).replace(r + ".", "")),
339
342
  setData: (h) => {
340
343
  i.setData(h);
341
344
  }
342
- }), u = (i) => {
345
+ }), o = (i) => {
343
346
  const h = E(r, i), g = e.getField(h);
344
347
  return g ? n(g) : {};
345
348
  }, v = (i) => {
@@ -348,31 +351,31 @@ function nr(e, r, t) {
348
351
  path: h
349
352
  });
350
353
  return n(g);
351
- }, o = () => e.getFields().filter((i) => {
354
+ }, l = () => e.getFields().filter((i) => {
352
355
  const h = i.path.value;
353
356
  return h.startsWith(r + ".") || h === r;
354
- }).map((i) => n(i)), l = () => e.getFields().filter((i) => {
357
+ }).map((i) => n(i)), c = () => e.getFields().filter((i) => {
355
358
  const h = i.path.value;
356
359
  return h.startsWith(r + ".") || h === r;
357
- }), f = d(() => l().some((i) => i.dirty.value)), p = d(() => l().some((i) => i.touched.value)), R = d(() => e.isValid.value), M = d(() => e.isValidated.value), $ = d(() => k(c(e.errors), r));
360
+ }), f = d(() => c().some((i) => i.dirty.value)), V = d(() => c().some((i) => i.touched.value)), R = d(() => e.isValid.value), M = d(() => e.isValidated.value), $ = d(() => k(u(e.errors), r));
358
361
  return {
359
- data: a,
360
- initialData: s,
362
+ data: s,
363
+ initialData: a,
361
364
  defineField: v,
362
- getField: u,
363
- getFields: o,
365
+ getField: o,
366
+ getFields: l,
364
367
  isDirty: f,
365
- isTouched: p,
368
+ isTouched: V,
366
369
  isValid: R,
367
370
  isValidated: M,
368
371
  errors: $,
369
372
  defineValidator: (i) => {
370
373
  const h = _(i) ? i : b(i), g = d(
371
- () => new ir(r, c(h))
374
+ () => new ir(r, u(h))
372
375
  );
373
376
  return e.defineValidator(g), h;
374
377
  },
375
- reset: () => l().forEach((i) => i.reset()),
378
+ reset: () => c().forEach((i) => i.reset()),
376
379
  validateForm: () => e.validateForm(),
377
380
  getSubForm: (i, h) => {
378
381
  const g = E(r, i);
@@ -384,29 +387,29 @@ function nr(e, r, t) {
384
387
  };
385
388
  }
386
389
  function vr(e) {
387
- const r = d(() => Object.freeze(y(e.initialData))), t = A(y(r)), a = m({
390
+ const r = d(() => Object.freeze(y(e.initialData))), t = A(y(r)), s = m({
388
391
  initialData: r,
389
392
  data: t
390
- }), s = H(a), n = sr(a, e), u = Q(s), v = () => {
391
- t.value = y(r), s.getFields().forEach(
393
+ }), a = H(s), n = ar(s, e), o = Q(a), v = () => {
394
+ t.value = y(r), a.getFields().forEach(
392
395
  (f) => f.reset()
393
396
  );
394
397
  };
395
- function o(f, p) {
396
- return nr(l, f);
398
+ function l(f, V) {
399
+ return nr(c, f);
397
400
  }
398
- const l = {
399
- ...s,
401
+ const c = {
402
+ ...a,
400
403
  ...n,
401
- ...u,
404
+ ...o,
402
405
  reset: v,
403
- getSubForm: o,
404
- initialData: D(a, "initialData"),
405
- data: D(a, "data")
406
+ getSubForm: l,
407
+ initialData: D(s, "initialData"),
408
+ data: D(s, "data")
406
409
  };
407
- return l;
410
+ return c;
408
411
  }
409
- const pr = /* @__PURE__ */ B({
412
+ const Vr = /* @__PURE__ */ B({
410
413
  __name: "Field",
411
414
  props: {
412
415
  form: {},
@@ -419,10 +422,10 @@ const pr = /* @__PURE__ */ B({
419
422
  const r = e, t = r.form.defineField({
420
423
  path: r.path
421
424
  });
422
- return (a, s) => G(a.$slots, "default", L(U(m(c(t)))));
425
+ return (s, a) => G(s.$slots, "default", L(U(m(u(t)))));
423
426
  }
424
427
  });
425
428
  export {
426
- pr as Field,
429
+ Vr as Field,
427
430
  vr as useForm
428
431
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-vue-forms",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -1,4 +1,4 @@
1
- import { computed, reactive, toRefs, unref, watch, type MaybeRef, type MaybeRefOrGetter } from 'vue'
1
+ import { computed, reactive, ref, toRefs, unref, watch, type MaybeRef, type MaybeRefOrGetter } from 'vue'
2
2
  import type { FormField } from '../types/form'
3
3
  import type { ValidationErrorMessage, ValidationErrors } from '../types/validation'
4
4
  import { cloneRefValue } from '../utils/general'
@@ -10,6 +10,23 @@ export interface UseFieldOptions<T, K extends string> {
10
10
  errors?: MaybeRef<ValidationErrors>
11
11
  }
12
12
 
13
+ export function getEmptyField(): FormField<unknown, string> {
14
+ return {
15
+ setData: () => {},
16
+ onBlur: () => {},
17
+ onFocus: () => {},
18
+ reset: () => {},
19
+ setErrors: () => {},
20
+ clearErrors: () => {},
21
+ data: ref(undefined),
22
+ initialValue: ref(undefined),
23
+ path: ref(''),
24
+ errors: ref([]),
25
+ touched: ref(false),
26
+ dirty: computed(() => false),
27
+ }
28
+ }
29
+
13
30
  export function useField<T, K extends string>(options: UseFieldOptions<T, K>): FormField<T, K> {
14
31
  const state = reactive({
15
32
  value: options.value,
@@ -2,7 +2,7 @@ import { computed, toRef, unref } from 'vue'
2
2
  import type { FieldsTuple, FormDataDefault, FormField } from '../types/form'
3
3
  import type { Paths, PickProps } from '../types/util'
4
4
  import { getLens, getNestedValue } from '../utils/path'
5
- import { useField, type UseFieldOptions } from './useField'
5
+ import { getEmptyField, useField, type UseFieldOptions } from './useField'
6
6
 
7
7
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
8
  type FieldRegistryCache<T> = Record<Paths<T>, FormField<any, string>>
@@ -26,22 +26,31 @@ export function useFieldRegistry<T extends FormDataDefault>(
26
26
  fields[path] = field
27
27
  }
28
28
 
29
- const getField = <K extends Paths<T>>(path: K) => {
30
- return (fields[path] ?? {}) as ResolvedFormField<T, K>
29
+ const getField = <K extends Paths<T>>(path: K): ResolvedFormField<T, K> => {
30
+ if (!fields[path]) {
31
+ const field = useField({
32
+ path,
33
+ value: getLens(toRef(formState, 'data'), path),
34
+ initialValue: computed(() => getNestedValue(formState.initialData, path)),
35
+ })
36
+
37
+ registerField(field)
38
+
39
+ return field
40
+ }
41
+
42
+ return fields[path] as ResolvedFormField<T, K>
31
43
  }
32
44
 
33
45
  const getFields = <TData extends T>() => {
34
46
  return Object.values(fields) as FieldsTuple<TData>
35
47
  }
36
48
 
37
- const defineField = <K extends Paths<T>>(options: DefineFieldOptions<PickProps<T, K>, K>) => {
38
- const field = useField({
39
- ...options,
40
- value: getLens(toRef(formState, 'data'), options.path),
41
- initialValue: computed(() => getNestedValue(formState.initialData, unref(options.path))),
42
- })
49
+ const defineField = <K extends Paths<T>>(options: DefineFieldOptions<PickProps<T, K>, K>): ResolvedFormField<T, K> => {
50
+ const field = getField(options.path)
43
51
 
44
- registerField(field)
52
+ // TODO: If more options are ever needed than only the path we have to update the field
53
+ // here with the new options
45
54
 
46
55
  return field
47
56
  }