@teamnovu/kit-vue-forms 0.2.13 → 0.2.14
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.
|
@@ -19,6 +19,7 @@ interface FieldRegistryOptions {
|
|
|
19
19
|
onBlur?: (path: string) => Awaitable<void>;
|
|
20
20
|
onFocus?: (path: string) => Awaitable<void>;
|
|
21
21
|
onChange?: <T>(path: string, value: T) => Awaitable<void>;
|
|
22
|
+
onRegistered?: <T>(path: string, value: T) => Awaitable<void>;
|
|
22
23
|
}
|
|
23
24
|
export declare function useFieldRegistry<T extends FormDataDefault, TOut = T>(formState: FormState<T>, validationState: ValidationState<T, TOut>, fieldRegistryOptions?: FieldRegistryOptions): {
|
|
24
25
|
fields: ComputedRef<FieldsTuple<T>>;
|
|
@@ -9,6 +9,7 @@ export interface ValidationFlags {
|
|
|
9
9
|
validateOnFormOpen?: MaybeRefOrGetter<boolean>;
|
|
10
10
|
validateOnSubmit?: MaybeRefOrGetter<boolean>;
|
|
11
11
|
validateOnDataChange?: MaybeRefOrGetter<boolean>;
|
|
12
|
+
validateOnFieldRegister?: MaybeRefOrGetter<boolean>;
|
|
12
13
|
}
|
|
13
14
|
export interface ValidatorOptions<T, TOut = T> {
|
|
14
15
|
schema?: MaybeRef<z.ZodType<TOut, unknown> | undefined>;
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { toValue as
|
|
5
|
-
import { cloneDeep as
|
|
1
|
+
var te = Object.defineProperty;
|
|
2
|
+
var re = (r, t, e) => t in r ? te(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
|
+
var P = (r, t, e) => re(r, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import { toValue as J, toRaw as ae, shallowRef as O, watch as b, computed as m, unref as h, isRef as x, reactive as A, toRefs as G, shallowReactive as se, toRef as U, onScopeDispose as ne, triggerRef as ie, ref as Z, getCurrentScope as oe, onBeforeUnmount as le, defineComponent as N, renderSlot as C, normalizeProps as T, guardReactiveProps as W, createBlock as K, openBlock as L, withCtx as M, resolveDynamicComponent as ue, mergeProps as ce, createSlots as de, renderList as fe } from "vue";
|
|
5
|
+
import { cloneDeep as he, merge as ve, omit as me } from "lodash-es";
|
|
6
6
|
import "zod";
|
|
7
7
|
function D(r) {
|
|
8
|
-
const t =
|
|
9
|
-
return
|
|
8
|
+
const t = J(r), e = ae(t);
|
|
9
|
+
return he(e);
|
|
10
10
|
}
|
|
11
|
-
function
|
|
11
|
+
function pe(r) {
|
|
12
12
|
return r.filter(
|
|
13
13
|
(t, e, a) => a.indexOf(t) === e
|
|
14
14
|
);
|
|
15
15
|
}
|
|
16
|
-
function
|
|
16
|
+
function q(...r) {
|
|
17
17
|
return r.slice(1).reduce((t, e) => {
|
|
18
18
|
if (!t && !e)
|
|
19
19
|
return;
|
|
@@ -23,19 +23,19 @@ function Z(...r) {
|
|
|
23
23
|
if (!a)
|
|
24
24
|
return t;
|
|
25
25
|
const s = (t ?? []).concat(e);
|
|
26
|
-
return
|
|
26
|
+
return pe(s);
|
|
27
27
|
}, r[0]) ?? [];
|
|
28
28
|
}
|
|
29
|
-
function
|
|
29
|
+
function Fe(...r) {
|
|
30
30
|
return r.map((e) => Object.keys(e)).flat().reduce((e, a) => {
|
|
31
31
|
const s = r.map((n) => n[a]).filter((n) => !!n);
|
|
32
32
|
return {
|
|
33
33
|
...e,
|
|
34
|
-
[a]:
|
|
34
|
+
[a]: q(e[a], ...s)
|
|
35
35
|
};
|
|
36
36
|
}, {});
|
|
37
37
|
}
|
|
38
|
-
function
|
|
38
|
+
function j(...r) {
|
|
39
39
|
if (!r.length)
|
|
40
40
|
return {
|
|
41
41
|
general: [],
|
|
@@ -44,25 +44,25 @@ function U(...r) {
|
|
|
44
44
|
const t = r[0];
|
|
45
45
|
return r.length === 1 ? t : r.slice(1).reduce(
|
|
46
46
|
(e, a) => ({
|
|
47
|
-
general:
|
|
48
|
-
propertyErrors:
|
|
47
|
+
general: q(e.general, a.general),
|
|
48
|
+
propertyErrors: Fe(e.propertyErrors ?? {}, a.propertyErrors ?? {})
|
|
49
49
|
}),
|
|
50
50
|
t
|
|
51
51
|
);
|
|
52
52
|
}
|
|
53
|
-
function
|
|
53
|
+
function Q(r) {
|
|
54
54
|
var a;
|
|
55
55
|
const t = (((a = r.general) == null ? void 0 : a.length) ?? 0) > 0, e = Object.entries(r.propertyErrors).filter(([, s]) => s == null ? void 0 : s.length).length > 0;
|
|
56
56
|
return t || e;
|
|
57
57
|
}
|
|
58
|
-
function
|
|
59
|
-
return !
|
|
58
|
+
function z(r) {
|
|
59
|
+
return !Q(r.errors);
|
|
60
60
|
}
|
|
61
|
-
function
|
|
61
|
+
function X(r, t) {
|
|
62
62
|
return (e) => async (a) => {
|
|
63
63
|
if (a == null || a.preventDefault(), t.canValidate("validateOnSubmit")) {
|
|
64
64
|
const s = await r.validateForm();
|
|
65
|
-
if (!
|
|
65
|
+
if (!z(s))
|
|
66
66
|
return;
|
|
67
67
|
const n = s.data ?? r.data.value;
|
|
68
68
|
await e(n);
|
|
@@ -70,11 +70,11 @@ function Q(r, t) {
|
|
|
70
70
|
await e(r.data.value);
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
|
-
class
|
|
73
|
+
class ye {
|
|
74
74
|
constructor(t = (e) => e) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
P(this, "weakMap", /* @__PURE__ */ new WeakMap());
|
|
76
|
+
P(this, "map", /* @__PURE__ */ new Map());
|
|
77
|
+
P(this, "hashFn");
|
|
78
78
|
this.hashFn = t;
|
|
79
79
|
}
|
|
80
80
|
isReferenceType(t) {
|
|
@@ -93,10 +93,10 @@ class Fe {
|
|
|
93
93
|
this.isReferenceType(a) ? this.weakMap.set(a, e) : this.map.set(a, e);
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
-
function
|
|
96
|
+
function Ve(r, t, e) {
|
|
97
97
|
const a = /* @__PURE__ */ new Set();
|
|
98
98
|
return t.map((s, n) => {
|
|
99
|
-
const c = [...r.get(s) ?? []], v = c.findIndex((
|
|
99
|
+
const c = [...r.get(s) ?? []], v = c.findIndex((F) => !a.has(F)), l = (v === -1 ? [] : c.slice(v))[0];
|
|
100
100
|
if (l)
|
|
101
101
|
return a.add(l), {
|
|
102
102
|
id: l,
|
|
@@ -111,12 +111,12 @@ function ye(r, t, e) {
|
|
|
111
111
|
};
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
|
-
function
|
|
115
|
-
const a = new
|
|
116
|
-
return
|
|
114
|
+
function Y(r, t, e) {
|
|
115
|
+
const a = new ye(e == null ? void 0 : e.hashFn), s = r.getField(t), n = O([]);
|
|
116
|
+
return b(
|
|
117
117
|
s.data,
|
|
118
118
|
(l) => {
|
|
119
|
-
n.value =
|
|
119
|
+
n.value = Ve(a, l, t);
|
|
120
120
|
},
|
|
121
121
|
{
|
|
122
122
|
immediate: !0,
|
|
@@ -129,37 +129,37 @@ function X(r, t, e) {
|
|
|
129
129
|
return s.setData([...d, l]), n.value.at(-1);
|
|
130
130
|
},
|
|
131
131
|
remove: (l) => {
|
|
132
|
-
const d = s.data.value ?? [],
|
|
132
|
+
const d = s.data.value ?? [], F = n.value.findIndex(
|
|
133
133
|
({ id: i }) => i === l
|
|
134
134
|
);
|
|
135
|
-
|
|
136
|
-
d.slice(0,
|
|
135
|
+
F !== -1 && s.setData(
|
|
136
|
+
d.slice(0, F).concat(d.slice(F + 1))
|
|
137
137
|
);
|
|
138
138
|
},
|
|
139
139
|
insert: (l, d) => {
|
|
140
|
-
const
|
|
140
|
+
const F = s.data.value ?? [];
|
|
141
141
|
return s.setData(
|
|
142
|
-
|
|
142
|
+
F.slice(0, d).concat([l]).concat(F.slice(d))
|
|
143
143
|
), n.value[d];
|
|
144
144
|
},
|
|
145
145
|
field: s
|
|
146
146
|
};
|
|
147
147
|
}
|
|
148
|
-
function
|
|
148
|
+
function H(r) {
|
|
149
149
|
return r === "" ? [] : r.split(/\s*\.\s*/).filter(Boolean);
|
|
150
150
|
}
|
|
151
|
-
function
|
|
152
|
-
const e = Array.isArray(t) ? t :
|
|
153
|
-
return
|
|
151
|
+
function ge(r, t) {
|
|
152
|
+
const e = Array.isArray(t) ? t : H(t);
|
|
153
|
+
return !!$(r, e.slice(0, -1));
|
|
154
154
|
}
|
|
155
|
-
function
|
|
156
|
-
return (Array.isArray(t) ? t :
|
|
155
|
+
function $(r, t) {
|
|
156
|
+
return (Array.isArray(t) ? t : H(t)).reduce(
|
|
157
157
|
(a, s) => a == null ? void 0 : a[s],
|
|
158
158
|
r
|
|
159
159
|
);
|
|
160
160
|
}
|
|
161
|
-
function
|
|
162
|
-
const a = Array.isArray(t) ? t :
|
|
161
|
+
function we(r, t, e) {
|
|
162
|
+
const a = Array.isArray(t) ? t : H(t), s = a.at(-1);
|
|
163
163
|
if (s) {
|
|
164
164
|
const n = a.slice(0, -1).reduce(
|
|
165
165
|
(c, v) => ((c == null ? void 0 : c[v]) === void 0 && (c[v] = {}), c == null ? void 0 : c[v]),
|
|
@@ -168,23 +168,23 @@ function ge(r, t, e) {
|
|
|
168
168
|
);
|
|
169
169
|
n[s] = e;
|
|
170
170
|
} else {
|
|
171
|
-
if (!
|
|
171
|
+
if (!x(r))
|
|
172
172
|
return;
|
|
173
173
|
r.value = e;
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
|
-
const
|
|
176
|
+
const ee = (r, t) => m({
|
|
177
177
|
get() {
|
|
178
|
-
return
|
|
178
|
+
return $(h(r), h(t));
|
|
179
179
|
},
|
|
180
180
|
set(e) {
|
|
181
|
-
|
|
181
|
+
we(r, h(t), e);
|
|
182
182
|
}
|
|
183
183
|
});
|
|
184
|
-
function
|
|
184
|
+
function I(r, t) {
|
|
185
185
|
return !r && !t ? "" : !r && t ? t : !t && r ? r : `${r}.${t}`;
|
|
186
186
|
}
|
|
187
|
-
function
|
|
187
|
+
function Ee(r, t) {
|
|
188
188
|
if (!t)
|
|
189
189
|
return r;
|
|
190
190
|
const e = `${t}.`, a = Object.fromEntries(
|
|
@@ -199,9 +199,9 @@ function we(r, t) {
|
|
|
199
199
|
propertyErrors: a
|
|
200
200
|
};
|
|
201
201
|
}
|
|
202
|
-
class
|
|
202
|
+
class Re {
|
|
203
203
|
constructor(t) {
|
|
204
|
-
|
|
204
|
+
P(this, "rc", 1);
|
|
205
205
|
this.drop = t;
|
|
206
206
|
}
|
|
207
207
|
inc() {
|
|
@@ -217,29 +217,29 @@ function De(r) {
|
|
|
217
217
|
existsInForm: !0
|
|
218
218
|
},
|
|
219
219
|
...r
|
|
220
|
-
}, a =
|
|
220
|
+
}, a = O(Object.freeze(D(e.initialValue))), s = A({
|
|
221
221
|
value: e.value,
|
|
222
222
|
path: e.path,
|
|
223
223
|
initialValue: a,
|
|
224
224
|
errors: e.errors,
|
|
225
225
|
touched: !1
|
|
226
226
|
});
|
|
227
|
-
|
|
228
|
-
|
|
227
|
+
b(
|
|
228
|
+
O(e.initialValue),
|
|
229
229
|
() => {
|
|
230
230
|
a.value = Object.freeze(D(e.initialValue)), s.value !== h(e.initialValue) && (s.value = D(e.initialValue));
|
|
231
231
|
},
|
|
232
232
|
{ flush: "sync" }
|
|
233
|
-
),
|
|
233
|
+
), b(() => s.value, (f) => {
|
|
234
234
|
var E;
|
|
235
235
|
(E = r.onChange) == null || E.call(r, f);
|
|
236
236
|
}, { deep: !0 });
|
|
237
|
-
const n =
|
|
237
|
+
const n = m(() => JSON.stringify(s.value) !== JSON.stringify(s.initialValue)), c = (f) => {
|
|
238
238
|
s.value = f;
|
|
239
239
|
}, v = () => {
|
|
240
240
|
var f;
|
|
241
241
|
s.touched = !0, s.errors = [], (f = e.onBlur) == null || f.call(e);
|
|
242
|
-
},
|
|
242
|
+
}, y = () => {
|
|
243
243
|
var f;
|
|
244
244
|
(f = e.onFocus) == null || f.call(e);
|
|
245
245
|
}, l = () => {
|
|
@@ -247,11 +247,11 @@ function De(r) {
|
|
|
247
247
|
h(e.existsInForm) && !/^\d+$/.test(f) && (s.value = D(s.initialValue)), s.touched = !1, s.errors = [];
|
|
248
248
|
}, d = (f) => {
|
|
249
249
|
n.value || c(D(f)), s.initialValue = f;
|
|
250
|
-
},
|
|
250
|
+
}, F = (f) => {
|
|
251
251
|
s.errors = f;
|
|
252
252
|
}, i = () => {
|
|
253
253
|
s.errors = [];
|
|
254
|
-
}, o =
|
|
254
|
+
}, o = G(s);
|
|
255
255
|
return {
|
|
256
256
|
data: o.value,
|
|
257
257
|
path: o.path,
|
|
@@ -262,28 +262,28 @@ function De(r) {
|
|
|
262
262
|
setData: c,
|
|
263
263
|
setInitialData: d,
|
|
264
264
|
onBlur: v,
|
|
265
|
-
onFocus:
|
|
265
|
+
onFocus: y,
|
|
266
266
|
reset: l,
|
|
267
|
-
setErrors:
|
|
267
|
+
setErrors: F,
|
|
268
268
|
clearErrors: i
|
|
269
269
|
};
|
|
270
270
|
}
|
|
271
|
-
const
|
|
271
|
+
const Se = {
|
|
272
272
|
keepValuesOnUnmount: !0
|
|
273
273
|
};
|
|
274
|
-
function
|
|
275
|
-
const e = () =>
|
|
276
|
-
return
|
|
274
|
+
function be(r, t) {
|
|
275
|
+
const e = () => $(r.initialData, t), a = O(e());
|
|
276
|
+
return b(
|
|
277
277
|
() => r.initialData,
|
|
278
278
|
() => {
|
|
279
|
-
a.value = e(),
|
|
279
|
+
a.value = e(), ie(a);
|
|
280
280
|
},
|
|
281
281
|
{ flush: "sync" }
|
|
282
282
|
), a;
|
|
283
283
|
}
|
|
284
|
-
function
|
|
285
|
-
const a = /* @__PURE__ */ new Map(), s =
|
|
286
|
-
...
|
|
284
|
+
function Pe(r, t, e) {
|
|
285
|
+
const a = /* @__PURE__ */ new Map(), s = se(/* @__PURE__ */ new Map()), n = {
|
|
286
|
+
...Se,
|
|
287
287
|
...e
|
|
288
288
|
}, c = (i) => {
|
|
289
289
|
const o = h(i.path);
|
|
@@ -291,73 +291,74 @@ function be(r, t, e) {
|
|
|
291
291
|
}, v = (i) => {
|
|
292
292
|
var o;
|
|
293
293
|
n != null && n.keepValuesOnUnmount || (o = s.get(i)) == null || o.reset(), s.delete(i);
|
|
294
|
-
},
|
|
294
|
+
}, y = (i) => {
|
|
295
295
|
var o;
|
|
296
|
-
a.has(i) ? (o = a.get(i)) == null || o.inc() : a.set(i, new
|
|
296
|
+
a.has(i) ? (o = a.get(i)) == null || o.inc() : a.set(i, new Re(() => v(i)));
|
|
297
297
|
}, l = (i) => {
|
|
298
298
|
var o;
|
|
299
299
|
a.has(i) && ((o = a.get(i)) == null || o.dec());
|
|
300
300
|
}, d = (i) => {
|
|
301
|
+
var E;
|
|
301
302
|
const { path: o } = i;
|
|
302
303
|
if (!s.has(o)) {
|
|
303
|
-
const
|
|
304
|
+
const g = De({
|
|
304
305
|
path: o,
|
|
305
|
-
value:
|
|
306
|
-
initialValue:
|
|
307
|
-
existsInForm:
|
|
308
|
-
errors:
|
|
306
|
+
value: ee(U(r, "data"), o),
|
|
307
|
+
initialValue: be(r, o),
|
|
308
|
+
existsInForm: m(() => ge(r.data, h(o))),
|
|
309
|
+
errors: m({
|
|
309
310
|
get() {
|
|
310
311
|
return t.errors.value.propertyErrors[o] || [];
|
|
311
312
|
},
|
|
312
|
-
set(
|
|
313
|
-
t.errors.value.propertyErrors[o] =
|
|
313
|
+
set(V) {
|
|
314
|
+
t.errors.value.propertyErrors[o] = V;
|
|
314
315
|
}
|
|
315
316
|
}),
|
|
316
317
|
onBlur: async () => {
|
|
317
|
-
var
|
|
318
|
+
var V, R;
|
|
318
319
|
await Promise.all([
|
|
319
|
-
(
|
|
320
|
-
(
|
|
320
|
+
(V = n == null ? void 0 : n.onBlur) == null ? void 0 : V.call(n, h(o)),
|
|
321
|
+
(R = i.onBlur) == null ? void 0 : R.call(i)
|
|
321
322
|
]);
|
|
322
323
|
},
|
|
323
324
|
onFocus: async () => {
|
|
324
|
-
var
|
|
325
|
+
var V, R;
|
|
325
326
|
await Promise.all([
|
|
326
|
-
(
|
|
327
|
-
(
|
|
327
|
+
(V = n == null ? void 0 : n.onFocus) == null ? void 0 : V.call(n, h(o)),
|
|
328
|
+
(R = i.onFocus) == null ? void 0 : R.call(i)
|
|
328
329
|
]);
|
|
329
330
|
},
|
|
330
|
-
onChange: async (
|
|
331
|
-
var
|
|
331
|
+
onChange: async (V) => {
|
|
332
|
+
var R, B;
|
|
332
333
|
await Promise.all([
|
|
333
|
-
(
|
|
334
|
-
(
|
|
334
|
+
(R = n == null ? void 0 : n.onChange) == null ? void 0 : R.call(n, h(o), V),
|
|
335
|
+
(B = i.onChange) == null ? void 0 : B.call(i, V)
|
|
335
336
|
]);
|
|
336
337
|
}
|
|
337
338
|
});
|
|
338
|
-
c(
|
|
339
|
+
c(g);
|
|
339
340
|
}
|
|
340
341
|
const f = s.get(o);
|
|
341
|
-
return
|
|
342
|
+
return y(o), ne(() => {
|
|
342
343
|
l(o);
|
|
343
|
-
}), f;
|
|
344
|
-
},
|
|
344
|
+
}), (E = n.onRegistered) == null || E.call(n, o, f), f;
|
|
345
|
+
}, F = (i) => d(i);
|
|
345
346
|
return {
|
|
346
|
-
fields:
|
|
347
|
+
fields: m(() => [...s.values()]),
|
|
347
348
|
getField: (i) => d({ path: i }),
|
|
348
349
|
registerField: c,
|
|
349
350
|
deregisterField: v,
|
|
350
|
-
defineField:
|
|
351
|
+
defineField: F
|
|
351
352
|
};
|
|
352
353
|
}
|
|
353
|
-
function
|
|
354
|
-
const t =
|
|
354
|
+
function $e(r) {
|
|
355
|
+
const t = m(() => r.fields.value.some((a) => h(a.dirty))), e = m(() => r.fields.value.some((a) => h(a.touched)));
|
|
355
356
|
return {
|
|
356
357
|
isDirty: t,
|
|
357
358
|
isTouched: e
|
|
358
359
|
};
|
|
359
360
|
}
|
|
360
|
-
function
|
|
361
|
+
function Ie(r) {
|
|
361
362
|
const t = r.issues.filter((a) => a.path.length === 0).map((a) => a.message), e = r.issues.filter((a) => a.path.length > 0).reduce((a, s) => {
|
|
362
363
|
const n = s.path.join(".");
|
|
363
364
|
return {
|
|
@@ -370,18 +371,20 @@ function $e(r) {
|
|
|
370
371
|
propertyErrors: e
|
|
371
372
|
};
|
|
372
373
|
}
|
|
373
|
-
const
|
|
374
|
+
const Oe = {
|
|
374
375
|
validationBeforeSubmit: {
|
|
375
376
|
validateOnBlur: !1,
|
|
376
377
|
validateOnFormOpen: !1,
|
|
377
378
|
validateOnSubmit: !0,
|
|
378
|
-
validateOnDataChange: !1
|
|
379
|
+
validateOnDataChange: !1,
|
|
380
|
+
validateOnFieldRegister: !1
|
|
379
381
|
},
|
|
380
382
|
validationAfterSubmit: {
|
|
381
383
|
validateOnBlur: !1,
|
|
382
384
|
validateOnFormOpen: !1,
|
|
383
385
|
validateOnSubmit: !0,
|
|
384
|
-
validateOnDataChange: !0
|
|
386
|
+
validateOnDataChange: !0,
|
|
387
|
+
validateOnFieldRegister: !0
|
|
385
388
|
}
|
|
386
389
|
}, w = {
|
|
387
390
|
errors: {
|
|
@@ -399,7 +402,7 @@ class Ce {
|
|
|
399
402
|
const e = await this.schema.safeParseAsync(t);
|
|
400
403
|
if (e.success)
|
|
401
404
|
return w;
|
|
402
|
-
const a =
|
|
405
|
+
const a = Ie(e.error);
|
|
403
406
|
return {
|
|
404
407
|
data: e.data,
|
|
405
408
|
errors: {
|
|
@@ -409,7 +412,7 @@ class Ce {
|
|
|
409
412
|
};
|
|
410
413
|
}
|
|
411
414
|
}
|
|
412
|
-
class
|
|
415
|
+
class Ae {
|
|
413
416
|
constructor(t) {
|
|
414
417
|
this.validateFn = t;
|
|
415
418
|
}
|
|
@@ -418,7 +421,7 @@ class Oe {
|
|
|
418
421
|
return w;
|
|
419
422
|
try {
|
|
420
423
|
const e = await this.validateFn(t);
|
|
421
|
-
return
|
|
424
|
+
return z(e) ? w : e;
|
|
422
425
|
} catch (e) {
|
|
423
426
|
return {
|
|
424
427
|
errors: {
|
|
@@ -429,11 +432,11 @@ class Oe {
|
|
|
429
432
|
}
|
|
430
433
|
}
|
|
431
434
|
}
|
|
432
|
-
class
|
|
435
|
+
class Be {
|
|
433
436
|
constructor(t, e) {
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
this.schema = t, this.validateFn = e, this.schemaValidator = new Ce(this.schema), this.functionValidator = new
|
|
437
|
+
P(this, "schemaValidator");
|
|
438
|
+
P(this, "functionValidator");
|
|
439
|
+
this.schema = t, this.validateFn = e, this.schemaValidator = new Ce(this.schema), this.functionValidator = new Ae(this.validateFn);
|
|
437
440
|
}
|
|
438
441
|
async validate(t) {
|
|
439
442
|
const [e, a] = await Promise.all([
|
|
@@ -442,31 +445,31 @@ class Ae {
|
|
|
442
445
|
]);
|
|
443
446
|
return {
|
|
444
447
|
data: e.data,
|
|
445
|
-
errors:
|
|
448
|
+
errors: j(e.errors, a.errors)
|
|
446
449
|
};
|
|
447
450
|
}
|
|
448
451
|
}
|
|
449
|
-
function
|
|
450
|
-
return
|
|
452
|
+
function _(r) {
|
|
453
|
+
return m(() => new Be(
|
|
451
454
|
h(r.schema),
|
|
452
455
|
h(r.validateFn)
|
|
453
456
|
));
|
|
454
457
|
}
|
|
455
|
-
function
|
|
456
|
-
const e =
|
|
457
|
-
validators:
|
|
458
|
+
function ke(r, t) {
|
|
459
|
+
const e = A({
|
|
460
|
+
validators: Z([_(t)]),
|
|
458
461
|
isValidated: !1,
|
|
459
462
|
errors: h(t.errors) ?? w.errors
|
|
460
463
|
}), a = (i = w.errors) => {
|
|
461
|
-
e.errors =
|
|
464
|
+
e.errors = j(h(t.errors) ?? w.errors, i);
|
|
462
465
|
};
|
|
463
|
-
|
|
466
|
+
b(() => h(t.errors), async () => {
|
|
464
467
|
if (e.isValidated) {
|
|
465
468
|
const i = await n();
|
|
466
469
|
a(i.errors);
|
|
467
470
|
} else
|
|
468
471
|
a();
|
|
469
|
-
}, { immediate: !0 }),
|
|
472
|
+
}, { immediate: !0 }), b(
|
|
470
473
|
[() => e.validators],
|
|
471
474
|
async (i) => {
|
|
472
475
|
if (e.isValidated)
|
|
@@ -477,12 +480,12 @@ function Be(r, t) {
|
|
|
477
480
|
e.errors = w.errors;
|
|
478
481
|
},
|
|
479
482
|
{ immediate: !0 }
|
|
480
|
-
),
|
|
483
|
+
), b([() => r.data, () => h(t.schema)], () => {
|
|
481
484
|
e.isValidated && c();
|
|
482
485
|
});
|
|
483
486
|
const s = (i) => {
|
|
484
|
-
const o =
|
|
485
|
-
return e.validators.push(o),
|
|
487
|
+
const o = x(i) ? i : _(i);
|
|
488
|
+
return e.validators.push(o), oe() && le(() => {
|
|
486
489
|
e.validators = e.validators.filter(
|
|
487
490
|
(f) => f !== o
|
|
488
491
|
);
|
|
@@ -491,17 +494,17 @@ function Be(r, t) {
|
|
|
491
494
|
async function n() {
|
|
492
495
|
var E;
|
|
493
496
|
const i = await Promise.all(
|
|
494
|
-
e.validators.filter((
|
|
495
|
-
), o = i.every((
|
|
497
|
+
e.validators.filter((g) => h(g) !== void 0).map((g) => h(g).validate(r.data))
|
|
498
|
+
), o = i.every((g) => z(g));
|
|
496
499
|
let { errors: f } = w;
|
|
497
500
|
if (!o) {
|
|
498
|
-
const
|
|
499
|
-
f =
|
|
501
|
+
const g = i.map((V) => V.errors);
|
|
502
|
+
f = j(...g);
|
|
500
503
|
}
|
|
501
504
|
return {
|
|
502
505
|
errors: f,
|
|
503
506
|
// TODO: Implement data disambiguation strategy
|
|
504
|
-
data: (E = i.findLast((
|
|
507
|
+
data: (E = i.findLast((g) => !!g.data)) == null ? void 0 : E.data
|
|
505
508
|
};
|
|
506
509
|
}
|
|
507
510
|
const c = async () => {
|
|
@@ -521,32 +524,32 @@ function Be(r, t) {
|
|
|
521
524
|
data: o.data,
|
|
522
525
|
errors: e.errors
|
|
523
526
|
};
|
|
524
|
-
},
|
|
527
|
+
}, y = m(() => !Q(e.errors)), l = () => {
|
|
525
528
|
e.isValidated = !1, e.errors = h(t.errors) ?? w.errors;
|
|
526
529
|
}, d = (i) => {
|
|
527
530
|
const o = e.isValidated ? t.validationAfterSubmit : t.validationBeforeSubmit;
|
|
528
|
-
return
|
|
529
|
-
},
|
|
531
|
+
return J((o == null ? void 0 : o[i]) ?? !1);
|
|
532
|
+
}, F = (i, o) => {
|
|
530
533
|
if (d(i))
|
|
531
534
|
return v(o);
|
|
532
535
|
};
|
|
533
536
|
return {
|
|
534
|
-
...
|
|
537
|
+
...G(e),
|
|
535
538
|
validateForm: c,
|
|
536
539
|
validateField: v,
|
|
537
|
-
validateStrategy:
|
|
540
|
+
validateStrategy: F,
|
|
538
541
|
canValidate: d,
|
|
539
542
|
defineValidator: s,
|
|
540
|
-
isValid:
|
|
543
|
+
isValid: y,
|
|
541
544
|
reset: l
|
|
542
545
|
};
|
|
543
546
|
}
|
|
544
|
-
class
|
|
547
|
+
class Me {
|
|
545
548
|
constructor(t, e) {
|
|
546
549
|
this.path = t, this.validator = e;
|
|
547
550
|
}
|
|
548
551
|
async validate(t) {
|
|
549
|
-
const e =
|
|
552
|
+
const e = $(t, this.path);
|
|
550
553
|
if (!this.validator)
|
|
551
554
|
return w;
|
|
552
555
|
const a = await this.validator.validate(e);
|
|
@@ -555,112 +558,115 @@ class ke {
|
|
|
555
558
|
general: a.errors.general || [],
|
|
556
559
|
propertyErrors: a.errors.propertyErrors ? Object.fromEntries(
|
|
557
560
|
Object.entries(a.errors.propertyErrors).map(
|
|
558
|
-
([s, n]) => [
|
|
561
|
+
([s, n]) => [I(this.path, s), n]
|
|
559
562
|
)
|
|
560
563
|
) : {}
|
|
561
564
|
}
|
|
562
565
|
};
|
|
563
566
|
}
|
|
564
567
|
}
|
|
565
|
-
function
|
|
566
|
-
const s =
|
|
568
|
+
function Ue(r, t, e, a) {
|
|
569
|
+
const s = ee(r.data, t), n = m(() => $(r.initialData.value, t)), c = (u) => ({
|
|
567
570
|
...u,
|
|
568
|
-
path:
|
|
569
|
-
setData: (
|
|
570
|
-
u.setData(
|
|
571
|
+
path: m(() => h(u.path).replace(t + ".", "")),
|
|
572
|
+
setData: (p) => {
|
|
573
|
+
u.setData(p);
|
|
571
574
|
}
|
|
572
575
|
}), v = (u) => {
|
|
573
|
-
const
|
|
574
|
-
return
|
|
575
|
-
},
|
|
576
|
-
const
|
|
576
|
+
const p = I(t, u), S = r.getField(p);
|
|
577
|
+
return S ? c(S) : {};
|
|
578
|
+
}, y = (u) => {
|
|
579
|
+
const p = I(t, u.path), S = r.defineField({
|
|
577
580
|
...u,
|
|
578
|
-
path:
|
|
581
|
+
path: p
|
|
579
582
|
});
|
|
580
|
-
return c(
|
|
581
|
-
}, l =
|
|
582
|
-
const
|
|
583
|
-
return
|
|
583
|
+
return c(S);
|
|
584
|
+
}, l = m(() => r.fields.value.filter((u) => {
|
|
585
|
+
const p = u.path.value;
|
|
586
|
+
return p.startsWith(t + ".") || p === t;
|
|
584
587
|
}).map((u) => c(u))), d = () => r.fields.value.filter((u) => {
|
|
585
|
-
const
|
|
586
|
-
return
|
|
587
|
-
}),
|
|
588
|
+
const p = u.path.value;
|
|
589
|
+
return p.startsWith(t + ".") || p === t;
|
|
590
|
+
}), F = m(() => d().some((u) => u.dirty.value)), i = m(() => d().some((u) => u.touched.value)), o = m(() => r.isValid.value), f = m(() => r.isValidated.value), E = m(() => Ee(h(r.errors), t)), k = {
|
|
588
591
|
data: s,
|
|
589
592
|
fields: l,
|
|
590
593
|
initialData: n,
|
|
591
|
-
defineField:
|
|
594
|
+
defineField: y,
|
|
592
595
|
getField: v,
|
|
593
|
-
isDirty:
|
|
596
|
+
isDirty: F,
|
|
594
597
|
isTouched: i,
|
|
595
598
|
isValid: o,
|
|
596
599
|
isValidated: f,
|
|
597
600
|
errors: E,
|
|
598
601
|
defineValidator: (u) => {
|
|
599
|
-
const
|
|
600
|
-
() => new
|
|
602
|
+
const p = x(u) ? u : _(u), S = m(
|
|
603
|
+
() => new Me(t, h(p))
|
|
601
604
|
);
|
|
602
|
-
return r.defineValidator(
|
|
605
|
+
return r.defineValidator(S), p;
|
|
603
606
|
},
|
|
604
607
|
reset: () => d().forEach((u) => u.reset()),
|
|
605
608
|
validateForm: () => r.validateForm(),
|
|
606
|
-
getSubForm: (u,
|
|
607
|
-
const
|
|
609
|
+
getSubForm: (u, p) => {
|
|
610
|
+
const S = I(t, u);
|
|
608
611
|
return r.getSubForm(
|
|
609
|
-
|
|
610
|
-
|
|
612
|
+
S,
|
|
613
|
+
p
|
|
611
614
|
);
|
|
612
615
|
},
|
|
613
|
-
submitHandler: (u) =>
|
|
614
|
-
getFieldArray: (u,
|
|
616
|
+
submitHandler: (u) => X(k, e ?? {})(u),
|
|
617
|
+
getFieldArray: (u, p) => Y(k, u, p)
|
|
615
618
|
};
|
|
616
|
-
return
|
|
619
|
+
return k;
|
|
617
620
|
}
|
|
618
|
-
const
|
|
621
|
+
const je = {
|
|
619
622
|
keepValuesOnUnmount: !1,
|
|
620
|
-
...
|
|
623
|
+
...Oe
|
|
621
624
|
};
|
|
622
625
|
function ze(r) {
|
|
623
|
-
r =
|
|
624
|
-
const t =
|
|
626
|
+
r = ve({}, je, r);
|
|
627
|
+
const t = m(() => D(r.initialData)), e = Z(D(t)), a = A({
|
|
625
628
|
initialData: t,
|
|
626
629
|
data: e
|
|
627
630
|
});
|
|
628
|
-
|
|
631
|
+
b(
|
|
629
632
|
t,
|
|
630
633
|
(l) => {
|
|
631
634
|
a.data = D(l);
|
|
632
635
|
},
|
|
633
636
|
{ flush: "sync" }
|
|
634
637
|
);
|
|
635
|
-
const s =
|
|
638
|
+
const s = ke(a, r), n = Pe(a, s, {
|
|
636
639
|
keepValuesOnUnmount: r.keepValuesOnUnmount,
|
|
637
640
|
onBlur: async (l) => {
|
|
638
641
|
s.validateStrategy("validateOnBlur", l);
|
|
639
642
|
},
|
|
640
643
|
onChange: async (l) => {
|
|
641
644
|
s.validateStrategy("validateOnDataChange", l);
|
|
645
|
+
},
|
|
646
|
+
onRegistered: async (l) => {
|
|
647
|
+
s.validateStrategy("validateOnFieldRegister", l);
|
|
642
648
|
}
|
|
643
|
-
}), c =
|
|
649
|
+
}), c = $e(n), v = () => {
|
|
644
650
|
e.value = D(t), s.reset();
|
|
645
651
|
for (const l of n.fields.value)
|
|
646
652
|
l.reset();
|
|
647
653
|
};
|
|
648
654
|
s.canValidate("validateOnFormOpen") && s.validateForm();
|
|
649
|
-
const
|
|
655
|
+
const y = {
|
|
650
656
|
...n,
|
|
651
657
|
...c,
|
|
652
|
-
...
|
|
658
|
+
...me(s, ["canValidate", "validateStrategy"]),
|
|
653
659
|
reset: v,
|
|
654
|
-
initialData:
|
|
655
|
-
data:
|
|
660
|
+
initialData: U(a, "initialData"),
|
|
661
|
+
data: U(a, "data"),
|
|
656
662
|
validateForm: s.validateForm,
|
|
657
|
-
submitHandler: (l) =>
|
|
658
|
-
getSubForm: (l, d) =>
|
|
659
|
-
getFieldArray: (l, d) =>
|
|
663
|
+
submitHandler: (l) => X(y, s)(l),
|
|
664
|
+
getSubForm: (l, d) => Ue(y, l, r),
|
|
665
|
+
getFieldArray: (l, d) => Y(y, l, d)
|
|
660
666
|
};
|
|
661
|
-
return
|
|
667
|
+
return y;
|
|
662
668
|
}
|
|
663
|
-
const
|
|
669
|
+
const _e = /* @__PURE__ */ N({
|
|
664
670
|
__name: "Field",
|
|
665
671
|
props: {
|
|
666
672
|
form: {},
|
|
@@ -676,10 +682,10 @@ const je = /* @__PURE__ */ x({
|
|
|
676
682
|
setup(r) {
|
|
677
683
|
const t = r, e = t.form.defineField({
|
|
678
684
|
path: t.path
|
|
679
|
-
}), a =
|
|
680
|
-
return (s, n) => C(s.$slots, "default",
|
|
685
|
+
}), a = A(e);
|
|
686
|
+
return (s, n) => C(s.$slots, "default", T(W(a)));
|
|
681
687
|
}
|
|
682
|
-
}), He = /* @__PURE__ */
|
|
688
|
+
}), He = /* @__PURE__ */ N({
|
|
683
689
|
inheritAttrs: !1,
|
|
684
690
|
__name: "FormFieldWrapper",
|
|
685
691
|
props: {
|
|
@@ -689,28 +695,28 @@ const je = /* @__PURE__ */ x({
|
|
|
689
695
|
path: {}
|
|
690
696
|
},
|
|
691
697
|
setup(r) {
|
|
692
|
-
return (t, e) => (
|
|
698
|
+
return (t, e) => (L(), K(_e, {
|
|
693
699
|
form: r.form,
|
|
694
700
|
path: r.path
|
|
695
701
|
}, {
|
|
696
|
-
default:
|
|
697
|
-
(
|
|
702
|
+
default: M(({ errors: a, data: s, setData: n, onBlur: c, onFocus: v }) => [
|
|
703
|
+
(L(), K(ue(r.component), ce({ ...r.componentProps, ...t.$attrs }, {
|
|
698
704
|
"on-blur": c,
|
|
699
705
|
"on-focus": v,
|
|
700
706
|
"model-value": s,
|
|
701
707
|
errors: a,
|
|
702
708
|
name: r.path,
|
|
703
709
|
"onUpdate:modelValue": n
|
|
704
|
-
}),
|
|
705
|
-
default:
|
|
710
|
+
}), de({
|
|
711
|
+
default: M(() => [
|
|
706
712
|
C(t.$slots, "default")
|
|
707
713
|
]),
|
|
708
714
|
_: 2
|
|
709
715
|
}, [
|
|
710
|
-
|
|
716
|
+
fe(t.$slots, (y, l) => ({
|
|
711
717
|
name: l,
|
|
712
|
-
fn:
|
|
713
|
-
C(t.$slots, l,
|
|
718
|
+
fn: M((d) => [
|
|
719
|
+
C(t.$slots, l, T(W(d ?? {})))
|
|
714
720
|
])
|
|
715
721
|
}))
|
|
716
722
|
]), 1040, ["on-blur", "on-focus", "model-value", "errors", "name", "onUpdate:modelValue"]))
|
|
@@ -718,19 +724,19 @@ const je = /* @__PURE__ */ x({
|
|
|
718
724
|
_: 3
|
|
719
725
|
}, 8, ["form", "path"]));
|
|
720
726
|
}
|
|
721
|
-
}), Ke = /* @__PURE__ */
|
|
727
|
+
}), Ke = /* @__PURE__ */ N({
|
|
722
728
|
__name: "FormPart",
|
|
723
729
|
props: {
|
|
724
730
|
form: {},
|
|
725
731
|
path: {}
|
|
726
732
|
},
|
|
727
733
|
setup(r) {
|
|
728
|
-
const t = r, e =
|
|
729
|
-
return (a, s) => C(a.$slots, "default",
|
|
734
|
+
const t = r, e = m(() => t.form.getSubForm(t.path));
|
|
735
|
+
return (a, s) => C(a.$slots, "default", T(W({ subform: e.value })));
|
|
730
736
|
}
|
|
731
737
|
});
|
|
732
738
|
export {
|
|
733
|
-
|
|
739
|
+
_e as Field,
|
|
734
740
|
He as FormFieldWrapper,
|
|
735
741
|
Ke as FormPart,
|
|
736
742
|
ze as useForm
|
package/package.json
CHANGED
|
@@ -47,6 +47,7 @@ interface FieldRegistryOptions {
|
|
|
47
47
|
onBlur?: (path: string) => Awaitable<void>
|
|
48
48
|
onFocus?: (path: string) => Awaitable<void>
|
|
49
49
|
onChange?: <T>(path: string, value: T) => Awaitable<void>
|
|
50
|
+
onRegistered?: <T>(path: string, value: T) => Awaitable<void>
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
const optionDefaults = {
|
|
@@ -165,6 +166,8 @@ export function useFieldRegistry<T extends FormDataDefault, TOut = T>(
|
|
|
165
166
|
untrack(path)
|
|
166
167
|
})
|
|
167
168
|
|
|
169
|
+
registryOptions.onRegistered?.(path, field)
|
|
170
|
+
|
|
168
171
|
return field
|
|
169
172
|
}
|
|
170
173
|
|
|
@@ -84,6 +84,9 @@ export function useForm<T extends FormDataDefault, TOut = T>(
|
|
|
84
84
|
onChange: async (path: string) => {
|
|
85
85
|
validationState.validateStrategy('validateOnDataChange', path)
|
|
86
86
|
},
|
|
87
|
+
onRegistered: async (path: string) => {
|
|
88
|
+
validationState.validateStrategy('validateOnFieldRegister', path)
|
|
89
|
+
},
|
|
87
90
|
})
|
|
88
91
|
const formState = useFormState(fieldRegistry)
|
|
89
92
|
|
|
@@ -11,12 +11,14 @@ export const defaults: ValidationOptions<FormDataDefault> = {
|
|
|
11
11
|
validateOnFormOpen: false,
|
|
12
12
|
validateOnSubmit: true,
|
|
13
13
|
validateOnDataChange: false,
|
|
14
|
+
validateOnFieldRegister: false,
|
|
14
15
|
},
|
|
15
16
|
validationAfterSubmit: {
|
|
16
17
|
validateOnBlur: false,
|
|
17
18
|
validateOnFormOpen: false,
|
|
18
19
|
validateOnSubmit: true,
|
|
19
20
|
validateOnDataChange: true,
|
|
21
|
+
validateOnFieldRegister: true,
|
|
20
22
|
},
|
|
21
23
|
}
|
|
22
24
|
|
|
@@ -25,6 +27,7 @@ export interface ValidationFlags {
|
|
|
25
27
|
validateOnFormOpen?: MaybeRefOrGetter<boolean>
|
|
26
28
|
validateOnSubmit?: MaybeRefOrGetter<boolean>
|
|
27
29
|
validateOnDataChange?: MaybeRefOrGetter<boolean>
|
|
30
|
+
validateOnFieldRegister?: MaybeRefOrGetter<boolean>
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
export interface ValidatorOptions<T, TOut = T> {
|