@teamnovu/kit-vue-forms 0.1.13 → 0.1.14

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,5 @@
1
+ import { Ref } from 'vue';
2
+ import { FormDataDefault } from '../types/form';
3
+ export declare function useFormData<T extends FormDataDefault>(initialData: Ref<T>): {
4
+ data: Ref<T, T>;
5
+ };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  var G = Object.defineProperty;
2
2
  var L = (t, e, r) => e in t ? G(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
3
  var w = (t, e, r) => L(t, typeof e != "symbol" ? e + "" : e, r);
4
- import { toValue as Z, toRaw as q, computed as u, unref as f, isRef as j, shallowRef as _, reactive as D, watch as F, toRefs as W, shallowReactive as H, toRef as S, onScopeDispose as Q, watchEffect as X, triggerRef as Y, ref as k, getCurrentScope as x, onBeforeUnmount as ee, defineComponent as $, renderSlot as C, normalizeProps as B, guardReactiveProps as I, resolveComponent as re, createBlock as A, openBlock as M, withCtx as z, resolveDynamicComponent as te, mergeProps as se } from "vue";
4
+ import { toValue as Z, toRaw as q, computed as u, unref as d, isRef as j, shallowRef as _, reactive as D, watch as F, toRefs as W, shallowReactive as H, toRef as S, onScopeDispose as Q, watchEffect as X, triggerRef as Y, ref as k, getCurrentScope as x, onBeforeUnmount as ee, defineComponent as $, renderSlot as C, normalizeProps as B, guardReactiveProps as I, resolveComponent as re, createBlock as A, openBlock as M, withCtx as z, resolveDynamicComponent as te, mergeProps as se } from "vue";
5
5
  import { cloneDeep as ae } from "lodash-es";
6
6
  import "zod";
7
7
  function y(t) {
@@ -20,12 +20,12 @@ function P(t, e) {
20
20
  function ie(t, e, r) {
21
21
  const s = Array.isArray(e) ? e : K(e), i = s.at(-1);
22
22
  if (i) {
23
- const o = s.slice(0, -1).reduce(
24
- (p, v) => p == null ? void 0 : p[v],
23
+ const n = s.slice(0, -1).reduce(
24
+ (h, v) => h == null ? void 0 : h[v],
25
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- f(t)
26
+ d(t)
27
27
  );
28
- o[i] = r;
28
+ n[i] = r;
29
29
  } else {
30
30
  if (!j(t))
31
31
  return;
@@ -34,21 +34,21 @@ function ie(t, e, r) {
34
34
  }
35
35
  const U = (t, e) => u({
36
36
  get() {
37
- return P(f(t), f(e));
37
+ return P(d(t), d(e));
38
38
  },
39
39
  set(r) {
40
- ie(t, f(e), r);
40
+ ie(t, d(e), r);
41
41
  }
42
42
  });
43
43
  function R(t, e) {
44
44
  return !t && !e ? "" : !t && e ? e : !e && t ? t : `${t}.${e}`;
45
45
  }
46
- function oe(t, e) {
46
+ function ne(t, e) {
47
47
  if (!e)
48
48
  return t;
49
49
  const r = `${e}.`, s = Object.fromEntries(
50
50
  Object.entries(t.propertyErrors).filter(([i]) => i.startsWith(r)).map(
51
- ([i, o]) => [i.slice(r.length), o]
51
+ ([i, n]) => [i.slice(r.length), n]
52
52
  )
53
53
  );
54
54
  return {
@@ -57,7 +57,7 @@ function oe(t, e) {
57
57
  propertyErrors: s
58
58
  };
59
59
  }
60
- class ne {
60
+ class oe {
61
61
  constructor(e) {
62
62
  w(this, "rc", 1);
63
63
  this.drop = e;
@@ -84,17 +84,17 @@ function le(t) {
84
84
  },
85
85
  { flush: "sync" }
86
86
  );
87
- const s = u(() => JSON.stringify(r.value) !== JSON.stringify(r.initialValue)), i = (d) => {
88
- r.value = d;
89
- }, o = () => {
87
+ const s = u(() => JSON.stringify(r.value) !== JSON.stringify(r.initialValue)), i = (f) => {
88
+ r.value = f;
89
+ }, n = () => {
90
90
  r.touched = !0;
91
- }, p = () => {
91
+ }, h = () => {
92
92
  }, v = () => {
93
93
  r.value = y(r.initialValue), r.touched = !1, r.errors = [];
94
- }, V = (d) => {
95
- s.value || i(y(d)), r.initialValue = d;
96
- }, n = (d) => {
97
- r.errors = d;
94
+ }, V = (f) => {
95
+ s.value || i(y(f)), r.initialValue = f;
96
+ }, o = (f) => {
97
+ r.errors = f;
98
98
  }, a = () => {
99
99
  r.errors = [];
100
100
  }, c = W(r);
@@ -107,10 +107,10 @@ function le(t) {
107
107
  dirty: s,
108
108
  setData: i,
109
109
  setInitialData: V,
110
- onBlur: o,
111
- onFocus: p,
110
+ onBlur: n,
111
+ onFocus: h,
112
112
  reset: v,
113
- setErrors: n,
113
+ setErrors: o,
114
114
  clearErrors: a
115
115
  };
116
116
  }
@@ -125,19 +125,19 @@ function ce(t) {
125
125
  }
126
126
  function ue(t, e) {
127
127
  const r = /* @__PURE__ */ new Map(), s = H(/* @__PURE__ */ new Map()), i = (a) => {
128
- const c = f(a.path);
128
+ const c = d(a.path);
129
129
  s.set(c, a);
130
- }, o = (a) => {
130
+ }, n = (a) => {
131
131
  s.delete(a);
132
- }, p = (a) => {
132
+ }, h = (a) => {
133
133
  var c;
134
- r.has(a) ? (c = r.get(a)) == null || c.inc() : r.set(a, new ne(() => o(a)));
134
+ r.has(a) ? (c = r.get(a)) == null || c.inc() : r.set(a, new oe(() => n(a)));
135
135
  }, v = (a) => {
136
136
  var c;
137
137
  r.has(a) && ((c = r.get(a)) == null || c.dec());
138
138
  }, V = (a) => {
139
139
  if (!s.has(a)) {
140
- const d = le({
140
+ const f = le({
141
141
  path: a,
142
142
  value: U(S(t, "data"), a),
143
143
  initialValue: ce(
@@ -152,23 +152,23 @@ function ue(t, e) {
152
152
  }
153
153
  })
154
154
  });
155
- i(d);
155
+ i(f);
156
156
  }
157
157
  const c = s.get(a);
158
- return p(a), Q(() => {
158
+ return h(a), Q(() => {
159
159
  v(a);
160
160
  }), c;
161
- }, n = (a) => V(a.path);
161
+ }, o = (a) => V(a.path);
162
162
  return {
163
163
  fields: u(() => [...s.values()]),
164
164
  getField: V,
165
165
  registerField: i,
166
- deregisterField: o,
167
- defineField: n
166
+ deregisterField: n,
167
+ defineField: o
168
168
  };
169
169
  }
170
170
  function de(t) {
171
- const e = u(() => t.fields.value.some((s) => f(s.dirty))), r = u(() => t.fields.value.some((s) => f(s.touched)));
171
+ const e = u(() => t.fields.value.some((s) => d(s.dirty))), r = u(() => t.fields.value.some((s) => d(s.touched)));
172
172
  return {
173
173
  isDirty: e,
174
174
  isTouched: r
@@ -192,9 +192,9 @@ function J(...t) {
192
192
  return fe(i);
193
193
  }, t[0]);
194
194
  }
195
- function pe(...t) {
195
+ function he(...t) {
196
196
  return t.map((r) => Object.keys(r)).flat().reduce((r, s) => {
197
- const i = t.map((o) => o[s]).filter(Boolean);
197
+ const i = t.map((n) => n[s]).filter(Boolean);
198
198
  return {
199
199
  ...r,
200
200
  [s]: J(...i)
@@ -211,7 +211,7 @@ function b(...t) {
211
211
  return t.length === 1 ? e : t.slice(1).reduce(
212
212
  (r, s) => ({
213
213
  general: J(r.general, s.general),
214
- propertyErrors: pe(r.propertyErrors ?? {}, s.propertyErrors ?? {})
214
+ propertyErrors: he(r.propertyErrors ?? {}, s.propertyErrors ?? {})
215
215
  }),
216
216
  e
217
217
  );
@@ -221,12 +221,12 @@ function N(t) {
221
221
  const e = (((s = t.general) == null ? void 0 : s.length) ?? 0) > 0, r = Object.entries(t.propertyErrors).filter(([, i]) => i == null ? void 0 : i.length).length > 0;
222
222
  return e || r;
223
223
  }
224
- function he(t) {
224
+ function pe(t) {
225
225
  const e = t.issues.filter((s) => s.path.length === 0).map((s) => s.message), r = t.issues.filter((s) => s.path.length > 0).reduce((s, i) => {
226
- const o = i.path.join(".");
226
+ const n = i.path.join(".");
227
227
  return {
228
228
  ...s,
229
- [o]: [...s[o] ?? [], i.message]
229
+ [n]: [...s[n] ?? [], i.message]
230
230
  };
231
231
  }, {});
232
232
  return {
@@ -251,7 +251,7 @@ class ve {
251
251
  const r = await this.schema.safeParseAsync(e);
252
252
  if (r.success)
253
253
  return m;
254
- const s = he(r.error);
254
+ const s = pe(r.error);
255
255
  return {
256
256
  isValid: !1,
257
257
  errors: {
@@ -301,72 +301,72 @@ class Ve {
301
301
  }
302
302
  function O(t) {
303
303
  return u(() => new Ve(
304
- f(t.schema),
305
- f(t.validateFn)
304
+ d(t.schema),
305
+ d(t.validateFn)
306
306
  ));
307
307
  }
308
308
  function ye(t, e) {
309
309
  const r = D({
310
310
  validators: k([O(e)]),
311
311
  isValidated: !1,
312
- errors: f(e.errors) ?? m.errors
313
- }), s = (n = m.errors) => {
314
- r.errors = b(f(e.errors) ?? m.errors, n);
312
+ errors: d(e.errors) ?? m.errors
313
+ }), s = (o = m.errors) => {
314
+ r.errors = b(d(e.errors) ?? m.errors, o);
315
315
  };
316
- F(() => f(e.errors), async () => {
316
+ F(() => d(e.errors), async () => {
317
317
  if (r.isValidated) {
318
- const n = await o();
319
- s(n.errors);
318
+ const o = await n();
319
+ s(o.errors);
320
320
  } else
321
321
  s();
322
322
  }, { immediate: !0 }), F(
323
323
  [() => r.validators],
324
- async (n) => {
324
+ async (o) => {
325
325
  if (r.isValidated)
326
- if (n) {
327
- const a = await o();
326
+ if (o) {
327
+ const a = await n();
328
328
  r.errors = a.errors;
329
329
  } else
330
330
  r.errors = m.errors;
331
331
  },
332
332
  { immediate: !0 }
333
- ), F(() => t.data, () => {
334
- r.isValidated && p();
333
+ ), F([() => t.data, () => d(e.schema)], () => {
334
+ r.isValidated && h();
335
335
  });
336
- const i = (n) => {
337
- const a = j(n) ? n : O(n);
336
+ const i = (o) => {
337
+ const a = j(o) ? o : O(o);
338
338
  return r.validators.push(a), x() && ee(() => {
339
339
  r.validators = r.validators.filter(
340
340
  (c) => c !== a
341
341
  );
342
342
  }), a;
343
343
  };
344
- async function o() {
345
- const n = await Promise.all(
346
- r.validators.filter((d) => f(d) !== void 0).map((d) => f(d).validate(t.data))
347
- ), a = n.every((d) => d.isValid);
344
+ async function n() {
345
+ const o = await Promise.all(
346
+ r.validators.filter((f) => d(f) !== void 0).map((f) => d(f).validate(t.data))
347
+ ), a = o.every((f) => f.isValid);
348
348
  let { errors: c } = m;
349
349
  if (!a) {
350
- const d = n.map((E) => E.errors);
351
- c = b(...d);
350
+ const f = o.map((E) => E.errors);
351
+ c = b(...f);
352
352
  }
353
353
  return {
354
354
  errors: c,
355
355
  isValid: a
356
356
  };
357
357
  }
358
- const p = async () => {
359
- const n = await o();
360
- return s(n.errors), r.isValidated = !0, {
361
- isValid: !N(n.errors),
358
+ const h = async () => {
359
+ const o = await n();
360
+ return s(o.errors), r.isValidated = !0, {
361
+ isValid: !N(o.errors),
362
362
  errors: r.errors
363
363
  };
364
364
  }, v = u(() => !N(r.errors)), V = () => {
365
- r.isValidated = !1, r.errors = f(e.errors) ?? m.errors;
365
+ r.isValidated = !1, r.errors = d(e.errors) ?? m.errors;
366
366
  };
367
367
  return {
368
368
  ...W(r),
369
- validateForm: p,
369
+ validateForm: h,
370
370
  defineValidator: i,
371
371
  isValid: v,
372
372
  reset: V
@@ -386,9 +386,9 @@ class ge {
386
386
  errors: {
387
387
  general: s.errors.general || [],
388
388
  propertyErrors: s.errors.propertyErrors ? Object.fromEntries(
389
- Object.entries(s.errors.propertyErrors).map(([i, o]) => [
389
+ Object.entries(s.errors.propertyErrors).map(([i, n]) => [
390
390
  R(this.path, i),
391
- o
391
+ n
392
392
  ])
393
393
  ) : {}
394
394
  }
@@ -396,52 +396,52 @@ class ge {
396
396
  }
397
397
  }
398
398
  function Ee(t, e, r) {
399
- const s = U(t.data, e), i = u(() => P(t.initialData.value, e)), o = (l) => ({
399
+ const s = U(t.data, e), i = u(() => P(t.initialData.value, e)), n = (l) => ({
400
400
  ...l,
401
- path: u(() => f(l.path).replace(e + ".", "")),
402
- setData: (h) => {
403
- l.setData(h);
401
+ path: u(() => d(l.path).replace(e + ".", "")),
402
+ setData: (p) => {
403
+ l.setData(p);
404
404
  }
405
- }), p = (l) => {
406
- const h = R(e, l), g = t.getField(h);
407
- return g ? o(g) : {};
405
+ }), h = (l) => {
406
+ const p = R(e, l), g = t.getField(p);
407
+ return g ? n(g) : {};
408
408
  }, v = (l) => {
409
- const h = R(e, l.path), g = t.defineField({
409
+ const p = R(e, l.path), g = t.defineField({
410
410
  ...l,
411
- path: h
411
+ path: p
412
412
  });
413
- return o(g);
413
+ return n(g);
414
414
  }, V = u(() => t.fields.value.filter((l) => {
415
- const h = l.path.value;
416
- return h.startsWith(e + ".") || h === e;
417
- }).map((l) => o(l))), n = () => t.fields.value.filter((l) => {
418
- const h = l.path.value;
419
- return h.startsWith(e + ".") || h === e;
420
- }), a = u(() => n().some((l) => l.dirty.value)), c = u(() => n().some((l) => l.touched.value)), d = u(() => t.isValid.value), E = u(() => t.isValidated.value), T = u(() => oe(f(t.errors), e));
415
+ const p = l.path.value;
416
+ return p.startsWith(e + ".") || p === e;
417
+ }).map((l) => n(l))), o = () => t.fields.value.filter((l) => {
418
+ const p = l.path.value;
419
+ return p.startsWith(e + ".") || p === e;
420
+ }), a = u(() => o().some((l) => l.dirty.value)), c = u(() => o().some((l) => l.touched.value)), f = u(() => t.isValid.value), E = u(() => t.isValidated.value), T = u(() => ne(d(t.errors), e));
421
421
  return {
422
422
  data: s,
423
423
  fields: V,
424
424
  initialData: i,
425
425
  defineField: v,
426
- getField: p,
426
+ getField: h,
427
427
  isDirty: a,
428
428
  isTouched: c,
429
- isValid: d,
429
+ isValid: f,
430
430
  isValidated: E,
431
431
  errors: T,
432
432
  defineValidator: (l) => {
433
- const h = j(l) ? l : O(l), g = u(
434
- () => new ge(e, f(h))
433
+ const p = j(l) ? l : O(l), g = u(
434
+ () => new ge(e, d(p))
435
435
  );
436
- return t.defineValidator(g), h;
436
+ return t.defineValidator(g), p;
437
437
  },
438
- reset: () => n().forEach((l) => l.reset()),
438
+ reset: () => o().forEach((l) => l.reset()),
439
439
  validateForm: () => t.validateForm(),
440
- getSubForm: (l, h) => {
440
+ getSubForm: (l, p) => {
441
441
  const g = R(e, l);
442
442
  return t.getSubForm(
443
443
  g,
444
- h
444
+ p
445
445
  );
446
446
  }
447
447
  };
@@ -454,24 +454,24 @@ function Oe(t) {
454
454
  F(e, (a) => {
455
455
  s.data = y(a);
456
456
  }, { flush: "sync" });
457
- const i = ye(s, t), o = ue(s, i), p = de(o), v = () => {
458
- r.value = y(e), i.reset(), o.fields.value.forEach(
457
+ const i = ye(s, t), n = ue(s, i), h = de(n), v = () => {
458
+ r.value = y(e), i.reset(), n.fields.value.forEach(
459
459
  (a) => a.reset()
460
460
  );
461
461
  };
462
462
  function V(a, c) {
463
- return Ee(n, a);
463
+ return Ee(o, a);
464
464
  }
465
- const n = {
466
- ...o,
465
+ const o = {
466
+ ...n,
467
467
  ...i,
468
- ...p,
468
+ ...h,
469
469
  reset: v,
470
470
  getSubForm: V,
471
471
  initialData: S(s, "initialData"),
472
472
  data: S(s, "data")
473
473
  };
474
- return n;
474
+ return o;
475
475
  }
476
476
  const je = /* @__PURE__ */ $({
477
477
  __name: "Field",
@@ -486,7 +486,7 @@ const je = /* @__PURE__ */ $({
486
486
  const e = t, r = e.form.defineField({
487
487
  path: e.path
488
488
  }), s = D(r);
489
- return (i, o) => C(i.$slots, "default", B(I(s)));
489
+ return (i, n) => C(i.$slots, "default", B(I(s)));
490
490
  }
491
491
  }), $e = /* @__PURE__ */ $({
492
492
  inheritAttrs: !1,
@@ -504,12 +504,12 @@ const je = /* @__PURE__ */ $({
504
504
  form: e.form,
505
505
  path: e.path
506
506
  }, {
507
- default: z(({ errors: i, data: o, setData: p }) => [
507
+ default: z(({ errors: i, data: n, setData: h }) => [
508
508
  (M(), A(te(e.component), se({ ...e.componentProps, ...e.$attrs }, {
509
- "model-value": o,
509
+ "model-value": n,
510
510
  errors: i,
511
511
  name: e.path,
512
- "onUpdate:modelValue": p
512
+ "onUpdate:modelValue": h
513
513
  }), {
514
514
  default: z(() => [
515
515
  C(e.$slots, "default")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-vue-forms",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -4,7 +4,7 @@ import type { FormDataDefault } from '../types/form'
4
4
  export function useFormData<T extends FormDataDefault>(
5
5
  initialData: Ref<T>,
6
6
  ) {
7
- const data = ref(unref(initialData))
7
+ const data = ref(unref(initialData)) as Ref<T>
8
8
 
9
9
  watch(initialData, (newData) => {
10
10
  if (newData !== data.value) {
@@ -157,7 +157,7 @@ export function useValidation<T extends FormDataDefault>(
157
157
  )
158
158
 
159
159
  // Watch for changes in form data to trigger validation
160
- watch(() => formState.data, () => {
160
+ watch([() => formState.data, () => unref(options.schema)], () => {
161
161
  if (validationState.isValidated) {
162
162
  validateForm()
163
163
  }