@teamnovu/kit-vue-forms 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,57 +1,58 @@
1
1
  var T = Object.defineProperty;
2
- var G = (r, e, t) => e in r ? T(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
- var E = (r, e, t) => G(r, typeof e != "symbol" ? e + "" : e, t);
4
- import { toValue as L, toRaw as Z, computed as c, unref as d, isRef as D, reactive as P, toRefs as W, shallowReactive as q, toRef as S, 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";
2
+ var G = (t, e, r) => e in t ? T(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
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";
5
5
  import { cloneDeep as re } from "lodash-es";
6
6
  import "zod";
7
- function g(r) {
8
- const e = L(r), t = Z(e);
9
- return re(t);
7
+ function g(t) {
8
+ const e = L(t), r = Z(e);
9
+ return re(r);
10
10
  }
11
- function K(r) {
12
- return r === "" ? [] : r.split(/\s*\.\s*/).filter(Boolean);
11
+ function K(t) {
12
+ return t === "" ? [] : t.split(/\s*\.\s*/).filter(Boolean);
13
13
  }
14
- function _(r, e) {
14
+ function P(t, e) {
15
15
  return (Array.isArray(e) ? e : K(e)).reduce(
16
16
  (s, o) => s == null ? void 0 : s[o],
17
- r
17
+ t
18
18
  );
19
19
  }
20
- function te(r, e, t) {
20
+ function te(t, e, r) {
21
21
  const s = Array.isArray(e) ? e : K(e), o = s.at(-1);
22
- if (!o && D(r))
23
- r.value = t;
24
- else if (D(r)) {
22
+ if (o) {
25
23
  const n = s.slice(0, -1).reduce(
26
24
  (p, h) => p[h],
27
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
- d(r)
26
+ d(t)
29
27
  );
30
- n[o] = t;
31
- } else
32
- return;
28
+ n[o] = r;
29
+ } else {
30
+ if (!O(t))
31
+ return;
32
+ t.value = r;
33
+ }
33
34
  }
34
- const U = (r, e) => c({
35
+ const U = (t, e) => c({
35
36
  get() {
36
- return _(d(r), d(e));
37
+ return P(d(t), d(e));
37
38
  },
38
- set(t) {
39
- te(r, d(e), t);
39
+ set(r) {
40
+ te(t, d(e), r);
40
41
  }
41
42
  });
42
- function R(r, e) {
43
- return !r && !e ? "" : !r && e ? e : !e && r ? r : `${r}.${e}`;
43
+ function R(t, e) {
44
+ return !t && !e ? "" : !t && e ? e : !e && t ? t : `${t}.${e}`;
44
45
  }
45
- function se(r, e) {
46
+ function se(t, e) {
46
47
  if (!e)
47
- return r;
48
- const t = `${e}.`, s = Object.fromEntries(
49
- Object.entries(r.propertyErrors).filter(([o]) => o.startsWith(t)).map(
50
- ([o, n]) => [o.slice(t.length), n]
48
+ return t;
49
+ const r = `${e}.`, s = Object.fromEntries(
50
+ Object.entries(t.propertyErrors).filter(([o]) => o.startsWith(r)).map(
51
+ ([o, n]) => [o.slice(r.length), n]
51
52
  )
52
53
  );
53
54
  return {
54
- general: r.general,
55
+ general: t.general,
55
56
  // Keep general errors
56
57
  propertyErrors: s
57
58
  };
@@ -68,14 +69,14 @@ class ae {
68
69
  this.rc > 0 && (this.rc -= 1, this.rc === 0 && this.drop && this.drop());
69
70
  }
70
71
  }
71
- function oe(r) {
72
- const e = P({
73
- value: r.value,
74
- path: r.path,
75
- initialValue: c(() => Object.freeze(g(r.initialValue))),
76
- errors: r.errors,
72
+ function oe(t) {
73
+ const e = D({
74
+ value: t.value,
75
+ path: t.path,
76
+ initialValue: c(() => Object.freeze(g(t.initialValue))),
77
+ errors: t.errors,
77
78
  touched: !1
78
- }), t = c(() => JSON.stringify(e.value) !== JSON.stringify(e.initialValue)), s = (a) => {
79
+ }), r = c(() => JSON.stringify(e.value) !== JSON.stringify(e.initialValue)), s = (a) => {
79
80
  e.value = a;
80
81
  }, o = () => {
81
82
  e.touched = !0;
@@ -93,7 +94,7 @@ function oe(r) {
93
94
  initialValue: i.initialValue,
94
95
  errors: i.errors,
95
96
  touched: i.touched,
96
- dirty: t,
97
+ dirty: r,
97
98
  setData: s,
98
99
  onBlur: o,
99
100
  onFocus: n,
@@ -102,24 +103,24 @@ function oe(r) {
102
103
  clearErrors: V
103
104
  };
104
105
  }
105
- function ne(r, e) {
106
- const t = /* @__PURE__ */ new Map(), s = q(/* @__PURE__ */ new Map()), o = (a) => {
106
+ function ne(t, e) {
107
+ const r = /* @__PURE__ */ new Map(), s = q(/* @__PURE__ */ new Map()), o = (a) => {
107
108
  const u = d(a.path);
108
109
  s.set(u, a);
109
110
  }, n = (a) => {
110
111
  s.delete(a);
111
112
  }, p = (a) => {
112
113
  var u;
113
- t.has(a) ? (u = t.get(a)) == null || u.inc() : t.set(a, new ae(() => n(a)));
114
+ r.has(a) ? (u = r.get(a)) == null || u.inc() : r.set(a, new ae(() => n(a)));
114
115
  }, h = (a) => {
115
116
  var u;
116
- t.has(a) && ((u = t.get(a)) == null || u.dec());
117
+ r.has(a) && ((u = r.get(a)) == null || u.dec());
117
118
  }, V = (a) => {
118
119
  if (!s.has(a)) {
119
120
  const v = oe({
120
121
  path: a,
121
- value: U(S(r, "data"), a),
122
- initialValue: c(() => _(r.initialData, a)),
122
+ value: U(_(t, "data"), a),
123
+ initialValue: c(() => P(t.initialData, a)),
123
124
  errors: c({
124
125
  get() {
125
126
  return e.errors.value.propertyErrors[a] || [];
@@ -144,62 +145,62 @@ function ne(r, e) {
144
145
  defineField: i
145
146
  };
146
147
  }
147
- function ie(r) {
148
- const e = c(() => r.fields.value.some((s) => d(s.dirty))), t = c(() => r.fields.value.some((s) => d(s.touched)));
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)));
149
150
  return {
150
151
  isDirty: e,
151
- isTouched: t
152
+ isTouched: r
152
153
  };
153
154
  }
154
- function le(r) {
155
- return r.filter(
156
- (e, t, s) => s.indexOf(e) === t
155
+ function le(t) {
156
+ return t.filter(
157
+ (e, r, s) => s.indexOf(e) === r
157
158
  );
158
159
  }
159
- function I(...r) {
160
- return r.slice(1).reduce((e, t) => {
161
- if (!e && !t)
160
+ function I(...t) {
161
+ return t.slice(1).reduce((e, r) => {
162
+ if (!e && !r)
162
163
  return;
163
- const s = ((t == null ? void 0 : t.length) ?? 0) > 0;
164
- if (!e && ((t == null ? void 0 : t.length) ?? 0) > 0)
165
- return t;
164
+ const s = ((r == null ? void 0 : r.length) ?? 0) > 0;
165
+ if (!e && ((r == null ? void 0 : r.length) ?? 0) > 0)
166
+ return r;
166
167
  if (!s)
167
168
  return e;
168
- const o = (e ?? []).concat(t);
169
+ const o = (e ?? []).concat(r);
169
170
  return le(o);
170
- }, r[0]);
171
+ }, t[0]);
171
172
  }
172
- function ce(...r) {
173
- return r.map((t) => Object.keys(t)).flat().reduce((t, s) => {
174
- const o = r.map((n) => n[s]).filter(Boolean);
173
+ function ce(...t) {
174
+ return t.map((r) => Object.keys(r)).flat().reduce((r, s) => {
175
+ const o = t.map((n) => n[s]).filter(Boolean);
175
176
  return {
176
- ...t,
177
+ ...r,
177
178
  [s]: I(...o)
178
179
  };
179
180
  }, {});
180
181
  }
181
- function O(...r) {
182
- if (!r.length)
182
+ function S(...t) {
183
+ if (!t.length)
183
184
  return {
184
185
  general: [],
185
186
  propertyErrors: {}
186
187
  };
187
- const e = r[0];
188
- return r.length === 1 ? e : r.slice(1).reduce(
189
- (t, s) => ({
190
- general: I(t.general, s.general),
191
- propertyErrors: ce(t.propertyErrors ?? {}, s.propertyErrors ?? {})
188
+ const e = t[0];
189
+ return t.length === 1 ? e : t.slice(1).reduce(
190
+ (r, s) => ({
191
+ general: I(r.general, s.general),
192
+ propertyErrors: ce(r.propertyErrors ?? {}, s.propertyErrors ?? {})
192
193
  }),
193
194
  e
194
195
  );
195
196
  }
196
- function N(r) {
197
+ function N(t) {
197
198
  var s;
198
- const e = (((s = r.general) == null ? void 0 : s.length) ?? 0) > 0, t = Object.entries(r.propertyErrors).filter(([, o]) => o == null ? void 0 : o.length).length > 0;
199
- return e || t;
199
+ const e = (((s = t.general) == null ? void 0 : s.length) ?? 0) > 0, r = Object.entries(t.propertyErrors).filter(([, o]) => o == null ? void 0 : o.length).length > 0;
200
+ return e || r;
200
201
  }
201
- function ue(r) {
202
- const e = r.issues.filter((s) => s.path.length === 0).map((s) => s.message), t = r.issues.filter((s) => s.path.length > 0).reduce((s, o) => {
202
+ function ue(t) {
203
+ 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) => {
203
204
  const n = o.path.join(".");
204
205
  return {
205
206
  ...s,
@@ -208,7 +209,7 @@ function ue(r) {
208
209
  }, {});
209
210
  return {
210
211
  general: e,
211
- propertyErrors: t
212
+ propertyErrors: r
212
213
  };
213
214
  }
214
215
  const m = {
@@ -225,10 +226,10 @@ class de {
225
226
  async validate(e) {
226
227
  if (!this.schema)
227
228
  return m;
228
- const t = await this.schema.safeParseAsync(e);
229
- if (t.success)
229
+ const r = await this.schema.safeParseAsync(e);
230
+ if (r.success)
230
231
  return m;
231
- const s = ue(t.error);
232
+ const s = ue(r.error);
232
233
  return {
233
234
  isValid: !1,
234
235
  errors: {
@@ -246,13 +247,13 @@ class fe {
246
247
  if (!this.validateFn)
247
248
  return m;
248
249
  try {
249
- const t = await this.validateFn(e);
250
- return t.isValid ? m : t;
251
- } catch (t) {
250
+ const r = await this.validateFn(e);
251
+ return r.isValid ? m : r;
252
+ } catch (r) {
252
253
  return {
253
254
  isValid: !1,
254
255
  errors: {
255
- general: [t.message || "Validation error"],
256
+ general: [r.message || "Validation error"],
256
257
  propertyErrors: {}
257
258
  }
258
259
  };
@@ -260,72 +261,72 @@ class fe {
260
261
  }
261
262
  }
262
263
  class pe {
263
- constructor(e, t) {
264
+ constructor(e, r) {
264
265
  E(this, "schemaValidator");
265
266
  E(this, "functionValidator");
266
- this.schema = e, this.validateFn = t, this.schemaValidator = new de(this.schema), this.functionValidator = new fe(this.validateFn);
267
+ this.schema = e, this.validateFn = r, this.schemaValidator = new de(this.schema), this.functionValidator = new fe(this.validateFn);
267
268
  }
268
269
  async validate(e) {
269
- const [t, s] = await Promise.all([
270
+ const [r, s] = await Promise.all([
270
271
  this.schemaValidator.validate(e),
271
272
  this.functionValidator.validate(e)
272
273
  ]);
273
274
  return {
274
- isValid: t.isValid && s.isValid,
275
- errors: O(t.errors, s.errors)
275
+ isValid: r.isValid && s.isValid,
276
+ errors: S(r.errors, s.errors)
276
277
  };
277
278
  }
278
279
  }
279
- function b(r) {
280
+ function b(t) {
280
281
  return c(() => new pe(
281
- d(r.schema),
282
- d(r.validateFn)
282
+ d(t.schema),
283
+ d(t.validateFn)
283
284
  ));
284
285
  }
285
- function he(r, e) {
286
- const t = P({
286
+ function he(t, e) {
287
+ const r = D({
287
288
  validators: k([b(e)]),
288
289
  isValidated: !1,
289
290
  errors: d(e.errors) ?? m.errors
290
291
  }), s = (i = m.errors) => {
291
- t.errors = O(d(e.errors) ?? m.errors, i);
292
+ r.errors = S(d(e.errors) ?? m.errors, i);
292
293
  };
293
294
  w(() => d(e.errors), async () => {
294
- if (t.isValidated) {
295
+ if (r.isValidated) {
295
296
  const i = await n();
296
297
  s(i.errors);
297
298
  } else
298
299
  s();
299
300
  }, { immediate: !0 }), w(
300
- [() => t.validators],
301
+ [() => r.validators],
301
302
  async (i) => {
302
- if (t.isValidated)
303
+ if (r.isValidated)
303
304
  if (i) {
304
305
  const a = await n();
305
- t.errors = a.errors;
306
+ r.errors = a.errors;
306
307
  } else
307
- t.errors = m.errors;
308
+ r.errors = m.errors;
308
309
  },
309
310
  { immediate: !0 }
310
- ), w(() => r.data, () => {
311
- t.isValidated && p();
311
+ ), w(() => t.data, () => {
312
+ r.isValidated && p();
312
313
  });
313
314
  const o = (i) => {
314
- const a = D(i) ? i : b(i);
315
- return t.validators.push(a), Q() && X(() => {
316
- t.validators = t.validators.filter(
315
+ const a = O(i) ? i : b(i);
316
+ return r.validators.push(a), Q() && X(() => {
317
+ r.validators = r.validators.filter(
317
318
  (u) => u !== a
318
319
  );
319
320
  }), a;
320
321
  };
321
322
  async function n() {
322
323
  const i = await Promise.all(
323
- t.validators.filter((v) => d(v) !== void 0).map((v) => d(v).validate(r.data))
324
+ r.validators.filter((v) => d(v) !== void 0).map((v) => d(v).validate(t.data))
324
325
  ), a = i.every((v) => v.isValid);
325
326
  let { errors: u } = m;
326
327
  if (!a) {
327
328
  const v = i.map((F) => F.errors);
328
- u = O(...v);
329
+ u = S(...v);
329
330
  }
330
331
  return {
331
332
  errors: u,
@@ -334,15 +335,15 @@ function he(r, e) {
334
335
  }
335
336
  const p = async () => {
336
337
  const i = await n();
337
- return s(i.errors), t.isValidated = !0, {
338
+ return s(i.errors), r.isValidated = !0, {
338
339
  isValid: !N(i.errors),
339
- errors: t.errors
340
+ errors: r.errors
340
341
  };
341
- }, h = c(() => !N(t.errors)), V = () => {
342
- t.isValidated = !1, t.errors = d(e.errors) ?? m.errors;
342
+ }, h = c(() => !N(r.errors)), V = () => {
343
+ r.isValidated = !1, r.errors = d(e.errors) ?? m.errors;
343
344
  };
344
345
  return {
345
- ...W(t),
346
+ ...W(r),
346
347
  validateForm: p,
347
348
  defineValidator: o,
348
349
  isValid: h,
@@ -350,14 +351,14 @@ function he(r, e) {
350
351
  };
351
352
  }
352
353
  class ve {
353
- constructor(e, t) {
354
- this.path = e, this.validator = t;
354
+ constructor(e, r) {
355
+ this.path = e, this.validator = r;
355
356
  }
356
357
  async validate(e) {
357
- const t = _(e, this.path);
358
+ const r = P(e, this.path);
358
359
  if (!this.validator)
359
360
  return m;
360
- const s = await this.validator.validate(t);
361
+ const s = await this.validator.validate(r);
361
362
  return {
362
363
  isValid: s.isValid,
363
364
  errors: {
@@ -372,29 +373,29 @@ class ve {
372
373
  };
373
374
  }
374
375
  }
375
- function me(r, e, t) {
376
- const s = U(r.data, e), o = c(() => _(r.initialData.value, e)), n = (l) => ({
376
+ function me(t, e, r) {
377
+ const s = U(t.data, e), o = c(() => P(t.initialData.value, e)), n = (l) => ({
377
378
  ...l,
378
379
  path: c(() => d(l.path).replace(e + ".", "")),
379
380
  setData: (f) => {
380
381
  l.setData(f);
381
382
  }
382
383
  }), p = (l) => {
383
- const f = R(e, l), y = r.getField(f);
384
+ const f = R(e, l), y = t.getField(f);
384
385
  return y ? n(y) : {};
385
386
  }, h = (l) => {
386
- const f = R(e, l.path), y = r.defineField({
387
+ const f = R(e, l.path), y = t.defineField({
387
388
  ...l,
388
389
  path: f
389
390
  });
390
391
  return n(y);
391
- }, V = c(() => r.fields.value.filter((l) => {
392
+ }, V = c(() => t.fields.value.filter((l) => {
392
393
  const f = l.path.value;
393
394
  return f.startsWith(e + ".") || f === e;
394
- }).map((l) => n(l))), i = () => r.fields.value.filter((l) => {
395
+ }).map((l) => n(l))), i = () => t.fields.value.filter((l) => {
395
396
  const f = l.path.value;
396
397
  return f.startsWith(e + ".") || f === e;
397
- }), a = c(() => i().some((l) => l.dirty.value)), u = c(() => i().some((l) => l.touched.value)), v = c(() => r.isValid.value), F = c(() => r.isValidated.value), J = c(() => se(d(r.errors), 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));
398
399
  return {
399
400
  data: s,
400
401
  fields: V,
@@ -407,32 +408,32 @@ function me(r, e, t) {
407
408
  isValidated: F,
408
409
  errors: J,
409
410
  defineValidator: (l) => {
410
- const f = D(l) ? l : b(l), y = c(
411
+ const f = O(l) ? l : b(l), y = c(
411
412
  () => new ve(e, d(f))
412
413
  );
413
- return r.defineValidator(y), f;
414
+ return t.defineValidator(y), f;
414
415
  },
415
416
  reset: () => i().forEach((l) => l.reset()),
416
- validateForm: () => r.validateForm(),
417
+ validateForm: () => t.validateForm(),
417
418
  getSubForm: (l, f) => {
418
419
  const y = R(e, l);
419
- return r.getSubForm(
420
+ return t.getSubForm(
420
421
  y,
421
422
  f
422
423
  );
423
424
  }
424
425
  };
425
426
  }
426
- function Pe(r) {
427
- const e = c(() => Object.freeze(g(r.initialData))), t = k(g(e)), s = P({
427
+ function Pe(t) {
428
+ const e = c(() => Object.freeze(g(t.initialData))), r = k(g(e)), s = D({
428
429
  initialData: e,
429
- data: t
430
+ data: r
430
431
  });
431
432
  w(e, (a) => {
432
433
  s.data = g(a);
433
434
  });
434
- const o = he(s, r), n = ne(s, o), p = ie(n), h = () => {
435
- t.value = g(e), o.reset(), n.fields.value.forEach(
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(
436
437
  (a) => a.reset()
437
438
  );
438
439
  };
@@ -445,8 +446,8 @@ function Pe(r) {
445
446
  ...p,
446
447
  reset: h,
447
448
  getSubForm: V,
448
- initialData: S(s, "initialData"),
449
- data: S(s, "data")
449
+ initialData: _(s, "initialData"),
450
+ data: _(s, "data")
450
451
  };
451
452
  return i;
452
453
  }
@@ -459,10 +460,10 @@ const _e = /* @__PURE__ */ $({
459
460
  path: {},
460
461
  errors: {}
461
462
  },
462
- setup(r) {
463
- const e = r, t = e.form.defineField({
463
+ setup(t) {
464
+ const e = t, r = e.form.defineField({
464
465
  path: e.path
465
- }), s = P(t);
466
+ }), s = D(r);
466
467
  return (o, n) => j(o.$slots, "default", z(B(s)));
467
468
  }
468
469
  }), Se = /* @__PURE__ */ $({
@@ -474,8 +475,8 @@ const _e = /* @__PURE__ */ $({
474
475
  form: {},
475
476
  path: {}
476
477
  },
477
- setup(r) {
478
- return (e, t) => {
478
+ setup(t) {
479
+ return (e, r) => {
479
480
  const s = Y("Field");
480
481
  return C(), A(s, {
481
482
  form: e.form,
@@ -498,20 +499,20 @@ const _e = /* @__PURE__ */ $({
498
499
  }, 8, ["form", "path"]);
499
500
  };
500
501
  }
501
- }), Oe = /* @__PURE__ */ $({
502
+ }), be = /* @__PURE__ */ $({
502
503
  __name: "FormPart",
503
504
  props: {
504
505
  form: {},
505
506
  path: {}
506
507
  },
507
- setup(r) {
508
- const e = r, t = c(() => e.form.getSubForm(e.path));
509
- return (s, o) => j(s.$slots, "default", z(B({ subform: t.value })));
508
+ setup(t) {
509
+ const e = t, r = c(() => e.form.getSubForm(e.path));
510
+ return (s, o) => j(s.$slots, "default", z(B({ subform: r.value })));
510
511
  }
511
512
  });
512
513
  export {
513
514
  _e as Field,
514
515
  Se as FormFieldWrapper,
515
- Oe as FormPart,
516
+ be as FormPart,
516
517
  Pe as useForm
517
518
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-vue-forms",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
package/src/utils/path.ts CHANGED
@@ -22,11 +22,13 @@ export function setNestedValue<T, K extends Paths<T>>(obj: MaybeRef<T>, path: K
22
22
 
23
23
  const lastKey = keys.at(-1)!
24
24
 
25
- if (!lastKey && isRef(obj)) {
25
+ if (!lastKey) {
26
+ if (!isRef(obj)) {
27
+ // We cannot do anything here as we have nothing we can assign to
28
+ return
29
+ }
30
+
26
31
  obj.value = value
27
- } else if (!isRef(obj)) {
28
- // We cannot do anything here as we have nothing we can assign to
29
- return
30
32
  } else {
31
33
  const target = keys
32
34
  .slice(0, -1)