@teamnovu/kit-vue-forms 0.1.15 → 0.1.16
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/useField.d.ts +3 -2
- package/dist/composables/useValidation.d.ts +1 -0
- package/dist/index.js +344 -315
- package/dist/utils/path.d.ts +4 -1
- package/package.json +1 -1
- package/src/composables/useField.ts +16 -4
- package/src/composables/useFieldRegistry.ts +2 -1
- package/src/composables/useForm.ts +47 -43
- package/src/composables/useValidation.ts +17 -0
- package/src/utils/path.ts +81 -52
- package/tests/useForm.test.ts +40 -0
- package/tests/useValidation.test.ts +26 -2
package/dist/index.js
CHANGED
|
@@ -1,65 +1,70 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { toValue as
|
|
5
|
-
import { cloneDeep as
|
|
1
|
+
var H = Object.defineProperty;
|
|
2
|
+
var Z = (t, e, r) => e in t ? H(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var S = (t, e, r) => Z(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
|
+
import { toValue as q, toRaw as Q, computed as p, unref as d, isRef as U, shallowRef as B, reactive as $, watch as P, toRefs as J, shallowReactive as X, toRef as O, onScopeDispose as Y, watchEffect as ee, triggerRef as re, ref as L, getCurrentScope as te, onBeforeUnmount as ae, defineComponent as k, renderSlot as b, normalizeProps as M, guardReactiveProps as z, resolveComponent as se, createBlock as K, openBlock as T, withCtx as j, resolveDynamicComponent as ne, mergeProps as oe, createSlots as ie, renderList as le } from "vue";
|
|
5
|
+
import { cloneDeep as ce } from "lodash-es";
|
|
6
6
|
import "zod";
|
|
7
|
-
function
|
|
8
|
-
const e =
|
|
9
|
-
return
|
|
7
|
+
function g(t) {
|
|
8
|
+
const e = q(t), r = Q(e);
|
|
9
|
+
return ce(r);
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
return
|
|
11
|
+
function W(t) {
|
|
12
|
+
return t === "" ? [] : t.split(/\s*\.\s*/).filter(Boolean);
|
|
13
13
|
}
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
function ue(t, e) {
|
|
15
|
+
const r = Array.isArray(e) ? e : W(e);
|
|
16
|
+
return !!D(t, r.slice(0, -1));
|
|
17
|
+
}
|
|
18
|
+
function D(t, e) {
|
|
19
|
+
return (Array.isArray(e) ? e : W(e)).reduce(
|
|
20
|
+
(a, s) => a == null ? void 0 : a[s],
|
|
21
|
+
t
|
|
18
22
|
);
|
|
19
23
|
}
|
|
20
|
-
function
|
|
21
|
-
const a = Array.isArray(e) ? e :
|
|
22
|
-
if (
|
|
23
|
-
const
|
|
24
|
-
(f,
|
|
24
|
+
function de(t, e, r) {
|
|
25
|
+
const a = Array.isArray(e) ? e : W(e), s = a.at(-1);
|
|
26
|
+
if (s) {
|
|
27
|
+
const n = a.slice(0, -1).reduce(
|
|
28
|
+
(f, m) => ((f == null ? void 0 : f[m]) === void 0 && (f[m] = {}), f == null ? void 0 : f[m]),
|
|
25
29
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
-
|
|
30
|
+
d(t)
|
|
27
31
|
);
|
|
28
|
-
|
|
32
|
+
n[s] = r;
|
|
29
33
|
} else {
|
|
30
|
-
if (!U(
|
|
34
|
+
if (!U(t))
|
|
31
35
|
return;
|
|
32
|
-
|
|
36
|
+
t.value = r;
|
|
33
37
|
}
|
|
34
38
|
}
|
|
35
|
-
const
|
|
39
|
+
const x = (t, e) => p({
|
|
36
40
|
get() {
|
|
37
|
-
return
|
|
41
|
+
return D(d(t), d(e));
|
|
38
42
|
},
|
|
39
|
-
set(
|
|
40
|
-
|
|
43
|
+
set(r) {
|
|
44
|
+
de(t, d(e), r);
|
|
41
45
|
}
|
|
42
46
|
});
|
|
43
|
-
function
|
|
44
|
-
return !
|
|
47
|
+
function _(t, e) {
|
|
48
|
+
return !t && !e ? "" : !t && e ? e : !e && t ? t : `${t}.${e}`;
|
|
45
49
|
}
|
|
46
|
-
function
|
|
50
|
+
function fe(t, e) {
|
|
47
51
|
if (!e)
|
|
48
|
-
return
|
|
49
|
-
const
|
|
50
|
-
Object.entries(
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
return t;
|
|
53
|
+
const r = `${e}.`, a = Object.fromEntries(
|
|
54
|
+
Object.entries(t.propertyErrors).filter(([s]) => s.startsWith(r)).map(([s, n]) => [
|
|
55
|
+
s.slice(r.length),
|
|
56
|
+
n
|
|
57
|
+
])
|
|
53
58
|
);
|
|
54
59
|
return {
|
|
55
|
-
general:
|
|
60
|
+
general: t.general,
|
|
56
61
|
// Keep general errors
|
|
57
62
|
propertyErrors: a
|
|
58
63
|
};
|
|
59
64
|
}
|
|
60
|
-
class
|
|
65
|
+
class he {
|
|
61
66
|
constructor(e) {
|
|
62
|
-
|
|
67
|
+
S(this, "rc", 1);
|
|
63
68
|
this.drop = e;
|
|
64
69
|
}
|
|
65
70
|
inc() {
|
|
@@ -69,216 +74,223 @@ class fe {
|
|
|
69
74
|
this.rc > 0 && (this.rc -= 1, this.rc === 0 && this.drop && this.drop());
|
|
70
75
|
}
|
|
71
76
|
}
|
|
72
|
-
function
|
|
73
|
-
const
|
|
77
|
+
function pe(t) {
|
|
78
|
+
const r = {
|
|
79
|
+
...{
|
|
80
|
+
existsInForm: !0
|
|
81
|
+
},
|
|
82
|
+
...t
|
|
83
|
+
}, a = B(Object.freeze(g(r.initialValue))), s = $({
|
|
74
84
|
value: r.value,
|
|
75
85
|
path: r.path,
|
|
76
|
-
initialValue:
|
|
86
|
+
initialValue: a,
|
|
77
87
|
errors: r.errors,
|
|
78
88
|
touched: !1
|
|
79
89
|
});
|
|
80
|
-
|
|
90
|
+
P(
|
|
81
91
|
B(r.initialValue),
|
|
82
92
|
() => {
|
|
83
|
-
|
|
93
|
+
a.value = Object.freeze(g(r.initialValue)), s.value = g(r.initialValue);
|
|
84
94
|
},
|
|
85
95
|
{ flush: "sync" }
|
|
86
96
|
);
|
|
87
|
-
const
|
|
88
|
-
|
|
97
|
+
const n = p(() => JSON.stringify(s.value) !== JSON.stringify(s.initialValue)), f = (h) => {
|
|
98
|
+
s.value = h;
|
|
99
|
+
}, m = () => {
|
|
100
|
+
var h;
|
|
101
|
+
s.touched = !0, s.errors = [], (h = r.onBlur) == null || h.call(r);
|
|
102
|
+
}, y = () => {
|
|
103
|
+
var h;
|
|
104
|
+
(h = r.onFocus) == null || h.call(r);
|
|
105
|
+
}, V = () => {
|
|
106
|
+
const h = s.path.split(".").at(-1) || "";
|
|
107
|
+
d(r.existsInForm) && !/^\d+$/.test(h) && (s.value = g(s.initialValue)), s.touched = !1, s.errors = [];
|
|
108
|
+
}, l = (h) => {
|
|
109
|
+
n.value || f(g(h)), s.initialValue = h;
|
|
110
|
+
}, c = (h) => {
|
|
111
|
+
s.errors = h;
|
|
89
112
|
}, i = () => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}, f = () => {
|
|
93
|
-
var s;
|
|
94
|
-
(s = r.onFocus) == null || s.call(r);
|
|
95
|
-
}, p = () => {
|
|
96
|
-
t.value = F(t.initialValue), t.touched = !1, t.errors = [];
|
|
97
|
-
}, V = (s) => {
|
|
98
|
-
a.value || n(F(s)), t.initialValue = s;
|
|
99
|
-
}, o = (s) => {
|
|
100
|
-
t.errors = s;
|
|
101
|
-
}, v = () => {
|
|
102
|
-
t.errors = [];
|
|
103
|
-
}, d = T(t);
|
|
113
|
+
s.errors = [];
|
|
114
|
+
}, o = J(s);
|
|
104
115
|
return {
|
|
105
|
-
data:
|
|
106
|
-
path:
|
|
107
|
-
initialValue:
|
|
108
|
-
errors:
|
|
109
|
-
touched:
|
|
110
|
-
dirty:
|
|
111
|
-
setData:
|
|
112
|
-
setInitialData:
|
|
113
|
-
onBlur:
|
|
114
|
-
onFocus:
|
|
115
|
-
reset:
|
|
116
|
-
setErrors:
|
|
117
|
-
clearErrors:
|
|
116
|
+
data: o.value,
|
|
117
|
+
path: o.path,
|
|
118
|
+
initialValue: o.initialValue,
|
|
119
|
+
errors: o.errors,
|
|
120
|
+
touched: o.touched,
|
|
121
|
+
dirty: n,
|
|
122
|
+
setData: f,
|
|
123
|
+
setInitialData: l,
|
|
124
|
+
onBlur: m,
|
|
125
|
+
onFocus: y,
|
|
126
|
+
reset: V,
|
|
127
|
+
setErrors: c,
|
|
128
|
+
clearErrors: i
|
|
118
129
|
};
|
|
119
130
|
}
|
|
120
131
|
const ve = {
|
|
121
132
|
keepValuesOnUnmount: !0
|
|
122
133
|
};
|
|
123
|
-
function me(
|
|
124
|
-
const e = B(
|
|
134
|
+
function me(t) {
|
|
135
|
+
const e = B(t());
|
|
125
136
|
return ee(
|
|
126
137
|
() => {
|
|
127
|
-
e.value =
|
|
138
|
+
e.value = t(), re(e);
|
|
128
139
|
},
|
|
129
140
|
{ flush: "sync" }
|
|
130
141
|
), e;
|
|
131
142
|
}
|
|
132
|
-
function
|
|
133
|
-
const a = /* @__PURE__ */ new Map(),
|
|
143
|
+
function Ve(t, e, r) {
|
|
144
|
+
const a = /* @__PURE__ */ new Map(), s = X(/* @__PURE__ */ new Map()), n = {
|
|
134
145
|
...ve,
|
|
135
|
-
...
|
|
136
|
-
}, f = (
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
},
|
|
140
|
-
var
|
|
141
|
-
|
|
142
|
-
},
|
|
143
|
-
var
|
|
144
|
-
a.has(
|
|
145
|
-
},
|
|
146
|
-
var
|
|
147
|
-
a.has(
|
|
148
|
-
},
|
|
149
|
-
const { path:
|
|
150
|
-
if (!
|
|
151
|
-
const
|
|
152
|
-
path:
|
|
153
|
-
value:
|
|
154
|
-
initialValue: me(() =>
|
|
155
|
-
|
|
146
|
+
...r
|
|
147
|
+
}, f = (i) => {
|
|
148
|
+
const o = d(i.path);
|
|
149
|
+
s.set(o, i);
|
|
150
|
+
}, m = (i) => {
|
|
151
|
+
var o;
|
|
152
|
+
n != null && n.keepValuesOnUnmount || (o = s.get(i)) == null || o.reset(), s.delete(i);
|
|
153
|
+
}, y = (i) => {
|
|
154
|
+
var o;
|
|
155
|
+
a.has(i) ? (o = a.get(i)) == null || o.inc() : a.set(i, new he(() => m(i)));
|
|
156
|
+
}, V = (i) => {
|
|
157
|
+
var o;
|
|
158
|
+
a.has(i) && ((o = a.get(i)) == null || o.dec());
|
|
159
|
+
}, l = (i) => {
|
|
160
|
+
const { path: o } = i;
|
|
161
|
+
if (!s.has(o)) {
|
|
162
|
+
const N = pe({
|
|
163
|
+
path: o,
|
|
164
|
+
value: x(O(t, "data"), o),
|
|
165
|
+
initialValue: me(() => D(t.initialData, o)),
|
|
166
|
+
existsInForm: p(() => ue(t.data, d(o))),
|
|
167
|
+
errors: p({
|
|
156
168
|
get() {
|
|
157
|
-
return e.errors.value.propertyErrors[
|
|
169
|
+
return e.errors.value.propertyErrors[o] || [];
|
|
158
170
|
},
|
|
159
|
-
set(
|
|
160
|
-
e.errors.value.propertyErrors[
|
|
171
|
+
set(E) {
|
|
172
|
+
e.errors.value.propertyErrors[o] = E;
|
|
161
173
|
}
|
|
162
174
|
}),
|
|
163
175
|
onBlur: async () => {
|
|
164
|
-
var
|
|
176
|
+
var E, R;
|
|
165
177
|
await Promise.all(
|
|
166
178
|
[
|
|
167
|
-
(
|
|
168
|
-
(
|
|
179
|
+
(E = n == null ? void 0 : n.onBlur) == null ? void 0 : E.call(n, d(o)),
|
|
180
|
+
(R = i.onBlur) == null ? void 0 : R.call(i)
|
|
169
181
|
]
|
|
170
182
|
);
|
|
171
183
|
},
|
|
172
184
|
onFocus: async () => {
|
|
173
|
-
var
|
|
185
|
+
var E, R;
|
|
174
186
|
await Promise.all(
|
|
175
187
|
[
|
|
176
|
-
(
|
|
177
|
-
(
|
|
188
|
+
(E = n == null ? void 0 : n.onFocus) == null ? void 0 : E.call(n, d(o)),
|
|
189
|
+
(R = i.onFocus) == null ? void 0 : R.call(i)
|
|
178
190
|
]
|
|
179
191
|
);
|
|
180
192
|
}
|
|
181
193
|
});
|
|
182
|
-
f(
|
|
194
|
+
f(N);
|
|
183
195
|
}
|
|
184
|
-
const
|
|
185
|
-
return
|
|
186
|
-
o
|
|
187
|
-
}),
|
|
188
|
-
},
|
|
196
|
+
const h = s.get(o);
|
|
197
|
+
return y(o), Y(() => {
|
|
198
|
+
V(o);
|
|
199
|
+
}), h;
|
|
200
|
+
}, c = (i) => l(i);
|
|
189
201
|
return {
|
|
190
|
-
fields:
|
|
191
|
-
getField: (
|
|
202
|
+
fields: p(() => [...s.values()]),
|
|
203
|
+
getField: (i) => l({ path: i }),
|
|
192
204
|
registerField: f,
|
|
193
|
-
deregisterField:
|
|
194
|
-
defineField:
|
|
205
|
+
deregisterField: m,
|
|
206
|
+
defineField: c
|
|
195
207
|
};
|
|
196
208
|
}
|
|
197
|
-
function
|
|
198
|
-
const e =
|
|
209
|
+
function ye(t) {
|
|
210
|
+
const e = p(() => t.fields.value.some((a) => d(a.dirty))), r = p(() => t.fields.value.some((a) => d(a.touched)));
|
|
199
211
|
return {
|
|
200
212
|
isDirty: e,
|
|
201
|
-
isTouched:
|
|
213
|
+
isTouched: r
|
|
202
214
|
};
|
|
203
215
|
}
|
|
204
|
-
function
|
|
205
|
-
return
|
|
206
|
-
(e,
|
|
216
|
+
function Fe(t) {
|
|
217
|
+
return t.filter(
|
|
218
|
+
(e, r, a) => a.indexOf(e) === r
|
|
207
219
|
);
|
|
208
220
|
}
|
|
209
|
-
function
|
|
210
|
-
return
|
|
211
|
-
if (!e && !
|
|
221
|
+
function G(...t) {
|
|
222
|
+
return t.slice(1).reduce((e, r) => {
|
|
223
|
+
if (!e && !r)
|
|
212
224
|
return;
|
|
213
|
-
const a = ((
|
|
214
|
-
if (!e && ((
|
|
215
|
-
return
|
|
225
|
+
const a = ((r == null ? void 0 : r.length) ?? 0) > 0;
|
|
226
|
+
if (!e && ((r == null ? void 0 : r.length) ?? 0) > 0)
|
|
227
|
+
return r;
|
|
216
228
|
if (!a)
|
|
217
229
|
return e;
|
|
218
|
-
const
|
|
219
|
-
return
|
|
220
|
-
},
|
|
230
|
+
const s = (e ?? []).concat(r);
|
|
231
|
+
return Fe(s);
|
|
232
|
+
}, t[0]);
|
|
221
233
|
}
|
|
222
|
-
function
|
|
223
|
-
return
|
|
224
|
-
const
|
|
234
|
+
function ge(...t) {
|
|
235
|
+
return t.map((r) => Object.keys(r)).flat().reduce((r, a) => {
|
|
236
|
+
const s = t.map((n) => n[a]).filter(Boolean);
|
|
225
237
|
return {
|
|
226
|
-
...
|
|
227
|
-
[a]:
|
|
238
|
+
...r,
|
|
239
|
+
[a]: G(...s)
|
|
228
240
|
};
|
|
229
241
|
}, {});
|
|
230
242
|
}
|
|
231
|
-
function
|
|
232
|
-
if (!
|
|
243
|
+
function C(...t) {
|
|
244
|
+
if (!t.length)
|
|
233
245
|
return {
|
|
234
246
|
general: [],
|
|
235
247
|
propertyErrors: {}
|
|
236
248
|
};
|
|
237
|
-
const e =
|
|
238
|
-
return
|
|
239
|
-
(
|
|
240
|
-
general:
|
|
241
|
-
propertyErrors:
|
|
249
|
+
const e = t[0];
|
|
250
|
+
return t.length === 1 ? e : t.slice(1).reduce(
|
|
251
|
+
(r, a) => ({
|
|
252
|
+
general: G(r.general, a.general),
|
|
253
|
+
propertyErrors: ge(r.propertyErrors ?? {}, a.propertyErrors ?? {})
|
|
242
254
|
}),
|
|
243
255
|
e
|
|
244
256
|
);
|
|
245
257
|
}
|
|
246
|
-
function
|
|
258
|
+
function A(t) {
|
|
247
259
|
var a;
|
|
248
|
-
const e = (((a =
|
|
249
|
-
return e ||
|
|
260
|
+
const e = (((a = t.general) == null ? void 0 : a.length) ?? 0) > 0, r = Object.entries(t.propertyErrors).filter(([, s]) => s == null ? void 0 : s.length).length > 0;
|
|
261
|
+
return e || r;
|
|
250
262
|
}
|
|
251
|
-
function
|
|
252
|
-
const e =
|
|
253
|
-
const
|
|
263
|
+
function Ee(t) {
|
|
264
|
+
const e = t.issues.filter((a) => a.path.length === 0).map((a) => a.message), r = t.issues.filter((a) => a.path.length > 0).reduce((a, s) => {
|
|
265
|
+
const n = s.path.join(".");
|
|
254
266
|
return {
|
|
255
267
|
...a,
|
|
256
|
-
[
|
|
268
|
+
[n]: [...a[n] ?? [], s.message]
|
|
257
269
|
};
|
|
258
270
|
}, {});
|
|
259
271
|
return {
|
|
260
272
|
general: e,
|
|
261
|
-
propertyErrors:
|
|
273
|
+
propertyErrors: r
|
|
262
274
|
};
|
|
263
275
|
}
|
|
264
|
-
const
|
|
276
|
+
const F = {
|
|
265
277
|
isValid: !0,
|
|
266
278
|
errors: {
|
|
267
279
|
general: [],
|
|
268
280
|
propertyErrors: {}
|
|
269
281
|
}
|
|
270
282
|
};
|
|
271
|
-
class
|
|
283
|
+
class we {
|
|
272
284
|
constructor(e) {
|
|
273
285
|
this.schema = e;
|
|
274
286
|
}
|
|
275
287
|
async validate(e) {
|
|
276
288
|
if (!this.schema)
|
|
277
|
-
return
|
|
278
|
-
const
|
|
279
|
-
if (
|
|
280
|
-
return
|
|
281
|
-
const a =
|
|
289
|
+
return F;
|
|
290
|
+
const r = await this.schema.safeParseAsync(e);
|
|
291
|
+
if (r.success)
|
|
292
|
+
return F;
|
|
293
|
+
const a = Ee(r.error);
|
|
282
294
|
return {
|
|
283
295
|
isValid: !1,
|
|
284
296
|
errors: {
|
|
@@ -288,229 +300,245 @@ class Ee {
|
|
|
288
300
|
};
|
|
289
301
|
}
|
|
290
302
|
}
|
|
291
|
-
class
|
|
303
|
+
class Re {
|
|
292
304
|
constructor(e) {
|
|
293
305
|
this.validateFn = e;
|
|
294
306
|
}
|
|
295
307
|
async validate(e) {
|
|
296
308
|
if (!this.validateFn)
|
|
297
|
-
return
|
|
309
|
+
return F;
|
|
298
310
|
try {
|
|
299
|
-
const
|
|
300
|
-
return
|
|
301
|
-
} catch (
|
|
311
|
+
const r = await this.validateFn(e);
|
|
312
|
+
return r.isValid ? F : r;
|
|
313
|
+
} catch (r) {
|
|
302
314
|
return {
|
|
303
315
|
isValid: !1,
|
|
304
316
|
errors: {
|
|
305
|
-
general: [
|
|
317
|
+
general: [r.message || "Validation error"],
|
|
306
318
|
propertyErrors: {}
|
|
307
319
|
}
|
|
308
320
|
};
|
|
309
321
|
}
|
|
310
322
|
}
|
|
311
323
|
}
|
|
312
|
-
class
|
|
313
|
-
constructor(e,
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
this.schema = e, this.validateFn =
|
|
324
|
+
class Pe {
|
|
325
|
+
constructor(e, r) {
|
|
326
|
+
S(this, "schemaValidator");
|
|
327
|
+
S(this, "functionValidator");
|
|
328
|
+
this.schema = e, this.validateFn = r, this.schemaValidator = new we(this.schema), this.functionValidator = new Re(this.validateFn);
|
|
317
329
|
}
|
|
318
330
|
async validate(e) {
|
|
319
|
-
const [
|
|
331
|
+
const [r, a] = await Promise.all([
|
|
320
332
|
this.schemaValidator.validate(e),
|
|
321
333
|
this.functionValidator.validate(e)
|
|
322
334
|
]);
|
|
323
335
|
return {
|
|
324
|
-
isValid:
|
|
325
|
-
errors:
|
|
336
|
+
isValid: r.isValid && a.isValid,
|
|
337
|
+
errors: C(r.errors, a.errors)
|
|
326
338
|
};
|
|
327
339
|
}
|
|
328
340
|
}
|
|
329
|
-
function
|
|
330
|
-
return
|
|
331
|
-
|
|
332
|
-
|
|
341
|
+
function I(t) {
|
|
342
|
+
return p(() => new Pe(
|
|
343
|
+
d(t.schema),
|
|
344
|
+
d(t.validateFn)
|
|
333
345
|
));
|
|
334
346
|
}
|
|
335
|
-
function De(
|
|
336
|
-
const
|
|
337
|
-
validators:
|
|
347
|
+
function De(t, e) {
|
|
348
|
+
const r = $({
|
|
349
|
+
validators: L([I(e)]),
|
|
338
350
|
isValidated: !1,
|
|
339
|
-
errors:
|
|
340
|
-
}), a = (
|
|
341
|
-
|
|
351
|
+
errors: d(e.errors) ?? F.errors
|
|
352
|
+
}), a = (l = F.errors) => {
|
|
353
|
+
r.errors = C(d(e.errors) ?? F.errors, l);
|
|
342
354
|
};
|
|
343
|
-
|
|
344
|
-
if (
|
|
345
|
-
const
|
|
346
|
-
a(
|
|
355
|
+
P(() => d(e.errors), async () => {
|
|
356
|
+
if (r.isValidated) {
|
|
357
|
+
const l = await n();
|
|
358
|
+
a(l.errors);
|
|
347
359
|
} else
|
|
348
360
|
a();
|
|
349
|
-
}, { immediate: !0 }),
|
|
350
|
-
[() =>
|
|
351
|
-
async (
|
|
352
|
-
if (
|
|
353
|
-
if (
|
|
354
|
-
const
|
|
355
|
-
|
|
361
|
+
}, { immediate: !0 }), P(
|
|
362
|
+
[() => r.validators],
|
|
363
|
+
async (l) => {
|
|
364
|
+
if (r.isValidated)
|
|
365
|
+
if (l) {
|
|
366
|
+
const c = await n();
|
|
367
|
+
r.errors = c.errors;
|
|
356
368
|
} else
|
|
357
|
-
|
|
369
|
+
r.errors = F.errors;
|
|
358
370
|
},
|
|
359
371
|
{ immediate: !0 }
|
|
360
|
-
),
|
|
361
|
-
|
|
372
|
+
), P([() => t.data, () => d(e.schema)], () => {
|
|
373
|
+
r.isValidated && f();
|
|
362
374
|
});
|
|
363
|
-
const
|
|
364
|
-
const
|
|
365
|
-
return
|
|
366
|
-
|
|
367
|
-
(
|
|
375
|
+
const s = (l) => {
|
|
376
|
+
const c = U(l) ? l : I(l);
|
|
377
|
+
return r.validators.push(c), te() && ae(() => {
|
|
378
|
+
r.validators = r.validators.filter(
|
|
379
|
+
(i) => i !== c
|
|
368
380
|
);
|
|
369
|
-
}),
|
|
381
|
+
}), c;
|
|
370
382
|
};
|
|
371
|
-
async function
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
),
|
|
375
|
-
let { errors:
|
|
376
|
-
if (!
|
|
377
|
-
const
|
|
378
|
-
|
|
383
|
+
async function n() {
|
|
384
|
+
const l = await Promise.all(
|
|
385
|
+
r.validators.filter((o) => d(o) !== void 0).map((o) => d(o).validate(t.data))
|
|
386
|
+
), c = l.every((o) => o.isValid);
|
|
387
|
+
let { errors: i } = F;
|
|
388
|
+
if (!c) {
|
|
389
|
+
const o = l.map((h) => h.errors);
|
|
390
|
+
i = C(...o);
|
|
379
391
|
}
|
|
380
392
|
return {
|
|
381
|
-
errors:
|
|
382
|
-
isValid:
|
|
393
|
+
errors: i,
|
|
394
|
+
isValid: c
|
|
383
395
|
};
|
|
384
396
|
}
|
|
385
397
|
const f = async () => {
|
|
386
|
-
const
|
|
387
|
-
return a(
|
|
388
|
-
isValid: !
|
|
389
|
-
errors:
|
|
398
|
+
const l = await n();
|
|
399
|
+
return a(l.errors), r.isValidated = !0, {
|
|
400
|
+
isValid: !A(l.errors),
|
|
401
|
+
errors: r.errors
|
|
390
402
|
};
|
|
391
|
-
},
|
|
392
|
-
|
|
403
|
+
}, m = async (l) => {
|
|
404
|
+
const c = await n();
|
|
405
|
+
return a({
|
|
406
|
+
general: c.errors.general,
|
|
407
|
+
propertyErrors: {
|
|
408
|
+
[l]: c.errors.propertyErrors[l]
|
|
409
|
+
}
|
|
410
|
+
}), {
|
|
411
|
+
isValid: !A(c.errors),
|
|
412
|
+
errors: r.errors
|
|
413
|
+
};
|
|
414
|
+
}, y = p(() => !A(r.errors)), V = () => {
|
|
415
|
+
r.isValidated = !1, r.errors = d(e.errors) ?? F.errors;
|
|
393
416
|
};
|
|
394
417
|
return {
|
|
395
|
-
...
|
|
418
|
+
...J(r),
|
|
396
419
|
validateForm: f,
|
|
397
|
-
|
|
398
|
-
|
|
420
|
+
validateField: m,
|
|
421
|
+
defineValidator: s,
|
|
422
|
+
isValid: y,
|
|
399
423
|
reset: V
|
|
400
424
|
};
|
|
401
425
|
}
|
|
402
426
|
class Se {
|
|
403
|
-
constructor(e,
|
|
404
|
-
this.path = e, this.validator =
|
|
427
|
+
constructor(e, r) {
|
|
428
|
+
this.path = e, this.validator = r;
|
|
405
429
|
}
|
|
406
430
|
async validate(e) {
|
|
407
|
-
const
|
|
431
|
+
const r = D(e, this.path);
|
|
408
432
|
if (!this.validator)
|
|
409
|
-
return
|
|
410
|
-
const a = await this.validator.validate(
|
|
433
|
+
return F;
|
|
434
|
+
const a = await this.validator.validate(r);
|
|
411
435
|
return {
|
|
412
436
|
isValid: a.isValid,
|
|
413
437
|
errors: {
|
|
414
438
|
general: a.errors.general || [],
|
|
415
439
|
propertyErrors: a.errors.propertyErrors ? Object.fromEntries(
|
|
416
|
-
Object.entries(a.errors.propertyErrors).map(([
|
|
417
|
-
|
|
418
|
-
|
|
440
|
+
Object.entries(a.errors.propertyErrors).map(([s, n]) => [
|
|
441
|
+
_(this.path, s),
|
|
442
|
+
n
|
|
419
443
|
])
|
|
420
444
|
) : {}
|
|
421
445
|
}
|
|
422
446
|
};
|
|
423
447
|
}
|
|
424
448
|
}
|
|
425
|
-
function
|
|
426
|
-
const a =
|
|
449
|
+
function _e(t, e, r) {
|
|
450
|
+
const a = x(t.data, e), s = p(() => D(t.initialData.value, e)), n = (u) => ({
|
|
427
451
|
...u,
|
|
428
|
-
path:
|
|
429
|
-
setData: (
|
|
430
|
-
u.setData(
|
|
452
|
+
path: p(() => d(u.path).replace(e + ".", "")),
|
|
453
|
+
setData: (v) => {
|
|
454
|
+
u.setData(v);
|
|
431
455
|
}
|
|
432
456
|
}), f = (u) => {
|
|
433
|
-
const
|
|
434
|
-
return
|
|
435
|
-
},
|
|
436
|
-
const
|
|
457
|
+
const v = _(e, u), w = t.getField(v);
|
|
458
|
+
return w ? n(w) : {};
|
|
459
|
+
}, m = (u) => {
|
|
460
|
+
const v = _(e, u.path), w = t.defineField({
|
|
437
461
|
...u,
|
|
438
|
-
path:
|
|
462
|
+
path: v
|
|
439
463
|
});
|
|
440
|
-
return
|
|
441
|
-
},
|
|
442
|
-
const
|
|
443
|
-
return
|
|
444
|
-
}).map((u) =>
|
|
445
|
-
const
|
|
446
|
-
return
|
|
447
|
-
}),
|
|
464
|
+
return n(w);
|
|
465
|
+
}, y = p(() => t.fields.value.filter((u) => {
|
|
466
|
+
const v = u.path.value;
|
|
467
|
+
return v.startsWith(e + ".") || v === e;
|
|
468
|
+
}).map((u) => n(u))), V = () => t.fields.value.filter((u) => {
|
|
469
|
+
const v = u.path.value;
|
|
470
|
+
return v.startsWith(e + ".") || v === e;
|
|
471
|
+
}), l = p(() => V().some((u) => u.dirty.value)), c = p(() => V().some((u) => u.touched.value)), i = p(() => t.isValid.value), o = p(() => t.isValidated.value), h = p(() => fe(d(t.errors), e));
|
|
448
472
|
return {
|
|
449
473
|
data: a,
|
|
450
|
-
fields:
|
|
451
|
-
initialData:
|
|
452
|
-
defineField:
|
|
474
|
+
fields: y,
|
|
475
|
+
initialData: s,
|
|
476
|
+
defineField: m,
|
|
453
477
|
getField: f,
|
|
454
|
-
isDirty:
|
|
455
|
-
isTouched:
|
|
456
|
-
isValid:
|
|
457
|
-
isValidated:
|
|
458
|
-
errors:
|
|
478
|
+
isDirty: l,
|
|
479
|
+
isTouched: c,
|
|
480
|
+
isValid: i,
|
|
481
|
+
isValidated: o,
|
|
482
|
+
errors: h,
|
|
459
483
|
defineValidator: (u) => {
|
|
460
|
-
const
|
|
461
|
-
() => new Se(e,
|
|
484
|
+
const v = U(u) ? u : I(u), w = p(
|
|
485
|
+
() => new Se(e, d(v))
|
|
462
486
|
);
|
|
463
|
-
return
|
|
487
|
+
return t.defineValidator(w), v;
|
|
464
488
|
},
|
|
465
|
-
reset: () =>
|
|
466
|
-
validateForm: () =>
|
|
467
|
-
getSubForm: (u,
|
|
468
|
-
const
|
|
469
|
-
return
|
|
470
|
-
|
|
471
|
-
|
|
489
|
+
reset: () => V().forEach((u) => u.reset()),
|
|
490
|
+
validateForm: () => t.validateForm(),
|
|
491
|
+
getSubForm: (u, v) => {
|
|
492
|
+
const w = _(e, u);
|
|
493
|
+
return t.getSubForm(
|
|
494
|
+
w,
|
|
495
|
+
v
|
|
472
496
|
);
|
|
473
497
|
}
|
|
474
498
|
};
|
|
475
499
|
}
|
|
476
|
-
function
|
|
477
|
-
const e =
|
|
500
|
+
function Oe(t) {
|
|
501
|
+
const e = p(
|
|
502
|
+
() => Object.freeze(g(t.initialData))
|
|
503
|
+
), r = L(g(e)), a = $({
|
|
478
504
|
initialData: e,
|
|
479
|
-
data:
|
|
505
|
+
data: r
|
|
480
506
|
});
|
|
481
|
-
|
|
507
|
+
P(
|
|
482
508
|
e,
|
|
483
|
-
(
|
|
484
|
-
a.data =
|
|
509
|
+
(c) => {
|
|
510
|
+
a.data = g(c);
|
|
485
511
|
},
|
|
486
512
|
{ flush: "sync" }
|
|
487
513
|
);
|
|
488
|
-
const
|
|
489
|
-
keepValuesOnUnmount:
|
|
490
|
-
onBlur: async () => {
|
|
491
|
-
|
|
514
|
+
const s = De(a, t), n = Ve(a, s, {
|
|
515
|
+
keepValuesOnUnmount: t.keepValuesOnUnmount,
|
|
516
|
+
onBlur: async (c) => {
|
|
517
|
+
d(t.validationStrategy) === "onTouch" && s.validateField(c);
|
|
492
518
|
}
|
|
493
|
-
}), f =
|
|
494
|
-
|
|
495
|
-
},
|
|
496
|
-
|
|
519
|
+
}), f = ye(n), m = (c) => async (i) => {
|
|
520
|
+
i.preventDefault(), d(t.validationStrategy) !== "none" && await s.validateForm(), s.isValid.value && await c(a.data);
|
|
521
|
+
}, y = () => {
|
|
522
|
+
r.value = g(e), s.reset();
|
|
523
|
+
for (const c of n.fields.value)
|
|
524
|
+
c.reset();
|
|
497
525
|
};
|
|
498
|
-
function
|
|
499
|
-
return
|
|
526
|
+
function V(c, i) {
|
|
527
|
+
return _e(l, c);
|
|
500
528
|
}
|
|
501
|
-
const
|
|
502
|
-
...i,
|
|
529
|
+
const l = {
|
|
503
530
|
...n,
|
|
531
|
+
...s,
|
|
504
532
|
...f,
|
|
505
|
-
reset:
|
|
506
|
-
getSubForm:
|
|
507
|
-
submitHandler:
|
|
508
|
-
initialData:
|
|
509
|
-
data:
|
|
533
|
+
reset: y,
|
|
534
|
+
getSubForm: V,
|
|
535
|
+
submitHandler: m,
|
|
536
|
+
initialData: O(a, "initialData"),
|
|
537
|
+
data: O(a, "data")
|
|
510
538
|
};
|
|
511
|
-
return
|
|
539
|
+
return d(t.validationStrategy) === "onFormOpen" && s.validateForm(), l;
|
|
512
540
|
}
|
|
513
|
-
const
|
|
541
|
+
const Ce = /* @__PURE__ */ k({
|
|
514
542
|
__name: "Field",
|
|
515
543
|
props: {
|
|
516
544
|
form: {},
|
|
@@ -518,16 +546,17 @@ const Ae = /* @__PURE__ */ k({
|
|
|
518
546
|
initialValue: {},
|
|
519
547
|
path: {},
|
|
520
548
|
errors: {},
|
|
549
|
+
existsInForm: {},
|
|
521
550
|
onBlur: { type: Function },
|
|
522
551
|
onFocus: { type: Function }
|
|
523
552
|
},
|
|
524
|
-
setup(
|
|
525
|
-
const e =
|
|
553
|
+
setup(t) {
|
|
554
|
+
const e = t, r = e.form.defineField({
|
|
526
555
|
path: e.path
|
|
527
|
-
}), a =
|
|
528
|
-
return (
|
|
556
|
+
}), a = $(r);
|
|
557
|
+
return (s, n) => b(s.$slots, "default", M(z(a)));
|
|
529
558
|
}
|
|
530
|
-
}),
|
|
559
|
+
}), Ie = /* @__PURE__ */ k({
|
|
531
560
|
inheritAttrs: !1,
|
|
532
561
|
__name: "FormFieldWrapper",
|
|
533
562
|
props: {
|
|
@@ -536,29 +565,29 @@ const Ae = /* @__PURE__ */ k({
|
|
|
536
565
|
form: {},
|
|
537
566
|
path: {}
|
|
538
567
|
},
|
|
539
|
-
setup(
|
|
540
|
-
return (e,
|
|
568
|
+
setup(t) {
|
|
569
|
+
return (e, r) => {
|
|
541
570
|
const a = se("Field");
|
|
542
|
-
return
|
|
571
|
+
return T(), K(a, {
|
|
543
572
|
form: e.form,
|
|
544
573
|
path: e.path
|
|
545
574
|
}, {
|
|
546
|
-
default: j(({ errors:
|
|
547
|
-
(
|
|
548
|
-
"model-value":
|
|
549
|
-
errors:
|
|
575
|
+
default: j(({ errors: s, data: n, setData: f }) => [
|
|
576
|
+
(T(), K(ne(e.component), oe({ ...e.componentProps, ...e.$attrs }, {
|
|
577
|
+
"model-value": n,
|
|
578
|
+
errors: s,
|
|
550
579
|
name: e.path,
|
|
551
580
|
"onUpdate:modelValue": f
|
|
552
|
-
}),
|
|
581
|
+
}), ie({
|
|
553
582
|
default: j(() => [
|
|
554
|
-
|
|
583
|
+
b(e.$slots, "default")
|
|
555
584
|
]),
|
|
556
585
|
_: 2
|
|
557
586
|
}, [
|
|
558
|
-
le(e.$slots, (
|
|
559
|
-
name:
|
|
560
|
-
fn: j((
|
|
561
|
-
|
|
587
|
+
le(e.$slots, (m, y) => ({
|
|
588
|
+
name: y,
|
|
589
|
+
fn: j((V) => [
|
|
590
|
+
b(e.$slots, y, M(z(V ?? {})))
|
|
562
591
|
])
|
|
563
592
|
}))
|
|
564
593
|
]), 1040, ["model-value", "errors", "name", "onUpdate:modelValue"]))
|
|
@@ -573,14 +602,14 @@ const Ae = /* @__PURE__ */ k({
|
|
|
573
602
|
form: {},
|
|
574
603
|
path: {}
|
|
575
604
|
},
|
|
576
|
-
setup(
|
|
577
|
-
const e =
|
|
578
|
-
return (a,
|
|
605
|
+
setup(t) {
|
|
606
|
+
const e = t, r = p(() => e.form.getSubForm(e.path));
|
|
607
|
+
return (a, s) => b(a.$slots, "default", M(z({ subform: r.value })));
|
|
579
608
|
}
|
|
580
609
|
});
|
|
581
610
|
export {
|
|
582
|
-
|
|
583
|
-
|
|
611
|
+
Ce as Field,
|
|
612
|
+
Ie as FormFieldWrapper,
|
|
584
613
|
Ue as FormPart,
|
|
585
|
-
|
|
614
|
+
Oe as useForm
|
|
586
615
|
};
|