@teamnovu/kit-vue-forms 0.1.8 → 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 +210 -206
- 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
- package/src/utils/path.ts +6 -4
|
@@ -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,57 +1,58 @@
|
|
|
1
1
|
var T = Object.defineProperty;
|
|
2
|
-
var G = (
|
|
3
|
-
var E = (
|
|
4
|
-
import { toValue as L, toRaw as Z, computed as
|
|
2
|
+
var G = (t, e, r) => e in t ? T(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var E = (t, e, r) => G(t, typeof e != "symbol" ? e + "" : e, r);
|
|
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
|
-
function g(
|
|
8
|
-
const e = L(
|
|
9
|
-
return re(
|
|
7
|
+
function g(t) {
|
|
8
|
+
const e = L(t), r = Z(e);
|
|
9
|
+
return re(r);
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
return
|
|
11
|
+
function I(t) {
|
|
12
|
+
return t === "" ? [] : t.split(/\s*\.\s*/).filter(Boolean);
|
|
13
13
|
}
|
|
14
|
-
function
|
|
15
|
-
return (Array.isArray(e) ? e :
|
|
14
|
+
function P(t, 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
|
-
function te(
|
|
21
|
-
const s = Array.isArray(e) ? e :
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
else if (D(r)) {
|
|
25
|
-
const n = s.slice(0, -1).reduce(
|
|
20
|
+
function te(t, e, r) {
|
|
21
|
+
const s = Array.isArray(e) ? e : I(e), o = s.at(-1);
|
|
22
|
+
if (o) {
|
|
23
|
+
const i = s.slice(0, -1).reduce(
|
|
26
24
|
(p, h) => p[h],
|
|
27
25
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
-
d(
|
|
26
|
+
d(t)
|
|
29
27
|
);
|
|
30
|
-
|
|
31
|
-
} else
|
|
32
|
-
|
|
28
|
+
i[o] = r;
|
|
29
|
+
} else {
|
|
30
|
+
if (!O(t))
|
|
31
|
+
return;
|
|
32
|
+
t.value = r;
|
|
33
|
+
}
|
|
33
34
|
}
|
|
34
|
-
const
|
|
35
|
+
const K = (t, e) => u({
|
|
35
36
|
get() {
|
|
36
|
-
return
|
|
37
|
+
return P(d(t), d(e));
|
|
37
38
|
},
|
|
38
|
-
set(
|
|
39
|
-
te(
|
|
39
|
+
set(r) {
|
|
40
|
+
te(t, d(e), r);
|
|
40
41
|
}
|
|
41
42
|
});
|
|
42
|
-
function R(
|
|
43
|
-
return !
|
|
43
|
+
function R(t, e) {
|
|
44
|
+
return !t && !e ? "" : !t && e ? e : !e && t ? t : `${t}.${e}`;
|
|
44
45
|
}
|
|
45
|
-
function se(
|
|
46
|
+
function se(t, e) {
|
|
46
47
|
if (!e)
|
|
47
|
-
return
|
|
48
|
-
const
|
|
49
|
-
Object.entries(
|
|
50
|
-
([o,
|
|
48
|
+
return t;
|
|
49
|
+
const r = `${e}.`, s = Object.fromEntries(
|
|
50
|
+
Object.entries(t.propertyErrors).filter(([o]) => o.startsWith(r)).map(
|
|
51
|
+
([o, i]) => [o.slice(r.length), i]
|
|
51
52
|
)
|
|
52
53
|
);
|
|
53
54
|
return {
|
|
54
|
-
general:
|
|
55
|
+
general: t.general,
|
|
55
56
|
// Keep general errors
|
|
56
57
|
propertyErrors: s
|
|
57
58
|
};
|
|
@@ -68,59 +69,62 @@ class ae {
|
|
|
68
69
|
this.rc > 0 && (this.rc -= 1, this.rc === 0 && this.drop && this.drop());
|
|
69
70
|
}
|
|
70
71
|
}
|
|
71
|
-
function oe(
|
|
72
|
-
const e =
|
|
73
|
-
value:
|
|
74
|
-
path:
|
|
75
|
-
initialValue:
|
|
76
|
-
errors:
|
|
72
|
+
function oe(t) {
|
|
73
|
+
const e = D({
|
|
74
|
+
value: t.value,
|
|
75
|
+
path: t.path,
|
|
76
|
+
initialValue: u(() => Object.freeze(g(t.initialValue))),
|
|
77
|
+
errors: t.errors,
|
|
77
78
|
touched: !1
|
|
78
|
-
}),
|
|
79
|
-
e.value =
|
|
79
|
+
}), r = u(() => JSON.stringify(e.value) !== JSON.stringify(e.initialValue)), s = (c) => {
|
|
80
|
+
e.value = c;
|
|
80
81
|
}, o = () => {
|
|
81
82
|
e.touched = !0;
|
|
82
|
-
},
|
|
83
|
+
}, i = () => {
|
|
83
84
|
}, p = () => {
|
|
84
85
|
e.value = g(e.initialValue), e.touched = !1, e.errors = [];
|
|
85
|
-
}, h = (
|
|
86
|
-
e.
|
|
87
|
-
}, V = () => {
|
|
86
|
+
}, h = (c) => {
|
|
87
|
+
r.value || s(g(c)), e.initialValue = c;
|
|
88
|
+
}, V = (c) => {
|
|
89
|
+
e.errors = c;
|
|
90
|
+
}, n = () => {
|
|
88
91
|
e.errors = [];
|
|
89
|
-
},
|
|
92
|
+
}, a = W(e);
|
|
90
93
|
return {
|
|
91
|
-
data:
|
|
92
|
-
path:
|
|
93
|
-
initialValue:
|
|
94
|
-
errors:
|
|
95
|
-
touched:
|
|
96
|
-
dirty:
|
|
94
|
+
data: a.value,
|
|
95
|
+
path: a.path,
|
|
96
|
+
initialValue: a.initialValue,
|
|
97
|
+
errors: a.errors,
|
|
98
|
+
touched: a.touched,
|
|
99
|
+
dirty: r,
|
|
97
100
|
setData: s,
|
|
101
|
+
setInitialData: h,
|
|
98
102
|
onBlur: o,
|
|
99
|
-
onFocus:
|
|
103
|
+
onFocus: i,
|
|
100
104
|
reset: p,
|
|
101
|
-
setErrors:
|
|
102
|
-
clearErrors:
|
|
105
|
+
setErrors: V,
|
|
106
|
+
clearErrors: n
|
|
103
107
|
};
|
|
104
108
|
}
|
|
105
|
-
function
|
|
106
|
-
const
|
|
107
|
-
const
|
|
108
|
-
s.set(
|
|
109
|
-
},
|
|
109
|
+
function ie(t, e) {
|
|
110
|
+
const r = /* @__PURE__ */ new Map(), s = q(/* @__PURE__ */ new Map()), o = (a) => {
|
|
111
|
+
const c = d(a.path);
|
|
112
|
+
s.set(c, a);
|
|
113
|
+
}, i = (a) => {
|
|
110
114
|
s.delete(a);
|
|
111
115
|
}, p = (a) => {
|
|
112
|
-
var
|
|
113
|
-
|
|
116
|
+
var c;
|
|
117
|
+
r.has(a) ? (c = r.get(a)) == null || c.inc() : r.set(a, new ae(() => i(a)));
|
|
114
118
|
}, h = (a) => {
|
|
115
|
-
var
|
|
116
|
-
|
|
119
|
+
var c;
|
|
120
|
+
r.has(a) && ((c = r.get(a)) == null || c.dec());
|
|
117
121
|
}, V = (a) => {
|
|
118
122
|
if (!s.has(a)) {
|
|
119
123
|
const v = oe({
|
|
120
124
|
path: a,
|
|
121
|
-
value:
|
|
122
|
-
initialValue:
|
|
123
|
-
errors:
|
|
125
|
+
value: K(_(t, "data"), a),
|
|
126
|
+
initialValue: u(() => P(t.initialData, a)),
|
|
127
|
+
errors: u({
|
|
124
128
|
get() {
|
|
125
129
|
return e.errors.value.propertyErrors[a] || [];
|
|
126
130
|
},
|
|
@@ -131,84 +135,84 @@ function ne(r, e) {
|
|
|
131
135
|
});
|
|
132
136
|
o(v);
|
|
133
137
|
}
|
|
134
|
-
const
|
|
138
|
+
const c = s.get(a);
|
|
135
139
|
return p(a), H(() => {
|
|
136
140
|
h(a);
|
|
137
|
-
}),
|
|
138
|
-
},
|
|
141
|
+
}), c;
|
|
142
|
+
}, n = (a) => V(a.path);
|
|
139
143
|
return {
|
|
140
|
-
fields:
|
|
144
|
+
fields: u(() => [...s.values()]),
|
|
141
145
|
getField: V,
|
|
142
146
|
registerField: o,
|
|
143
|
-
deregisterField:
|
|
144
|
-
defineField:
|
|
147
|
+
deregisterField: i,
|
|
148
|
+
defineField: n
|
|
145
149
|
};
|
|
146
150
|
}
|
|
147
|
-
function
|
|
148
|
-
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)));
|
|
149
153
|
return {
|
|
150
154
|
isDirty: e,
|
|
151
|
-
isTouched:
|
|
155
|
+
isTouched: r
|
|
152
156
|
};
|
|
153
157
|
}
|
|
154
|
-
function le(
|
|
155
|
-
return
|
|
156
|
-
(e,
|
|
158
|
+
function le(t) {
|
|
159
|
+
return t.filter(
|
|
160
|
+
(e, r, s) => s.indexOf(e) === r
|
|
157
161
|
);
|
|
158
162
|
}
|
|
159
|
-
function
|
|
160
|
-
return
|
|
161
|
-
if (!e && !
|
|
163
|
+
function U(...t) {
|
|
164
|
+
return t.slice(1).reduce((e, r) => {
|
|
165
|
+
if (!e && !r)
|
|
162
166
|
return;
|
|
163
|
-
const s = ((
|
|
164
|
-
if (!e && ((
|
|
165
|
-
return
|
|
167
|
+
const s = ((r == null ? void 0 : r.length) ?? 0) > 0;
|
|
168
|
+
if (!e && ((r == null ? void 0 : r.length) ?? 0) > 0)
|
|
169
|
+
return r;
|
|
166
170
|
if (!s)
|
|
167
171
|
return e;
|
|
168
|
-
const o = (e ?? []).concat(
|
|
172
|
+
const o = (e ?? []).concat(r);
|
|
169
173
|
return le(o);
|
|
170
|
-
},
|
|
174
|
+
}, t[0]);
|
|
171
175
|
}
|
|
172
|
-
function ce(...
|
|
173
|
-
return
|
|
174
|
-
const o =
|
|
176
|
+
function ce(...t) {
|
|
177
|
+
return t.map((r) => Object.keys(r)).flat().reduce((r, s) => {
|
|
178
|
+
const o = t.map((i) => i[s]).filter(Boolean);
|
|
175
179
|
return {
|
|
176
|
-
...
|
|
177
|
-
[s]:
|
|
180
|
+
...r,
|
|
181
|
+
[s]: U(...o)
|
|
178
182
|
};
|
|
179
183
|
}, {});
|
|
180
184
|
}
|
|
181
|
-
function
|
|
182
|
-
if (!
|
|
185
|
+
function S(...t) {
|
|
186
|
+
if (!t.length)
|
|
183
187
|
return {
|
|
184
188
|
general: [],
|
|
185
189
|
propertyErrors: {}
|
|
186
190
|
};
|
|
187
|
-
const e =
|
|
188
|
-
return
|
|
189
|
-
(
|
|
190
|
-
general:
|
|
191
|
-
propertyErrors: ce(
|
|
191
|
+
const e = t[0];
|
|
192
|
+
return t.length === 1 ? e : t.slice(1).reduce(
|
|
193
|
+
(r, s) => ({
|
|
194
|
+
general: U(r.general, s.general),
|
|
195
|
+
propertyErrors: ce(r.propertyErrors ?? {}, s.propertyErrors ?? {})
|
|
192
196
|
}),
|
|
193
197
|
e
|
|
194
198
|
);
|
|
195
199
|
}
|
|
196
|
-
function N(
|
|
200
|
+
function N(t) {
|
|
197
201
|
var s;
|
|
198
|
-
const e = (((s =
|
|
199
|
-
return e ||
|
|
202
|
+
const e = (((s = t.general) == null ? void 0 : s.length) ?? 0) > 0, r = Object.entries(t.propertyErrors).filter(([, o]) => o == null ? void 0 : o.length).length > 0;
|
|
203
|
+
return e || r;
|
|
200
204
|
}
|
|
201
|
-
function ue(
|
|
202
|
-
const e =
|
|
203
|
-
const
|
|
205
|
+
function ue(t) {
|
|
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) => {
|
|
207
|
+
const i = o.path.join(".");
|
|
204
208
|
return {
|
|
205
209
|
...s,
|
|
206
|
-
[
|
|
210
|
+
[i]: [...s[i] ?? [], o.message]
|
|
207
211
|
};
|
|
208
212
|
}, {});
|
|
209
213
|
return {
|
|
210
214
|
general: e,
|
|
211
|
-
propertyErrors:
|
|
215
|
+
propertyErrors: r
|
|
212
216
|
};
|
|
213
217
|
}
|
|
214
218
|
const m = {
|
|
@@ -225,10 +229,10 @@ class de {
|
|
|
225
229
|
async validate(e) {
|
|
226
230
|
if (!this.schema)
|
|
227
231
|
return m;
|
|
228
|
-
const
|
|
229
|
-
if (
|
|
232
|
+
const r = await this.schema.safeParseAsync(e);
|
|
233
|
+
if (r.success)
|
|
230
234
|
return m;
|
|
231
|
-
const s = ue(
|
|
235
|
+
const s = ue(r.error);
|
|
232
236
|
return {
|
|
233
237
|
isValid: !1,
|
|
234
238
|
errors: {
|
|
@@ -246,13 +250,13 @@ class fe {
|
|
|
246
250
|
if (!this.validateFn)
|
|
247
251
|
return m;
|
|
248
252
|
try {
|
|
249
|
-
const
|
|
250
|
-
return
|
|
251
|
-
} catch (
|
|
253
|
+
const r = await this.validateFn(e);
|
|
254
|
+
return r.isValid ? m : r;
|
|
255
|
+
} catch (r) {
|
|
252
256
|
return {
|
|
253
257
|
isValid: !1,
|
|
254
258
|
errors: {
|
|
255
|
-
general: [
|
|
259
|
+
general: [r.message || "Validation error"],
|
|
256
260
|
propertyErrors: {}
|
|
257
261
|
}
|
|
258
262
|
};
|
|
@@ -260,89 +264,89 @@ class fe {
|
|
|
260
264
|
}
|
|
261
265
|
}
|
|
262
266
|
class pe {
|
|
263
|
-
constructor(e,
|
|
267
|
+
constructor(e, r) {
|
|
264
268
|
E(this, "schemaValidator");
|
|
265
269
|
E(this, "functionValidator");
|
|
266
|
-
this.schema = e, this.validateFn =
|
|
270
|
+
this.schema = e, this.validateFn = r, this.schemaValidator = new de(this.schema), this.functionValidator = new fe(this.validateFn);
|
|
267
271
|
}
|
|
268
272
|
async validate(e) {
|
|
269
|
-
const [
|
|
273
|
+
const [r, s] = await Promise.all([
|
|
270
274
|
this.schemaValidator.validate(e),
|
|
271
275
|
this.functionValidator.validate(e)
|
|
272
276
|
]);
|
|
273
277
|
return {
|
|
274
|
-
isValid:
|
|
275
|
-
errors:
|
|
278
|
+
isValid: r.isValid && s.isValid,
|
|
279
|
+
errors: S(r.errors, s.errors)
|
|
276
280
|
};
|
|
277
281
|
}
|
|
278
282
|
}
|
|
279
|
-
function b(
|
|
280
|
-
return
|
|
281
|
-
d(
|
|
282
|
-
d(
|
|
283
|
+
function b(t) {
|
|
284
|
+
return u(() => new pe(
|
|
285
|
+
d(t.schema),
|
|
286
|
+
d(t.validateFn)
|
|
283
287
|
));
|
|
284
288
|
}
|
|
285
|
-
function he(
|
|
286
|
-
const
|
|
289
|
+
function he(t, e) {
|
|
290
|
+
const r = D({
|
|
287
291
|
validators: k([b(e)]),
|
|
288
292
|
isValidated: !1,
|
|
289
293
|
errors: d(e.errors) ?? m.errors
|
|
290
|
-
}), s = (
|
|
291
|
-
|
|
294
|
+
}), s = (n = m.errors) => {
|
|
295
|
+
r.errors = S(d(e.errors) ?? m.errors, n);
|
|
292
296
|
};
|
|
293
297
|
w(() => d(e.errors), async () => {
|
|
294
|
-
if (
|
|
295
|
-
const
|
|
296
|
-
s(
|
|
298
|
+
if (r.isValidated) {
|
|
299
|
+
const n = await i();
|
|
300
|
+
s(n.errors);
|
|
297
301
|
} else
|
|
298
302
|
s();
|
|
299
303
|
}, { immediate: !0 }), w(
|
|
300
|
-
[() =>
|
|
301
|
-
async (
|
|
302
|
-
if (
|
|
303
|
-
if (
|
|
304
|
-
const a = await
|
|
305
|
-
|
|
304
|
+
[() => r.validators],
|
|
305
|
+
async (n) => {
|
|
306
|
+
if (r.isValidated)
|
|
307
|
+
if (n) {
|
|
308
|
+
const a = await i();
|
|
309
|
+
r.errors = a.errors;
|
|
306
310
|
} else
|
|
307
|
-
|
|
311
|
+
r.errors = m.errors;
|
|
308
312
|
},
|
|
309
313
|
{ immediate: !0 }
|
|
310
|
-
), w(() =>
|
|
311
|
-
|
|
314
|
+
), w(() => t.data, () => {
|
|
315
|
+
r.isValidated && p();
|
|
312
316
|
});
|
|
313
|
-
const o = (
|
|
314
|
-
const a =
|
|
315
|
-
return
|
|
316
|
-
|
|
317
|
-
(
|
|
317
|
+
const o = (n) => {
|
|
318
|
+
const a = O(n) ? n : b(n);
|
|
319
|
+
return r.validators.push(a), Q() && X(() => {
|
|
320
|
+
r.validators = r.validators.filter(
|
|
321
|
+
(c) => c !== a
|
|
318
322
|
);
|
|
319
323
|
}), a;
|
|
320
324
|
};
|
|
321
|
-
async function
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
), a =
|
|
325
|
-
let { errors:
|
|
325
|
+
async function i() {
|
|
326
|
+
const n = await Promise.all(
|
|
327
|
+
r.validators.filter((v) => d(v) !== void 0).map((v) => d(v).validate(t.data))
|
|
328
|
+
), a = n.every((v) => v.isValid);
|
|
329
|
+
let { errors: c } = m;
|
|
326
330
|
if (!a) {
|
|
327
|
-
const v =
|
|
328
|
-
|
|
331
|
+
const v = n.map((F) => F.errors);
|
|
332
|
+
c = S(...v);
|
|
329
333
|
}
|
|
330
334
|
return {
|
|
331
|
-
errors:
|
|
335
|
+
errors: c,
|
|
332
336
|
isValid: a
|
|
333
337
|
};
|
|
334
338
|
}
|
|
335
339
|
const p = async () => {
|
|
336
|
-
const
|
|
337
|
-
return s(
|
|
338
|
-
isValid: !N(
|
|
339
|
-
errors:
|
|
340
|
+
const n = await i();
|
|
341
|
+
return s(n.errors), r.isValidated = !0, {
|
|
342
|
+
isValid: !N(n.errors),
|
|
343
|
+
errors: r.errors
|
|
340
344
|
};
|
|
341
|
-
}, h =
|
|
342
|
-
|
|
345
|
+
}, h = u(() => !N(r.errors)), V = () => {
|
|
346
|
+
r.isValidated = !1, r.errors = d(e.errors) ?? m.errors;
|
|
343
347
|
};
|
|
344
348
|
return {
|
|
345
|
-
...W(
|
|
349
|
+
...W(r),
|
|
346
350
|
validateForm: p,
|
|
347
351
|
defineValidator: o,
|
|
348
352
|
isValid: h,
|
|
@@ -350,51 +354,51 @@ function he(r, e) {
|
|
|
350
354
|
};
|
|
351
355
|
}
|
|
352
356
|
class ve {
|
|
353
|
-
constructor(e,
|
|
354
|
-
this.path = e, this.validator =
|
|
357
|
+
constructor(e, r) {
|
|
358
|
+
this.path = e, this.validator = r;
|
|
355
359
|
}
|
|
356
360
|
async validate(e) {
|
|
357
|
-
const
|
|
361
|
+
const r = P(e, this.path);
|
|
358
362
|
if (!this.validator)
|
|
359
363
|
return m;
|
|
360
|
-
const s = await this.validator.validate(
|
|
364
|
+
const s = await this.validator.validate(r);
|
|
361
365
|
return {
|
|
362
366
|
isValid: s.isValid,
|
|
363
367
|
errors: {
|
|
364
368
|
general: s.errors.general || [],
|
|
365
369
|
propertyErrors: s.errors.propertyErrors ? Object.fromEntries(
|
|
366
|
-
Object.entries(s.errors.propertyErrors).map(([o,
|
|
370
|
+
Object.entries(s.errors.propertyErrors).map(([o, i]) => [
|
|
367
371
|
R(this.path, o),
|
|
368
|
-
|
|
372
|
+
i
|
|
369
373
|
])
|
|
370
374
|
) : {}
|
|
371
375
|
}
|
|
372
376
|
};
|
|
373
377
|
}
|
|
374
378
|
}
|
|
375
|
-
function me(
|
|
376
|
-
const s =
|
|
379
|
+
function me(t, e, r) {
|
|
380
|
+
const s = K(t.data, e), o = u(() => P(t.initialData.value, e)), i = (l) => ({
|
|
377
381
|
...l,
|
|
378
|
-
path:
|
|
382
|
+
path: u(() => d(l.path).replace(e + ".", "")),
|
|
379
383
|
setData: (f) => {
|
|
380
384
|
l.setData(f);
|
|
381
385
|
}
|
|
382
386
|
}), p = (l) => {
|
|
383
|
-
const f = R(e, l), y =
|
|
384
|
-
return y ?
|
|
387
|
+
const f = R(e, l), y = t.getField(f);
|
|
388
|
+
return y ? i(y) : {};
|
|
385
389
|
}, h = (l) => {
|
|
386
|
-
const f = R(e, l.path), y =
|
|
390
|
+
const f = R(e, l.path), y = t.defineField({
|
|
387
391
|
...l,
|
|
388
392
|
path: f
|
|
389
393
|
});
|
|
390
|
-
return
|
|
391
|
-
}, V =
|
|
394
|
+
return i(y);
|
|
395
|
+
}, V = u(() => t.fields.value.filter((l) => {
|
|
392
396
|
const f = l.path.value;
|
|
393
397
|
return f.startsWith(e + ".") || f === e;
|
|
394
|
-
}).map((l) =>
|
|
398
|
+
}).map((l) => i(l))), n = () => t.fields.value.filter((l) => {
|
|
395
399
|
const f = l.path.value;
|
|
396
400
|
return f.startsWith(e + ".") || f === e;
|
|
397
|
-
}), 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));
|
|
398
402
|
return {
|
|
399
403
|
data: s,
|
|
400
404
|
fields: V,
|
|
@@ -402,53 +406,53 @@ function me(r, e, t) {
|
|
|
402
406
|
defineField: h,
|
|
403
407
|
getField: p,
|
|
404
408
|
isDirty: a,
|
|
405
|
-
isTouched:
|
|
409
|
+
isTouched: c,
|
|
406
410
|
isValid: v,
|
|
407
411
|
isValidated: F,
|
|
408
412
|
errors: J,
|
|
409
413
|
defineValidator: (l) => {
|
|
410
|
-
const f =
|
|
414
|
+
const f = O(l) ? l : b(l), y = u(
|
|
411
415
|
() => new ve(e, d(f))
|
|
412
416
|
);
|
|
413
|
-
return
|
|
417
|
+
return t.defineValidator(y), f;
|
|
414
418
|
},
|
|
415
|
-
reset: () =>
|
|
416
|
-
validateForm: () =>
|
|
419
|
+
reset: () => n().forEach((l) => l.reset()),
|
|
420
|
+
validateForm: () => t.validateForm(),
|
|
417
421
|
getSubForm: (l, f) => {
|
|
418
422
|
const y = R(e, l);
|
|
419
|
-
return
|
|
423
|
+
return t.getSubForm(
|
|
420
424
|
y,
|
|
421
425
|
f
|
|
422
426
|
);
|
|
423
427
|
}
|
|
424
428
|
};
|
|
425
429
|
}
|
|
426
|
-
function Pe(
|
|
427
|
-
const e =
|
|
430
|
+
function Pe(t) {
|
|
431
|
+
const e = u(() => Object.freeze(g(t.initialData))), r = k(g(e)), s = D({
|
|
428
432
|
initialData: e,
|
|
429
|
-
data:
|
|
433
|
+
data: r
|
|
430
434
|
});
|
|
431
435
|
w(e, (a) => {
|
|
432
436
|
s.data = g(a);
|
|
433
437
|
});
|
|
434
|
-
const o = he(s,
|
|
435
|
-
|
|
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(
|
|
436
440
|
(a) => a.reset()
|
|
437
441
|
);
|
|
438
442
|
};
|
|
439
|
-
function V(a,
|
|
440
|
-
return me(
|
|
443
|
+
function V(a, c) {
|
|
444
|
+
return me(n, a);
|
|
441
445
|
}
|
|
442
|
-
const
|
|
443
|
-
...
|
|
446
|
+
const n = {
|
|
447
|
+
...i,
|
|
444
448
|
...o,
|
|
445
449
|
...p,
|
|
446
450
|
reset: h,
|
|
447
451
|
getSubForm: V,
|
|
448
|
-
initialData:
|
|
449
|
-
data:
|
|
452
|
+
initialData: _(s, "initialData"),
|
|
453
|
+
data: _(s, "data")
|
|
450
454
|
};
|
|
451
|
-
return
|
|
455
|
+
return n;
|
|
452
456
|
}
|
|
453
457
|
const _e = /* @__PURE__ */ $({
|
|
454
458
|
__name: "Field",
|
|
@@ -459,11 +463,11 @@ const _e = /* @__PURE__ */ $({
|
|
|
459
463
|
path: {},
|
|
460
464
|
errors: {}
|
|
461
465
|
},
|
|
462
|
-
setup(
|
|
463
|
-
const e =
|
|
466
|
+
setup(t) {
|
|
467
|
+
const e = t, r = e.form.defineField({
|
|
464
468
|
path: e.path
|
|
465
|
-
}), s =
|
|
466
|
-
return (o,
|
|
469
|
+
}), s = D(r);
|
|
470
|
+
return (o, i) => j(o.$slots, "default", z(B(s)));
|
|
467
471
|
}
|
|
468
472
|
}), Se = /* @__PURE__ */ $({
|
|
469
473
|
inheritAttrs: !1,
|
|
@@ -474,16 +478,16 @@ const _e = /* @__PURE__ */ $({
|
|
|
474
478
|
form: {},
|
|
475
479
|
path: {}
|
|
476
480
|
},
|
|
477
|
-
setup(
|
|
478
|
-
return (e,
|
|
481
|
+
setup(t) {
|
|
482
|
+
return (e, r) => {
|
|
479
483
|
const s = Y("Field");
|
|
480
484
|
return C(), A(s, {
|
|
481
485
|
form: e.form,
|
|
482
486
|
path: e.path
|
|
483
487
|
}, {
|
|
484
|
-
default: M(({ errors: o, data:
|
|
488
|
+
default: M(({ errors: o, data: i, setData: p }) => [
|
|
485
489
|
(C(), A(x(e.component), ee({ ...e.componentProps, ...e.$attrs }, {
|
|
486
|
-
"model-value":
|
|
490
|
+
"model-value": i,
|
|
487
491
|
errors: o,
|
|
488
492
|
name: e.path,
|
|
489
493
|
"onUpdate:modelValue": p
|
|
@@ -498,20 +502,20 @@ const _e = /* @__PURE__ */ $({
|
|
|
498
502
|
}, 8, ["form", "path"]);
|
|
499
503
|
};
|
|
500
504
|
}
|
|
501
|
-
}),
|
|
505
|
+
}), be = /* @__PURE__ */ $({
|
|
502
506
|
__name: "FormPart",
|
|
503
507
|
props: {
|
|
504
508
|
form: {},
|
|
505
509
|
path: {}
|
|
506
510
|
},
|
|
507
|
-
setup(
|
|
508
|
-
const e =
|
|
509
|
-
return (s, o) => j(s.$slots, "default", z(B({ subform:
|
|
511
|
+
setup(t) {
|
|
512
|
+
const e = t, r = u(() => e.form.getSubForm(e.path));
|
|
513
|
+
return (s, o) => j(s.$slots, "default", z(B({ subform: r.value })));
|
|
510
514
|
}
|
|
511
515
|
});
|
|
512
516
|
export {
|
|
513
517
|
_e as Field,
|
|
514
518
|
Se as FormFieldWrapper,
|
|
515
|
-
|
|
519
|
+
be as FormPart,
|
|
516
520
|
Pe as useForm
|
|
517
521
|
};
|
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
package/src/utils/path.ts
CHANGED
|
@@ -22,11 +22,13 @@ export function setNestedValue<T, K extends Paths<T>>(obj: MaybeRef<T>, path: K
|
|
|
22
22
|
|
|
23
23
|
const lastKey = keys.at(-1)!
|
|
24
24
|
|
|
25
|
-
if (!lastKey
|
|
25
|
+
if (!lastKey) {
|
|
26
|
+
if (!isRef(obj)) {
|
|
27
|
+
// We cannot do anything here as we have nothing we can assign to
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
26
31
|
obj.value = value
|
|
27
|
-
} else if (!isRef(obj)) {
|
|
28
|
-
// We cannot do anything here as we have nothing we can assign to
|
|
29
|
-
return
|
|
30
32
|
} else {
|
|
31
33
|
const target = keys
|
|
32
34
|
.slice(0, -1)
|