@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.
- package/dist/composables/useForm.d.ts +3 -3
- package/dist/index.js +44 -44
- package/dist/utils/submitHandler.d.ts +1 -1
- package/package.json +1 -1
- package/src/composables/useForm.ts +6 -3
- package/src/utils/submitHandler.ts +2 -2
- package/tests/nestedPath.test.ts +2 -1
- package/tests/useField.test.ts +1 -1
- package/tests/useForm.test.ts +1 -3
- package/tests/useValidation.test.ts +6 -2
|
@@ -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
|
|
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:
|
|
15
|
-
}): Form<z.input<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
|
|
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
|
|
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:
|
|
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
|
|
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((
|
|
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
|
|
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 ?? [],
|
|
133
|
+
const o = s.data.value ?? [], p = n.value.findIndex(
|
|
134
134
|
({ id: i }) => i === c
|
|
135
135
|
);
|
|
136
|
-
|
|
137
|
-
o.slice(0,
|
|
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
|
|
141
|
+
const p = s.data.value ?? [];
|
|
142
142
|
return s.setData(
|
|
143
|
-
|
|
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) =>
|
|
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 =
|
|
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
|
-
},
|
|
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:
|
|
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:
|
|
306
|
-
errors:
|
|
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
|
-
},
|
|
335
|
+
}, p = (i) => o(i);
|
|
336
336
|
return {
|
|
337
|
-
fields:
|
|
337
|
+
fields: v(() => [...s.values()]),
|
|
338
338
|
getField: (i) => o({ path: i }),
|
|
339
339
|
registerField: u,
|
|
340
340
|
deregisterField: m,
|
|
341
|
-
defineField:
|
|
341
|
+
defineField: p
|
|
342
342
|
};
|
|
343
343
|
}
|
|
344
344
|
function Pe(r) {
|
|
345
|
-
const t =
|
|
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
|
|
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
|
|
453
|
-
e.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
|
|
463
|
-
return e.validators.push(
|
|
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 !==
|
|
465
|
+
(i) => i !== p
|
|
466
466
|
);
|
|
467
|
-
}),
|
|
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
|
-
),
|
|
473
|
+
), p = o.every((f) => N(f));
|
|
474
474
|
let { errors: i } = V;
|
|
475
|
-
if (!
|
|
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
|
|
491
|
+
const p = await n();
|
|
492
492
|
return a({
|
|
493
|
-
general:
|
|
493
|
+
general: p.errors.general,
|
|
494
494
|
propertyErrors: {
|
|
495
|
-
[o]:
|
|
495
|
+
[o]: p.errors.propertyErrors[o]
|
|
496
496
|
}
|
|
497
497
|
}), {
|
|
498
|
-
data:
|
|
498
|
+
data: p.data,
|
|
499
499
|
errors: e.errors
|
|
500
500
|
};
|
|
501
|
-
}, y =
|
|
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 =
|
|
535
|
+
const s = Q(r.data, t), n = v(() => I(r.initialData.value, t)), u = (d) => ({
|
|
536
536
|
...d,
|
|
537
|
-
path:
|
|
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 =
|
|
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
|
-
}),
|
|
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:
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|
|
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
|
@@ -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<
|
|
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:
|
|
40
|
+
initialData: MaybeRef<InitialData>
|
|
38
41
|
},
|
|
39
|
-
): Form<z.input<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
|
|
17
|
-
event
|
|
16
|
+
return async (event?: SubmitEvent) => {
|
|
17
|
+
event?.preventDefault()
|
|
18
18
|
const strategy = unref(options.validationStrategy)
|
|
19
19
|
|
|
20
20
|
if (strategy === 'none') {
|
package/tests/nestedPath.test.ts
CHANGED
|
@@ -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++) {
|
package/tests/useField.test.ts
CHANGED
package/tests/useForm.test.ts
CHANGED
|
@@ -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:
|
|
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.
|
|
310
|
+
email: z.email(),
|
|
311
311
|
})
|
|
312
312
|
|
|
313
|
-
const initialData = {
|
|
313
|
+
const initialData = {
|
|
314
|
+
name: 'A',
|
|
315
|
+
email: 'invalid-email',
|
|
316
|
+
}
|
|
317
|
+
|
|
314
318
|
const form = useForm({
|
|
315
319
|
initialData,
|
|
316
320
|
schema,
|