@teamnovu/kit-vue-forms 0.0.20 → 0.0.21
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/components/Field.vue.d.ts +7 -7
- package/dist/index.js +68 -68
- package/package.json +1 -1
- package/src/components/Field.vue +1 -4
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Form } from '../types/form.ts';
|
|
2
2
|
import { Paths, PickProps } from '../types/util.ts';
|
|
3
3
|
import { UseFieldOptions } from '../composables/useField.ts';
|
|
4
|
-
import { VNodeProps, AllowedComponentProps, ComponentCustomProps, PublicProps, ShallowUnwrapRef, VNode } from 'vue';
|
|
4
|
+
import { VNodeProps, AllowedComponentProps, ComponentCustomProps, PublicProps, ShallowUnwrapRef, Ref, VNode } from 'vue';
|
|
5
5
|
import { ValidationErrors, ErrorMessage } from '../index.js';
|
|
6
6
|
export interface FieldProps<TData extends object, TPath extends string> extends UseFieldOptions<PickProps<TData, TPath>, TPath> {
|
|
7
7
|
form: Form<TData>;
|
|
@@ -12,12 +12,12 @@ declare const _default: <TData extends object, TPath extends Paths<TData>>(__VLS
|
|
|
12
12
|
attrs: any;
|
|
13
13
|
slots: {
|
|
14
14
|
default?(_: {
|
|
15
|
-
data: PickProps<TData, TPath
|
|
16
|
-
path: TPath
|
|
17
|
-
initialValue: PickProps<TData, TPath
|
|
18
|
-
errors: ValidationErrors
|
|
19
|
-
touched: boolean
|
|
20
|
-
dirty: boolean
|
|
15
|
+
data: Ref<PickProps<TData, TPath>, PickProps<TData, TPath>>;
|
|
16
|
+
path: Ref<TPath, TPath>;
|
|
17
|
+
initialValue: Readonly< Ref<PickProps<TData, TPath>, PickProps<TData, TPath>>>;
|
|
18
|
+
errors: Ref<ValidationErrors>;
|
|
19
|
+
touched: Ref<boolean>;
|
|
20
|
+
dirty: Ref<boolean>;
|
|
21
21
|
setData: (newData: PickProps<TData, TPath>) => void;
|
|
22
22
|
onBlur: () => void;
|
|
23
23
|
onFocus: () => void;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var T = Object.defineProperty;
|
|
2
2
|
var G = (e, r, t) => r in e ? T(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t;
|
|
3
|
-
var
|
|
4
|
-
import { toValue as L, toRaw as Z, computed as u, unref as d, reactive as
|
|
3
|
+
var P = (e, r, t) => G(e, typeof r != "symbol" ? r + "" : r, t);
|
|
4
|
+
import { toValue as L, toRaw as Z, computed as u, unref as d, reactive as b, toRefs as N, toRef as _, ref as W, watch as y, isRef as z, getCurrentScope as q, onBeforeUnmount as H, defineComponent as S, renderSlot as j, normalizeProps as M, guardReactiveProps as B, resolveComponent as Q, createBlock as O, openBlock as $, withCtx as A, resolveDynamicComponent as X, mergeProps as Y } from "vue";
|
|
5
5
|
import { cloneDeep as x } from "lodash-es";
|
|
6
6
|
import "zod";
|
|
7
7
|
function g(e) {
|
|
@@ -11,7 +11,7 @@ function g(e) {
|
|
|
11
11
|
function K(e) {
|
|
12
12
|
return e === "" ? [] : e.split(/\s*\.\s*/).filter(Boolean);
|
|
13
13
|
}
|
|
14
|
-
function
|
|
14
|
+
function E(e, r) {
|
|
15
15
|
return (Array.isArray(r) ? r : K(r)).reduce(
|
|
16
16
|
(s, a) => s == null ? void 0 : s[a],
|
|
17
17
|
e
|
|
@@ -21,16 +21,16 @@ function rr(e, r, t) {
|
|
|
21
21
|
const s = Array.isArray(r) ? r : K(r);
|
|
22
22
|
if (s.length === 0)
|
|
23
23
|
throw new Error("Path cannot be empty");
|
|
24
|
-
const a = s.at(-1),
|
|
24
|
+
const a = s.at(-1), n = s.slice(0, -1).reduce(
|
|
25
25
|
(h, c) => h[c],
|
|
26
26
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
27
|
e
|
|
28
28
|
);
|
|
29
|
-
|
|
29
|
+
n[a] = t;
|
|
30
30
|
}
|
|
31
31
|
const U = (e, r) => u({
|
|
32
32
|
get() {
|
|
33
|
-
return
|
|
33
|
+
return E(d(e), d(r));
|
|
34
34
|
},
|
|
35
35
|
set(t) {
|
|
36
36
|
rr(d(e), d(r), t);
|
|
@@ -44,7 +44,7 @@ function er(e, r) {
|
|
|
44
44
|
return e;
|
|
45
45
|
const t = `${r}.`, s = Object.fromEntries(
|
|
46
46
|
Object.entries(e.propertyErrors).filter(([a]) => a.startsWith(t)).map(
|
|
47
|
-
([a,
|
|
47
|
+
([a, n]) => [a.slice(t.length), n]
|
|
48
48
|
)
|
|
49
49
|
);
|
|
50
50
|
return {
|
|
@@ -54,7 +54,7 @@ function er(e, r) {
|
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
56
|
function tr(e) {
|
|
57
|
-
const r =
|
|
57
|
+
const r = b({
|
|
58
58
|
value: e.value,
|
|
59
59
|
path: e.path,
|
|
60
60
|
initialValue: u(() => Object.freeze(g(e.initialValue))),
|
|
@@ -64,12 +64,12 @@ function tr(e) {
|
|
|
64
64
|
r.value = f;
|
|
65
65
|
}, a = () => {
|
|
66
66
|
r.touched = !0;
|
|
67
|
-
},
|
|
67
|
+
}, n = () => {
|
|
68
68
|
}, h = () => {
|
|
69
69
|
r.value = g(r.initialValue), r.touched = !1, r.errors = [];
|
|
70
70
|
}, c = (f) => {
|
|
71
71
|
r.errors = f;
|
|
72
|
-
},
|
|
72
|
+
}, o = () => {
|
|
73
73
|
r.errors = [];
|
|
74
74
|
}, l = N(r);
|
|
75
75
|
return {
|
|
@@ -81,22 +81,22 @@ function tr(e) {
|
|
|
81
81
|
dirty: t,
|
|
82
82
|
setData: s,
|
|
83
83
|
onBlur: a,
|
|
84
|
-
onFocus:
|
|
84
|
+
onFocus: n,
|
|
85
85
|
reset: h,
|
|
86
86
|
setErrors: c,
|
|
87
|
-
clearErrors:
|
|
87
|
+
clearErrors: o
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
function sr(e, r) {
|
|
91
91
|
const t = {}, s = (c) => {
|
|
92
|
-
const
|
|
93
|
-
t[
|
|
92
|
+
const o = d(c.path);
|
|
93
|
+
t[o] = c;
|
|
94
94
|
}, a = (c) => {
|
|
95
95
|
if (!t[c]) {
|
|
96
|
-
const
|
|
96
|
+
const o = tr({
|
|
97
97
|
path: c,
|
|
98
98
|
value: U(_(e, "data"), c),
|
|
99
|
-
initialValue: u(() =>
|
|
99
|
+
initialValue: u(() => E(e.initialData, c)),
|
|
100
100
|
errors: u({
|
|
101
101
|
get() {
|
|
102
102
|
return r.errors.value.propertyErrors[c] || [];
|
|
@@ -106,7 +106,7 @@ function sr(e, r) {
|
|
|
106
106
|
}
|
|
107
107
|
})
|
|
108
108
|
});
|
|
109
|
-
return s(
|
|
109
|
+
return s(o), o;
|
|
110
110
|
}
|
|
111
111
|
return t[c];
|
|
112
112
|
};
|
|
@@ -145,14 +145,14 @@ function k(...e) {
|
|
|
145
145
|
}
|
|
146
146
|
function nr(...e) {
|
|
147
147
|
return e.map((t) => Object.keys(t)).flat().reduce((t, s) => {
|
|
148
|
-
const a = e.map((
|
|
148
|
+
const a = e.map((n) => n[s]).filter(Boolean);
|
|
149
149
|
return {
|
|
150
150
|
...t,
|
|
151
151
|
[s]: k(...a)
|
|
152
152
|
};
|
|
153
153
|
}, {});
|
|
154
154
|
}
|
|
155
|
-
function
|
|
155
|
+
function D(...e) {
|
|
156
156
|
if (!e.length)
|
|
157
157
|
return {
|
|
158
158
|
general: [],
|
|
@@ -174,10 +174,10 @@ function C(e) {
|
|
|
174
174
|
}
|
|
175
175
|
function ir(e) {
|
|
176
176
|
const r = e.issues.filter((s) => s.path.length === 0).map((s) => s.message), t = e.issues.filter((s) => s.path.length > 0).reduce((s, a) => {
|
|
177
|
-
const
|
|
177
|
+
const n = a.path.join(".");
|
|
178
178
|
return {
|
|
179
179
|
...s,
|
|
180
|
-
[
|
|
180
|
+
[n]: [...s[n] ?? [], a.message]
|
|
181
181
|
};
|
|
182
182
|
}, {});
|
|
183
183
|
return {
|
|
@@ -235,8 +235,8 @@ class cr {
|
|
|
235
235
|
}
|
|
236
236
|
class ur {
|
|
237
237
|
constructor(r, t) {
|
|
238
|
-
|
|
239
|
-
|
|
238
|
+
P(this, "schemaValidator");
|
|
239
|
+
P(this, "functionValidator");
|
|
240
240
|
this.schema = r, this.validateFn = t, this.schemaValidator = new lr(this.schema), this.functionValidator = new cr(this.validateFn);
|
|
241
241
|
}
|
|
242
242
|
async validate(r) {
|
|
@@ -246,30 +246,30 @@ class ur {
|
|
|
246
246
|
]);
|
|
247
247
|
return {
|
|
248
248
|
isValid: t.isValid && s.isValid,
|
|
249
|
-
errors:
|
|
249
|
+
errors: D(t.errors, s.errors)
|
|
250
250
|
};
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
|
-
function
|
|
253
|
+
function R(e) {
|
|
254
254
|
return u(() => new ur(
|
|
255
255
|
d(e.schema),
|
|
256
256
|
d(e.validateFn)
|
|
257
257
|
));
|
|
258
258
|
}
|
|
259
259
|
function dr(e, r) {
|
|
260
|
-
const t =
|
|
261
|
-
validators: W([
|
|
260
|
+
const t = b({
|
|
261
|
+
validators: W([R(r)]),
|
|
262
262
|
isValidated: !1,
|
|
263
263
|
errors: d(r.errors) ?? m.errors
|
|
264
264
|
});
|
|
265
265
|
y(() => d(r.errors), async () => {
|
|
266
|
-
const
|
|
267
|
-
o
|
|
266
|
+
const o = await a();
|
|
267
|
+
n(o.errors);
|
|
268
268
|
}, { immediate: !0 }), y(
|
|
269
269
|
[() => t.validators],
|
|
270
|
-
async (
|
|
270
|
+
async (o) => {
|
|
271
271
|
if (t.isValidated)
|
|
272
|
-
if (
|
|
272
|
+
if (o) {
|
|
273
273
|
const l = await a();
|
|
274
274
|
t.errors = l.errors;
|
|
275
275
|
} else
|
|
@@ -279,8 +279,8 @@ function dr(e, r) {
|
|
|
279
279
|
), y(() => e.data, () => {
|
|
280
280
|
t.isValidated && h();
|
|
281
281
|
});
|
|
282
|
-
const s = (
|
|
283
|
-
const l = z(
|
|
282
|
+
const s = (o) => {
|
|
283
|
+
const l = z(o) ? o : R(o);
|
|
284
284
|
return t.validators.push(l), q() && H(() => {
|
|
285
285
|
t.validators = t.validators.filter(
|
|
286
286
|
(f) => f !== l
|
|
@@ -288,25 +288,25 @@ function dr(e, r) {
|
|
|
288
288
|
}), l;
|
|
289
289
|
};
|
|
290
290
|
async function a() {
|
|
291
|
-
const
|
|
291
|
+
const o = await Promise.all(
|
|
292
292
|
t.validators.filter((v) => d(v) !== void 0).map((v) => d(v).validate(e.data))
|
|
293
|
-
), l =
|
|
293
|
+
), l = o.every((v) => v.isValid);
|
|
294
294
|
let { errors: f } = m;
|
|
295
295
|
if (!l) {
|
|
296
|
-
const v =
|
|
297
|
-
f =
|
|
296
|
+
const v = o.map((w) => w.errors);
|
|
297
|
+
f = D(...v);
|
|
298
298
|
}
|
|
299
299
|
return {
|
|
300
300
|
errors: f,
|
|
301
301
|
isValid: l
|
|
302
302
|
};
|
|
303
303
|
}
|
|
304
|
-
const
|
|
305
|
-
t.errors =
|
|
304
|
+
const n = (o) => {
|
|
305
|
+
t.errors = D(d(r.errors) ?? m.errors, o);
|
|
306
306
|
}, h = async () => {
|
|
307
|
-
const
|
|
308
|
-
return o
|
|
309
|
-
isValid: !C(
|
|
307
|
+
const o = await a();
|
|
308
|
+
return n(o.errors), t.isValidated = !0, {
|
|
309
|
+
isValid: !C(o.errors),
|
|
310
310
|
errors: t.errors
|
|
311
311
|
};
|
|
312
312
|
}, c = u(() => !C(t.errors));
|
|
@@ -322,7 +322,7 @@ class fr {
|
|
|
322
322
|
this.path = r, this.validator = t;
|
|
323
323
|
}
|
|
324
324
|
async validate(r) {
|
|
325
|
-
const t =
|
|
325
|
+
const t = E(r, this.path);
|
|
326
326
|
if (!this.validator)
|
|
327
327
|
return m;
|
|
328
328
|
const s = await this.validator.validate(t);
|
|
@@ -331,9 +331,9 @@ class fr {
|
|
|
331
331
|
errors: {
|
|
332
332
|
general: s.errors.general || [],
|
|
333
333
|
propertyErrors: s.errors.propertyErrors ? Object.fromEntries(
|
|
334
|
-
Object.entries(s.errors.propertyErrors).map(([a,
|
|
334
|
+
Object.entries(s.errors.propertyErrors).map(([a, n]) => [
|
|
335
335
|
F(this.path, a),
|
|
336
|
-
|
|
336
|
+
n
|
|
337
337
|
])
|
|
338
338
|
) : {}
|
|
339
339
|
}
|
|
@@ -341,7 +341,7 @@ class fr {
|
|
|
341
341
|
}
|
|
342
342
|
}
|
|
343
343
|
function pr(e, r, t) {
|
|
344
|
-
const s = U(e.data, r), a = u(() =>
|
|
344
|
+
const s = U(e.data, r), a = u(() => E(e.initialData.value, r)), n = (i) => ({
|
|
345
345
|
...i,
|
|
346
346
|
path: u(() => d(i.path).replace(r + ".", "")),
|
|
347
347
|
setData: (p) => {
|
|
@@ -349,33 +349,33 @@ function pr(e, r, t) {
|
|
|
349
349
|
}
|
|
350
350
|
}), h = (i) => {
|
|
351
351
|
const p = F(r, i), V = e.getField(p);
|
|
352
|
-
return V ?
|
|
352
|
+
return V ? n(V) : {};
|
|
353
353
|
}, c = (i) => {
|
|
354
354
|
const p = F(r, i.path), V = e.defineField({
|
|
355
355
|
...i,
|
|
356
356
|
path: p
|
|
357
357
|
});
|
|
358
|
-
return
|
|
359
|
-
},
|
|
358
|
+
return n(V);
|
|
359
|
+
}, o = () => e.getFields().filter((i) => {
|
|
360
360
|
const p = i.path.value;
|
|
361
361
|
return p.startsWith(r + ".") || p === r;
|
|
362
|
-
}).map((i) =>
|
|
362
|
+
}).map((i) => n(i)), l = () => e.getFields().filter((i) => {
|
|
363
363
|
const p = i.path.value;
|
|
364
364
|
return p.startsWith(r + ".") || p === r;
|
|
365
|
-
}), f = u(() => l().some((i) => i.dirty.value)), v = u(() => l().some((i) => i.touched.value)),
|
|
365
|
+
}), f = u(() => l().some((i) => i.dirty.value)), v = u(() => l().some((i) => i.touched.value)), w = u(() => e.isValid.value), I = u(() => e.isValidated.value), J = u(() => er(d(e.errors), r));
|
|
366
366
|
return {
|
|
367
367
|
data: s,
|
|
368
368
|
initialData: a,
|
|
369
369
|
defineField: c,
|
|
370
370
|
getField: h,
|
|
371
|
-
getFields:
|
|
371
|
+
getFields: o,
|
|
372
372
|
isDirty: f,
|
|
373
373
|
isTouched: v,
|
|
374
|
-
isValid:
|
|
374
|
+
isValid: w,
|
|
375
375
|
isValidated: I,
|
|
376
376
|
errors: J,
|
|
377
377
|
defineValidator: (i) => {
|
|
378
|
-
const p = z(i) ? i :
|
|
378
|
+
const p = z(i) ? i : R(i), V = u(
|
|
379
379
|
() => new fr(r, d(p))
|
|
380
380
|
);
|
|
381
381
|
return e.defineValidator(V), p;
|
|
@@ -392,27 +392,27 @@ function pr(e, r, t) {
|
|
|
392
392
|
};
|
|
393
393
|
}
|
|
394
394
|
function wr(e) {
|
|
395
|
-
const r = u(() => Object.freeze(g(e.initialData))), t = W(g(r)), s =
|
|
395
|
+
const r = u(() => Object.freeze(g(e.initialData))), t = W(g(r)), s = b({
|
|
396
396
|
initialData: r,
|
|
397
397
|
data: t
|
|
398
398
|
});
|
|
399
399
|
y(r, (f) => {
|
|
400
400
|
s.data = g(f);
|
|
401
401
|
});
|
|
402
|
-
const a = dr(s, e),
|
|
403
|
-
t.value = g(r),
|
|
402
|
+
const a = dr(s, e), n = sr(s, a), h = ar(n), c = () => {
|
|
403
|
+
t.value = g(r), n.getFields().forEach(
|
|
404
404
|
(f) => f.reset()
|
|
405
405
|
);
|
|
406
406
|
};
|
|
407
|
-
function
|
|
407
|
+
function o(f, v) {
|
|
408
408
|
return pr(l, f);
|
|
409
409
|
}
|
|
410
410
|
const l = {
|
|
411
|
-
...
|
|
411
|
+
...n,
|
|
412
412
|
...a,
|
|
413
413
|
...h,
|
|
414
414
|
reset: c,
|
|
415
|
-
getSubForm:
|
|
415
|
+
getSubForm: o,
|
|
416
416
|
initialData: _(s, "initialData"),
|
|
417
417
|
data: _(s, "data")
|
|
418
418
|
};
|
|
@@ -430,10 +430,10 @@ const Pr = /* @__PURE__ */ S({
|
|
|
430
430
|
setup(e) {
|
|
431
431
|
const r = e, t = r.form.defineField({
|
|
432
432
|
path: r.path
|
|
433
|
-
})
|
|
434
|
-
return (
|
|
433
|
+
});
|
|
434
|
+
return (s, a) => j(s.$slots, "default", M(B(d(t))));
|
|
435
435
|
}
|
|
436
|
-
}),
|
|
436
|
+
}), _r = /* @__PURE__ */ S({
|
|
437
437
|
inheritAttrs: !1,
|
|
438
438
|
__name: "FormFieldWrapper",
|
|
439
439
|
props: {
|
|
@@ -449,9 +449,9 @@ const Pr = /* @__PURE__ */ S({
|
|
|
449
449
|
form: r.form,
|
|
450
450
|
path: r.path
|
|
451
451
|
}, {
|
|
452
|
-
default: A(({ errors: a, data:
|
|
452
|
+
default: A(({ errors: a, data: n, setData: h }) => [
|
|
453
453
|
($(), O(X(r.component), Y({ ...r.componentProps, ...r.$attrs }, {
|
|
454
|
-
"model-value":
|
|
454
|
+
"model-value": n,
|
|
455
455
|
errors: a,
|
|
456
456
|
name: r.path,
|
|
457
457
|
"onUpdate:modelValue": h
|
|
@@ -466,7 +466,7 @@ const Pr = /* @__PURE__ */ S({
|
|
|
466
466
|
}, 8, ["form", "path"]);
|
|
467
467
|
};
|
|
468
468
|
}
|
|
469
|
-
}),
|
|
469
|
+
}), Dr = /* @__PURE__ */ S({
|
|
470
470
|
__name: "FormPart",
|
|
471
471
|
props: {
|
|
472
472
|
form: {},
|
|
@@ -479,7 +479,7 @@ const Pr = /* @__PURE__ */ S({
|
|
|
479
479
|
});
|
|
480
480
|
export {
|
|
481
481
|
Pr as Field,
|
|
482
|
-
|
|
483
|
-
|
|
482
|
+
_r as FormFieldWrapper,
|
|
483
|
+
Dr as FormPart,
|
|
484
484
|
wr as useForm
|
|
485
485
|
};
|
package/package.json
CHANGED
package/src/components/Field.vue
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<slot v-bind="
|
|
2
|
+
<slot v-bind="field" />
|
|
3
3
|
</template>
|
|
4
4
|
|
|
5
5
|
<script
|
|
6
6
|
setup lang="ts"
|
|
7
7
|
generic="TData extends object, TPath extends Paths<TData>"
|
|
8
8
|
>
|
|
9
|
-
import { reactive } from 'vue'
|
|
10
9
|
import type { Form } from '../types/form.ts'
|
|
11
10
|
import type { Paths, PickProps } from '../types/util.ts'
|
|
12
11
|
import type { UseFieldOptions } from '../composables/useField.ts'
|
|
@@ -20,6 +19,4 @@ const props = defineProps<FieldProps<TData, TPath>>()
|
|
|
20
19
|
const field = props.form.defineField({
|
|
21
20
|
path: props.path,
|
|
22
21
|
})
|
|
23
|
-
|
|
24
|
-
const slotData = reactive(field)
|
|
25
22
|
</script>
|