@teamnovu/kit-vue-forms 0.1.7 → 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,7 +1,7 @@
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, reactive as D, toRefs as N, shallowReactive as q, toRef as _, onScopeDispose as H, ref as W, watch as w, isRef as k, getCurrentScope as Q, onBeforeUnmount as X, defineComponent as j, renderSlot as O, normalizeProps as z, guardReactiveProps as B, resolveComponent as Y, createBlock as $, openBlock as A, withCtx as C, resolveDynamicComponent as x, mergeProps as ee } from "vue";
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
7
  function g(t) {
@@ -18,19 +18,26 @@ function P(t, e) {
18
18
  );
19
19
  }
20
20
  function te(t, e, r) {
21
- const s = Array.isArray(e) ? e : K(e), o = s.at(-1), n = s.slice(0, -1).reduce(
22
- (p, h) => p[h],
23
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
- t
25
- );
26
- n[o] = o ? r : n;
21
+ const s = Array.isArray(e) ? e : K(e), o = s.at(-1);
22
+ if (o) {
23
+ const n = s.slice(0, -1).reduce(
24
+ (p, h) => p[h],
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ d(t)
27
+ );
28
+ n[o] = r;
29
+ } else {
30
+ if (!O(t))
31
+ return;
32
+ t.value = r;
33
+ }
27
34
  }
28
35
  const U = (t, e) => c({
29
36
  get() {
30
37
  return P(d(t), d(e));
31
38
  },
32
39
  set(r) {
33
- te(d(t), d(e), r);
40
+ te(t, d(e), r);
34
41
  }
35
42
  });
36
43
  function R(t, e) {
@@ -80,7 +87,7 @@ function oe(t) {
80
87
  e.errors = a;
81
88
  }, V = () => {
82
89
  e.errors = [];
83
- }, i = N(e);
90
+ }, i = W(e);
84
91
  return {
85
92
  data: i.value,
86
93
  path: i.path,
@@ -187,7 +194,7 @@ function S(...t) {
187
194
  e
188
195
  );
189
196
  }
190
- function M(t) {
197
+ function N(t) {
191
198
  var s;
192
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;
193
200
  return e || r;
@@ -278,7 +285,7 @@ function b(t) {
278
285
  }
279
286
  function he(t, e) {
280
287
  const r = D({
281
- validators: W([b(e)]),
288
+ validators: k([b(e)]),
282
289
  isValidated: !1,
283
290
  errors: d(e.errors) ?? m.errors
284
291
  }), s = (i = m.errors) => {
@@ -305,7 +312,7 @@ function he(t, e) {
305
312
  r.isValidated && p();
306
313
  });
307
314
  const o = (i) => {
308
- const a = k(i) ? i : b(i);
315
+ const a = O(i) ? i : b(i);
309
316
  return r.validators.push(a), Q() && X(() => {
310
317
  r.validators = r.validators.filter(
311
318
  (u) => u !== a
@@ -329,14 +336,14 @@ function he(t, e) {
329
336
  const p = async () => {
330
337
  const i = await n();
331
338
  return s(i.errors), r.isValidated = !0, {
332
- isValid: !M(i.errors),
339
+ isValid: !N(i.errors),
333
340
  errors: r.errors
334
341
  };
335
- }, h = c(() => !M(r.errors)), V = () => {
342
+ }, h = c(() => !N(r.errors)), V = () => {
336
343
  r.isValidated = !1, r.errors = d(e.errors) ?? m.errors;
337
344
  };
338
345
  return {
339
- ...N(r),
346
+ ...W(r),
340
347
  validateForm: p,
341
348
  defineValidator: o,
342
349
  isValid: h,
@@ -401,7 +408,7 @@ function me(t, e, r) {
401
408
  isValidated: F,
402
409
  errors: J,
403
410
  defineValidator: (l) => {
404
- const f = k(l) ? l : b(l), y = c(
411
+ const f = O(l) ? l : b(l), y = c(
405
412
  () => new ve(e, d(f))
406
413
  );
407
414
  return t.defineValidator(y), f;
@@ -418,7 +425,7 @@ function me(t, e, r) {
418
425
  };
419
426
  }
420
427
  function Pe(t) {
421
- const e = c(() => Object.freeze(g(t.initialData))), r = W(g(e)), s = D({
428
+ const e = c(() => Object.freeze(g(t.initialData))), r = k(g(e)), s = D({
422
429
  initialData: e,
423
430
  data: r
424
431
  });
@@ -444,7 +451,7 @@ function Pe(t) {
444
451
  };
445
452
  return i;
446
453
  }
447
- const _e = /* @__PURE__ */ j({
454
+ const _e = /* @__PURE__ */ $({
448
455
  __name: "Field",
449
456
  props: {
450
457
  form: {},
@@ -457,9 +464,9 @@ const _e = /* @__PURE__ */ j({
457
464
  const e = t, r = e.form.defineField({
458
465
  path: e.path
459
466
  }), s = D(r);
460
- return (o, n) => O(o.$slots, "default", z(B(s)));
467
+ return (o, n) => j(o.$slots, "default", z(B(s)));
461
468
  }
462
- }), Se = /* @__PURE__ */ j({
469
+ }), Se = /* @__PURE__ */ $({
463
470
  inheritAttrs: !1,
464
471
  __name: "FormFieldWrapper",
465
472
  props: {
@@ -471,19 +478,19 @@ const _e = /* @__PURE__ */ j({
471
478
  setup(t) {
472
479
  return (e, r) => {
473
480
  const s = Y("Field");
474
- return A(), $(s, {
481
+ return C(), A(s, {
475
482
  form: e.form,
476
483
  path: e.path
477
484
  }, {
478
- default: C(({ errors: o, data: n, setData: p }) => [
479
- (A(), $(x(e.component), ee({ ...e.componentProps, ...e.$attrs }, {
485
+ default: M(({ errors: o, data: n, setData: p }) => [
486
+ (C(), A(x(e.component), ee({ ...e.componentProps, ...e.$attrs }, {
480
487
  "model-value": n,
481
488
  errors: o,
482
489
  name: e.path,
483
490
  "onUpdate:modelValue": p
484
491
  }), {
485
- default: C(() => [
486
- O(e.$slots, "default")
492
+ default: M(() => [
493
+ j(e.$slots, "default")
487
494
  ]),
488
495
  _: 2
489
496
  }, 1040, ["model-value", "errors", "name", "onUpdate:modelValue"]))
@@ -492,7 +499,7 @@ const _e = /* @__PURE__ */ j({
492
499
  }, 8, ["form", "path"]);
493
500
  };
494
501
  }
495
- }), be = /* @__PURE__ */ j({
502
+ }), be = /* @__PURE__ */ $({
496
503
  __name: "FormPart",
497
504
  props: {
498
505
  form: {},
@@ -500,7 +507,7 @@ const _e = /* @__PURE__ */ j({
500
507
  },
501
508
  setup(t) {
502
509
  const e = t, r = c(() => e.form.getSubForm(e.path));
503
- return (s, o) => O(s.$slots, "default", z(B({ subform: r.value })));
510
+ return (s, o) => j(s.$slots, "default", z(B({ subform: r.value })));
504
511
  }
505
512
  });
506
513
  export {
@@ -3,7 +3,7 @@ import { Paths, PickProps, SplitPath } from '../types/util';
3
3
  import { ErrorBag } from '../types/validation';
4
4
  export declare function splitPath(path: string): string[];
5
5
  export declare function getNestedValue<T, K extends Paths<T>>(obj: T, path: K | SplitPath<K>): PickProps<T, K>;
6
- export declare function setNestedValue<T, K extends Paths<T>>(obj: T, path: K | SplitPath<K>, value: PickProps<T, K>): void;
6
+ export declare function setNestedValue<T, K extends Paths<T>>(obj: MaybeRef<T>, path: K | SplitPath<K>, value: PickProps<T, K>): void;
7
7
  export declare const getLens: <T, K extends Paths<T>>(data: MaybeRef<T>, key: MaybeRef<K | SplitPath<K>>) => WritableComputedRef<PickProps<T, K>, PickProps<T, K>>;
8
8
  type JoinPath<Base extends string, Sub extends string> = `${Base}${Base extends '' ? '' : Sub extends '' ? '' : '.'}${Sub}`;
9
9
  export declare function joinPath<Base extends string, Sub extends string>(basePath: Base, subPath: Sub): JoinPath<Base, Sub>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-vue-forms",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
package/src/utils/path.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { computed, isRef, reactive, shallowRef, triggerRef, unref, watch, type MaybeRef } from 'vue'
1
+ import { computed, isRef, unref, type MaybeRef } from 'vue'
2
2
  import type { Paths, PickProps, SplitPath } from '../types/util'
3
3
  import type { ErrorBag, ValidationErrors } from '../types/validation'
4
4
 
@@ -17,19 +17,29 @@ export function getNestedValue<T, K extends Paths<T>>(obj: T, path: K | SplitPat
17
17
  ) as PickProps<T, K>
18
18
  }
19
19
 
20
- export function setNestedValue<T, K extends Paths<T>>(obj: T, path: K | SplitPath<K>, value: PickProps<T, K>): void {
20
+ export function setNestedValue<T, K extends Paths<T>>(obj: MaybeRef<T>, path: K | SplitPath<K>, value: PickProps<T, K>): void {
21
21
  const keys = Array.isArray(path) ? path : splitPath(path)
22
22
 
23
23
  const lastKey = keys.at(-1)!
24
- const target = keys
25
- .slice(0, -1)
26
- .reduce(
27
- (current, key) => current[key],
28
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
- obj as Record<string, any>,
30
- )
31
24
 
32
- target[lastKey] = lastKey ? value : target
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
+
31
+ obj.value = value
32
+ } else {
33
+ const target = keys
34
+ .slice(0, -1)
35
+ .reduce(
36
+ (current, key) => current[key],
37
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
+ unref(obj) as Record<string, any>,
39
+ )
40
+
41
+ target[lastKey] = value
42
+ }
33
43
  }
34
44
 
35
45
  export const getLens = <T, K extends Paths<T>>(data: MaybeRef<T>, key: MaybeRef<K | SplitPath<K>>) => {
@@ -38,7 +48,7 @@ export const getLens = <T, K extends Paths<T>>(data: MaybeRef<T>, key: MaybeRef<
38
48
  return getNestedValue(unref(data), unref(key))
39
49
  },
40
50
  set(value: PickProps<T, K>) {
41
- setNestedValue(unref(data), unref(key), value)
51
+ setNestedValue(data, unref(key), value)
42
52
  },
43
53
  })
44
54
  }