@teamnovu/kit-vue-forms 0.1.9 → 0.1.10
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 +1 -0
- package/dist/index.js +98 -95
- package/dist/types/form.d.ts +1 -0
- package/package.json +1 -1
- package/src/composables/useField.ts +12 -0
- package/src/types/form.ts +1 -0
|
@@ -19,6 +19,7 @@ declare const _default: <TData extends object, TPath extends Paths<TData>>(__VLS
|
|
|
19
19
|
touched: boolean;
|
|
20
20
|
dirty: boolean;
|
|
21
21
|
setData: (newData: PickProps<TData, TPath>) => void;
|
|
22
|
+
setInitialData: (newData: PickProps<TData, TPath>) => void;
|
|
22
23
|
onBlur: () => void;
|
|
23
24
|
onFocus: () => void;
|
|
24
25
|
reset: () => void;
|
package/dist/index.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
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
|
|
4
|
+
import { toValue as L, toRaw as Z, computed as u, 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) {
|
|
8
8
|
const e = L(t), r = Z(e);
|
|
9
9
|
return re(r);
|
|
10
10
|
}
|
|
11
|
-
function
|
|
11
|
+
function I(t) {
|
|
12
12
|
return t === "" ? [] : t.split(/\s*\.\s*/).filter(Boolean);
|
|
13
13
|
}
|
|
14
14
|
function P(t, e) {
|
|
15
|
-
return (Array.isArray(e) ? e :
|
|
15
|
+
return (Array.isArray(e) ? e : I(e)).reduce(
|
|
16
16
|
(s, o) => s == null ? void 0 : s[o],
|
|
17
17
|
t
|
|
18
18
|
);
|
|
19
19
|
}
|
|
20
20
|
function te(t, e, r) {
|
|
21
|
-
const s = Array.isArray(e) ? e :
|
|
21
|
+
const s = Array.isArray(e) ? e : I(e), o = s.at(-1);
|
|
22
22
|
if (o) {
|
|
23
|
-
const
|
|
23
|
+
const i = s.slice(0, -1).reduce(
|
|
24
24
|
(p, h) => p[h],
|
|
25
25
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
26
|
d(t)
|
|
27
27
|
);
|
|
28
|
-
|
|
28
|
+
i[o] = r;
|
|
29
29
|
} else {
|
|
30
30
|
if (!O(t))
|
|
31
31
|
return;
|
|
32
32
|
t.value = r;
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
-
const
|
|
35
|
+
const K = (t, e) => u({
|
|
36
36
|
get() {
|
|
37
37
|
return P(d(t), d(e));
|
|
38
38
|
},
|
|
@@ -48,7 +48,7 @@ function se(t, e) {
|
|
|
48
48
|
return t;
|
|
49
49
|
const r = `${e}.`, s = Object.fromEntries(
|
|
50
50
|
Object.entries(t.propertyErrors).filter(([o]) => o.startsWith(r)).map(
|
|
51
|
-
([o,
|
|
51
|
+
([o, i]) => [o.slice(r.length), i]
|
|
52
52
|
)
|
|
53
53
|
);
|
|
54
54
|
return {
|
|
@@ -73,55 +73,58 @@ function oe(t) {
|
|
|
73
73
|
const e = D({
|
|
74
74
|
value: t.value,
|
|
75
75
|
path: t.path,
|
|
76
|
-
initialValue:
|
|
76
|
+
initialValue: u(() => Object.freeze(g(t.initialValue))),
|
|
77
77
|
errors: t.errors,
|
|
78
78
|
touched: !1
|
|
79
|
-
}), r =
|
|
80
|
-
e.value =
|
|
79
|
+
}), r = u(() => JSON.stringify(e.value) !== JSON.stringify(e.initialValue)), s = (c) => {
|
|
80
|
+
e.value = c;
|
|
81
81
|
}, o = () => {
|
|
82
82
|
e.touched = !0;
|
|
83
|
-
},
|
|
83
|
+
}, i = () => {
|
|
84
84
|
}, p = () => {
|
|
85
85
|
e.value = g(e.initialValue), e.touched = !1, e.errors = [];
|
|
86
|
-
}, h = (
|
|
87
|
-
e.
|
|
88
|
-
}, V = () => {
|
|
86
|
+
}, h = (c) => {
|
|
87
|
+
r.value || s(g(c)), e.initialValue = c;
|
|
88
|
+
}, V = (c) => {
|
|
89
|
+
e.errors = c;
|
|
90
|
+
}, n = () => {
|
|
89
91
|
e.errors = [];
|
|
90
|
-
},
|
|
92
|
+
}, a = W(e);
|
|
91
93
|
return {
|
|
92
|
-
data:
|
|
93
|
-
path:
|
|
94
|
-
initialValue:
|
|
95
|
-
errors:
|
|
96
|
-
touched:
|
|
94
|
+
data: a.value,
|
|
95
|
+
path: a.path,
|
|
96
|
+
initialValue: a.initialValue,
|
|
97
|
+
errors: a.errors,
|
|
98
|
+
touched: a.touched,
|
|
97
99
|
dirty: r,
|
|
98
100
|
setData: s,
|
|
101
|
+
setInitialData: h,
|
|
99
102
|
onBlur: o,
|
|
100
|
-
onFocus:
|
|
103
|
+
onFocus: i,
|
|
101
104
|
reset: p,
|
|
102
|
-
setErrors:
|
|
103
|
-
clearErrors:
|
|
105
|
+
setErrors: V,
|
|
106
|
+
clearErrors: n
|
|
104
107
|
};
|
|
105
108
|
}
|
|
106
|
-
function
|
|
109
|
+
function ie(t, e) {
|
|
107
110
|
const r = /* @__PURE__ */ new Map(), s = q(/* @__PURE__ */ new Map()), o = (a) => {
|
|
108
|
-
const
|
|
109
|
-
s.set(
|
|
110
|
-
},
|
|
111
|
+
const c = d(a.path);
|
|
112
|
+
s.set(c, a);
|
|
113
|
+
}, i = (a) => {
|
|
111
114
|
s.delete(a);
|
|
112
115
|
}, p = (a) => {
|
|
113
|
-
var
|
|
114
|
-
r.has(a) ? (
|
|
116
|
+
var c;
|
|
117
|
+
r.has(a) ? (c = r.get(a)) == null || c.inc() : r.set(a, new ae(() => i(a)));
|
|
115
118
|
}, h = (a) => {
|
|
116
|
-
var
|
|
117
|
-
r.has(a) && ((
|
|
119
|
+
var c;
|
|
120
|
+
r.has(a) && ((c = r.get(a)) == null || c.dec());
|
|
118
121
|
}, V = (a) => {
|
|
119
122
|
if (!s.has(a)) {
|
|
120
123
|
const v = oe({
|
|
121
124
|
path: a,
|
|
122
|
-
value:
|
|
123
|
-
initialValue:
|
|
124
|
-
errors:
|
|
125
|
+
value: K(_(t, "data"), a),
|
|
126
|
+
initialValue: u(() => P(t.initialData, a)),
|
|
127
|
+
errors: u({
|
|
125
128
|
get() {
|
|
126
129
|
return e.errors.value.propertyErrors[a] || [];
|
|
127
130
|
},
|
|
@@ -132,21 +135,21 @@ function ne(t, e) {
|
|
|
132
135
|
});
|
|
133
136
|
o(v);
|
|
134
137
|
}
|
|
135
|
-
const
|
|
138
|
+
const c = s.get(a);
|
|
136
139
|
return p(a), H(() => {
|
|
137
140
|
h(a);
|
|
138
|
-
}),
|
|
139
|
-
},
|
|
141
|
+
}), c;
|
|
142
|
+
}, n = (a) => V(a.path);
|
|
140
143
|
return {
|
|
141
|
-
fields:
|
|
144
|
+
fields: u(() => [...s.values()]),
|
|
142
145
|
getField: V,
|
|
143
146
|
registerField: o,
|
|
144
|
-
deregisterField:
|
|
145
|
-
defineField:
|
|
147
|
+
deregisterField: i,
|
|
148
|
+
defineField: n
|
|
146
149
|
};
|
|
147
150
|
}
|
|
148
|
-
function
|
|
149
|
-
const e =
|
|
151
|
+
function ne(t) {
|
|
152
|
+
const e = u(() => t.fields.value.some((s) => d(s.dirty))), r = u(() => t.fields.value.some((s) => d(s.touched)));
|
|
150
153
|
return {
|
|
151
154
|
isDirty: e,
|
|
152
155
|
isTouched: r
|
|
@@ -157,7 +160,7 @@ function le(t) {
|
|
|
157
160
|
(e, r, s) => s.indexOf(e) === r
|
|
158
161
|
);
|
|
159
162
|
}
|
|
160
|
-
function
|
|
163
|
+
function U(...t) {
|
|
161
164
|
return t.slice(1).reduce((e, r) => {
|
|
162
165
|
if (!e && !r)
|
|
163
166
|
return;
|
|
@@ -172,10 +175,10 @@ function I(...t) {
|
|
|
172
175
|
}
|
|
173
176
|
function ce(...t) {
|
|
174
177
|
return t.map((r) => Object.keys(r)).flat().reduce((r, s) => {
|
|
175
|
-
const o = t.map((
|
|
178
|
+
const o = t.map((i) => i[s]).filter(Boolean);
|
|
176
179
|
return {
|
|
177
180
|
...r,
|
|
178
|
-
[s]:
|
|
181
|
+
[s]: U(...o)
|
|
179
182
|
};
|
|
180
183
|
}, {});
|
|
181
184
|
}
|
|
@@ -188,7 +191,7 @@ function S(...t) {
|
|
|
188
191
|
const e = t[0];
|
|
189
192
|
return t.length === 1 ? e : t.slice(1).reduce(
|
|
190
193
|
(r, s) => ({
|
|
191
|
-
general:
|
|
194
|
+
general: U(r.general, s.general),
|
|
192
195
|
propertyErrors: ce(r.propertyErrors ?? {}, s.propertyErrors ?? {})
|
|
193
196
|
}),
|
|
194
197
|
e
|
|
@@ -201,10 +204,10 @@ function N(t) {
|
|
|
201
204
|
}
|
|
202
205
|
function ue(t) {
|
|
203
206
|
const e = t.issues.filter((s) => s.path.length === 0).map((s) => s.message), r = t.issues.filter((s) => s.path.length > 0).reduce((s, o) => {
|
|
204
|
-
const
|
|
207
|
+
const i = o.path.join(".");
|
|
205
208
|
return {
|
|
206
209
|
...s,
|
|
207
|
-
[
|
|
210
|
+
[i]: [...s[i] ?? [], o.message]
|
|
208
211
|
};
|
|
209
212
|
}, {});
|
|
210
213
|
return {
|
|
@@ -278,7 +281,7 @@ class pe {
|
|
|
278
281
|
}
|
|
279
282
|
}
|
|
280
283
|
function b(t) {
|
|
281
|
-
return
|
|
284
|
+
return u(() => new pe(
|
|
282
285
|
d(t.schema),
|
|
283
286
|
d(t.validateFn)
|
|
284
287
|
));
|
|
@@ -288,21 +291,21 @@ function he(t, e) {
|
|
|
288
291
|
validators: k([b(e)]),
|
|
289
292
|
isValidated: !1,
|
|
290
293
|
errors: d(e.errors) ?? m.errors
|
|
291
|
-
}), s = (
|
|
292
|
-
r.errors = S(d(e.errors) ?? m.errors,
|
|
294
|
+
}), s = (n = m.errors) => {
|
|
295
|
+
r.errors = S(d(e.errors) ?? m.errors, n);
|
|
293
296
|
};
|
|
294
297
|
w(() => d(e.errors), async () => {
|
|
295
298
|
if (r.isValidated) {
|
|
296
|
-
const
|
|
297
|
-
s(
|
|
299
|
+
const n = await i();
|
|
300
|
+
s(n.errors);
|
|
298
301
|
} else
|
|
299
302
|
s();
|
|
300
303
|
}, { immediate: !0 }), w(
|
|
301
304
|
[() => r.validators],
|
|
302
|
-
async (
|
|
305
|
+
async (n) => {
|
|
303
306
|
if (r.isValidated)
|
|
304
|
-
if (
|
|
305
|
-
const a = await
|
|
307
|
+
if (n) {
|
|
308
|
+
const a = await i();
|
|
306
309
|
r.errors = a.errors;
|
|
307
310
|
} else
|
|
308
311
|
r.errors = m.errors;
|
|
@@ -311,35 +314,35 @@ function he(t, e) {
|
|
|
311
314
|
), w(() => t.data, () => {
|
|
312
315
|
r.isValidated && p();
|
|
313
316
|
});
|
|
314
|
-
const o = (
|
|
315
|
-
const a = O(
|
|
317
|
+
const o = (n) => {
|
|
318
|
+
const a = O(n) ? n : b(n);
|
|
316
319
|
return r.validators.push(a), Q() && X(() => {
|
|
317
320
|
r.validators = r.validators.filter(
|
|
318
|
-
(
|
|
321
|
+
(c) => c !== a
|
|
319
322
|
);
|
|
320
323
|
}), a;
|
|
321
324
|
};
|
|
322
|
-
async function
|
|
323
|
-
const
|
|
325
|
+
async function i() {
|
|
326
|
+
const n = await Promise.all(
|
|
324
327
|
r.validators.filter((v) => d(v) !== void 0).map((v) => d(v).validate(t.data))
|
|
325
|
-
), a =
|
|
326
|
-
let { errors:
|
|
328
|
+
), a = n.every((v) => v.isValid);
|
|
329
|
+
let { errors: c } = m;
|
|
327
330
|
if (!a) {
|
|
328
|
-
const v =
|
|
329
|
-
|
|
331
|
+
const v = n.map((F) => F.errors);
|
|
332
|
+
c = S(...v);
|
|
330
333
|
}
|
|
331
334
|
return {
|
|
332
|
-
errors:
|
|
335
|
+
errors: c,
|
|
333
336
|
isValid: a
|
|
334
337
|
};
|
|
335
338
|
}
|
|
336
339
|
const p = async () => {
|
|
337
|
-
const
|
|
338
|
-
return s(
|
|
339
|
-
isValid: !N(
|
|
340
|
+
const n = await i();
|
|
341
|
+
return s(n.errors), r.isValidated = !0, {
|
|
342
|
+
isValid: !N(n.errors),
|
|
340
343
|
errors: r.errors
|
|
341
344
|
};
|
|
342
|
-
}, h =
|
|
345
|
+
}, h = u(() => !N(r.errors)), V = () => {
|
|
343
346
|
r.isValidated = !1, r.errors = d(e.errors) ?? m.errors;
|
|
344
347
|
};
|
|
345
348
|
return {
|
|
@@ -364,9 +367,9 @@ class ve {
|
|
|
364
367
|
errors: {
|
|
365
368
|
general: s.errors.general || [],
|
|
366
369
|
propertyErrors: s.errors.propertyErrors ? Object.fromEntries(
|
|
367
|
-
Object.entries(s.errors.propertyErrors).map(([o,
|
|
370
|
+
Object.entries(s.errors.propertyErrors).map(([o, i]) => [
|
|
368
371
|
R(this.path, o),
|
|
369
|
-
|
|
372
|
+
i
|
|
370
373
|
])
|
|
371
374
|
) : {}
|
|
372
375
|
}
|
|
@@ -374,28 +377,28 @@ class ve {
|
|
|
374
377
|
}
|
|
375
378
|
}
|
|
376
379
|
function me(t, e, r) {
|
|
377
|
-
const s =
|
|
380
|
+
const s = K(t.data, e), o = u(() => P(t.initialData.value, e)), i = (l) => ({
|
|
378
381
|
...l,
|
|
379
|
-
path:
|
|
382
|
+
path: u(() => d(l.path).replace(e + ".", "")),
|
|
380
383
|
setData: (f) => {
|
|
381
384
|
l.setData(f);
|
|
382
385
|
}
|
|
383
386
|
}), p = (l) => {
|
|
384
387
|
const f = R(e, l), y = t.getField(f);
|
|
385
|
-
return y ?
|
|
388
|
+
return y ? i(y) : {};
|
|
386
389
|
}, h = (l) => {
|
|
387
390
|
const f = R(e, l.path), y = t.defineField({
|
|
388
391
|
...l,
|
|
389
392
|
path: f
|
|
390
393
|
});
|
|
391
|
-
return
|
|
392
|
-
}, V =
|
|
394
|
+
return i(y);
|
|
395
|
+
}, V = u(() => t.fields.value.filter((l) => {
|
|
393
396
|
const f = l.path.value;
|
|
394
397
|
return f.startsWith(e + ".") || f === e;
|
|
395
|
-
}).map((l) =>
|
|
398
|
+
}).map((l) => i(l))), n = () => t.fields.value.filter((l) => {
|
|
396
399
|
const f = l.path.value;
|
|
397
400
|
return f.startsWith(e + ".") || f === e;
|
|
398
|
-
}), a =
|
|
401
|
+
}), a = u(() => n().some((l) => l.dirty.value)), c = u(() => n().some((l) => l.touched.value)), v = u(() => t.isValid.value), F = u(() => t.isValidated.value), J = u(() => se(d(t.errors), e));
|
|
399
402
|
return {
|
|
400
403
|
data: s,
|
|
401
404
|
fields: V,
|
|
@@ -403,17 +406,17 @@ function me(t, e, r) {
|
|
|
403
406
|
defineField: h,
|
|
404
407
|
getField: p,
|
|
405
408
|
isDirty: a,
|
|
406
|
-
isTouched:
|
|
409
|
+
isTouched: c,
|
|
407
410
|
isValid: v,
|
|
408
411
|
isValidated: F,
|
|
409
412
|
errors: J,
|
|
410
413
|
defineValidator: (l) => {
|
|
411
|
-
const f = O(l) ? l : b(l), y =
|
|
414
|
+
const f = O(l) ? l : b(l), y = u(
|
|
412
415
|
() => new ve(e, d(f))
|
|
413
416
|
);
|
|
414
417
|
return t.defineValidator(y), f;
|
|
415
418
|
},
|
|
416
|
-
reset: () =>
|
|
419
|
+
reset: () => n().forEach((l) => l.reset()),
|
|
417
420
|
validateForm: () => t.validateForm(),
|
|
418
421
|
getSubForm: (l, f) => {
|
|
419
422
|
const y = R(e, l);
|
|
@@ -425,23 +428,23 @@ function me(t, e, r) {
|
|
|
425
428
|
};
|
|
426
429
|
}
|
|
427
430
|
function Pe(t) {
|
|
428
|
-
const e =
|
|
431
|
+
const e = u(() => Object.freeze(g(t.initialData))), r = k(g(e)), s = D({
|
|
429
432
|
initialData: e,
|
|
430
433
|
data: r
|
|
431
434
|
});
|
|
432
435
|
w(e, (a) => {
|
|
433
436
|
s.data = g(a);
|
|
434
437
|
});
|
|
435
|
-
const o = he(s, t),
|
|
436
|
-
r.value = g(e), o.reset(),
|
|
438
|
+
const o = he(s, t), i = ie(s, o), p = ne(i), h = () => {
|
|
439
|
+
r.value = g(e), o.reset(), i.fields.value.forEach(
|
|
437
440
|
(a) => a.reset()
|
|
438
441
|
);
|
|
439
442
|
};
|
|
440
|
-
function V(a,
|
|
441
|
-
return me(
|
|
443
|
+
function V(a, c) {
|
|
444
|
+
return me(n, a);
|
|
442
445
|
}
|
|
443
|
-
const
|
|
444
|
-
...
|
|
446
|
+
const n = {
|
|
447
|
+
...i,
|
|
445
448
|
...o,
|
|
446
449
|
...p,
|
|
447
450
|
reset: h,
|
|
@@ -449,7 +452,7 @@ function Pe(t) {
|
|
|
449
452
|
initialData: _(s, "initialData"),
|
|
450
453
|
data: _(s, "data")
|
|
451
454
|
};
|
|
452
|
-
return
|
|
455
|
+
return n;
|
|
453
456
|
}
|
|
454
457
|
const _e = /* @__PURE__ */ $({
|
|
455
458
|
__name: "Field",
|
|
@@ -464,7 +467,7 @@ const _e = /* @__PURE__ */ $({
|
|
|
464
467
|
const e = t, r = e.form.defineField({
|
|
465
468
|
path: e.path
|
|
466
469
|
}), s = D(r);
|
|
467
|
-
return (o,
|
|
470
|
+
return (o, i) => j(o.$slots, "default", z(B(s)));
|
|
468
471
|
}
|
|
469
472
|
}), Se = /* @__PURE__ */ $({
|
|
470
473
|
inheritAttrs: !1,
|
|
@@ -482,9 +485,9 @@ const _e = /* @__PURE__ */ $({
|
|
|
482
485
|
form: e.form,
|
|
483
486
|
path: e.path
|
|
484
487
|
}, {
|
|
485
|
-
default: M(({ errors: o, data:
|
|
488
|
+
default: M(({ errors: o, data: i, setData: p }) => [
|
|
486
489
|
(C(), A(x(e.component), ee({ ...e.componentProps, ...e.$attrs }, {
|
|
487
|
-
"model-value":
|
|
490
|
+
"model-value": i,
|
|
488
491
|
errors: o,
|
|
489
492
|
name: e.path,
|
|
490
493
|
"onUpdate:modelValue": p
|
|
@@ -506,7 +509,7 @@ const _e = /* @__PURE__ */ $({
|
|
|
506
509
|
path: {}
|
|
507
510
|
},
|
|
508
511
|
setup(t) {
|
|
509
|
-
const e = t, r =
|
|
512
|
+
const e = t, r = u(() => e.form.getSubForm(e.path));
|
|
510
513
|
return (s, o) => j(s.$slots, "default", z(B({ subform: r.value })));
|
|
511
514
|
}
|
|
512
515
|
});
|
package/dist/types/form.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -41,6 +41,17 @@ export function useField<T, K extends string>(options: UseFieldOptions<T, K>): F
|
|
|
41
41
|
state.errors = []
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Sets the initial data for the field. If the field is not dirty, it also updates the current data.
|
|
46
|
+
* @param newData - The new initial data to set.
|
|
47
|
+
*/
|
|
48
|
+
const setInitialData = (newData: T): void => {
|
|
49
|
+
if (!dirty.value) {
|
|
50
|
+
setData(cloneRefValue(newData))
|
|
51
|
+
}
|
|
52
|
+
state.initialValue = newData
|
|
53
|
+
}
|
|
54
|
+
|
|
44
55
|
const setErrors = (newErrors: ValidationErrorMessage[]): void => {
|
|
45
56
|
state.errors = newErrors
|
|
46
57
|
}
|
|
@@ -59,6 +70,7 @@ export function useField<T, K extends string>(options: UseFieldOptions<T, K>): F
|
|
|
59
70
|
touched: refs.touched as FormField<T, K>['touched'],
|
|
60
71
|
dirty,
|
|
61
72
|
setData,
|
|
73
|
+
setInitialData,
|
|
62
74
|
onBlur,
|
|
63
75
|
onFocus,
|
|
64
76
|
reset,
|
package/src/types/form.ts
CHANGED