@teamnovu/kit-vue-forms 0.0.10 → 0.0.12
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.
|
@@ -7,4 +7,5 @@ export interface UseFieldOptions<T, K extends string> {
|
|
|
7
7
|
path: K;
|
|
8
8
|
errors?: MaybeRef<ValidationErrors>;
|
|
9
9
|
}
|
|
10
|
+
export declare function getEmptyField(): FormField<unknown, string>;
|
|
10
11
|
export declare function useField<T, K extends string>(options: UseFieldOptions<T, K>): FormField<T, K>;
|
|
@@ -13,7 +13,7 @@ export declare function useFieldRegistry<T extends FormDataDefault>(formState: F
|
|
|
13
13
|
getField: <K extends Paths<T>>(path: K) => ResolvedFormField<T, K>;
|
|
14
14
|
getFields: <TData extends T>() => FieldsTuple<TData>;
|
|
15
15
|
registerField: <K extends Paths<T>>(field: ResolvedFormField<T, K>) => void;
|
|
16
|
-
defineField: <K extends Paths<T>>(options: DefineFieldOptions<PickProps<T, K>, K>) =>
|
|
16
|
+
defineField: <K extends Paths<T>>(options: DefineFieldOptions<PickProps<T, K>, K>) => ResolvedFormField<T, K>;
|
|
17
17
|
};
|
|
18
18
|
export type FieldRegistry<T extends FormDataDefault> = ReturnType<typeof useFieldRegistry<T>>;
|
|
19
19
|
export {};
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var x = Object.defineProperty;
|
|
2
2
|
var K = (e, r, t) => r in e ? x(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t;
|
|
3
3
|
var S = (e, r, t) => K(e, typeof r != "symbol" ? r + "" : r, t);
|
|
4
|
-
import { toValue as W, toRaw as I, computed as d, unref as
|
|
4
|
+
import { toValue as W, toRaw as I, computed as d, unref as u, reactive as m, watch as F, toRefs as O, toRef as D, ref as A, isRef as _, getCurrentScope as J, onBeforeUnmount as T, defineComponent as B, renderSlot as G, normalizeProps as L, guardReactiveProps as U } from "vue";
|
|
5
5
|
import "zod";
|
|
6
6
|
function y(e) {
|
|
7
7
|
const r = W(e), t = I(r);
|
|
@@ -12,27 +12,27 @@ function N(e) {
|
|
|
12
12
|
}
|
|
13
13
|
function w(e, r) {
|
|
14
14
|
return (Array.isArray(r) ? r : N(r)).reduce(
|
|
15
|
-
(
|
|
15
|
+
(s, a) => s == null ? void 0 : s[a],
|
|
16
16
|
e
|
|
17
17
|
);
|
|
18
18
|
}
|
|
19
19
|
function Z(e, r, t) {
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
20
|
+
const s = Array.isArray(r) ? r : N(r);
|
|
21
|
+
if (s.length === 0)
|
|
22
22
|
throw new Error("Path cannot be empty");
|
|
23
|
-
const
|
|
24
|
-
(
|
|
23
|
+
const a = s.at(-1), n = s.slice(0, -1).reduce(
|
|
24
|
+
(o, v) => o[v],
|
|
25
25
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
26
|
e
|
|
27
27
|
);
|
|
28
|
-
n[
|
|
28
|
+
n[a] = t;
|
|
29
29
|
}
|
|
30
30
|
const z = (e, r) => d({
|
|
31
31
|
get() {
|
|
32
|
-
return w(
|
|
32
|
+
return w(u(e), u(r));
|
|
33
33
|
},
|
|
34
34
|
set(t) {
|
|
35
|
-
Z(
|
|
35
|
+
Z(u(e), u(r), t);
|
|
36
36
|
}
|
|
37
37
|
});
|
|
38
38
|
function E(e, r) {
|
|
@@ -41,15 +41,15 @@ function E(e, r) {
|
|
|
41
41
|
function k(e, r) {
|
|
42
42
|
if (!r)
|
|
43
43
|
return e;
|
|
44
|
-
const t = `${r}.`,
|
|
45
|
-
Object.entries(e.propertyErrors).filter(([
|
|
46
|
-
([
|
|
44
|
+
const t = `${r}.`, s = Object.fromEntries(
|
|
45
|
+
Object.entries(e.propertyErrors).filter(([a]) => a.startsWith(t)).map(
|
|
46
|
+
([a, n]) => [a.slice(t.length), n]
|
|
47
47
|
)
|
|
48
48
|
);
|
|
49
49
|
return {
|
|
50
50
|
general: e.general,
|
|
51
51
|
// Keep general errors
|
|
52
|
-
propertyErrors:
|
|
52
|
+
propertyErrors: s
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
55
|
function q(e) {
|
|
@@ -57,61 +57,64 @@ function q(e) {
|
|
|
57
57
|
value: e.value,
|
|
58
58
|
path: e.path,
|
|
59
59
|
initialValue: d(() => Object.freeze(y(e.initialValue))),
|
|
60
|
-
errors:
|
|
60
|
+
errors: u(e.errors) || [],
|
|
61
61
|
touched: !1
|
|
62
62
|
});
|
|
63
|
-
F(() =>
|
|
63
|
+
F(() => u(e.errors), (f) => {
|
|
64
64
|
r.errors = f || [];
|
|
65
65
|
});
|
|
66
|
-
const t = d(() => JSON.stringify(r.value) !== JSON.stringify(r.initialValue)),
|
|
66
|
+
const t = d(() => JSON.stringify(r.value) !== JSON.stringify(r.initialValue)), s = (f) => {
|
|
67
67
|
r.value = f;
|
|
68
|
-
},
|
|
68
|
+
}, a = () => {
|
|
69
69
|
r.touched = !0;
|
|
70
70
|
}, n = () => {
|
|
71
|
-
},
|
|
71
|
+
}, o = () => {
|
|
72
72
|
r.value = y(r.initialValue), r.touched = !1, r.errors = [];
|
|
73
73
|
}, v = (f) => {
|
|
74
74
|
r.errors = f;
|
|
75
|
-
},
|
|
75
|
+
}, l = () => {
|
|
76
76
|
r.errors = [];
|
|
77
|
-
},
|
|
77
|
+
}, c = O(r);
|
|
78
78
|
return {
|
|
79
|
-
data:
|
|
80
|
-
path:
|
|
81
|
-
initialValue:
|
|
82
|
-
errors:
|
|
83
|
-
touched:
|
|
79
|
+
data: c.value,
|
|
80
|
+
path: c.path,
|
|
81
|
+
initialValue: c.initialValue,
|
|
82
|
+
errors: c.errors,
|
|
83
|
+
touched: c.touched,
|
|
84
84
|
dirty: t,
|
|
85
|
-
setData:
|
|
86
|
-
onBlur:
|
|
85
|
+
setData: s,
|
|
86
|
+
onBlur: a,
|
|
87
87
|
onFocus: n,
|
|
88
|
-
reset:
|
|
88
|
+
reset: o,
|
|
89
89
|
setErrors: v,
|
|
90
|
-
clearErrors:
|
|
90
|
+
clearErrors: l
|
|
91
91
|
};
|
|
92
92
|
}
|
|
93
93
|
function H(e) {
|
|
94
|
-
const r = {}, t = (
|
|
95
|
-
const v =
|
|
96
|
-
r[v] =
|
|
94
|
+
const r = {}, t = (o) => {
|
|
95
|
+
const v = u(o.path);
|
|
96
|
+
r[v] = o;
|
|
97
|
+
}, s = (o) => {
|
|
98
|
+
if (!r[o]) {
|
|
99
|
+
const v = q({
|
|
100
|
+
path: o,
|
|
101
|
+
value: z(D(e, "data"), o),
|
|
102
|
+
initialValue: d(() => w(e.initialData, o))
|
|
103
|
+
});
|
|
104
|
+
return t(v), v;
|
|
105
|
+
}
|
|
106
|
+
return r[o];
|
|
97
107
|
};
|
|
98
108
|
return {
|
|
99
109
|
fields: r,
|
|
100
|
-
getField:
|
|
110
|
+
getField: s,
|
|
101
111
|
getFields: () => Object.values(r),
|
|
102
112
|
registerField: t,
|
|
103
|
-
defineField: (
|
|
104
|
-
const v = q({
|
|
105
|
-
...u,
|
|
106
|
-
value: z(D(e, "data"), u.path),
|
|
107
|
-
initialValue: d(() => w(e.initialData, c(u.path)))
|
|
108
|
-
});
|
|
109
|
-
return t(v), v;
|
|
110
|
-
}
|
|
113
|
+
defineField: (o) => s(o.path)
|
|
111
114
|
};
|
|
112
115
|
}
|
|
113
116
|
function Q(e) {
|
|
114
|
-
const r = d(() => e.getFields().some((
|
|
117
|
+
const r = d(() => e.getFields().some((s) => u(s.dirty))), t = d(() => e.getFields().some((s) => u(s.touched)));
|
|
115
118
|
return {
|
|
116
119
|
isDirty: r,
|
|
117
120
|
isTouched: t
|
|
@@ -119,28 +122,28 @@ function Q(e) {
|
|
|
119
122
|
}
|
|
120
123
|
function X(e) {
|
|
121
124
|
return e.filter(
|
|
122
|
-
(r, t,
|
|
125
|
+
(r, t, s) => s.indexOf(r) === t
|
|
123
126
|
);
|
|
124
127
|
}
|
|
125
128
|
function C(...e) {
|
|
126
129
|
return e.slice(1).reduce((r, t) => {
|
|
127
130
|
if (!r && !t)
|
|
128
131
|
return;
|
|
129
|
-
const
|
|
132
|
+
const s = ((t == null ? void 0 : t.length) ?? 0) > 0;
|
|
130
133
|
if (!r && ((t == null ? void 0 : t.length) ?? 0) > 0)
|
|
131
134
|
return t;
|
|
132
|
-
if (!
|
|
135
|
+
if (!s)
|
|
133
136
|
return r;
|
|
134
|
-
const
|
|
135
|
-
return X(
|
|
137
|
+
const a = (r ?? []).concat(t);
|
|
138
|
+
return X(a);
|
|
136
139
|
}, e[0]);
|
|
137
140
|
}
|
|
138
141
|
function Y(...e) {
|
|
139
|
-
return e.map((t) => Object.keys(t)).flat().reduce((t,
|
|
140
|
-
const
|
|
142
|
+
return e.map((t) => Object.keys(t)).flat().reduce((t, s) => {
|
|
143
|
+
const a = e.map((n) => n[s]).filter(Boolean);
|
|
141
144
|
return {
|
|
142
145
|
...t,
|
|
143
|
-
[
|
|
146
|
+
[s]: C(...a)
|
|
144
147
|
};
|
|
145
148
|
}, {});
|
|
146
149
|
}
|
|
@@ -152,24 +155,24 @@ function P(...e) {
|
|
|
152
155
|
};
|
|
153
156
|
const r = e[0];
|
|
154
157
|
return e.length === 1 ? r : e.slice(1).reduce(
|
|
155
|
-
(t,
|
|
156
|
-
general: C(t.general,
|
|
157
|
-
propertyErrors: Y(t.propertyErrors ?? {},
|
|
158
|
+
(t, s) => ({
|
|
159
|
+
general: C(t.general, s.general),
|
|
160
|
+
propertyErrors: Y(t.propertyErrors ?? {}, s.propertyErrors ?? {})
|
|
158
161
|
}),
|
|
159
162
|
r
|
|
160
163
|
);
|
|
161
164
|
}
|
|
162
165
|
function j(e) {
|
|
163
|
-
var
|
|
164
|
-
const r = (((
|
|
166
|
+
var s;
|
|
167
|
+
const r = (((s = e.general) == null ? void 0 : s.length) ?? 0) > 0, t = Object.entries(e.propertyErrors).filter(([, a]) => a == null ? void 0 : a.length).length > 0;
|
|
165
168
|
return r || t;
|
|
166
169
|
}
|
|
167
170
|
function rr(e) {
|
|
168
|
-
const r = e.issues.filter((
|
|
169
|
-
const n =
|
|
171
|
+
const r = e.issues.filter((s) => s.path.length === 0).map((s) => s.message), t = e.issues.filter((s) => s.path.length > 0).reduce((s, a) => {
|
|
172
|
+
const n = a.path.join(".");
|
|
170
173
|
return {
|
|
171
|
-
...
|
|
172
|
-
[n]: [...
|
|
174
|
+
...s,
|
|
175
|
+
[n]: [...s[n] ?? [], a.message]
|
|
173
176
|
};
|
|
174
177
|
}, {});
|
|
175
178
|
return {
|
|
@@ -177,7 +180,7 @@ function rr(e) {
|
|
|
177
180
|
propertyErrors: t
|
|
178
181
|
};
|
|
179
182
|
}
|
|
180
|
-
const
|
|
183
|
+
const p = {
|
|
181
184
|
isValid: !0,
|
|
182
185
|
errors: {
|
|
183
186
|
general: [],
|
|
@@ -190,16 +193,16 @@ class er {
|
|
|
190
193
|
}
|
|
191
194
|
async validate(r) {
|
|
192
195
|
if (!this.schema)
|
|
193
|
-
return
|
|
196
|
+
return p;
|
|
194
197
|
const t = await this.schema.safeParseAsync(r);
|
|
195
198
|
if (t.success)
|
|
196
|
-
return
|
|
197
|
-
const
|
|
199
|
+
return p;
|
|
200
|
+
const s = rr(t.error);
|
|
198
201
|
return {
|
|
199
202
|
isValid: !1,
|
|
200
203
|
errors: {
|
|
201
|
-
general:
|
|
202
|
-
propertyErrors:
|
|
204
|
+
general: s.general ?? [],
|
|
205
|
+
propertyErrors: s.propertyErrors ?? {}
|
|
203
206
|
}
|
|
204
207
|
};
|
|
205
208
|
}
|
|
@@ -210,10 +213,10 @@ class tr {
|
|
|
210
213
|
}
|
|
211
214
|
async validate(r) {
|
|
212
215
|
if (!this.validateFn)
|
|
213
|
-
return
|
|
216
|
+
return p;
|
|
214
217
|
try {
|
|
215
218
|
const t = await this.validateFn(r);
|
|
216
|
-
return t.isValid ?
|
|
219
|
+
return t.isValid ? p : t;
|
|
217
220
|
} catch (t) {
|
|
218
221
|
return {
|
|
219
222
|
isValid: !1,
|
|
@@ -225,87 +228,87 @@ class tr {
|
|
|
225
228
|
}
|
|
226
229
|
}
|
|
227
230
|
}
|
|
228
|
-
class
|
|
231
|
+
class sr {
|
|
229
232
|
constructor(r, t) {
|
|
230
233
|
S(this, "schemaValidator");
|
|
231
234
|
S(this, "functionValidator");
|
|
232
235
|
this.schema = r, this.validateFn = t, this.schemaValidator = new er(this.schema), this.functionValidator = new tr(this.validateFn);
|
|
233
236
|
}
|
|
234
237
|
async validate(r) {
|
|
235
|
-
const [t,
|
|
238
|
+
const [t, s] = await Promise.all([
|
|
236
239
|
this.schemaValidator.validate(r),
|
|
237
240
|
this.functionValidator.validate(r)
|
|
238
241
|
]);
|
|
239
242
|
return {
|
|
240
|
-
isValid: t.isValid &&
|
|
241
|
-
errors: P(t.errors,
|
|
243
|
+
isValid: t.isValid && s.isValid,
|
|
244
|
+
errors: P(t.errors, s.errors)
|
|
242
245
|
};
|
|
243
246
|
}
|
|
244
247
|
}
|
|
245
248
|
function b(e) {
|
|
246
|
-
return d(() => new
|
|
247
|
-
|
|
248
|
-
|
|
249
|
+
return d(() => new sr(
|
|
250
|
+
u(e.schema),
|
|
251
|
+
u(e.validateFn)
|
|
249
252
|
));
|
|
250
253
|
}
|
|
251
|
-
function
|
|
254
|
+
function ar(e, r) {
|
|
252
255
|
const t = m({
|
|
253
256
|
validators: A([b(r)]),
|
|
254
257
|
isValidated: !1,
|
|
255
|
-
errors:
|
|
258
|
+
errors: u(r.errors) ?? p.errors
|
|
256
259
|
});
|
|
257
|
-
F(() =>
|
|
258
|
-
const
|
|
259
|
-
n(
|
|
260
|
+
F(() => u(r.errors), async () => {
|
|
261
|
+
const l = await a();
|
|
262
|
+
n(l.errors);
|
|
260
263
|
}, { immediate: !0 }), F(
|
|
261
264
|
[() => t.validators],
|
|
262
|
-
async (
|
|
265
|
+
async (l) => {
|
|
263
266
|
if (t.isValidated)
|
|
264
|
-
if (
|
|
265
|
-
const
|
|
266
|
-
t.errors =
|
|
267
|
+
if (l) {
|
|
268
|
+
const c = await a();
|
|
269
|
+
t.errors = c.errors;
|
|
267
270
|
} else
|
|
268
|
-
t.errors =
|
|
271
|
+
t.errors = p.errors;
|
|
269
272
|
},
|
|
270
273
|
{ immediate: !0 }
|
|
271
274
|
), F(() => e.data, () => {
|
|
272
|
-
t.isValidated &&
|
|
275
|
+
t.isValidated && o();
|
|
273
276
|
});
|
|
274
|
-
const
|
|
275
|
-
const
|
|
276
|
-
return t.validators.push(
|
|
277
|
+
const s = (l) => {
|
|
278
|
+
const c = _(l) ? l : b(l);
|
|
279
|
+
return t.validators.push(c), J() && T(() => {
|
|
277
280
|
t.validators = t.validators.filter(
|
|
278
|
-
(f) => f !==
|
|
281
|
+
(f) => f !== c
|
|
279
282
|
);
|
|
280
|
-
}),
|
|
283
|
+
}), c;
|
|
281
284
|
};
|
|
282
|
-
async function
|
|
283
|
-
const
|
|
284
|
-
t.validators.filter((
|
|
285
|
-
),
|
|
286
|
-
let { errors: f } =
|
|
287
|
-
if (!
|
|
288
|
-
const
|
|
289
|
-
f = P(...
|
|
285
|
+
async function a() {
|
|
286
|
+
const l = await Promise.all(
|
|
287
|
+
t.validators.filter((V) => u(V) !== void 0).map((V) => u(V).validate(e.data))
|
|
288
|
+
), c = l.every((V) => V.isValid);
|
|
289
|
+
let { errors: f } = p;
|
|
290
|
+
if (!c) {
|
|
291
|
+
const V = l.map((R) => R.errors);
|
|
292
|
+
f = P(...V);
|
|
290
293
|
}
|
|
291
294
|
return {
|
|
292
295
|
errors: f,
|
|
293
|
-
isValid:
|
|
296
|
+
isValid: c
|
|
294
297
|
};
|
|
295
298
|
}
|
|
296
|
-
const n = (
|
|
297
|
-
t.errors = P(
|
|
298
|
-
},
|
|
299
|
-
const
|
|
300
|
-
return n(
|
|
301
|
-
isValid: !j(
|
|
299
|
+
const n = (l) => {
|
|
300
|
+
t.errors = P(u(r.errors) ?? p.errors, l);
|
|
301
|
+
}, o = async () => {
|
|
302
|
+
const l = await a();
|
|
303
|
+
return n(l.errors), t.isValidated = !0, {
|
|
304
|
+
isValid: !j(l.errors),
|
|
302
305
|
errors: t.errors
|
|
303
306
|
};
|
|
304
307
|
}, v = d(() => !j(t.errors));
|
|
305
308
|
return {
|
|
306
309
|
...O(t),
|
|
307
|
-
validateForm:
|
|
308
|
-
defineValidator:
|
|
310
|
+
validateForm: o,
|
|
311
|
+
defineValidator: s,
|
|
309
312
|
isValid: v
|
|
310
313
|
};
|
|
311
314
|
}
|
|
@@ -316,15 +319,15 @@ class ir {
|
|
|
316
319
|
async validate(r) {
|
|
317
320
|
const t = w(r, this.path);
|
|
318
321
|
if (!this.validator)
|
|
319
|
-
return
|
|
320
|
-
const
|
|
322
|
+
return p;
|
|
323
|
+
const s = await this.validator.validate(t);
|
|
321
324
|
return {
|
|
322
|
-
isValid:
|
|
325
|
+
isValid: s.isValid,
|
|
323
326
|
errors: {
|
|
324
|
-
general:
|
|
325
|
-
propertyErrors:
|
|
326
|
-
Object.entries(
|
|
327
|
-
E(this.path,
|
|
327
|
+
general: s.errors.general || [],
|
|
328
|
+
propertyErrors: s.errors.propertyErrors ? Object.fromEntries(
|
|
329
|
+
Object.entries(s.errors.propertyErrors).map(([a, n]) => [
|
|
330
|
+
E(this.path, a),
|
|
328
331
|
n
|
|
329
332
|
])
|
|
330
333
|
) : {}
|
|
@@ -333,13 +336,13 @@ class ir {
|
|
|
333
336
|
}
|
|
334
337
|
}
|
|
335
338
|
function nr(e, r, t) {
|
|
336
|
-
const
|
|
339
|
+
const s = z(e.data, r), a = d(() => w(e.initialData.value, r)), n = (i) => ({
|
|
337
340
|
...i,
|
|
338
|
-
path: d(() =>
|
|
341
|
+
path: d(() => u(i.path).replace(r + ".", "")),
|
|
339
342
|
setData: (h) => {
|
|
340
343
|
i.setData(h);
|
|
341
344
|
}
|
|
342
|
-
}),
|
|
345
|
+
}), o = (i) => {
|
|
343
346
|
const h = E(r, i), g = e.getField(h);
|
|
344
347
|
return g ? n(g) : {};
|
|
345
348
|
}, v = (i) => {
|
|
@@ -348,31 +351,31 @@ function nr(e, r, t) {
|
|
|
348
351
|
path: h
|
|
349
352
|
});
|
|
350
353
|
return n(g);
|
|
351
|
-
},
|
|
354
|
+
}, l = () => e.getFields().filter((i) => {
|
|
352
355
|
const h = i.path.value;
|
|
353
356
|
return h.startsWith(r + ".") || h === r;
|
|
354
|
-
}).map((i) => n(i)),
|
|
357
|
+
}).map((i) => n(i)), c = () => e.getFields().filter((i) => {
|
|
355
358
|
const h = i.path.value;
|
|
356
359
|
return h.startsWith(r + ".") || h === r;
|
|
357
|
-
}), f = d(() =>
|
|
360
|
+
}), f = d(() => c().some((i) => i.dirty.value)), V = d(() => c().some((i) => i.touched.value)), R = d(() => e.isValid.value), M = d(() => e.isValidated.value), $ = d(() => k(u(e.errors), r));
|
|
358
361
|
return {
|
|
359
|
-
data:
|
|
360
|
-
initialData:
|
|
362
|
+
data: s,
|
|
363
|
+
initialData: a,
|
|
361
364
|
defineField: v,
|
|
362
|
-
getField:
|
|
363
|
-
getFields:
|
|
365
|
+
getField: o,
|
|
366
|
+
getFields: l,
|
|
364
367
|
isDirty: f,
|
|
365
|
-
isTouched:
|
|
368
|
+
isTouched: V,
|
|
366
369
|
isValid: R,
|
|
367
370
|
isValidated: M,
|
|
368
371
|
errors: $,
|
|
369
372
|
defineValidator: (i) => {
|
|
370
373
|
const h = _(i) ? i : b(i), g = d(
|
|
371
|
-
() => new ir(r,
|
|
374
|
+
() => new ir(r, u(h))
|
|
372
375
|
);
|
|
373
376
|
return e.defineValidator(g), h;
|
|
374
377
|
},
|
|
375
|
-
reset: () =>
|
|
378
|
+
reset: () => c().forEach((i) => i.reset()),
|
|
376
379
|
validateForm: () => e.validateForm(),
|
|
377
380
|
getSubForm: (i, h) => {
|
|
378
381
|
const g = E(r, i);
|
|
@@ -384,29 +387,29 @@ function nr(e, r, t) {
|
|
|
384
387
|
};
|
|
385
388
|
}
|
|
386
389
|
function vr(e) {
|
|
387
|
-
const r = d(() => Object.freeze(y(e.initialData))), t = A(y(r)),
|
|
390
|
+
const r = d(() => Object.freeze(y(e.initialData))), t = A(y(r)), s = m({
|
|
388
391
|
initialData: r,
|
|
389
392
|
data: t
|
|
390
|
-
}),
|
|
391
|
-
t.value = y(r),
|
|
393
|
+
}), a = H(s), n = ar(s, e), o = Q(a), v = () => {
|
|
394
|
+
t.value = y(r), a.getFields().forEach(
|
|
392
395
|
(f) => f.reset()
|
|
393
396
|
);
|
|
394
397
|
};
|
|
395
|
-
function
|
|
396
|
-
return nr(
|
|
398
|
+
function l(f, V) {
|
|
399
|
+
return nr(c, f);
|
|
397
400
|
}
|
|
398
|
-
const
|
|
399
|
-
...
|
|
401
|
+
const c = {
|
|
402
|
+
...a,
|
|
400
403
|
...n,
|
|
401
|
-
...
|
|
404
|
+
...o,
|
|
402
405
|
reset: v,
|
|
403
|
-
getSubForm:
|
|
404
|
-
initialData: D(
|
|
405
|
-
data: D(
|
|
406
|
+
getSubForm: l,
|
|
407
|
+
initialData: D(s, "initialData"),
|
|
408
|
+
data: D(s, "data")
|
|
406
409
|
};
|
|
407
|
-
return
|
|
410
|
+
return c;
|
|
408
411
|
}
|
|
409
|
-
const
|
|
412
|
+
const Vr = /* @__PURE__ */ B({
|
|
410
413
|
__name: "Field",
|
|
411
414
|
props: {
|
|
412
415
|
form: {},
|
|
@@ -419,10 +422,10 @@ const pr = /* @__PURE__ */ B({
|
|
|
419
422
|
const r = e, t = r.form.defineField({
|
|
420
423
|
path: r.path
|
|
421
424
|
});
|
|
422
|
-
return (
|
|
425
|
+
return (s, a) => G(s.$slots, "default", L(U(m(u(t)))));
|
|
423
426
|
}
|
|
424
427
|
});
|
|
425
428
|
export {
|
|
426
|
-
|
|
429
|
+
Vr as Field,
|
|
427
430
|
vr as useForm
|
|
428
431
|
};
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { computed, reactive, toRefs, unref, watch, type MaybeRef, type MaybeRefOrGetter } from 'vue'
|
|
1
|
+
import { computed, reactive, ref, toRefs, unref, watch, type MaybeRef, type MaybeRefOrGetter } from 'vue'
|
|
2
2
|
import type { FormField } from '../types/form'
|
|
3
3
|
import type { ValidationErrorMessage, ValidationErrors } from '../types/validation'
|
|
4
4
|
import { cloneRefValue } from '../utils/general'
|
|
@@ -10,6 +10,23 @@ export interface UseFieldOptions<T, K extends string> {
|
|
|
10
10
|
errors?: MaybeRef<ValidationErrors>
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
export function getEmptyField(): FormField<unknown, string> {
|
|
14
|
+
return {
|
|
15
|
+
setData: () => {},
|
|
16
|
+
onBlur: () => {},
|
|
17
|
+
onFocus: () => {},
|
|
18
|
+
reset: () => {},
|
|
19
|
+
setErrors: () => {},
|
|
20
|
+
clearErrors: () => {},
|
|
21
|
+
data: ref(undefined),
|
|
22
|
+
initialValue: ref(undefined),
|
|
23
|
+
path: ref(''),
|
|
24
|
+
errors: ref([]),
|
|
25
|
+
touched: ref(false),
|
|
26
|
+
dirty: computed(() => false),
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
13
30
|
export function useField<T, K extends string>(options: UseFieldOptions<T, K>): FormField<T, K> {
|
|
14
31
|
const state = reactive({
|
|
15
32
|
value: options.value,
|
|
@@ -2,7 +2,7 @@ import { computed, toRef, unref } from 'vue'
|
|
|
2
2
|
import type { FieldsTuple, FormDataDefault, FormField } from '../types/form'
|
|
3
3
|
import type { Paths, PickProps } from '../types/util'
|
|
4
4
|
import { getLens, getNestedValue } from '../utils/path'
|
|
5
|
-
import { useField, type UseFieldOptions } from './useField'
|
|
5
|
+
import { getEmptyField, useField, type UseFieldOptions } from './useField'
|
|
6
6
|
|
|
7
7
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
8
|
type FieldRegistryCache<T> = Record<Paths<T>, FormField<any, string>>
|
|
@@ -26,22 +26,31 @@ export function useFieldRegistry<T extends FormDataDefault>(
|
|
|
26
26
|
fields[path] = field
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
const getField = <K extends Paths<T>>(path: K) => {
|
|
30
|
-
|
|
29
|
+
const getField = <K extends Paths<T>>(path: K): ResolvedFormField<T, K> => {
|
|
30
|
+
if (!fields[path]) {
|
|
31
|
+
const field = useField({
|
|
32
|
+
path,
|
|
33
|
+
value: getLens(toRef(formState, 'data'), path),
|
|
34
|
+
initialValue: computed(() => getNestedValue(formState.initialData, path)),
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
registerField(field)
|
|
38
|
+
|
|
39
|
+
return field
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return fields[path] as ResolvedFormField<T, K>
|
|
31
43
|
}
|
|
32
44
|
|
|
33
45
|
const getFields = <TData extends T>() => {
|
|
34
46
|
return Object.values(fields) as FieldsTuple<TData>
|
|
35
47
|
}
|
|
36
48
|
|
|
37
|
-
const defineField = <K extends Paths<T>>(options: DefineFieldOptions<PickProps<T, K>, K>) => {
|
|
38
|
-
const field =
|
|
39
|
-
...options,
|
|
40
|
-
value: getLens(toRef(formState, 'data'), options.path),
|
|
41
|
-
initialValue: computed(() => getNestedValue(formState.initialData, unref(options.path))),
|
|
42
|
-
})
|
|
49
|
+
const defineField = <K extends Paths<T>>(options: DefineFieldOptions<PickProps<T, K>, K>): ResolvedFormField<T, K> => {
|
|
50
|
+
const field = getField(options.path)
|
|
43
51
|
|
|
44
|
-
|
|
52
|
+
// TODO: If more options are ever needed than only the path we have to update the field
|
|
53
|
+
// here with the new options
|
|
45
54
|
|
|
46
55
|
return field
|
|
47
56
|
}
|