@volverjs/form-vue 0.0.10-beta.2 → 0.0.10-beta.4
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/VvFormWrapper.d.ts +3 -2
- package/dist/index.d.ts +6 -4
- package/dist/index.es.js +234 -225
- package/dist/index.umd.js +1 -1
- package/package.json +4 -4
- package/src/VvForm.ts +1 -0
- package/src/VvFormField.ts +28 -24
- package/src/VvFormWrapper.ts +19 -13
package/dist/VvFormWrapper.d.ts
CHANGED
|
@@ -23,10 +23,11 @@ export declare const defineFormWrapper: <Schema extends AnyZodObject | ZodEffect
|
|
|
23
23
|
default: undefined;
|
|
24
24
|
};
|
|
25
25
|
}, {
|
|
26
|
-
|
|
26
|
+
formData: Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
|
|
27
|
+
errors: Readonly<Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
|
|
27
28
|
invalid: import("vue").ComputedRef<boolean>;
|
|
28
29
|
fields: Ref<Set<string>>;
|
|
29
|
-
|
|
30
|
+
fieldsErrors: Ref<Map<string, Record<string, {
|
|
30
31
|
_errors: string[];
|
|
31
32
|
}>>>;
|
|
32
33
|
}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("valid" | "invalid")[], "valid" | "invalid", import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
|
package/dist/index.d.ts
CHANGED
|
@@ -56,10 +56,11 @@ export declare const formFactory: <Schema extends AnyZodObject | ZodEffects<AnyZ
|
|
|
56
56
|
default: undefined;
|
|
57
57
|
};
|
|
58
58
|
}, {
|
|
59
|
-
|
|
59
|
+
formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
|
|
60
|
+
errors: Readonly<import("vue").Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
|
|
60
61
|
invalid: import("vue").ComputedRef<boolean>;
|
|
61
62
|
fields: import("vue").Ref<Set<string>>;
|
|
62
|
-
|
|
63
|
+
fieldsErrors: import("vue").Ref<Map<string, Record<string, {
|
|
63
64
|
_errors: string[];
|
|
64
65
|
}>>>;
|
|
65
66
|
}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("valid" | "invalid")[], "valid" | "invalid", import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
|
|
@@ -217,10 +218,11 @@ export declare const useForm: <Schema extends AnyZodObject | ZodEffects<AnyZodOb
|
|
|
217
218
|
default: undefined;
|
|
218
219
|
};
|
|
219
220
|
}, {
|
|
220
|
-
|
|
221
|
+
formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
|
|
222
|
+
errors: Readonly<import("vue").Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
|
|
221
223
|
invalid: import("vue").ComputedRef<boolean>;
|
|
222
224
|
fields: import("vue").Ref<Set<string>>;
|
|
223
|
-
|
|
225
|
+
fieldsErrors: import("vue").Ref<Map<string, Record<string, {
|
|
224
226
|
_errors: string[];
|
|
225
227
|
}>>>;
|
|
226
228
|
}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("valid" | "invalid")[], "valid" | "invalid", import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
|
package/dist/index.es.js
CHANGED
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
import { defineComponent as
|
|
2
|
-
import { watchThrottled as
|
|
3
|
-
import { ZodObject as
|
|
1
|
+
import { defineComponent as E, computed as V, onMounted as q, onBeforeUnmount as R, inject as S, toRefs as I, watch as y, provide as $, readonly as w, resolveComponent as h, defineAsyncComponent as H, h as x, ref as g, isProxy as Q, toRaw as X, withModifiers as Y } from "vue";
|
|
2
|
+
import { watchThrottled as D } from "@vueuse/core";
|
|
3
|
+
import { ZodObject as L, ZodDefault as T, ZodNullable as j, ZodSchema as F, ZodEffects as ee } from "zod";
|
|
4
4
|
function O(e) {
|
|
5
5
|
return Array.isArray(e);
|
|
6
6
|
}
|
|
7
7
|
function te(e) {
|
|
8
8
|
return typeof e < "u";
|
|
9
9
|
}
|
|
10
|
-
function
|
|
10
|
+
function N(e) {
|
|
11
11
|
return e === null;
|
|
12
12
|
}
|
|
13
|
-
function
|
|
13
|
+
function W(e) {
|
|
14
14
|
return typeof e == "object";
|
|
15
15
|
}
|
|
16
|
-
function
|
|
16
|
+
function _(e) {
|
|
17
17
|
return typeof e == "string";
|
|
18
18
|
}
|
|
19
19
|
function k(e) {
|
|
20
20
|
return typeof e > "u";
|
|
21
21
|
}
|
|
22
|
-
const re = /^[0-9]+$/,
|
|
23
|
-
function
|
|
24
|
-
const
|
|
25
|
-
if (!
|
|
26
|
-
return
|
|
27
|
-
const
|
|
28
|
-
if (
|
|
29
|
-
for (const t of
|
|
22
|
+
const re = /^[0-9]+$/, ne = ["__proto__", "prototype", "constructor"];
|
|
23
|
+
function A(e, o, a) {
|
|
24
|
+
const s = te(a) ? a : void 0;
|
|
25
|
+
if (!W(e) || !_(o))
|
|
26
|
+
return s;
|
|
27
|
+
const r = K(o);
|
|
28
|
+
if (r.length !== 0) {
|
|
29
|
+
for (const t of r) {
|
|
30
30
|
if (t === "*")
|
|
31
31
|
continue;
|
|
32
|
-
const
|
|
33
|
-
return
|
|
32
|
+
const l = function(u) {
|
|
33
|
+
return u.map((c) => k(c) || N(c) ? c : O(c) ? l(c) : c[t]);
|
|
34
34
|
};
|
|
35
|
-
if (O(e) && !re.test(t) ? e =
|
|
35
|
+
if (O(e) && !re.test(t) ? e = l(e) : e = e[t], k(e) || N(e))
|
|
36
36
|
break;
|
|
37
37
|
}
|
|
38
|
-
return k(e) ?
|
|
38
|
+
return k(e) ? s : e;
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
-
function
|
|
42
|
-
if (!
|
|
41
|
+
function B(e, o, a) {
|
|
42
|
+
if (!W(e) || !_(o))
|
|
43
43
|
return;
|
|
44
|
-
const
|
|
45
|
-
if (
|
|
44
|
+
const s = K(o);
|
|
45
|
+
if (s.length === 0)
|
|
46
46
|
return;
|
|
47
|
-
const
|
|
48
|
-
for (let t = 0; t <
|
|
49
|
-
const
|
|
50
|
-
if (t ===
|
|
51
|
-
e[
|
|
47
|
+
const r = s.length;
|
|
48
|
+
for (let t = 0; t < r; t++) {
|
|
49
|
+
const l = s[t];
|
|
50
|
+
if (t === r - 1) {
|
|
51
|
+
e[l] = a;
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
|
-
if (
|
|
55
|
-
const
|
|
56
|
-
for (const
|
|
57
|
-
|
|
54
|
+
if (l === "*" && O(e)) {
|
|
55
|
+
const u = s.slice(t + 1).join(".");
|
|
56
|
+
for (const c of e)
|
|
57
|
+
B(c, u, a);
|
|
58
58
|
return;
|
|
59
59
|
}
|
|
60
|
-
k(e[
|
|
60
|
+
k(e[l]) && (e[l] = {}), e = e[l];
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
function
|
|
64
|
-
const
|
|
65
|
-
return
|
|
63
|
+
function K(e) {
|
|
64
|
+
const o = e.split(/[.]|(?:\[(\d|\*)\])/).filter((a) => !!a);
|
|
65
|
+
return o.some((a) => ne.indexOf(a) !== -1) ? [] : o;
|
|
66
66
|
}
|
|
67
|
-
var
|
|
68
|
-
const
|
|
67
|
+
var i = /* @__PURE__ */ ((e) => (e.text = "text", e.number = "number", e.email = "email", e.password = "password", e.tel = "tel", e.url = "url", e.search = "search", e.date = "date", e.time = "time", e.datetimeLocal = "datetimeLocal", e.month = "month", e.week = "week", e.color = "color", e.select = "select", e.checkbox = "checkbox", e.radio = "radio", e.textarea = "textarea", e.radioGroup = "radioGroup", e.checkboxGroup = "checkboxGroup", e.combobox = "combobox", e.custom = "custom", e))(i || {});
|
|
68
|
+
const oe = (e, o, a, s = {}) => E({
|
|
69
69
|
name: "FieldComponent",
|
|
70
70
|
props: {
|
|
71
71
|
type: {
|
|
72
72
|
type: String,
|
|
73
|
-
validator: (
|
|
74
|
-
default:
|
|
73
|
+
validator: (r) => Object.values(i).includes(r),
|
|
74
|
+
default: i.custom
|
|
75
75
|
},
|
|
76
76
|
is: {
|
|
77
77
|
type: [Object, String],
|
|
@@ -96,122 +96,125 @@ const ne = (e, n, o, l = {}) => $({
|
|
|
96
96
|
},
|
|
97
97
|
emits: ["invalid", "valid", "update:formData", "update:modelValue"],
|
|
98
98
|
expose: ["invalid", "invalidLabel", "errors"],
|
|
99
|
-
setup(
|
|
100
|
-
const
|
|
99
|
+
setup(r, { slots: t, emit: l }) {
|
|
100
|
+
const u = V({
|
|
101
101
|
get() {
|
|
102
|
-
if (
|
|
103
|
-
return
|
|
104
|
-
Object(
|
|
105
|
-
String(
|
|
102
|
+
if (n != null && n.modelValue)
|
|
103
|
+
return A(
|
|
104
|
+
Object(n.modelValue.value),
|
|
105
|
+
String(r.name)
|
|
106
106
|
);
|
|
107
107
|
},
|
|
108
108
|
set(f) {
|
|
109
|
-
|
|
110
|
-
Object(
|
|
111
|
-
String(
|
|
109
|
+
n != null && n.modelValue && (B(
|
|
110
|
+
Object(n.modelValue.value),
|
|
111
|
+
String(r.name),
|
|
112
112
|
f
|
|
113
|
-
),
|
|
114
|
-
newValue:
|
|
115
|
-
formData:
|
|
113
|
+
), l("update:modelValue", {
|
|
114
|
+
newValue: u.value,
|
|
115
|
+
formData: n == null ? void 0 : n.modelValue
|
|
116
116
|
}));
|
|
117
117
|
}
|
|
118
118
|
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}),
|
|
122
|
-
|
|
119
|
+
q(() => {
|
|
120
|
+
u.value === void 0 && r.defaultValue !== void 0 && (u.value = r.defaultValue);
|
|
121
|
+
}), R(() => {
|
|
122
|
+
J(), U();
|
|
123
123
|
});
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
if (
|
|
128
|
-
return
|
|
124
|
+
const c = S(o, void 0);
|
|
125
|
+
c && c.fields.value.add(r.name);
|
|
126
|
+
const n = S(e), { props: v, name: m } = I(r), d = V(() => {
|
|
127
|
+
if (n != null && n.errors.value)
|
|
128
|
+
return A(n.errors.value, String(r.name));
|
|
129
129
|
}), p = V(() => {
|
|
130
130
|
var f;
|
|
131
131
|
return (f = d.value) == null ? void 0 : f._errors;
|
|
132
|
-
}), b = V(() => d.value !== void 0),
|
|
133
|
-
b.value ? (
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
132
|
+
}), b = V(() => d.value !== void 0), J = y(b, () => {
|
|
133
|
+
b.value ? (l("invalid", p.value), c && c.errors.value.set(
|
|
134
|
+
r.name,
|
|
135
|
+
{
|
|
136
|
+
_errors: p.value
|
|
137
|
+
}
|
|
138
|
+
)) : (l("valid", u.value), c && c.errors.value.delete(
|
|
139
|
+
r.name
|
|
137
140
|
));
|
|
138
|
-
}),
|
|
139
|
-
() =>
|
|
141
|
+
}), U = y(
|
|
142
|
+
() => n == null ? void 0 : n.modelValue,
|
|
140
143
|
() => {
|
|
141
|
-
|
|
144
|
+
l("update:formData", n == null ? void 0 : n.modelValue);
|
|
142
145
|
},
|
|
143
146
|
{ deep: !0 }
|
|
144
|
-
),
|
|
145
|
-
|
|
146
|
-
},
|
|
147
|
-
...
|
|
148
|
-
name:
|
|
147
|
+
), G = (f) => {
|
|
148
|
+
u.value = f;
|
|
149
|
+
}, C = V(() => typeof v.value == "function" ? v.value(n == null ? void 0 : n.modelValue) : v.value), z = V(() => ({
|
|
150
|
+
...C.value,
|
|
151
|
+
name: C.value.name ?? r.name,
|
|
149
152
|
invalid: b.value,
|
|
150
|
-
valid:
|
|
153
|
+
valid: r.showValid ? !!(!b.value && u.value) : void 0,
|
|
151
154
|
type: ((f) => {
|
|
152
155
|
if ([
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
156
|
+
i.text,
|
|
157
|
+
i.number,
|
|
158
|
+
i.email,
|
|
159
|
+
i.password,
|
|
160
|
+
i.tel,
|
|
161
|
+
i.url,
|
|
162
|
+
i.search,
|
|
163
|
+
i.date,
|
|
164
|
+
i.time,
|
|
165
|
+
i.datetimeLocal,
|
|
166
|
+
i.month,
|
|
167
|
+
i.week,
|
|
168
|
+
i.color
|
|
166
169
|
].includes(f))
|
|
167
170
|
return f;
|
|
168
|
-
})(
|
|
171
|
+
})(r.type),
|
|
169
172
|
invalidLabel: p.value,
|
|
170
|
-
modelValue:
|
|
171
|
-
errors:
|
|
172
|
-
"onUpdate:modelValue":
|
|
173
|
+
modelValue: u.value,
|
|
174
|
+
errors: r.is ? d.value : void 0,
|
|
175
|
+
"onUpdate:modelValue": G
|
|
173
176
|
}));
|
|
174
|
-
return
|
|
175
|
-
name: w(
|
|
177
|
+
return $(a, {
|
|
178
|
+
name: w(m),
|
|
176
179
|
errors: w(d)
|
|
177
180
|
}), { component: V(() => {
|
|
178
|
-
if (
|
|
181
|
+
if (r.type === i.custom)
|
|
179
182
|
return {
|
|
180
183
|
render() {
|
|
181
184
|
var f;
|
|
182
185
|
return ((f = t.default) == null ? void 0 : f.call(t, {
|
|
183
|
-
modelValue:
|
|
184
|
-
onUpdate:
|
|
186
|
+
modelValue: u.value,
|
|
187
|
+
onUpdate: G,
|
|
185
188
|
invalid: b.value,
|
|
186
189
|
invalidLabel: p.value,
|
|
187
|
-
formData:
|
|
188
|
-
formErrors:
|
|
190
|
+
formData: n == null ? void 0 : n.modelValue.value,
|
|
191
|
+
formErrors: n == null ? void 0 : n.errors.value,
|
|
189
192
|
errors: d.value
|
|
190
193
|
})) ?? t.defalut;
|
|
191
194
|
}
|
|
192
195
|
};
|
|
193
|
-
if (!
|
|
196
|
+
if (!s.lazyLoad) {
|
|
194
197
|
let f;
|
|
195
|
-
switch (
|
|
196
|
-
case
|
|
198
|
+
switch (r.type) {
|
|
199
|
+
case i.select:
|
|
197
200
|
f = h("VvSelect");
|
|
198
201
|
break;
|
|
199
|
-
case
|
|
202
|
+
case i.checkbox:
|
|
200
203
|
f = h("VvCheckbox");
|
|
201
204
|
break;
|
|
202
|
-
case
|
|
205
|
+
case i.radio:
|
|
203
206
|
f = h("VvRadio");
|
|
204
207
|
break;
|
|
205
|
-
case
|
|
208
|
+
case i.textarea:
|
|
206
209
|
f = h("VvTextarea");
|
|
207
210
|
break;
|
|
208
|
-
case
|
|
211
|
+
case i.radioGroup:
|
|
209
212
|
f = h("VvRadioGroup");
|
|
210
213
|
break;
|
|
211
|
-
case
|
|
214
|
+
case i.checkboxGroup:
|
|
212
215
|
f = h("VvCheckboxGroup");
|
|
213
216
|
break;
|
|
214
|
-
case
|
|
217
|
+
case i.combobox:
|
|
215
218
|
f = h("VvCombobox");
|
|
216
219
|
break;
|
|
217
220
|
default:
|
|
@@ -223,65 +226,65 @@ const ne = (e, n, o, l = {}) => $({
|
|
|
223
226
|
`[form-vue warn]: ${f} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`
|
|
224
227
|
);
|
|
225
228
|
}
|
|
226
|
-
return
|
|
227
|
-
switch (
|
|
228
|
-
case
|
|
229
|
+
return H(async () => {
|
|
230
|
+
switch (s.sideEffects && await Promise.resolve(s.sideEffects(r.type)), r.type) {
|
|
231
|
+
case i.textarea:
|
|
229
232
|
return import("@volverjs/ui-vue/vv-textarea");
|
|
230
|
-
case
|
|
233
|
+
case i.radio:
|
|
231
234
|
return import("@volverjs/ui-vue/vv-radio");
|
|
232
|
-
case
|
|
235
|
+
case i.radioGroup:
|
|
233
236
|
return import("@volverjs/ui-vue/vv-radio-group");
|
|
234
|
-
case
|
|
237
|
+
case i.checkbox:
|
|
235
238
|
return import("@volverjs/ui-vue/vv-checkbox");
|
|
236
|
-
case
|
|
239
|
+
case i.checkboxGroup:
|
|
237
240
|
return import("@volverjs/ui-vue/vv-checkbox-group");
|
|
238
|
-
case
|
|
241
|
+
case i.combobox:
|
|
239
242
|
return import("@volverjs/ui-vue/vv-combobox");
|
|
240
243
|
}
|
|
241
244
|
return import("@volverjs/ui-vue/vv-input-text");
|
|
242
245
|
});
|
|
243
|
-
}), hasProps:
|
|
246
|
+
}), hasProps: z, invalid: b };
|
|
244
247
|
},
|
|
245
248
|
render() {
|
|
246
|
-
return this.is ? x(this.is, this.hasProps, this.$slots) : this.type ===
|
|
249
|
+
return this.is ? x(this.is, this.hasProps, this.$slots) : this.type === i.custom ? x(this.component, null, this.$slots) : x(this.component, this.hasProps, this.$slots);
|
|
247
250
|
}
|
|
248
|
-
}),
|
|
249
|
-
const
|
|
250
|
-
let
|
|
251
|
-
for (;
|
|
252
|
-
|
|
253
|
-
return
|
|
254
|
-
},
|
|
251
|
+
}), M = (e, o = {}) => {
|
|
252
|
+
const a = (t) => {
|
|
253
|
+
let l = t;
|
|
254
|
+
for (; l instanceof ee; )
|
|
255
|
+
l = l.innerType();
|
|
256
|
+
return l;
|
|
257
|
+
}, s = a(e);
|
|
255
258
|
return {
|
|
256
|
-
...(
|
|
259
|
+
...(s instanceof L ? s._def.unknownKeys === "passthrough" : !1) ? o : {},
|
|
257
260
|
...Object.fromEntries(
|
|
258
|
-
Object.entries(
|
|
259
|
-
const
|
|
260
|
-
let
|
|
261
|
-
if (
|
|
262
|
-
return [t,
|
|
263
|
-
if (
|
|
264
|
-
const
|
|
265
|
-
if (
|
|
266
|
-
return [t,
|
|
261
|
+
Object.entries(s.shape).map(([t, l]) => {
|
|
262
|
+
const u = o[t], c = a(l);
|
|
263
|
+
let n;
|
|
264
|
+
if (c instanceof T && (n = c._def.defaultValue()), u === null && c instanceof j)
|
|
265
|
+
return [t, u];
|
|
266
|
+
if (c instanceof F) {
|
|
267
|
+
const v = c.safeParse(o[t]);
|
|
268
|
+
if (v.success)
|
|
269
|
+
return [t, v.data ?? n];
|
|
267
270
|
}
|
|
268
|
-
return
|
|
271
|
+
return c instanceof L ? [
|
|
269
272
|
t,
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
+
M(
|
|
274
|
+
c,
|
|
275
|
+
u && typeof u == "object" ? u : {}
|
|
273
276
|
)
|
|
274
|
-
] : [t,
|
|
277
|
+
] : [t, n];
|
|
275
278
|
})
|
|
276
279
|
)
|
|
277
280
|
};
|
|
278
|
-
},
|
|
279
|
-
const
|
|
281
|
+
}, ae = (e, o, a) => {
|
|
282
|
+
const s = g(), r = g(), t = g();
|
|
280
283
|
return {
|
|
281
|
-
errors:
|
|
282
|
-
status:
|
|
284
|
+
errors: s,
|
|
285
|
+
status: r,
|
|
283
286
|
formData: t,
|
|
284
|
-
component:
|
|
287
|
+
component: E({
|
|
285
288
|
name: "FormComponent",
|
|
286
289
|
props: {
|
|
287
290
|
modelValue: {
|
|
@@ -295,50 +298,50 @@ const ne = (e, n, o, l = {}) => $({
|
|
|
295
298
|
},
|
|
296
299
|
emits: ["invalid", "valid", "submit", "update:modelValue"],
|
|
297
300
|
expose: ["submit", "errors", "status"],
|
|
298
|
-
setup(
|
|
299
|
-
t.value =
|
|
301
|
+
setup(l, { emit: u }) {
|
|
302
|
+
t.value = M(
|
|
300
303
|
e,
|
|
301
|
-
|
|
304
|
+
l.modelValue
|
|
302
305
|
);
|
|
303
|
-
const
|
|
306
|
+
const c = (a == null ? void 0 : a.continuosValidation) || l.continuosValidation;
|
|
304
307
|
y(
|
|
305
|
-
() =>
|
|
306
|
-
(
|
|
307
|
-
if (
|
|
308
|
-
const d =
|
|
308
|
+
() => l.modelValue,
|
|
309
|
+
(m) => {
|
|
310
|
+
if (m) {
|
|
311
|
+
const d = Q(m) ? X(m) : m;
|
|
309
312
|
t.value = typeof (d == null ? void 0 : d.clone) == "function" ? d.clone() : JSON.parse(JSON.stringify(d));
|
|
310
313
|
}
|
|
311
314
|
},
|
|
312
315
|
{ deep: !0 }
|
|
313
|
-
),
|
|
316
|
+
), D(
|
|
314
317
|
t,
|
|
315
|
-
(
|
|
316
|
-
(
|
|
318
|
+
(m) => {
|
|
319
|
+
(s.value || c) && n(), (!m || !l.modelValue || JSON.stringify(m) !== JSON.stringify(l.modelValue)) && u("update:modelValue", m);
|
|
317
320
|
},
|
|
318
|
-
{ deep: !0, throttle: (
|
|
321
|
+
{ deep: !0, throttle: (a == null ? void 0 : a.updateThrottle) ?? 500 }
|
|
319
322
|
);
|
|
320
|
-
const
|
|
321
|
-
const d = e.safeParse(
|
|
322
|
-
return d.success ? (
|
|
323
|
-
},
|
|
324
|
-
return
|
|
323
|
+
const n = (m = t.value) => {
|
|
324
|
+
const d = e.safeParse(m);
|
|
325
|
+
return d.success ? (s.value = void 0, r.value = "valid", t.value = d.data, u("update:modelValue", t.value), u("valid", d.data), !0) : (s.value = d.error.format(), r.value = "invalid", u("invalid", s.value), !1);
|
|
326
|
+
}, v = () => n() ? (u("submit", t.value), !0) : !1;
|
|
327
|
+
return $(o, {
|
|
325
328
|
modelValue: t,
|
|
326
|
-
submit:
|
|
327
|
-
errors: w(
|
|
328
|
-
}), { submit:
|
|
329
|
+
submit: v,
|
|
330
|
+
errors: w(s)
|
|
331
|
+
}), { submit: v };
|
|
329
332
|
},
|
|
330
333
|
render() {
|
|
331
334
|
return x(
|
|
332
335
|
"form",
|
|
333
336
|
{
|
|
334
|
-
onSubmit:
|
|
337
|
+
onSubmit: Y(this.submit, ["prevent"])
|
|
335
338
|
},
|
|
336
339
|
this.$slots
|
|
337
340
|
);
|
|
338
341
|
}
|
|
339
342
|
})
|
|
340
343
|
};
|
|
341
|
-
},
|
|
344
|
+
}, se = (e, o) => E({
|
|
342
345
|
name: "WrapperComponent",
|
|
343
346
|
props: {
|
|
344
347
|
name: {
|
|
@@ -352,98 +355,104 @@ const ne = (e, n, o, l = {}) => $({
|
|
|
352
355
|
},
|
|
353
356
|
emits: ["invalid", "valid"],
|
|
354
357
|
expose: ["fields", "invalid"],
|
|
355
|
-
setup(
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
name: w(
|
|
359
|
-
errors:
|
|
360
|
-
fields:
|
|
358
|
+
setup(a, { emit: s }) {
|
|
359
|
+
const r = S(e), t = S(o, void 0), l = g(/* @__PURE__ */ new Set()), u = g(/* @__PURE__ */ new Map()), { name: c } = I(a);
|
|
360
|
+
$(o, {
|
|
361
|
+
name: w(c),
|
|
362
|
+
errors: u,
|
|
363
|
+
fields: l
|
|
361
364
|
}), y(
|
|
362
|
-
|
|
363
|
-
(
|
|
364
|
-
t != null && t.fields &&
|
|
365
|
-
t == null || t.fields.value.add(
|
|
365
|
+
l,
|
|
366
|
+
(v) => {
|
|
367
|
+
t != null && t.fields && v.forEach((m) => {
|
|
368
|
+
t == null || t.fields.value.add(m);
|
|
366
369
|
});
|
|
367
370
|
},
|
|
368
371
|
{ deep: !0 }
|
|
369
372
|
), y(
|
|
370
|
-
() => new Map(
|
|
371
|
-
(
|
|
372
|
-
t != null && t.errors && (Array.from(
|
|
373
|
+
() => new Map(u.value),
|
|
374
|
+
(v, m) => {
|
|
375
|
+
t != null && t.errors && (Array.from(m.keys()).forEach((d) => {
|
|
373
376
|
t.errors.value.delete(d);
|
|
374
|
-
}), Array.from(
|
|
375
|
-
const p =
|
|
377
|
+
}), Array.from(v.keys()).forEach((d) => {
|
|
378
|
+
const p = v.get(d);
|
|
376
379
|
p && t.errors.value.set(d, p);
|
|
377
380
|
}));
|
|
378
381
|
},
|
|
379
382
|
{ deep: !0 }
|
|
380
383
|
);
|
|
381
|
-
const
|
|
382
|
-
return y(
|
|
383
|
-
|
|
384
|
-
}), {
|
|
384
|
+
const n = V(() => r != null && r.errors.value ? u.value.size > 0 : !1);
|
|
385
|
+
return y(n, () => {
|
|
386
|
+
n.value ? s("invalid") : s("valid");
|
|
387
|
+
}), {
|
|
388
|
+
formData: r == null ? void 0 : r.modelValue,
|
|
389
|
+
errors: r == null ? void 0 : r.errors,
|
|
390
|
+
invalid: n,
|
|
391
|
+
fields: l,
|
|
392
|
+
fieldsErrors: u
|
|
393
|
+
};
|
|
385
394
|
},
|
|
386
395
|
render() {
|
|
387
|
-
var
|
|
396
|
+
var a, s, r, t;
|
|
388
397
|
return this.tag ? x(
|
|
389
398
|
this.tag,
|
|
390
399
|
null,
|
|
391
|
-
((
|
|
400
|
+
((s = (a = this.$slots).default) == null ? void 0 : s.call(a, {
|
|
392
401
|
invalid: this.invalid,
|
|
393
|
-
formData:
|
|
394
|
-
errors:
|
|
395
|
-
fieldsErrors: this.
|
|
402
|
+
formData: this.formData,
|
|
403
|
+
errors: this.errors,
|
|
404
|
+
fieldsErrors: this.fieldsErrors
|
|
396
405
|
})) ?? this.$slots.defalut
|
|
397
|
-
) : ((
|
|
406
|
+
) : ((t = (r = this.$slots).default) == null ? void 0 : t.call(r, {
|
|
398
407
|
invalid: this.invalid,
|
|
399
|
-
formData:
|
|
400
|
-
errors:
|
|
401
|
-
fieldsErrors: this.
|
|
408
|
+
formData: this.formData,
|
|
409
|
+
errors: this.errors,
|
|
410
|
+
fieldsErrors: this.fieldsErrors
|
|
402
411
|
})) ?? this.$slots.defalut;
|
|
403
412
|
}
|
|
404
|
-
}),
|
|
405
|
-
const
|
|
413
|
+
}), P = (e, o = {}) => {
|
|
414
|
+
const a = Symbol(), s = Symbol(), r = Symbol(), {
|
|
406
415
|
component: t,
|
|
407
|
-
errors:
|
|
408
|
-
status:
|
|
409
|
-
formData:
|
|
410
|
-
} =
|
|
411
|
-
o,
|
|
412
|
-
l
|
|
413
|
-
), m = ne(
|
|
414
|
-
o,
|
|
415
|
-
l,
|
|
416
|
+
errors: l,
|
|
417
|
+
status: u,
|
|
418
|
+
formData: c
|
|
419
|
+
} = ae(e, a, o), n = se(
|
|
416
420
|
a,
|
|
417
|
-
|
|
421
|
+
s
|
|
422
|
+
), v = oe(
|
|
423
|
+
a,
|
|
424
|
+
s,
|
|
425
|
+
r,
|
|
426
|
+
o
|
|
418
427
|
);
|
|
419
428
|
return {
|
|
420
429
|
VvForm: t,
|
|
421
|
-
VvFormWrapper:
|
|
422
|
-
VvFormField:
|
|
423
|
-
formInjectionKey:
|
|
424
|
-
formWrapperInjectionKey:
|
|
425
|
-
formFieldInjectionKey:
|
|
426
|
-
errors:
|
|
427
|
-
status:
|
|
428
|
-
formData:
|
|
430
|
+
VvFormWrapper: n,
|
|
431
|
+
VvFormField: v,
|
|
432
|
+
formInjectionKey: a,
|
|
433
|
+
formWrapperInjectionKey: s,
|
|
434
|
+
formFieldInjectionKey: r,
|
|
435
|
+
errors: l,
|
|
436
|
+
status: u,
|
|
437
|
+
formData: c
|
|
429
438
|
};
|
|
430
439
|
}, Z = Symbol(), fe = (e) => {
|
|
431
|
-
let
|
|
432
|
-
return e.schema && (
|
|
433
|
-
...
|
|
434
|
-
install(
|
|
435
|
-
|
|
440
|
+
let o = {};
|
|
441
|
+
return e.schema && (o = P(e.schema, e)), {
|
|
442
|
+
...o,
|
|
443
|
+
install(a, { global: s = !1 } = {}) {
|
|
444
|
+
a.provide(Z, e), s && (a.config.globalProperties.$vvForm = e, o != null && o.VvForm && a.component("VvForm", o.VvForm), o != null && o.VvFormWrapper && a.component("VvFormWrapper", o.VvFormWrapper), o != null && o.VvFormField && a.component("VvFormField", o.VvFormField));
|
|
436
445
|
}
|
|
437
446
|
};
|
|
438
|
-
}, de = (e,
|
|
439
|
-
const
|
|
440
|
-
return
|
|
447
|
+
}, de = (e, o = {}) => {
|
|
448
|
+
const a = { ...S(Z, {}), ...o };
|
|
449
|
+
return P(e, a);
|
|
441
450
|
};
|
|
442
451
|
export {
|
|
443
|
-
|
|
452
|
+
i as FormFieldType,
|
|
444
453
|
fe as createForm,
|
|
445
|
-
|
|
446
|
-
|
|
454
|
+
M as defaultObjectBySchema,
|
|
455
|
+
P as formFactory,
|
|
447
456
|
Z as pluginInjectionKey,
|
|
448
457
|
de as useForm
|
|
449
458
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(p,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],t):(p=typeof globalThis<"u"?globalThis:p||self,t(p["@volverjs/form-vue"]={},p.Vue,p.VueUseCore,p.zod))})(this,function(p,t,
|
|
1
|
+
(function(p,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],t):(p=typeof globalThis<"u"?globalThis:p||self,t(p["@volverjs/form-vue"]={},p.Vue,p.VueUseCore,p.zod))})(this,function(p,t,A,b){"use strict";function x(e){return Array.isArray(e)}function B(e){return typeof e<"u"}function O(e){return e===null}function C(e){return typeof e=="object"}function E(e){return typeof e=="string"}function g(e){return typeof e>"u"}const W=/^[0-9]+$/,_=["__proto__","prototype","constructor"];function $(e,a,s){const l=B(s)?s:void 0;if(!C(e)||!E(a))return l;const n=I(a);if(n.length!==0){for(const r of n){if(r==="*")continue;const u=function(c){return c.map(f=>g(f)||O(f)?f:x(f)?u(f):f[r])};if(x(e)&&!W.test(r)?e=u(e):e=e[r],g(e)||O(e))break}return g(e)?l:e}}function G(e,a,s){if(!C(e)||!E(a))return;const l=I(a);if(l.length===0)return;const n=l.length;for(let r=0;r<n;r++){const u=l[r];if(r===n-1){e[u]=s;return}if(u==="*"&&x(e)){const c=l.slice(r+1).join(".");for(const f of e)G(f,c,s);return}g(e[u])&&(e[u]={}),e=e[u]}}function I(e){const a=e.split(/[.]|(?:\[(\d|\*)\])/).filter(s=>!!s);return a.some(s=>_.indexOf(s)!==-1)?[]:a}var i=(e=>(e.text="text",e.number="number",e.email="email",e.password="password",e.tel="tel",e.url="url",e.search="search",e.date="date",e.time="time",e.datetimeLocal="datetimeLocal",e.month="month",e.week="week",e.color="color",e.select="select",e.checkbox="checkbox",e.radio="radio",e.textarea="textarea",e.radioGroup="radioGroup",e.checkboxGroup="checkboxGroup",e.combobox="combobox",e.custom="custom",e))(i||{});const K=(e,a,s,l={})=>t.defineComponent({name:"FieldComponent",props:{type:{type:String,validator:n=>Object.values(i).includes(n),default:i.custom},is:{type:[Object,String],default:void 0},name:{type:[String,Number,Boolean,Symbol],required:!0},props:{type:[Object,Function],default:()=>({})},showValid:{type:Boolean,default:!1},defaultValue:{type:[String,Number,Boolean,Array,Object],default:void 0}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(n,{slots:r,emit:u}){const c=t.computed({get(){if(o!=null&&o.modelValue)return $(Object(o.modelValue.value),String(n.name))},set(d){o!=null&&o.modelValue&&(G(Object(o.modelValue.value),String(n.name),d),u("update:modelValue",{newValue:c.value,formData:o==null?void 0:o.modelValue}))}});t.onMounted(()=>{c.value===void 0&&n.defaultValue!==void 0&&(c.value=n.defaultValue)}),t.onBeforeUnmount(()=>{U(),J()});const f=t.inject(a,void 0);f&&f.fields.value.add(n.name);const o=t.inject(e),{props:h,name:v}=t.toRefs(n),m=t.computed(()=>{if(o!=null&&o.errors.value)return $(o.errors.value,String(n.name))}),V=t.computed(()=>{var d;return(d=m.value)==null?void 0:d._errors}),y=t.computed(()=>m.value!==void 0),U=t.watch(y,()=>{y.value?(u("invalid",V.value),f&&f.errors.value.set(n.name,{_errors:V.value})):(u("valid",c.value),f&&f.errors.value.delete(n.name))}),J=t.watch(()=>o==null?void 0:o.modelValue,()=>{u("update:formData",o==null?void 0:o.modelValue)},{deep:!0}),L=d=>{c.value=d},N=t.computed(()=>typeof h.value=="function"?h.value(o==null?void 0:o.modelValue):h.value),R=t.computed(()=>({...N.value,name:N.value.name??n.name,invalid:y.value,valid:n.showValid?!!(!y.value&&c.value):void 0,type:(d=>{if([i.text,i.number,i.email,i.password,i.tel,i.url,i.search,i.date,i.time,i.datetimeLocal,i.month,i.week,i.color].includes(d))return d})(n.type),invalidLabel:V.value,modelValue:c.value,errors:n.is?m.value:void 0,"onUpdate:modelValue":L}));return t.provide(s,{name:t.readonly(v),errors:t.readonly(m)}),{component:t.computed(()=>{if(n.type===i.custom)return{render(){var d;return((d=r.default)==null?void 0:d.call(r,{modelValue:c.value,onUpdate:L,invalid:y.value,invalidLabel:V.value,formData:o==null?void 0:o.modelValue.value,formErrors:o==null?void 0:o.errors.value,errors:m.value}))??r.defalut}};if(!l.lazyLoad){let d;switch(n.type){case i.select:d=t.resolveComponent("VvSelect");break;case i.checkbox:d=t.resolveComponent("VvCheckbox");break;case i.radio:d=t.resolveComponent("VvRadio");break;case i.textarea:d=t.resolveComponent("VvTextarea");break;case i.radioGroup:d=t.resolveComponent("VvRadioGroup");break;case i.checkboxGroup:d=t.resolveComponent("VvCheckboxGroup");break;case i.combobox:d=t.resolveComponent("VvCombobox");break;default:d=t.resolveComponent("VvInputText")}if(typeof d!="string")return d;console.warn(`[form-vue warn]: ${d} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return t.defineAsyncComponent(async()=>{switch(l.sideEffects&&await Promise.resolve(l.sideEffects(n.type)),n.type){case i.textarea:return import("@volverjs/ui-vue/vv-textarea");case i.radio:return import("@volverjs/ui-vue/vv-radio");case i.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case i.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case i.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case i.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:R,invalid:y}},render(){return this.is?t.h(this.is,this.hasProps,this.$slots):this.type===i.custom?t.h(this.component,null,this.$slots):t.h(this.component,this.hasProps,this.$slots)}}),S=(e,a={})=>{const s=r=>{let u=r;for(;u instanceof b.ZodEffects;)u=u.innerType();return u},l=s(e);return{...(l instanceof b.ZodObject?l._def.unknownKeys==="passthrough":!1)?a:{},...Object.fromEntries(Object.entries(l.shape).map(([r,u])=>{const c=a[r],f=s(u);let o;if(f instanceof b.ZodDefault&&(o=f._def.defaultValue()),c===null&&f instanceof b.ZodNullable)return[r,c];if(f instanceof b.ZodSchema){const h=f.safeParse(a[r]);if(h.success)return[r,h.data??o]}return f instanceof b.ZodObject?[r,S(f,c&&typeof c=="object"?c:{})]:[r,o]}))}},M=(e,a,s)=>{const l=t.ref(),n=t.ref(),r=t.ref();return{errors:l,status:n,formData:r,component:t.defineComponent({name:"FormComponent",props:{modelValue:{type:Object,default:()=>({})},continuosValidation:{type:Boolean,default:!1}},emits:["invalid","valid","submit","update:modelValue"],expose:["submit","errors","status"],setup(u,{emit:c}){r.value=S(e,u.modelValue);const f=(s==null?void 0:s.continuosValidation)||u.continuosValidation;t.watch(()=>u.modelValue,v=>{if(v){const m=t.isProxy(v)?t.toRaw(v):v;r.value=typeof(m==null?void 0:m.clone)=="function"?m.clone():JSON.parse(JSON.stringify(m))}},{deep:!0}),A.watchThrottled(r,v=>{(l.value||f)&&o(),(!v||!u.modelValue||JSON.stringify(v)!==JSON.stringify(u.modelValue))&&c("update:modelValue",v)},{deep:!0,throttle:(s==null?void 0:s.updateThrottle)??500});const o=(v=r.value)=>{const m=e.safeParse(v);return m.success?(l.value=void 0,n.value="valid",r.value=m.data,c("update:modelValue",r.value),c("valid",m.data),!0):(l.value=m.error.format(),n.value="invalid",c("invalid",l.value),!1)},h=()=>o()?(c("submit",r.value),!0):!1;return t.provide(a,{modelValue:r,submit:h,errors:t.readonly(l)}),{submit:h}},render(){return t.h("form",{onSubmit:t.withModifiers(this.submit,["prevent"])},this.$slots)}})}},P=(e,a)=>t.defineComponent({name:"WrapperComponent",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(s,{emit:l}){const n=t.inject(e),r=t.inject(a,void 0),u=t.ref(new Set),c=t.ref(new Map),{name:f}=t.toRefs(s);t.provide(a,{name:t.readonly(f),errors:c,fields:u}),t.watch(u,h=>{r!=null&&r.fields&&h.forEach(v=>{r==null||r.fields.value.add(v)})},{deep:!0}),t.watch(()=>new Map(c.value),(h,v)=>{r!=null&&r.errors&&(Array.from(v.keys()).forEach(m=>{r.errors.value.delete(m)}),Array.from(h.keys()).forEach(m=>{const V=h.get(m);V&&r.errors.value.set(m,V)}))},{deep:!0});const o=t.computed(()=>n!=null&&n.errors.value?c.value.size>0:!1);return t.watch(o,()=>{o.value?l("invalid"):l("valid")}),{formData:n==null?void 0:n.modelValue,errors:n==null?void 0:n.errors,invalid:o,fields:u,fieldsErrors:c}},render(){var s,l,n,r;return this.tag?t.h(this.tag,null,((l=(s=this.$slots).default)==null?void 0:l.call(s,{invalid:this.invalid,formData:this.formData,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut):((r=(n=this.$slots).default)==null?void 0:r.call(n,{invalid:this.invalid,formData:this.formData,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}),k=(e,a={})=>{const s=Symbol(),l=Symbol(),n=Symbol(),{component:r,errors:u,status:c,formData:f}=M(e,s,a),o=P(s,l),h=K(s,l,n,a);return{VvForm:r,VvFormWrapper:o,VvFormField:h,formInjectionKey:s,formWrapperInjectionKey:l,formFieldInjectionKey:n,errors:u,status:c,formData:f}},w=Symbol(),Z=e=>{let a={};return e.schema&&(a=k(e.schema,e)),{...a,install(s,{global:l=!1}={}){s.provide(w,e),l&&(s.config.globalProperties.$vvForm=e,a!=null&&a.VvForm&&s.component("VvForm",a.VvForm),a!=null&&a.VvFormWrapper&&s.component("VvFormWrapper",a.VvFormWrapper),a!=null&&a.VvFormField&&s.component("VvFormField",a.VvFormField))}}},q=(e,a={})=>{const s={...t.inject(w,{}),...a};return k(e,s)};p.FormFieldType=i,p.createForm=Z,p.defaultObjectBySchema=S,p.formFactory=k,p.pluginInjectionKey=w,p.useForm=q,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"bugs": {
|
|
20
20
|
"url": "https://github.com/volverjs/form-vue/issues"
|
|
21
21
|
},
|
|
22
|
-
"version": "0.0.10-beta.
|
|
22
|
+
"version": "0.0.10-beta.4",
|
|
23
23
|
"engines": {
|
|
24
24
|
"node": ">= 16.x"
|
|
25
25
|
},
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"*.d.ts"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@volverjs/ui-vue": "0.0.6-beta.
|
|
39
|
-
"@vueuse/core": "^
|
|
38
|
+
"@volverjs/ui-vue": "0.0.6-beta.7",
|
|
39
|
+
"@vueuse/core": "^10.0.2",
|
|
40
40
|
"ts-dot-prop": "^2.1.2",
|
|
41
41
|
"vue": "^3.2.47",
|
|
42
42
|
"zod": "^3.21.4"
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"eslint": "^8.38.0",
|
|
56
56
|
"eslint-config-prettier": "^8.8.0",
|
|
57
57
|
"eslint-plugin-prettier": "^4.2.1",
|
|
58
|
-
"happy-dom": "^9.1
|
|
58
|
+
"happy-dom": "^9.6.1",
|
|
59
59
|
"prettier": "^2.8.7",
|
|
60
60
|
"typescript": "^5.0.4",
|
|
61
61
|
"vite": "^4.2.1",
|
package/src/VvForm.ts
CHANGED
package/src/VvFormField.ts
CHANGED
|
@@ -84,22 +84,22 @@ export const defineFormField = <
|
|
|
84
84
|
// v-model
|
|
85
85
|
const modelValue = computed({
|
|
86
86
|
get() {
|
|
87
|
-
if (!
|
|
87
|
+
if (!injectedFormData?.modelValue) return
|
|
88
88
|
return get(
|
|
89
|
-
Object(
|
|
89
|
+
Object(injectedFormData.modelValue.value),
|
|
90
90
|
String(props.name),
|
|
91
91
|
)
|
|
92
92
|
},
|
|
93
93
|
set(value) {
|
|
94
|
-
if (!
|
|
94
|
+
if (!injectedFormData?.modelValue) return
|
|
95
95
|
set(
|
|
96
|
-
Object(
|
|
96
|
+
Object(injectedFormData.modelValue.value),
|
|
97
97
|
String(props.name),
|
|
98
98
|
value,
|
|
99
99
|
)
|
|
100
100
|
emit('update:modelValue', {
|
|
101
101
|
newValue: modelValue.value,
|
|
102
|
-
formData:
|
|
102
|
+
formData: injectedFormData?.modelValue,
|
|
103
103
|
})
|
|
104
104
|
},
|
|
105
105
|
})
|
|
@@ -114,24 +114,24 @@ export const defineFormField = <
|
|
|
114
114
|
|
|
115
115
|
onBeforeUnmount(() => {
|
|
116
116
|
unwatchInvalid()
|
|
117
|
-
|
|
117
|
+
unwatchInjectedFormData()
|
|
118
118
|
})
|
|
119
119
|
|
|
120
120
|
// inject data from parent form wrapper
|
|
121
|
-
const
|
|
122
|
-
if (
|
|
123
|
-
|
|
121
|
+
const injectedWrapperData = inject(wrapperProvideKey, undefined)
|
|
122
|
+
if (injectedWrapperData) {
|
|
123
|
+
injectedWrapperData.fields.value.add(props.name as string)
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
// inject data from parent form
|
|
127
|
-
const
|
|
127
|
+
const injectedFormData = inject(formProvideKey)
|
|
128
128
|
const { props: fieldProps, name: fieldName } = toRefs(props)
|
|
129
129
|
|
|
130
130
|
const errors = computed(() => {
|
|
131
|
-
if (!
|
|
131
|
+
if (!injectedFormData?.errors.value) {
|
|
132
132
|
return undefined
|
|
133
133
|
}
|
|
134
|
-
return get(
|
|
134
|
+
return get(injectedFormData.errors.value, String(props.name))
|
|
135
135
|
})
|
|
136
136
|
const invalidLabel = computed(() => {
|
|
137
137
|
return errors.value?._errors
|
|
@@ -142,24 +142,27 @@ export const defineFormField = <
|
|
|
142
142
|
const unwatchInvalid = watch(invalid, () => {
|
|
143
143
|
if (invalid.value) {
|
|
144
144
|
emit('invalid', invalidLabel.value)
|
|
145
|
-
if (
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
145
|
+
if (injectedWrapperData) {
|
|
146
|
+
injectedWrapperData.errors.value.set(
|
|
147
|
+
props.name as string,
|
|
148
|
+
{
|
|
149
|
+
_errors: invalidLabel.value,
|
|
150
|
+
},
|
|
151
|
+
)
|
|
149
152
|
}
|
|
150
153
|
} else {
|
|
151
154
|
emit('valid', modelValue.value)
|
|
152
|
-
if (
|
|
153
|
-
|
|
155
|
+
if (injectedWrapperData) {
|
|
156
|
+
injectedWrapperData.errors.value.delete(
|
|
154
157
|
props.name as string,
|
|
155
158
|
)
|
|
156
159
|
}
|
|
157
160
|
}
|
|
158
161
|
})
|
|
159
|
-
const
|
|
160
|
-
() =>
|
|
162
|
+
const unwatchInjectedFormData = watch(
|
|
163
|
+
() => injectedFormData?.modelValue,
|
|
161
164
|
() => {
|
|
162
|
-
emit('update:formData',
|
|
165
|
+
emit('update:formData', injectedFormData?.modelValue)
|
|
163
166
|
},
|
|
164
167
|
{ deep: true },
|
|
165
168
|
)
|
|
@@ -168,7 +171,7 @@ export const defineFormField = <
|
|
|
168
171
|
}
|
|
169
172
|
const hasFieldProps = computed(() => {
|
|
170
173
|
if (typeof fieldProps.value === 'function') {
|
|
171
|
-
return fieldProps.value(
|
|
174
|
+
return fieldProps.value(injectedFormData?.modelValue)
|
|
172
175
|
}
|
|
173
176
|
return fieldProps.value
|
|
174
177
|
})
|
|
@@ -222,8 +225,9 @@ export const defineFormField = <
|
|
|
222
225
|
onUpdate,
|
|
223
226
|
invalid: invalid.value,
|
|
224
227
|
invalidLabel: invalidLabel.value,
|
|
225
|
-
formData:
|
|
226
|
-
|
|
228
|
+
formData:
|
|
229
|
+
injectedFormData?.modelValue.value,
|
|
230
|
+
formErrors: injectedFormData?.errors.value,
|
|
227
231
|
errors: errors.value,
|
|
228
232
|
}) ?? slots.defalut
|
|
229
233
|
)
|
package/src/VvFormWrapper.ts
CHANGED
|
@@ -38,10 +38,10 @@ export const defineFormWrapper = <
|
|
|
38
38
|
emits: ['invalid', 'valid'],
|
|
39
39
|
expose: ['fields', 'invalid'],
|
|
40
40
|
setup(props, { emit }) {
|
|
41
|
-
const
|
|
41
|
+
const injectedFormData = inject(formProvideKey)
|
|
42
42
|
const wrapperProvided = inject(wrapperProvideKey, undefined)
|
|
43
43
|
const fields = ref(new Set<string>())
|
|
44
|
-
const
|
|
44
|
+
const fieldsErrors: Ref<
|
|
45
45
|
Map<string, Record<string, { _errors: string[] }>>
|
|
46
46
|
> = ref(new Map())
|
|
47
47
|
const { name } = toRefs(props)
|
|
@@ -49,7 +49,7 @@ export const defineFormWrapper = <
|
|
|
49
49
|
// provide data to child fields
|
|
50
50
|
provide(wrapperProvideKey, {
|
|
51
51
|
name: readonly(name),
|
|
52
|
-
errors,
|
|
52
|
+
errors: fieldsErrors,
|
|
53
53
|
fields,
|
|
54
54
|
})
|
|
55
55
|
|
|
@@ -68,7 +68,7 @@ export const defineFormWrapper = <
|
|
|
68
68
|
|
|
69
69
|
// add fields to parent wrapper
|
|
70
70
|
watch(
|
|
71
|
-
() => new Map(
|
|
71
|
+
() => new Map(fieldsErrors.value),
|
|
72
72
|
(newValue, oldValue) => {
|
|
73
73
|
if (wrapperProvided?.errors) {
|
|
74
74
|
Array.from(oldValue.keys()).forEach((key) => {
|
|
@@ -86,10 +86,10 @@ export const defineFormWrapper = <
|
|
|
86
86
|
)
|
|
87
87
|
|
|
88
88
|
const invalid = computed(() => {
|
|
89
|
-
if (!
|
|
89
|
+
if (!injectedFormData?.errors.value) {
|
|
90
90
|
return false
|
|
91
91
|
}
|
|
92
|
-
return
|
|
92
|
+
return fieldsErrors.value.size > 0
|
|
93
93
|
})
|
|
94
94
|
|
|
95
95
|
watch(invalid, () => {
|
|
@@ -100,7 +100,13 @@ export const defineFormWrapper = <
|
|
|
100
100
|
}
|
|
101
101
|
})
|
|
102
102
|
|
|
103
|
-
return {
|
|
103
|
+
return {
|
|
104
|
+
formData: injectedFormData?.modelValue,
|
|
105
|
+
errors: injectedFormData?.errors,
|
|
106
|
+
invalid,
|
|
107
|
+
fields,
|
|
108
|
+
fieldsErrors,
|
|
109
|
+
}
|
|
104
110
|
},
|
|
105
111
|
render() {
|
|
106
112
|
if (this.tag) {
|
|
@@ -109,18 +115,18 @@ export const defineFormWrapper = <
|
|
|
109
115
|
null,
|
|
110
116
|
this.$slots.default?.({
|
|
111
117
|
invalid: this.invalid,
|
|
112
|
-
formData: this.
|
|
113
|
-
errors: this.
|
|
114
|
-
fieldsErrors: this.
|
|
118
|
+
formData: this.formData,
|
|
119
|
+
errors: this.errors,
|
|
120
|
+
fieldsErrors: this.fieldsErrors,
|
|
115
121
|
}) ?? this.$slots.defalut,
|
|
116
122
|
)
|
|
117
123
|
}
|
|
118
124
|
return (
|
|
119
125
|
this.$slots.default?.({
|
|
120
126
|
invalid: this.invalid,
|
|
121
|
-
formData: this.
|
|
122
|
-
errors: this.
|
|
123
|
-
fieldsErrors: this.
|
|
127
|
+
formData: this.formData,
|
|
128
|
+
errors: this.errors,
|
|
129
|
+
fieldsErrors: this.fieldsErrors,
|
|
124
130
|
}) ?? this.$slots.defalut
|
|
125
131
|
)
|
|
126
132
|
},
|