@teamnovu/kit-vue-forms 0.1.25 → 0.1.27
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/useSubmitHandler.d.ts +8 -0
- package/dist/index.js +52 -52
- package/dist/index.mjs +436 -0
- package/dist/types/form.d.ts +1 -1
- package/package.json +10 -10
- package/src/composables/useFieldRegistry.ts +2 -2
- package/src/composables/useForm.ts +1 -1
- package/src/composables/useSubform.ts +1 -1
- package/src/types/form.ts +1 -1
- package/tests/useFieldArray.test.ts +186 -183
- package/tests/useForm.test.ts +5 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Awaitable, MaybeRef } from '@vueuse/core';
|
|
2
|
+
import { Form, FormDataDefault } from '../types/form';
|
|
3
|
+
import { ValidationStrategy } from '../types/validation';
|
|
4
|
+
interface SubmitHandlerOptions {
|
|
5
|
+
validationStrategy?: MaybeRef<ValidationStrategy>;
|
|
6
|
+
}
|
|
7
|
+
export declare function useSubmitHandler<T extends FormDataDefault>(form: Omit<Form<T>, 'submitHandler'>, options: SubmitHandlerOptions): (onSubmit: (data: T) => Awaitable<void>) => (event: SubmitEvent) => Promise<void>;
|
|
8
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var Q = Object.defineProperty;
|
|
2
2
|
var X = (r, t, e) => t in r ? Q(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
3
|
var D = (r, t, e) => X(r, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
-
import { toValue as Y, toRaw as ee, unref as
|
|
4
|
+
import { toValue as Y, toRaw as ee, unref as f, shallowRef as $, watch as R, computed as v, isRef as C, reactive as k, toRefs as K, shallowReactive as te, toRef as j, onScopeDispose as re, triggerRef as ae, ref as J, getCurrentScope as se, onBeforeUnmount as ne, defineComponent as T, renderSlot as b, normalizeProps as x, guardReactiveProps as N, resolveComponent as ie, createBlock as z, openBlock as H, withCtx as U, resolveDynamicComponent as oe, mergeProps as le, createSlots as ce, renderList as ue } from "vue";
|
|
5
5
|
import { cloneDeep as de } from "lodash-es";
|
|
6
6
|
import "zod";
|
|
7
7
|
function g(r) {
|
|
@@ -10,7 +10,7 @@ function g(r) {
|
|
|
10
10
|
}
|
|
11
11
|
function L(r, t) {
|
|
12
12
|
return (e) => async (a) => {
|
|
13
|
-
a.preventDefault(),
|
|
13
|
+
a.preventDefault(), f(t.validationStrategy) !== "none" && await r.validateForm(), r.isValid.value && await e(r.data.value);
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
class fe {
|
|
@@ -39,7 +39,7 @@ class fe {
|
|
|
39
39
|
function he(r, t, e) {
|
|
40
40
|
const a = /* @__PURE__ */ new Set();
|
|
41
41
|
return t.map((s, n) => {
|
|
42
|
-
const d = [...r.get(s) ?? []], m = d.findIndex((
|
|
42
|
+
const d = [...r.get(s) ?? []], m = d.findIndex((h) => !a.has(h)), c = (m === -1 ? [] : d.slice(m))[0];
|
|
43
43
|
if (c)
|
|
44
44
|
return a.add(c), {
|
|
45
45
|
id: c,
|
|
@@ -72,17 +72,17 @@ function G(r, t, e) {
|
|
|
72
72
|
return s.setData([...i, c]), n.value.at(-1);
|
|
73
73
|
},
|
|
74
74
|
remove: (c) => {
|
|
75
|
-
const i = s.data.value ?? [],
|
|
75
|
+
const i = s.data.value ?? [], h = n.value.findIndex(
|
|
76
76
|
({ id: l }) => l === c
|
|
77
77
|
);
|
|
78
|
-
|
|
79
|
-
i.slice(0,
|
|
78
|
+
h !== -1 && s.setData(
|
|
79
|
+
i.slice(0, h).concat(i.slice(h + 1))
|
|
80
80
|
);
|
|
81
81
|
},
|
|
82
82
|
insert: (c, i) => {
|
|
83
|
-
const
|
|
83
|
+
const h = s.data.value ?? [];
|
|
84
84
|
return s.setData(
|
|
85
|
-
|
|
85
|
+
h.slice(0, i).concat([c]).concat(h.slice(i))
|
|
86
86
|
), n.value[i];
|
|
87
87
|
},
|
|
88
88
|
field: s
|
|
@@ -107,7 +107,7 @@ function ve(r, t, e) {
|
|
|
107
107
|
const n = a.slice(0, -1).reduce(
|
|
108
108
|
(d, m) => ((d == null ? void 0 : d[m]) === void 0 && (d[m] = {}), d == null ? void 0 : d[m]),
|
|
109
109
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
110
|
-
|
|
110
|
+
f(r)
|
|
111
111
|
);
|
|
112
112
|
n[s] = e;
|
|
113
113
|
} else {
|
|
@@ -118,10 +118,10 @@ function ve(r, t, e) {
|
|
|
118
118
|
}
|
|
119
119
|
const Z = (r, t) => v({
|
|
120
120
|
get() {
|
|
121
|
-
return S(
|
|
121
|
+
return S(f(r), f(t));
|
|
122
122
|
},
|
|
123
123
|
set(e) {
|
|
124
|
-
ve(r,
|
|
124
|
+
ve(r, f(t), e);
|
|
125
125
|
}
|
|
126
126
|
});
|
|
127
127
|
function I(r, t) {
|
|
@@ -170,7 +170,7 @@ function Fe(r) {
|
|
|
170
170
|
R(
|
|
171
171
|
$(e.initialValue),
|
|
172
172
|
() => {
|
|
173
|
-
a.value = Object.freeze(g(e.initialValue)), s.value !==
|
|
173
|
+
a.value = Object.freeze(g(e.initialValue)), s.value !== f(e.initialValue) && (s.value = g(e.initialValue));
|
|
174
174
|
},
|
|
175
175
|
{ flush: "sync" }
|
|
176
176
|
);
|
|
@@ -184,10 +184,10 @@ function Fe(r) {
|
|
|
184
184
|
(p = e.onFocus) == null || p.call(e);
|
|
185
185
|
}, c = () => {
|
|
186
186
|
const p = s.path.split(".").at(-1) || "";
|
|
187
|
-
|
|
187
|
+
f(e.existsInForm) && !/^\d+$/.test(p) && (s.value = g(s.initialValue)), s.touched = !1, s.errors = [];
|
|
188
188
|
}, i = (p) => {
|
|
189
189
|
n.value || d(g(p)), s.initialValue = p;
|
|
190
|
-
},
|
|
190
|
+
}, h = (p) => {
|
|
191
191
|
s.errors = p;
|
|
192
192
|
}, l = () => {
|
|
193
193
|
s.errors = [];
|
|
@@ -204,7 +204,7 @@ function Fe(r) {
|
|
|
204
204
|
onBlur: m,
|
|
205
205
|
onFocus: F,
|
|
206
206
|
reset: c,
|
|
207
|
-
setErrors:
|
|
207
|
+
setErrors: h,
|
|
208
208
|
clearErrors: l
|
|
209
209
|
};
|
|
210
210
|
}
|
|
@@ -226,11 +226,11 @@ function Ee(r, t, e) {
|
|
|
226
226
|
...ye,
|
|
227
227
|
...e
|
|
228
228
|
}, d = (l) => {
|
|
229
|
-
const o =
|
|
229
|
+
const o = f(l.path);
|
|
230
230
|
s.set(o, l);
|
|
231
231
|
}, m = (l) => {
|
|
232
232
|
var o;
|
|
233
|
-
n
|
|
233
|
+
f((n == null ? void 0 : n.keepValuesOnUnmount) ?? !0) || (o = s.get(l)) == null || o.reset(), s.delete(l);
|
|
234
234
|
}, F = (l) => {
|
|
235
235
|
var o;
|
|
236
236
|
a.has(l) ? (o = a.get(l)) == null || o.inc() : a.set(l, new Ve(() => m(l)));
|
|
@@ -244,7 +244,7 @@ function Ee(r, t, e) {
|
|
|
244
244
|
path: o,
|
|
245
245
|
value: Z(j(r, "data"), o),
|
|
246
246
|
initialValue: ge(r, o),
|
|
247
|
-
existsInForm: v(() => pe(r.data,
|
|
247
|
+
existsInForm: v(() => pe(r.data, f(o))),
|
|
248
248
|
errors: v({
|
|
249
249
|
get() {
|
|
250
250
|
return t.errors.value.propertyErrors[o] || [];
|
|
@@ -256,14 +256,14 @@ function Ee(r, t, e) {
|
|
|
256
256
|
onBlur: async () => {
|
|
257
257
|
var E, P;
|
|
258
258
|
await Promise.all([
|
|
259
|
-
(E = n == null ? void 0 : n.onBlur) == null ? void 0 : E.call(n,
|
|
259
|
+
(E = n == null ? void 0 : n.onBlur) == null ? void 0 : E.call(n, f(o)),
|
|
260
260
|
(P = l.onBlur) == null ? void 0 : P.call(l)
|
|
261
261
|
]);
|
|
262
262
|
},
|
|
263
263
|
onFocus: async () => {
|
|
264
264
|
var E, P;
|
|
265
265
|
await Promise.all([
|
|
266
|
-
(E = n == null ? void 0 : n.onFocus) == null ? void 0 : E.call(n,
|
|
266
|
+
(E = n == null ? void 0 : n.onFocus) == null ? void 0 : E.call(n, f(o)),
|
|
267
267
|
(P = l.onFocus) == null ? void 0 : P.call(l)
|
|
268
268
|
]);
|
|
269
269
|
}
|
|
@@ -274,17 +274,17 @@ function Ee(r, t, e) {
|
|
|
274
274
|
return F(o), re(() => {
|
|
275
275
|
c(o);
|
|
276
276
|
}), p;
|
|
277
|
-
},
|
|
277
|
+
}, h = (l) => i(l);
|
|
278
278
|
return {
|
|
279
279
|
fields: v(() => [...s.values()]),
|
|
280
280
|
getField: (l) => i({ path: l }),
|
|
281
281
|
registerField: d,
|
|
282
282
|
deregisterField: m,
|
|
283
|
-
defineField:
|
|
283
|
+
defineField: h
|
|
284
284
|
};
|
|
285
285
|
}
|
|
286
286
|
function we(r) {
|
|
287
|
-
const t = v(() => r.fields.value.some((a) =>
|
|
287
|
+
const t = v(() => r.fields.value.some((a) => f(a.dirty))), e = v(() => r.fields.value.some((a) => f(a.touched)));
|
|
288
288
|
return {
|
|
289
289
|
isDirty: t,
|
|
290
290
|
isTouched: e
|
|
@@ -417,19 +417,19 @@ class $e {
|
|
|
417
417
|
}
|
|
418
418
|
function O(r) {
|
|
419
419
|
return v(() => new $e(
|
|
420
|
-
|
|
421
|
-
|
|
420
|
+
f(r.schema),
|
|
421
|
+
f(r.validateFn)
|
|
422
422
|
));
|
|
423
423
|
}
|
|
424
424
|
function be(r, t) {
|
|
425
425
|
const e = k({
|
|
426
426
|
validators: J([O(t)]),
|
|
427
427
|
isValidated: !1,
|
|
428
|
-
errors:
|
|
428
|
+
errors: f(t.errors) ?? y.errors
|
|
429
429
|
}), a = (i = y.errors) => {
|
|
430
|
-
e.errors = B(
|
|
430
|
+
e.errors = B(f(t.errors) ?? y.errors, i);
|
|
431
431
|
};
|
|
432
|
-
R(() =>
|
|
432
|
+
R(() => f(t.errors), async () => {
|
|
433
433
|
if (e.isValidated) {
|
|
434
434
|
const i = await n();
|
|
435
435
|
a(i.errors);
|
|
@@ -440,35 +440,35 @@ function be(r, t) {
|
|
|
440
440
|
async (i) => {
|
|
441
441
|
if (e.isValidated)
|
|
442
442
|
if (i) {
|
|
443
|
-
const
|
|
444
|
-
e.errors =
|
|
443
|
+
const h = await n();
|
|
444
|
+
e.errors = h.errors;
|
|
445
445
|
} else
|
|
446
446
|
e.errors = y.errors;
|
|
447
447
|
},
|
|
448
448
|
{ immediate: !0 }
|
|
449
|
-
), R([() => r.data, () =>
|
|
449
|
+
), R([() => r.data, () => f(t.schema)], () => {
|
|
450
450
|
e.isValidated && d();
|
|
451
451
|
});
|
|
452
452
|
const s = (i) => {
|
|
453
|
-
const
|
|
454
|
-
return e.validators.push(
|
|
453
|
+
const h = C(i) ? i : O(i);
|
|
454
|
+
return e.validators.push(h), se() && ne(() => {
|
|
455
455
|
e.validators = e.validators.filter(
|
|
456
|
-
(l) => l !==
|
|
456
|
+
(l) => l !== h
|
|
457
457
|
);
|
|
458
|
-
}),
|
|
458
|
+
}), h;
|
|
459
459
|
};
|
|
460
460
|
async function n() {
|
|
461
461
|
const i = await Promise.all(
|
|
462
|
-
e.validators.filter((o) =>
|
|
463
|
-
),
|
|
462
|
+
e.validators.filter((o) => f(o) !== void 0).map((o) => f(o).validate(r.data))
|
|
463
|
+
), h = i.every((o) => o.isValid);
|
|
464
464
|
let { errors: l } = y;
|
|
465
|
-
if (!
|
|
465
|
+
if (!h) {
|
|
466
466
|
const o = i.map((p) => p.errors);
|
|
467
467
|
l = B(...o);
|
|
468
468
|
}
|
|
469
469
|
return {
|
|
470
470
|
errors: l,
|
|
471
|
-
isValid:
|
|
471
|
+
isValid: h
|
|
472
472
|
};
|
|
473
473
|
}
|
|
474
474
|
const d = async () => {
|
|
@@ -478,18 +478,18 @@ function be(r, t) {
|
|
|
478
478
|
errors: e.errors
|
|
479
479
|
};
|
|
480
480
|
}, m = async (i) => {
|
|
481
|
-
const
|
|
481
|
+
const h = await n();
|
|
482
482
|
return a({
|
|
483
|
-
general:
|
|
483
|
+
general: h.errors.general,
|
|
484
484
|
propertyErrors: {
|
|
485
|
-
[i]:
|
|
485
|
+
[i]: h.errors.propertyErrors[i]
|
|
486
486
|
}
|
|
487
487
|
}), {
|
|
488
|
-
isValid: !_(
|
|
488
|
+
isValid: !_(h.errors),
|
|
489
489
|
errors: e.errors
|
|
490
490
|
};
|
|
491
491
|
}, F = v(() => !_(e.errors)), c = () => {
|
|
492
|
-
e.isValidated = !1, e.errors =
|
|
492
|
+
e.isValidated = !1, e.errors = f(t.errors) ?? y.errors;
|
|
493
493
|
};
|
|
494
494
|
return {
|
|
495
495
|
...K(e),
|
|
@@ -525,7 +525,7 @@ class ke {
|
|
|
525
525
|
function Ae(r, t, e, a) {
|
|
526
526
|
const s = Z(r.data, t), n = v(() => S(r.initialData.value, t)), d = (u) => ({
|
|
527
527
|
...u,
|
|
528
|
-
path: v(() =>
|
|
528
|
+
path: v(() => f(u.path).replace(t + ".", "")),
|
|
529
529
|
setData: (V) => {
|
|
530
530
|
u.setData(V);
|
|
531
531
|
}
|
|
@@ -544,20 +544,20 @@ function Ae(r, t, e, a) {
|
|
|
544
544
|
}).map((u) => d(u))), i = () => r.fields.value.filter((u) => {
|
|
545
545
|
const V = u.path.value;
|
|
546
546
|
return V.startsWith(t + ".") || V === t;
|
|
547
|
-
}),
|
|
547
|
+
}), h = v(() => i().some((u) => u.dirty.value)), l = v(() => i().some((u) => u.touched.value)), o = v(() => r.isValid.value), p = v(() => r.isValidated.value), A = v(() => me(f(r.errors), t)), M = {
|
|
548
548
|
data: s,
|
|
549
549
|
fields: c,
|
|
550
550
|
initialData: n,
|
|
551
551
|
defineField: F,
|
|
552
552
|
getField: m,
|
|
553
|
-
isDirty:
|
|
553
|
+
isDirty: h,
|
|
554
554
|
isTouched: l,
|
|
555
555
|
isValid: o,
|
|
556
556
|
isValidated: p,
|
|
557
557
|
errors: A,
|
|
558
558
|
defineValidator: (u) => {
|
|
559
559
|
const V = C(u) ? u : O(u), w = v(
|
|
560
|
-
() => new ke(t,
|
|
560
|
+
() => new ke(t, f(V))
|
|
561
561
|
);
|
|
562
562
|
return r.defineValidator(w), V;
|
|
563
563
|
},
|
|
@@ -571,7 +571,7 @@ function Ae(r, t, e, a) {
|
|
|
571
571
|
);
|
|
572
572
|
},
|
|
573
573
|
submitHandler: (u) => L(M, e ?? {})(u),
|
|
574
|
-
|
|
574
|
+
getFieldArray: (u, V) => G(M, u, V)
|
|
575
575
|
};
|
|
576
576
|
return M;
|
|
577
577
|
}
|
|
@@ -590,14 +590,14 @@ function Ce(r) {
|
|
|
590
590
|
const s = be(a, r), n = Ee(a, s, {
|
|
591
591
|
keepValuesOnUnmount: r.keepValuesOnUnmount,
|
|
592
592
|
onBlur: async (c) => {
|
|
593
|
-
|
|
593
|
+
f(r.validationStrategy) === "onTouch" && s.validateField(c);
|
|
594
594
|
}
|
|
595
595
|
}), d = we(n), m = () => {
|
|
596
596
|
e.value = g(t), s.reset();
|
|
597
597
|
for (const c of n.fields.value)
|
|
598
598
|
c.reset();
|
|
599
599
|
};
|
|
600
|
-
|
|
600
|
+
f(r.validationStrategy) === "onFormOpen" && s.validateForm();
|
|
601
601
|
const F = {
|
|
602
602
|
...n,
|
|
603
603
|
...s,
|
|
@@ -607,7 +607,7 @@ function Ce(r) {
|
|
|
607
607
|
data: j(a, "data"),
|
|
608
608
|
submitHandler: (c) => L(F, r)(c),
|
|
609
609
|
getSubForm: (c, i) => Ae(F, c, r),
|
|
610
|
-
|
|
610
|
+
getFieldArray: (c, i) => G(F, c, i)
|
|
611
611
|
};
|
|
612
612
|
return F;
|
|
613
613
|
}
|