@teamnovu/kit-vue-forms 0.1.9 → 0.1.10

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.
@@ -19,6 +19,7 @@ declare const _default: <TData extends object, TPath extends Paths<TData>>(__VLS
19
19
  touched: boolean;
20
20
  dirty: boolean;
21
21
  setData: (newData: PickProps<TData, TPath>) => void;
22
+ setInitialData: (newData: PickProps<TData, TPath>) => void;
22
23
  onBlur: () => void;
23
24
  onFocus: () => void;
24
25
  reset: () => void;
package/dist/index.js CHANGED
@@ -1,38 +1,38 @@
1
1
  var T = Object.defineProperty;
2
2
  var G = (t, e, r) => e in t ? T(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
3
  var E = (t, e, r) => G(t, typeof e != "symbol" ? e + "" : e, r);
4
- import { toValue as L, toRaw as Z, computed as c, unref as d, isRef as O, reactive as D, toRefs as W, shallowReactive as q, toRef as _, onScopeDispose as H, ref as k, watch as w, getCurrentScope as Q, onBeforeUnmount as X, defineComponent as $, renderSlot as j, normalizeProps as z, guardReactiveProps as B, resolveComponent as Y, createBlock as A, openBlock as C, withCtx as M, resolveDynamicComponent as x, mergeProps as ee } from "vue";
4
+ import { toValue as L, toRaw as Z, computed as u, unref as d, isRef as O, reactive as D, toRefs as W, shallowReactive as q, toRef as _, onScopeDispose as H, ref as k, watch as w, getCurrentScope as Q, onBeforeUnmount as X, defineComponent as $, renderSlot as j, normalizeProps as z, guardReactiveProps as B, resolveComponent as Y, createBlock as A, openBlock as C, withCtx as M, resolveDynamicComponent as x, mergeProps as ee } from "vue";
5
5
  import { cloneDeep as re } from "lodash-es";
6
6
  import "zod";
7
7
  function g(t) {
8
8
  const e = L(t), r = Z(e);
9
9
  return re(r);
10
10
  }
11
- function K(t) {
11
+ function I(t) {
12
12
  return t === "" ? [] : t.split(/\s*\.\s*/).filter(Boolean);
13
13
  }
14
14
  function P(t, e) {
15
- return (Array.isArray(e) ? e : K(e)).reduce(
15
+ return (Array.isArray(e) ? e : I(e)).reduce(
16
16
  (s, o) => s == null ? void 0 : s[o],
17
17
  t
18
18
  );
19
19
  }
20
20
  function te(t, e, r) {
21
- const s = Array.isArray(e) ? e : K(e), o = s.at(-1);
21
+ const s = Array.isArray(e) ? e : I(e), o = s.at(-1);
22
22
  if (o) {
23
- const n = s.slice(0, -1).reduce(
23
+ const i = s.slice(0, -1).reduce(
24
24
  (p, h) => p[h],
25
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
26
  d(t)
27
27
  );
28
- n[o] = r;
28
+ i[o] = r;
29
29
  } else {
30
30
  if (!O(t))
31
31
  return;
32
32
  t.value = r;
33
33
  }
34
34
  }
35
- const U = (t, e) => c({
35
+ const K = (t, e) => u({
36
36
  get() {
37
37
  return P(d(t), d(e));
38
38
  },
@@ -48,7 +48,7 @@ function se(t, e) {
48
48
  return t;
49
49
  const r = `${e}.`, s = Object.fromEntries(
50
50
  Object.entries(t.propertyErrors).filter(([o]) => o.startsWith(r)).map(
51
- ([o, n]) => [o.slice(r.length), n]
51
+ ([o, i]) => [o.slice(r.length), i]
52
52
  )
53
53
  );
54
54
  return {
@@ -73,55 +73,58 @@ function oe(t) {
73
73
  const e = D({
74
74
  value: t.value,
75
75
  path: t.path,
76
- initialValue: c(() => Object.freeze(g(t.initialValue))),
76
+ initialValue: u(() => Object.freeze(g(t.initialValue))),
77
77
  errors: t.errors,
78
78
  touched: !1
79
- }), r = c(() => JSON.stringify(e.value) !== JSON.stringify(e.initialValue)), s = (a) => {
80
- e.value = a;
79
+ }), r = u(() => JSON.stringify(e.value) !== JSON.stringify(e.initialValue)), s = (c) => {
80
+ e.value = c;
81
81
  }, o = () => {
82
82
  e.touched = !0;
83
- }, n = () => {
83
+ }, i = () => {
84
84
  }, p = () => {
85
85
  e.value = g(e.initialValue), e.touched = !1, e.errors = [];
86
- }, h = (a) => {
87
- e.errors = a;
88
- }, V = () => {
86
+ }, h = (c) => {
87
+ r.value || s(g(c)), e.initialValue = c;
88
+ }, V = (c) => {
89
+ e.errors = c;
90
+ }, n = () => {
89
91
  e.errors = [];
90
- }, i = W(e);
92
+ }, a = W(e);
91
93
  return {
92
- data: i.value,
93
- path: i.path,
94
- initialValue: i.initialValue,
95
- errors: i.errors,
96
- touched: i.touched,
94
+ data: a.value,
95
+ path: a.path,
96
+ initialValue: a.initialValue,
97
+ errors: a.errors,
98
+ touched: a.touched,
97
99
  dirty: r,
98
100
  setData: s,
101
+ setInitialData: h,
99
102
  onBlur: o,
100
- onFocus: n,
103
+ onFocus: i,
101
104
  reset: p,
102
- setErrors: h,
103
- clearErrors: V
105
+ setErrors: V,
106
+ clearErrors: n
104
107
  };
105
108
  }
106
- function ne(t, e) {
109
+ function ie(t, e) {
107
110
  const r = /* @__PURE__ */ new Map(), s = q(/* @__PURE__ */ new Map()), o = (a) => {
108
- const u = d(a.path);
109
- s.set(u, a);
110
- }, n = (a) => {
111
+ const c = d(a.path);
112
+ s.set(c, a);
113
+ }, i = (a) => {
111
114
  s.delete(a);
112
115
  }, p = (a) => {
113
- var u;
114
- r.has(a) ? (u = r.get(a)) == null || u.inc() : r.set(a, new ae(() => n(a)));
116
+ var c;
117
+ r.has(a) ? (c = r.get(a)) == null || c.inc() : r.set(a, new ae(() => i(a)));
115
118
  }, h = (a) => {
116
- var u;
117
- r.has(a) && ((u = r.get(a)) == null || u.dec());
119
+ var c;
120
+ r.has(a) && ((c = r.get(a)) == null || c.dec());
118
121
  }, V = (a) => {
119
122
  if (!s.has(a)) {
120
123
  const v = oe({
121
124
  path: a,
122
- value: U(_(t, "data"), a),
123
- initialValue: c(() => P(t.initialData, a)),
124
- errors: c({
125
+ value: K(_(t, "data"), a),
126
+ initialValue: u(() => P(t.initialData, a)),
127
+ errors: u({
125
128
  get() {
126
129
  return e.errors.value.propertyErrors[a] || [];
127
130
  },
@@ -132,21 +135,21 @@ function ne(t, e) {
132
135
  });
133
136
  o(v);
134
137
  }
135
- const u = s.get(a);
138
+ const c = s.get(a);
136
139
  return p(a), H(() => {
137
140
  h(a);
138
- }), u;
139
- }, i = (a) => V(a.path);
141
+ }), c;
142
+ }, n = (a) => V(a.path);
140
143
  return {
141
- fields: c(() => [...s.values()]),
144
+ fields: u(() => [...s.values()]),
142
145
  getField: V,
143
146
  registerField: o,
144
- deregisterField: n,
145
- defineField: i
147
+ deregisterField: i,
148
+ defineField: n
146
149
  };
147
150
  }
148
- function ie(t) {
149
- const e = c(() => t.fields.value.some((s) => d(s.dirty))), r = c(() => t.fields.value.some((s) => d(s.touched)));
151
+ function ne(t) {
152
+ const e = u(() => t.fields.value.some((s) => d(s.dirty))), r = u(() => t.fields.value.some((s) => d(s.touched)));
150
153
  return {
151
154
  isDirty: e,
152
155
  isTouched: r
@@ -157,7 +160,7 @@ function le(t) {
157
160
  (e, r, s) => s.indexOf(e) === r
158
161
  );
159
162
  }
160
- function I(...t) {
163
+ function U(...t) {
161
164
  return t.slice(1).reduce((e, r) => {
162
165
  if (!e && !r)
163
166
  return;
@@ -172,10 +175,10 @@ function I(...t) {
172
175
  }
173
176
  function ce(...t) {
174
177
  return t.map((r) => Object.keys(r)).flat().reduce((r, s) => {
175
- const o = t.map((n) => n[s]).filter(Boolean);
178
+ const o = t.map((i) => i[s]).filter(Boolean);
176
179
  return {
177
180
  ...r,
178
- [s]: I(...o)
181
+ [s]: U(...o)
179
182
  };
180
183
  }, {});
181
184
  }
@@ -188,7 +191,7 @@ function S(...t) {
188
191
  const e = t[0];
189
192
  return t.length === 1 ? e : t.slice(1).reduce(
190
193
  (r, s) => ({
191
- general: I(r.general, s.general),
194
+ general: U(r.general, s.general),
192
195
  propertyErrors: ce(r.propertyErrors ?? {}, s.propertyErrors ?? {})
193
196
  }),
194
197
  e
@@ -201,10 +204,10 @@ function N(t) {
201
204
  }
202
205
  function ue(t) {
203
206
  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, o) => {
204
- const n = o.path.join(".");
207
+ const i = o.path.join(".");
205
208
  return {
206
209
  ...s,
207
- [n]: [...s[n] ?? [], o.message]
210
+ [i]: [...s[i] ?? [], o.message]
208
211
  };
209
212
  }, {});
210
213
  return {
@@ -278,7 +281,7 @@ class pe {
278
281
  }
279
282
  }
280
283
  function b(t) {
281
- return c(() => new pe(
284
+ return u(() => new pe(
282
285
  d(t.schema),
283
286
  d(t.validateFn)
284
287
  ));
@@ -288,21 +291,21 @@ function he(t, e) {
288
291
  validators: k([b(e)]),
289
292
  isValidated: !1,
290
293
  errors: d(e.errors) ?? m.errors
291
- }), s = (i = m.errors) => {
292
- r.errors = S(d(e.errors) ?? m.errors, i);
294
+ }), s = (n = m.errors) => {
295
+ r.errors = S(d(e.errors) ?? m.errors, n);
293
296
  };
294
297
  w(() => d(e.errors), async () => {
295
298
  if (r.isValidated) {
296
- const i = await n();
297
- s(i.errors);
299
+ const n = await i();
300
+ s(n.errors);
298
301
  } else
299
302
  s();
300
303
  }, { immediate: !0 }), w(
301
304
  [() => r.validators],
302
- async (i) => {
305
+ async (n) => {
303
306
  if (r.isValidated)
304
- if (i) {
305
- const a = await n();
307
+ if (n) {
308
+ const a = await i();
306
309
  r.errors = a.errors;
307
310
  } else
308
311
  r.errors = m.errors;
@@ -311,35 +314,35 @@ function he(t, e) {
311
314
  ), w(() => t.data, () => {
312
315
  r.isValidated && p();
313
316
  });
314
- const o = (i) => {
315
- const a = O(i) ? i : b(i);
317
+ const o = (n) => {
318
+ const a = O(n) ? n : b(n);
316
319
  return r.validators.push(a), Q() && X(() => {
317
320
  r.validators = r.validators.filter(
318
- (u) => u !== a
321
+ (c) => c !== a
319
322
  );
320
323
  }), a;
321
324
  };
322
- async function n() {
323
- const i = await Promise.all(
325
+ async function i() {
326
+ const n = await Promise.all(
324
327
  r.validators.filter((v) => d(v) !== void 0).map((v) => d(v).validate(t.data))
325
- ), a = i.every((v) => v.isValid);
326
- let { errors: u } = m;
328
+ ), a = n.every((v) => v.isValid);
329
+ let { errors: c } = m;
327
330
  if (!a) {
328
- const v = i.map((F) => F.errors);
329
- u = S(...v);
331
+ const v = n.map((F) => F.errors);
332
+ c = S(...v);
330
333
  }
331
334
  return {
332
- errors: u,
335
+ errors: c,
333
336
  isValid: a
334
337
  };
335
338
  }
336
339
  const p = async () => {
337
- const i = await n();
338
- return s(i.errors), r.isValidated = !0, {
339
- isValid: !N(i.errors),
340
+ const n = await i();
341
+ return s(n.errors), r.isValidated = !0, {
342
+ isValid: !N(n.errors),
340
343
  errors: r.errors
341
344
  };
342
- }, h = c(() => !N(r.errors)), V = () => {
345
+ }, h = u(() => !N(r.errors)), V = () => {
343
346
  r.isValidated = !1, r.errors = d(e.errors) ?? m.errors;
344
347
  };
345
348
  return {
@@ -364,9 +367,9 @@ class ve {
364
367
  errors: {
365
368
  general: s.errors.general || [],
366
369
  propertyErrors: s.errors.propertyErrors ? Object.fromEntries(
367
- Object.entries(s.errors.propertyErrors).map(([o, n]) => [
370
+ Object.entries(s.errors.propertyErrors).map(([o, i]) => [
368
371
  R(this.path, o),
369
- n
372
+ i
370
373
  ])
371
374
  ) : {}
372
375
  }
@@ -374,28 +377,28 @@ class ve {
374
377
  }
375
378
  }
376
379
  function me(t, e, r) {
377
- const s = U(t.data, e), o = c(() => P(t.initialData.value, e)), n = (l) => ({
380
+ const s = K(t.data, e), o = u(() => P(t.initialData.value, e)), i = (l) => ({
378
381
  ...l,
379
- path: c(() => d(l.path).replace(e + ".", "")),
382
+ path: u(() => d(l.path).replace(e + ".", "")),
380
383
  setData: (f) => {
381
384
  l.setData(f);
382
385
  }
383
386
  }), p = (l) => {
384
387
  const f = R(e, l), y = t.getField(f);
385
- return y ? n(y) : {};
388
+ return y ? i(y) : {};
386
389
  }, h = (l) => {
387
390
  const f = R(e, l.path), y = t.defineField({
388
391
  ...l,
389
392
  path: f
390
393
  });
391
- return n(y);
392
- }, V = c(() => t.fields.value.filter((l) => {
394
+ return i(y);
395
+ }, V = u(() => t.fields.value.filter((l) => {
393
396
  const f = l.path.value;
394
397
  return f.startsWith(e + ".") || f === e;
395
- }).map((l) => n(l))), i = () => t.fields.value.filter((l) => {
398
+ }).map((l) => i(l))), n = () => t.fields.value.filter((l) => {
396
399
  const f = l.path.value;
397
400
  return f.startsWith(e + ".") || f === e;
398
- }), a = c(() => i().some((l) => l.dirty.value)), u = c(() => i().some((l) => l.touched.value)), v = c(() => t.isValid.value), F = c(() => t.isValidated.value), J = c(() => se(d(t.errors), e));
401
+ }), a = u(() => n().some((l) => l.dirty.value)), c = u(() => n().some((l) => l.touched.value)), v = u(() => t.isValid.value), F = u(() => t.isValidated.value), J = u(() => se(d(t.errors), e));
399
402
  return {
400
403
  data: s,
401
404
  fields: V,
@@ -403,17 +406,17 @@ function me(t, e, r) {
403
406
  defineField: h,
404
407
  getField: p,
405
408
  isDirty: a,
406
- isTouched: u,
409
+ isTouched: c,
407
410
  isValid: v,
408
411
  isValidated: F,
409
412
  errors: J,
410
413
  defineValidator: (l) => {
411
- const f = O(l) ? l : b(l), y = c(
414
+ const f = O(l) ? l : b(l), y = u(
412
415
  () => new ve(e, d(f))
413
416
  );
414
417
  return t.defineValidator(y), f;
415
418
  },
416
- reset: () => i().forEach((l) => l.reset()),
419
+ reset: () => n().forEach((l) => l.reset()),
417
420
  validateForm: () => t.validateForm(),
418
421
  getSubForm: (l, f) => {
419
422
  const y = R(e, l);
@@ -425,23 +428,23 @@ function me(t, e, r) {
425
428
  };
426
429
  }
427
430
  function Pe(t) {
428
- const e = c(() => Object.freeze(g(t.initialData))), r = k(g(e)), s = D({
431
+ const e = u(() => Object.freeze(g(t.initialData))), r = k(g(e)), s = D({
429
432
  initialData: e,
430
433
  data: r
431
434
  });
432
435
  w(e, (a) => {
433
436
  s.data = g(a);
434
437
  });
435
- const o = he(s, t), n = ne(s, o), p = ie(n), h = () => {
436
- r.value = g(e), o.reset(), n.fields.value.forEach(
438
+ const o = he(s, t), i = ie(s, o), p = ne(i), h = () => {
439
+ r.value = g(e), o.reset(), i.fields.value.forEach(
437
440
  (a) => a.reset()
438
441
  );
439
442
  };
440
- function V(a, u) {
441
- return me(i, a);
443
+ function V(a, c) {
444
+ return me(n, a);
442
445
  }
443
- const i = {
444
- ...n,
446
+ const n = {
447
+ ...i,
445
448
  ...o,
446
449
  ...p,
447
450
  reset: h,
@@ -449,7 +452,7 @@ function Pe(t) {
449
452
  initialData: _(s, "initialData"),
450
453
  data: _(s, "data")
451
454
  };
452
- return i;
455
+ return n;
453
456
  }
454
457
  const _e = /* @__PURE__ */ $({
455
458
  __name: "Field",
@@ -464,7 +467,7 @@ const _e = /* @__PURE__ */ $({
464
467
  const e = t, r = e.form.defineField({
465
468
  path: e.path
466
469
  }), s = D(r);
467
- return (o, n) => j(o.$slots, "default", z(B(s)));
470
+ return (o, i) => j(o.$slots, "default", z(B(s)));
468
471
  }
469
472
  }), Se = /* @__PURE__ */ $({
470
473
  inheritAttrs: !1,
@@ -482,9 +485,9 @@ const _e = /* @__PURE__ */ $({
482
485
  form: e.form,
483
486
  path: e.path
484
487
  }, {
485
- default: M(({ errors: o, data: n, setData: p }) => [
488
+ default: M(({ errors: o, data: i, setData: p }) => [
486
489
  (C(), A(x(e.component), ee({ ...e.componentProps, ...e.$attrs }, {
487
- "model-value": n,
490
+ "model-value": i,
488
491
  errors: o,
489
492
  name: e.path,
490
493
  "onUpdate:modelValue": p
@@ -506,7 +509,7 @@ const _e = /* @__PURE__ */ $({
506
509
  path: {}
507
510
  },
508
511
  setup(t) {
509
- const e = t, r = c(() => e.form.getSubForm(e.path));
512
+ const e = t, r = u(() => e.form.getSubForm(e.path));
510
513
  return (s, o) => j(s.$slots, "default", z(B({ subform: r.value })));
511
514
  }
512
515
  });
@@ -13,6 +13,7 @@ export interface FormField<T, P extends string> {
13
13
  touched: Ref<boolean>;
14
14
  dirty: Ref<boolean>;
15
15
  setData: (newData: T) => void;
16
+ setInitialData: (newData: T) => void;
16
17
  onBlur: () => void;
17
18
  onFocus: () => void;
18
19
  reset: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-vue-forms",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -41,6 +41,17 @@ export function useField<T, K extends string>(options: UseFieldOptions<T, K>): F
41
41
  state.errors = []
42
42
  }
43
43
 
44
+ /**
45
+ * Sets the initial data for the field. If the field is not dirty, it also updates the current data.
46
+ * @param newData - The new initial data to set.
47
+ */
48
+ const setInitialData = (newData: T): void => {
49
+ if (!dirty.value) {
50
+ setData(cloneRefValue(newData))
51
+ }
52
+ state.initialValue = newData
53
+ }
54
+
44
55
  const setErrors = (newErrors: ValidationErrorMessage[]): void => {
45
56
  state.errors = newErrors
46
57
  }
@@ -59,6 +70,7 @@ export function useField<T, K extends string>(options: UseFieldOptions<T, K>): F
59
70
  touched: refs.touched as FormField<T, K>['touched'],
60
71
  dirty,
61
72
  setData,
73
+ setInitialData,
62
74
  onBlur,
63
75
  onFocus,
64
76
  reset,
package/src/types/form.ts CHANGED
@@ -15,6 +15,7 @@ export interface FormField<T, P extends string> {
15
15
  touched: Ref<boolean>
16
16
  dirty: Ref<boolean>
17
17
  setData: (newData: T) => void
18
+ setInitialData: (newData: T) => void
18
19
  onBlur: () => void
19
20
  onFocus: () => void
20
21
  reset: () => void