@teamnovu/kit-vue-forms 0.2.0 → 0.2.2

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.
@@ -9,10 +9,10 @@ export interface UseFormOptions<T extends FormDataDefault, TOut = T> extends Val
9
9
  validationStrategy?: MaybeRef<ValidationStrategy>;
10
10
  keepValuesOnUnmount?: MaybeRef<boolean>;
11
11
  }
12
- export declare function useForm<TSchema extends z.ZodType<FormDataDefault, FormDataDefault>>(options: Omit<UseFormOptions<z.input<TSchema>, z.output<TSchema>>, 'initialData'> & {
12
+ export declare function useForm<TSchema extends z.ZodType<FormDataDefault, FormDataDefault>, InitialData extends DeepPartial<z.input<TSchema>>>(options: Omit<UseFormOptions<z.input<TSchema>, z.output<TSchema>>, 'initialData'> & {
13
13
  schema: MaybeRef<TSchema>;
14
- initialData: MaybeRefOrGetter<DeepPartial<z.input<TSchema>>>;
15
- }): Form<z.input<TSchema>, z.output<TSchema>>;
14
+ initialData: MaybeRef<InitialData>;
15
+ }): Form<z.input<TSchema> & InitialData, z.output<TSchema> & InitialData>;
16
16
  export declare function useForm<T extends FormDataDefault>(options: Omit<UseFormOptions<T, T>, 'schema'> & {
17
17
  schema?: undefined;
18
18
  }): Form<T, T>;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  var X = Object.defineProperty;
2
2
  var Y = (r, t, e) => t in r ? X(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
3
  var R = (r, t, e) => Y(r, typeof t != "symbol" ? t + "" : t, e);
4
- import { toValue as ee, toRaw as te, unref as h, shallowRef as b, watch as D, computed as p, isRef as O, reactive as A, toRefs as K, shallowReactive as re, toRef as _, onScopeDispose as ae, triggerRef as se, ref as L, getCurrentScope as ne, onBeforeUnmount as oe, defineComponent as C, renderSlot as k, normalizeProps as T, guardReactiveProps as x, resolveComponent as ie, createBlock as z, openBlock as H, withCtx as U, resolveDynamicComponent as le, mergeProps as ce, createSlots as ue, renderList as de } from "vue";
4
+ import { toValue as ee, toRaw as te, unref as h, shallowRef as b, watch as D, computed as v, isRef as O, reactive as A, toRefs as K, shallowReactive as re, toRef as _, onScopeDispose as ae, triggerRef as se, ref as L, getCurrentScope as ne, onBeforeUnmount as oe, defineComponent as C, renderSlot as k, normalizeProps as T, guardReactiveProps as x, resolveComponent as ie, createBlock as z, openBlock as H, withCtx as U, resolveDynamicComponent as le, mergeProps as ce, createSlots as ue, renderList as de } from "vue";
5
5
  import { cloneDeep as fe } from "lodash-es";
6
6
  import "zod";
7
7
  function g(r) {
@@ -26,7 +26,7 @@ function J(...r) {
26
26
  return he(s);
27
27
  }, r[0]);
28
28
  }
29
- function ve(...r) {
29
+ function pe(...r) {
30
30
  return r.map((e) => Object.keys(e)).flat().reduce((e, a) => {
31
31
  const s = r.map((n) => n[a]).filter(Boolean);
32
32
  return {
@@ -45,7 +45,7 @@ function j(...r) {
45
45
  return r.length === 1 ? t : r.slice(1).reduce(
46
46
  (e, a) => ({
47
47
  general: J(e.general, a.general),
48
- propertyErrors: ve(e.propertyErrors ?? {}, a.propertyErrors ?? {})
48
+ propertyErrors: pe(e.propertyErrors ?? {}, a.propertyErrors ?? {})
49
49
  }),
50
50
  t
51
51
  );
@@ -60,7 +60,7 @@ function N(r) {
60
60
  }
61
61
  function Z(r, t) {
62
62
  return (e) => async (a) => {
63
- if (a.preventDefault(), h(t.validationStrategy) === "none") {
63
+ if (a == null || a.preventDefault(), h(t.validationStrategy) === "none") {
64
64
  await e(r.data.value);
65
65
  return;
66
66
  }
@@ -71,7 +71,7 @@ function Z(r, t) {
71
71
  await e(u);
72
72
  };
73
73
  }
74
- class pe {
74
+ class ve {
75
75
  constructor(t = (e) => e) {
76
76
  R(this, "weakMap", /* @__PURE__ */ new WeakMap());
77
77
  R(this, "map", /* @__PURE__ */ new Map());
@@ -97,7 +97,7 @@ class pe {
97
97
  function me(r, t, e) {
98
98
  const a = /* @__PURE__ */ new Set();
99
99
  return t.map((s, n) => {
100
- const u = [...r.get(s) ?? []], m = u.findIndex((v) => !a.has(v)), c = (m === -1 ? [] : u.slice(m))[0];
100
+ const u = [...r.get(s) ?? []], m = u.findIndex((p) => !a.has(p)), c = (m === -1 ? [] : u.slice(m))[0];
101
101
  if (c)
102
102
  return a.add(c), {
103
103
  id: c,
@@ -113,7 +113,7 @@ function me(r, t, e) {
113
113
  });
114
114
  }
115
115
  function q(r, t, e) {
116
- const a = new pe(e == null ? void 0 : e.hashFn), s = r.getField(t), n = b([]);
116
+ const a = new ve(e == null ? void 0 : e.hashFn), s = r.getField(t), n = b([]);
117
117
  return D(
118
118
  s.data,
119
119
  (c) => {
@@ -130,17 +130,17 @@ function q(r, t, e) {
130
130
  return s.setData([...o, c]), n.value.at(-1);
131
131
  },
132
132
  remove: (c) => {
133
- const o = s.data.value ?? [], v = n.value.findIndex(
133
+ const o = s.data.value ?? [], p = n.value.findIndex(
134
134
  ({ id: i }) => i === c
135
135
  );
136
- v !== -1 && s.setData(
137
- o.slice(0, v).concat(o.slice(v + 1))
136
+ p !== -1 && s.setData(
137
+ o.slice(0, p).concat(o.slice(p + 1))
138
138
  );
139
139
  },
140
140
  insert: (c, o) => {
141
- const v = s.data.value ?? [];
141
+ const p = s.data.value ?? [];
142
142
  return s.setData(
143
- v.slice(0, o).concat([c]).concat(v.slice(o))
143
+ p.slice(0, o).concat([c]).concat(p.slice(o))
144
144
  ), n.value[o];
145
145
  },
146
146
  field: s
@@ -174,7 +174,7 @@ function ye(r, t, e) {
174
174
  r.value = e;
175
175
  }
176
176
  }
177
- const Q = (r, t) => p({
177
+ const Q = (r, t) => v({
178
178
  get() {
179
179
  return I(h(r), h(t));
180
180
  },
@@ -232,7 +232,7 @@ function we(r) {
232
232
  },
233
233
  { flush: "sync" }
234
234
  );
235
- const n = p(() => JSON.stringify(s.value) !== JSON.stringify(s.initialValue)), u = (f) => {
235
+ const n = v(() => JSON.stringify(s.value) !== JSON.stringify(s.initialValue)), u = (f) => {
236
236
  s.value = f;
237
237
  }, m = () => {
238
238
  var f;
@@ -245,7 +245,7 @@ function we(r) {
245
245
  h(e.existsInForm) && !/^\d+$/.test(f) && (s.value = g(s.initialValue)), s.touched = !1, s.errors = [];
246
246
  }, o = (f) => {
247
247
  n.value || u(g(f)), s.initialValue = f;
248
- }, v = (f) => {
248
+ }, p = (f) => {
249
249
  s.errors = f;
250
250
  }, i = () => {
251
251
  s.errors = [];
@@ -262,7 +262,7 @@ function we(r) {
262
262
  onBlur: m,
263
263
  onFocus: y,
264
264
  reset: c,
265
- setErrors: v,
265
+ setErrors: p,
266
266
  clearErrors: i
267
267
  };
268
268
  }
@@ -302,8 +302,8 @@ function De(r, t, e) {
302
302
  path: l,
303
303
  value: Q(_(r, "data"), l),
304
304
  initialValue: Re(r, l),
305
- existsInForm: p(() => Fe(r.data, h(l))),
306
- errors: p({
305
+ existsInForm: v(() => Fe(r.data, h(l))),
306
+ errors: v({
307
307
  get() {
308
308
  return t.errors.value.propertyErrors[l] || [];
309
309
  },
@@ -332,17 +332,17 @@ function De(r, t, e) {
332
332
  return y(l), ae(() => {
333
333
  c(l);
334
334
  }), f;
335
- }, v = (i) => o(i);
335
+ }, p = (i) => o(i);
336
336
  return {
337
- fields: p(() => [...s.values()]),
337
+ fields: v(() => [...s.values()]),
338
338
  getField: (i) => o({ path: i }),
339
339
  registerField: u,
340
340
  deregisterField: m,
341
- defineField: v
341
+ defineField: p
342
342
  };
343
343
  }
344
344
  function Pe(r) {
345
- const t = p(() => r.fields.value.some((a) => h(a.dirty))), e = p(() => r.fields.value.some((a) => h(a.touched)));
345
+ const t = v(() => r.fields.value.some((a) => h(a.dirty))), e = v(() => r.fields.value.some((a) => h(a.touched)));
346
346
  return {
347
347
  isDirty: t,
348
348
  isTouched: e
@@ -425,7 +425,7 @@ class be {
425
425
  }
426
426
  }
427
427
  function B(r) {
428
- return p(() => new be(
428
+ return v(() => new be(
429
429
  h(r.schema),
430
430
  h(r.validateFn)
431
431
  ));
@@ -449,8 +449,8 @@ function ke(r, t) {
449
449
  async (o) => {
450
450
  if (e.isValidated)
451
451
  if (o) {
452
- const v = await n();
453
- e.errors = v.errors;
452
+ const p = await n();
453
+ e.errors = p.errors;
454
454
  } else
455
455
  e.errors = V.errors;
456
456
  },
@@ -459,20 +459,20 @@ function ke(r, t) {
459
459
  e.isValidated && u();
460
460
  });
461
461
  const s = (o) => {
462
- const v = O(o) ? o : B(o);
463
- return e.validators.push(v), ne() && oe(() => {
462
+ const p = O(o) ? o : B(o);
463
+ return e.validators.push(p), ne() && oe(() => {
464
464
  e.validators = e.validators.filter(
465
- (i) => i !== v
465
+ (i) => i !== p
466
466
  );
467
- }), v;
467
+ }), p;
468
468
  };
469
469
  async function n() {
470
470
  var l;
471
471
  const o = await Promise.all(
472
472
  e.validators.filter((f) => h(f) !== void 0).map((f) => h(f).validate(r.data))
473
- ), v = o.every((f) => N(f));
473
+ ), p = o.every((f) => N(f));
474
474
  let { errors: i } = V;
475
- if (!v) {
475
+ if (!p) {
476
476
  const f = o.map((S) => S.errors);
477
477
  i = j(...f);
478
478
  }
@@ -488,17 +488,17 @@ function ke(r, t) {
488
488
  errors: e.errors
489
489
  };
490
490
  }, m = async (o) => {
491
- const v = await n();
491
+ const p = await n();
492
492
  return a({
493
- general: v.errors.general,
493
+ general: p.errors.general,
494
494
  propertyErrors: {
495
- [o]: v.errors.propertyErrors[o]
495
+ [o]: p.errors.propertyErrors[o]
496
496
  }
497
497
  }), {
498
- data: v.data,
498
+ data: p.data,
499
499
  errors: e.errors
500
500
  };
501
- }, y = p(() => !G(e.errors)), c = () => {
501
+ }, y = v(() => !G(e.errors)), c = () => {
502
502
  e.isValidated = !1, e.errors = h(t.errors) ?? V.errors;
503
503
  };
504
504
  return {
@@ -532,9 +532,9 @@ class Ae {
532
532
  }
533
533
  }
534
534
  function Me(r, t, e, a) {
535
- const s = Q(r.data, t), n = p(() => I(r.initialData.value, t)), u = (d) => ({
535
+ const s = Q(r.data, t), n = v(() => I(r.initialData.value, t)), u = (d) => ({
536
536
  ...d,
537
- path: p(() => h(d.path).replace(t + ".", "")),
537
+ path: v(() => h(d.path).replace(t + ".", "")),
538
538
  setData: (F) => {
539
539
  d.setData(F);
540
540
  }
@@ -547,25 +547,25 @@ function Me(r, t, e, a) {
547
547
  path: F
548
548
  });
549
549
  return u(E);
550
- }, c = p(() => r.fields.value.filter((d) => {
550
+ }, c = v(() => r.fields.value.filter((d) => {
551
551
  const F = d.path.value;
552
552
  return F.startsWith(t + ".") || F === t;
553
553
  }).map((d) => u(d))), o = () => r.fields.value.filter((d) => {
554
554
  const F = d.path.value;
555
555
  return F.startsWith(t + ".") || F === t;
556
- }), v = p(() => o().some((d) => d.dirty.value)), i = p(() => o().some((d) => d.touched.value)), l = p(() => r.isValid.value), f = p(() => r.isValidated.value), S = p(() => Ve(h(r.errors), t)), M = {
556
+ }), p = v(() => o().some((d) => d.dirty.value)), i = v(() => o().some((d) => d.touched.value)), l = v(() => r.isValid.value), f = v(() => r.isValidated.value), S = v(() => Ve(h(r.errors), t)), M = {
557
557
  data: s,
558
558
  fields: c,
559
559
  initialData: n,
560
560
  defineField: y,
561
561
  getField: m,
562
- isDirty: v,
562
+ isDirty: p,
563
563
  isTouched: i,
564
564
  isValid: l,
565
565
  isValidated: f,
566
566
  errors: S,
567
567
  defineValidator: (d) => {
568
- const F = O(d) ? d : B(d), E = p(
568
+ const F = O(d) ? d : B(d), E = v(
569
569
  () => new Ae(t, h(F))
570
570
  );
571
571
  return r.defineValidator(E), F;
@@ -585,7 +585,7 @@ function Me(r, t, e, a) {
585
585
  return M;
586
586
  }
587
587
  function Te(r) {
588
- const t = p(() => g(r.initialData)), e = L(g(t)), a = A({
588
+ const t = v(() => g(r.initialData)), e = L(g(t)), a = A({
589
589
  initialData: t,
590
590
  data: e
591
591
  });
@@ -686,7 +686,7 @@ const xe = /* @__PURE__ */ C({
686
686
  path: {}
687
687
  },
688
688
  setup(r) {
689
- const t = r, e = p(() => t.form.getSubForm(t.path));
689
+ const t = r, e = v(() => t.form.getSubForm(t.path));
690
690
  return (a, s) => k(a.$slots, "default", T(x({ subform: e.value })));
691
691
  }
692
692
  });
@@ -4,5 +4,5 @@ import { ValidationStrategy } from '../types/validation';
4
4
  interface SubmitHandlerOptions {
5
5
  validationStrategy?: MaybeRef<ValidationStrategy>;
6
6
  }
7
- export declare function makeSubmitHandler<T extends FormDataDefault, TOut = T>(form: Form<T, TOut>, options: SubmitHandlerOptions): (onSubmit: (data: TOut) => Awaitable<void>) => (event: SubmitEvent) => Promise<void>;
7
+ export declare function makeSubmitHandler<T extends FormDataDefault, TOut = T>(form: Form<T, TOut>, options: SubmitHandlerOptions): (onSubmit: (data: TOut) => Awaitable<void>) => (event?: SubmitEvent) => Promise<void>;
8
8
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-vue-forms",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -31,12 +31,15 @@ export interface UseFormOptions<T extends FormDataDefault, TOut = T>
31
31
  /* eslint-disable no-redeclare */
32
32
  // Overload: with schema - infer types from schema
33
33
  // initialData can be partial, but provided fields must match schema types
34
- export function useForm<TSchema extends z.ZodType<FormDataDefault, FormDataDefault>>(
34
+ export function useForm<
35
+ TSchema extends z.ZodType<FormDataDefault, FormDataDefault>,
36
+ InitialData extends DeepPartial<z.input<TSchema>>,
37
+ >(
35
38
  options: Omit<UseFormOptions<z.input<TSchema>, z.output<TSchema>>, 'initialData'> & {
36
39
  schema: MaybeRef<TSchema>
37
- initialData: MaybeRefOrGetter<DeepPartial<z.input<TSchema>>>
40
+ initialData: MaybeRef<InitialData>
38
41
  },
39
- ): Form<z.input<TSchema>, z.output<TSchema>>
42
+ ): Form<z.input<TSchema> & InitialData, z.output<TSchema> & InitialData>
40
43
 
41
44
  // Overload: without schema - infer types from initialData
42
45
  export function useForm<T extends FormDataDefault>(
@@ -13,8 +13,8 @@ export function makeSubmitHandler<T extends FormDataDefault, TOut = T>(
13
13
  options: SubmitHandlerOptions,
14
14
  ) {
15
15
  return (onSubmit: (data: TOut) => Awaitable<void>) => {
16
- return async (event: SubmitEvent) => {
17
- event.preventDefault()
16
+ return async (event?: SubmitEvent) => {
17
+ event?.preventDefault()
18
18
  const strategy = unref(options.validationStrategy)
19
19
 
20
20
  if (strategy === 'none') {
@@ -3,6 +3,7 @@ import { nextTick } from 'vue'
3
3
  import { z } from 'zod'
4
4
  import { useForm } from '../src/composables/useForm'
5
5
  import { isValidResult } from '../src/utils/validation'
6
+ import { FormField } from '../src'
6
7
 
7
8
  describe('Nested Path Handling', () => {
8
9
  interface TestFormData {
@@ -556,7 +557,7 @@ describe('Nested Path Handling', () => {
556
557
  it('should handle large numbers of nested fields', () => {
557
558
  const form = useForm({ initialData })
558
559
 
559
- const fields = []
560
+ const fields: FormField<string, string>[] = []
560
561
 
561
562
  // Create many nested fields
562
563
  for (let i = 0; i < 100; i++) {
@@ -65,7 +65,7 @@ describe('useField', () => {
65
65
  it('should handle errors', () => {
66
66
  const field = useField({
67
67
  path: 'name',
68
- errors: ['Required field'],
68
+ errors: ref(['Required field']),
69
69
  })
70
70
 
71
71
  expect(field.errors.value).toEqual(['Required field'])
@@ -4,8 +4,6 @@ import { z } from 'zod'
4
4
  import { useForm } from '../src/composables/useForm'
5
5
  import { isValidResult } from '../src/utils/validation'
6
6
 
7
- const scope = effectScope()
8
-
9
7
  describe('useForm', () => {
10
8
  it('should initialize form with initial data', () => {
11
9
  const initialData = {
@@ -481,7 +479,7 @@ describe('useForm', () => {
481
479
  { timeout: 500 },
482
480
  async () => {
483
481
  const initialData = ref({
484
- name: null as null | number,
482
+ name: undefined as undefined | number,
485
483
  })
486
484
 
487
485
  const schema = z.object({
@@ -307,10 +307,14 @@ describe('useValidation', () => {
307
307
  it('should not validate other fields than the blurred one', async () => {
308
308
  const schema = z.object({
309
309
  name: z.string().min(2),
310
- email: z.string().email(),
310
+ email: z.email(),
311
311
  })
312
312
 
313
- const initialData = { name: 'A', email: 'invalid-email' }
313
+ const initialData = {
314
+ name: 'A',
315
+ email: 'invalid-email',
316
+ }
317
+
314
318
  const form = useForm({
315
319
  initialData,
316
320
  schema,