@volverjs/form-vue 0.0.1
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/LICENSE +21 -0
- package/README.md +281 -0
- package/dist/VvForm.d.ts +29 -0
- package/dist/VvFormField.d.ts +3 -0
- package/dist/VvFormWrapper.d.ts +33 -0
- package/dist/enums.d.ts +23 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.es.js +473 -0
- package/dist/index.umd.js +1 -0
- package/dist/types.d.ts +30 -0
- package/dist/utils.d.ts +2 -0
- package/package.json +91 -0
- package/src/VvForm.ts +125 -0
- package/src/VvFormField.ts +299 -0
- package/src/VvFormWrapper.ts +122 -0
- package/src/enums.ts +23 -0
- package/src/index.ts +97 -0
- package/src/shims.d.ts +1 -0
- package/src/types.d.ts +30 -0
- package/src/utils.ts +22 -0
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import { defineComponent as w, computed as h, onMounted as q, inject as S, toRefs as G, watch as V, provide as $, readonly as A, resolveComponent as b, defineAsyncComponent as Z, h as O, ref as x, isProxy as Y, toRaw as H, withModifiers as Q } from "vue";
|
|
2
|
+
import { watchThrottled as X } from "@vueuse/core";
|
|
3
|
+
import { ZodDefault as P, ZodObject as T } from "zod";
|
|
4
|
+
function E(e) {
|
|
5
|
+
return Array.isArray(e);
|
|
6
|
+
}
|
|
7
|
+
function F(e) {
|
|
8
|
+
return typeof e < "u";
|
|
9
|
+
}
|
|
10
|
+
function N(e) {
|
|
11
|
+
return e === null;
|
|
12
|
+
}
|
|
13
|
+
function L(e) {
|
|
14
|
+
return typeof e == "object";
|
|
15
|
+
}
|
|
16
|
+
function D(e) {
|
|
17
|
+
return typeof e == "string";
|
|
18
|
+
}
|
|
19
|
+
function M(e) {
|
|
20
|
+
return typeof e > "u";
|
|
21
|
+
}
|
|
22
|
+
const ee = /^[0-9]+$/, re = ["__proto__", "prototype", "constructor"];
|
|
23
|
+
function _(e, r, t) {
|
|
24
|
+
const a = F(t) ? t : void 0;
|
|
25
|
+
if (!L(e) || !D(r))
|
|
26
|
+
return a;
|
|
27
|
+
const n = W(r);
|
|
28
|
+
if (n.length !== 0) {
|
|
29
|
+
for (const o of n) {
|
|
30
|
+
if (o === "*")
|
|
31
|
+
continue;
|
|
32
|
+
const i = function(s) {
|
|
33
|
+
return s.map((f) => M(f) || N(f) ? f : E(f) ? i(f) : f[o]);
|
|
34
|
+
};
|
|
35
|
+
if (E(e) && !ee.test(o) ? e = i(e) : e = e[o], M(e) || N(e))
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
return M(e) ? a : e;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function U(e, r, t) {
|
|
42
|
+
if (!L(e) || !D(r))
|
|
43
|
+
return;
|
|
44
|
+
const a = W(r);
|
|
45
|
+
if (a.length === 0)
|
|
46
|
+
return;
|
|
47
|
+
const n = a.length;
|
|
48
|
+
for (let o = 0; o < n; o++) {
|
|
49
|
+
const i = a[o];
|
|
50
|
+
if (o === n - 1) {
|
|
51
|
+
e[i] = t;
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (i === "*" && E(e)) {
|
|
55
|
+
const s = a.slice(o + 1).join(".");
|
|
56
|
+
for (const f of e)
|
|
57
|
+
U(f, s, t);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
M(e[i]) && (e[i] = {}), e = e[i];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function W(e) {
|
|
64
|
+
const r = e.split(/[.]|(?:\[(\d|\*)\])/).filter((t) => !!t);
|
|
65
|
+
return r.some((t) => re.indexOf(t) !== -1) ? [] : r;
|
|
66
|
+
}
|
|
67
|
+
var l = /* @__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))(l || {});
|
|
68
|
+
const te = (e, r, t, a = {}) => w({
|
|
69
|
+
name: "FieldComponent",
|
|
70
|
+
props: {
|
|
71
|
+
type: {
|
|
72
|
+
type: String,
|
|
73
|
+
validator: (n) => Object.values(l).includes(n),
|
|
74
|
+
default: l.custom
|
|
75
|
+
},
|
|
76
|
+
is: {
|
|
77
|
+
type: [Object, String],
|
|
78
|
+
default: void 0
|
|
79
|
+
},
|
|
80
|
+
name: {
|
|
81
|
+
type: [String, Number, Boolean, Symbol],
|
|
82
|
+
required: !0
|
|
83
|
+
},
|
|
84
|
+
props: {
|
|
85
|
+
type: [Object, Function],
|
|
86
|
+
default: () => ({})
|
|
87
|
+
},
|
|
88
|
+
showValid: {
|
|
89
|
+
type: Boolean,
|
|
90
|
+
default: !1
|
|
91
|
+
},
|
|
92
|
+
defaultValue: {
|
|
93
|
+
type: [String, Number, Boolean, Array, Object],
|
|
94
|
+
default: void 0
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
emits: ["invalid", "valid", "update:formData", "update:modelValue"],
|
|
98
|
+
expose: ["invalid", "invalidLabel", "errors"],
|
|
99
|
+
setup(n, { slots: o, emit: i }) {
|
|
100
|
+
const s = h({
|
|
101
|
+
get() {
|
|
102
|
+
if (u != null && u.modelValue)
|
|
103
|
+
return _(
|
|
104
|
+
Object(u.modelValue.value),
|
|
105
|
+
String(n.name)
|
|
106
|
+
);
|
|
107
|
+
},
|
|
108
|
+
set(c) {
|
|
109
|
+
u != null && u.modelValue && (U(
|
|
110
|
+
Object(u.modelValue.value),
|
|
111
|
+
String(n.name),
|
|
112
|
+
c
|
|
113
|
+
), i("update:modelValue", {
|
|
114
|
+
newValue: s.value,
|
|
115
|
+
formData: u == null ? void 0 : u.modelValue
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
q(() => {
|
|
120
|
+
s.value === void 0 && n.defaultValue !== void 0 && (s.value = n.defaultValue);
|
|
121
|
+
});
|
|
122
|
+
const f = S(r, void 0);
|
|
123
|
+
f && f.fields.value.add(n.name);
|
|
124
|
+
const u = S(e), { props: d, name: m } = G(n), v = h(() => {
|
|
125
|
+
if (u != null && u.errors.value)
|
|
126
|
+
return _(u.errors.value, String(n.name));
|
|
127
|
+
}), y = h(() => {
|
|
128
|
+
var c;
|
|
129
|
+
return (c = v.value) == null ? void 0 : c._errors;
|
|
130
|
+
}), p = h(() => v.value !== void 0);
|
|
131
|
+
V(p, () => {
|
|
132
|
+
p.value ? (i("invalid", y.value), f && f.errors.value.set(n.name, {
|
|
133
|
+
_errors: y.value
|
|
134
|
+
})) : (i("valid", s.value), f && f.errors.value.delete(
|
|
135
|
+
n.name
|
|
136
|
+
));
|
|
137
|
+
}), V(
|
|
138
|
+
() => u == null ? void 0 : u.modelValue,
|
|
139
|
+
() => {
|
|
140
|
+
i("update:formData", u == null ? void 0 : u.modelValue);
|
|
141
|
+
},
|
|
142
|
+
{ deep: !0 }
|
|
143
|
+
);
|
|
144
|
+
const k = (c) => {
|
|
145
|
+
s.value = c;
|
|
146
|
+
}, I = h(() => typeof d.value == "function" ? d.value(u == null ? void 0 : u.modelValue) : d.value), R = h(() => ({
|
|
147
|
+
...I.value,
|
|
148
|
+
name: I.value.name ?? n.name,
|
|
149
|
+
invalid: p.value,
|
|
150
|
+
valid: n.showValid ? Boolean(!p.value && s.value) : void 0,
|
|
151
|
+
type: ((c) => {
|
|
152
|
+
if ([
|
|
153
|
+
l.text,
|
|
154
|
+
l.number,
|
|
155
|
+
l.email,
|
|
156
|
+
l.password,
|
|
157
|
+
l.tel,
|
|
158
|
+
l.url,
|
|
159
|
+
l.search,
|
|
160
|
+
l.date,
|
|
161
|
+
l.time,
|
|
162
|
+
l.datetimeLocal,
|
|
163
|
+
l.month,
|
|
164
|
+
l.week,
|
|
165
|
+
l.color
|
|
166
|
+
].includes(c))
|
|
167
|
+
return c;
|
|
168
|
+
})(n.type),
|
|
169
|
+
invalidLabel: y.value,
|
|
170
|
+
modelValue: s.value,
|
|
171
|
+
errors: n.is ? v.value : void 0,
|
|
172
|
+
"onUpdate:modelValue": k
|
|
173
|
+
}));
|
|
174
|
+
return $(t, {
|
|
175
|
+
name: A(m),
|
|
176
|
+
errors: A(v)
|
|
177
|
+
}), { component: h(() => {
|
|
178
|
+
if (n.type === l.custom)
|
|
179
|
+
return {
|
|
180
|
+
render() {
|
|
181
|
+
var c;
|
|
182
|
+
return ((c = o.default) == null ? void 0 : c.call(o, {
|
|
183
|
+
modelValue: s.value,
|
|
184
|
+
onUpdate: k,
|
|
185
|
+
invalid: p.value,
|
|
186
|
+
invalidLabel: y.value,
|
|
187
|
+
formData: u == null ? void 0 : u.modelValue.value,
|
|
188
|
+
formErrors: u == null ? void 0 : u.errors.value,
|
|
189
|
+
errors: v.value
|
|
190
|
+
})) ?? o.defalut;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
if (!a.lazyLoad) {
|
|
194
|
+
let c;
|
|
195
|
+
switch (n.type) {
|
|
196
|
+
case l.select:
|
|
197
|
+
c = b("VvSelect");
|
|
198
|
+
break;
|
|
199
|
+
case l.checkbox:
|
|
200
|
+
c = b("VvCheckbox");
|
|
201
|
+
break;
|
|
202
|
+
case l.radio:
|
|
203
|
+
c = b("VvRadio");
|
|
204
|
+
break;
|
|
205
|
+
case l.textarea:
|
|
206
|
+
c = b("VvTextarea");
|
|
207
|
+
break;
|
|
208
|
+
case l.radioGroup:
|
|
209
|
+
c = b("VvRadioGroup");
|
|
210
|
+
break;
|
|
211
|
+
case l.checkboxGroup:
|
|
212
|
+
c = b("VvCheckboxGroup");
|
|
213
|
+
break;
|
|
214
|
+
case l.combobox:
|
|
215
|
+
c = b("VvCombobox");
|
|
216
|
+
break;
|
|
217
|
+
default:
|
|
218
|
+
c = b("VvInputText");
|
|
219
|
+
}
|
|
220
|
+
if (typeof c != "string")
|
|
221
|
+
return c;
|
|
222
|
+
console.warn(
|
|
223
|
+
`[form-vue warn]: ${c} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
return Z(async () => {
|
|
227
|
+
switch (a.sideEffects && await Promise.resolve(a.sideEffects(n.type)), n.type) {
|
|
228
|
+
case l.textarea:
|
|
229
|
+
return import("@volverjs/ui-vue/vv-textarea");
|
|
230
|
+
case l.radio:
|
|
231
|
+
return import("@volverjs/ui-vue/vv-radio");
|
|
232
|
+
case l.radioGroup:
|
|
233
|
+
return import("@volverjs/ui-vue/vv-radio-group");
|
|
234
|
+
case l.checkbox:
|
|
235
|
+
return import("@volverjs/ui-vue/vv-checkbox");
|
|
236
|
+
case l.checkboxGroup:
|
|
237
|
+
return import("@volverjs/ui-vue/vv-checkbox-group");
|
|
238
|
+
case l.combobox:
|
|
239
|
+
return import("@volverjs/ui-vue/vv-combobox");
|
|
240
|
+
}
|
|
241
|
+
return import("@volverjs/ui-vue/vv-input-text");
|
|
242
|
+
});
|
|
243
|
+
}), hasProps: R, invalid: p };
|
|
244
|
+
},
|
|
245
|
+
render() {
|
|
246
|
+
return this.is ? O(this.is, this.hasProps, this.$slots) : this.type === l.custom ? O(this.component, null, this.$slots) : O(this.component, this.hasProps, this.$slots);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
var ne = function(r) {
|
|
250
|
+
return ae(r) && !oe(r);
|
|
251
|
+
};
|
|
252
|
+
function ae(e) {
|
|
253
|
+
return !!e && typeof e == "object";
|
|
254
|
+
}
|
|
255
|
+
function oe(e) {
|
|
256
|
+
var r = Object.prototype.toString.call(e);
|
|
257
|
+
return r === "[object RegExp]" || r === "[object Date]" || ie(e);
|
|
258
|
+
}
|
|
259
|
+
var ue = typeof Symbol == "function" && Symbol.for, le = ue ? Symbol.for("react.element") : 60103;
|
|
260
|
+
function ie(e) {
|
|
261
|
+
return e.$$typeof === le;
|
|
262
|
+
}
|
|
263
|
+
function se(e) {
|
|
264
|
+
return Array.isArray(e) ? [] : {};
|
|
265
|
+
}
|
|
266
|
+
function j(e, r) {
|
|
267
|
+
return r.clone !== !1 && r.isMergeableObject(e) ? g(se(e), e, r) : e;
|
|
268
|
+
}
|
|
269
|
+
function ce(e, r, t) {
|
|
270
|
+
return e.concat(r).map(function(a) {
|
|
271
|
+
return j(a, t);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
function fe(e, r) {
|
|
275
|
+
if (!r.customMerge)
|
|
276
|
+
return g;
|
|
277
|
+
var t = r.customMerge(e);
|
|
278
|
+
return typeof t == "function" ? t : g;
|
|
279
|
+
}
|
|
280
|
+
function de(e) {
|
|
281
|
+
return Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(e).filter(function(r) {
|
|
282
|
+
return Object.propertyIsEnumerable.call(e, r);
|
|
283
|
+
}) : [];
|
|
284
|
+
}
|
|
285
|
+
function C(e) {
|
|
286
|
+
return Object.keys(e).concat(de(e));
|
|
287
|
+
}
|
|
288
|
+
function B(e, r) {
|
|
289
|
+
try {
|
|
290
|
+
return r in e;
|
|
291
|
+
} catch {
|
|
292
|
+
return !1;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
function me(e, r) {
|
|
296
|
+
return B(e, r) && !(Object.hasOwnProperty.call(e, r) && Object.propertyIsEnumerable.call(e, r));
|
|
297
|
+
}
|
|
298
|
+
function ve(e, r, t) {
|
|
299
|
+
var a = {};
|
|
300
|
+
return t.isMergeableObject(e) && C(e).forEach(function(n) {
|
|
301
|
+
a[n] = j(e[n], t);
|
|
302
|
+
}), C(r).forEach(function(n) {
|
|
303
|
+
me(e, n) || (B(e, n) && t.isMergeableObject(r[n]) ? a[n] = fe(n, t)(e[n], r[n], t) : a[n] = j(r[n], t));
|
|
304
|
+
}), a;
|
|
305
|
+
}
|
|
306
|
+
function g(e, r, t) {
|
|
307
|
+
t = t || {}, t.arrayMerge = t.arrayMerge || ce, t.isMergeableObject = t.isMergeableObject || ne, t.cloneUnlessOtherwiseSpecified = j;
|
|
308
|
+
var a = Array.isArray(r), n = Array.isArray(e), o = a === n;
|
|
309
|
+
return o ? a ? t.arrayMerge(e, r, t) : ve(e, r, t) : j(r, t);
|
|
310
|
+
}
|
|
311
|
+
g.all = function(r, t) {
|
|
312
|
+
if (!Array.isArray(r))
|
|
313
|
+
throw new Error("first argument should be an array");
|
|
314
|
+
return r.reduce(function(a, n) {
|
|
315
|
+
return g(a, n, t);
|
|
316
|
+
}, {});
|
|
317
|
+
};
|
|
318
|
+
var be = g, he = be;
|
|
319
|
+
const J = (e, r = {}) => he(
|
|
320
|
+
Object.fromEntries(
|
|
321
|
+
Object.entries(e.shape).map(([t, a]) => a instanceof P ? [t, a._def.defaultValue()] : a instanceof T ? [t, J(a)] : [t, void 0])
|
|
322
|
+
),
|
|
323
|
+
r
|
|
324
|
+
), ye = (e, r, t) => w({
|
|
325
|
+
name: "FormComponent",
|
|
326
|
+
props: {
|
|
327
|
+
modelValue: {
|
|
328
|
+
type: Object,
|
|
329
|
+
default: () => ({})
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
emits: ["invalid", "valid", "submit", "update:modelValue"],
|
|
333
|
+
expose: ["submit", "errors", "status"],
|
|
334
|
+
setup(a, { emit: n }) {
|
|
335
|
+
const o = x(
|
|
336
|
+
J(e, a.modelValue)
|
|
337
|
+
);
|
|
338
|
+
V(
|
|
339
|
+
() => a.modelValue,
|
|
340
|
+
(d) => {
|
|
341
|
+
if (d) {
|
|
342
|
+
const m = Y(d) ? H(d) : d;
|
|
343
|
+
o.value = typeof (m == null ? void 0 : m.clone) == "function" ? m.clone() : JSON.parse(JSON.stringify(m));
|
|
344
|
+
}
|
|
345
|
+
},
|
|
346
|
+
{ deep: !0 }
|
|
347
|
+
), X(
|
|
348
|
+
o,
|
|
349
|
+
(d) => {
|
|
350
|
+
i.value && f(), (!d || !a.modelValue || JSON.stringify(d) !== JSON.stringify(a.modelValue)) && n("update:modelValue", d);
|
|
351
|
+
},
|
|
352
|
+
{ deep: !0, throttle: (t == null ? void 0 : t.updateThrottle) ?? 500 }
|
|
353
|
+
);
|
|
354
|
+
const i = x(), s = x(), f = (d = o.value) => {
|
|
355
|
+
const m = e.safeParse(d);
|
|
356
|
+
return m.success ? (i.value = void 0, s.value = "valid", o.value = m.data, n("valid", m.data), !0) : (i.value = m.error.format(), s.value = "invalid", n("invalid", i.value), !1);
|
|
357
|
+
}, u = () => f() ? (n("submit", o.value), !0) : !1;
|
|
358
|
+
return $(r, {
|
|
359
|
+
modelValue: o,
|
|
360
|
+
submit: u,
|
|
361
|
+
errors: A(i)
|
|
362
|
+
}), { submit: u };
|
|
363
|
+
},
|
|
364
|
+
render() {
|
|
365
|
+
return O(
|
|
366
|
+
"form",
|
|
367
|
+
{
|
|
368
|
+
onSubmit: Q(this.submit, ["prevent"])
|
|
369
|
+
},
|
|
370
|
+
this.$slots
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
}), pe = (e, r) => w({
|
|
374
|
+
name: "WrapperComponent",
|
|
375
|
+
props: {
|
|
376
|
+
name: {
|
|
377
|
+
type: String,
|
|
378
|
+
required: !0
|
|
379
|
+
},
|
|
380
|
+
tag: {
|
|
381
|
+
type: String,
|
|
382
|
+
default: void 0
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
emits: ["invalid", "valid"],
|
|
386
|
+
expose: ["fields", "invalid"],
|
|
387
|
+
setup(t, { emit: a }) {
|
|
388
|
+
const n = S(e), o = S(r, void 0), i = x(/* @__PURE__ */ new Set()), s = x(/* @__PURE__ */ new Map()), { name: f } = G(t);
|
|
389
|
+
$(r, {
|
|
390
|
+
name: A(f),
|
|
391
|
+
errors: s,
|
|
392
|
+
fields: i
|
|
393
|
+
}), V(
|
|
394
|
+
i,
|
|
395
|
+
(d) => {
|
|
396
|
+
o != null && o.fields && d.forEach((m) => {
|
|
397
|
+
o == null || o.fields.value.add(m);
|
|
398
|
+
});
|
|
399
|
+
},
|
|
400
|
+
{ deep: !0 }
|
|
401
|
+
), V(
|
|
402
|
+
() => new Map(s.value),
|
|
403
|
+
(d, m) => {
|
|
404
|
+
o != null && o.errors && (Array.from(m.keys()).forEach((v) => {
|
|
405
|
+
o.errors.value.delete(v);
|
|
406
|
+
}), Array.from(d.keys()).forEach((v) => {
|
|
407
|
+
const y = d.get(v);
|
|
408
|
+
y && o.errors.value.set(v, y);
|
|
409
|
+
}));
|
|
410
|
+
},
|
|
411
|
+
{ deep: !0 }
|
|
412
|
+
);
|
|
413
|
+
const u = h(() => n != null && n.errors.value ? s.value.size > 0 : !1);
|
|
414
|
+
return V(u, () => {
|
|
415
|
+
u.value ? a("invalid") : a("valid");
|
|
416
|
+
}), { formProvided: n, invalid: u, fields: i, errors: s };
|
|
417
|
+
},
|
|
418
|
+
render() {
|
|
419
|
+
var t, a, n, o, i, s, f, u;
|
|
420
|
+
return this.tag ? O(
|
|
421
|
+
this.tag,
|
|
422
|
+
null,
|
|
423
|
+
((o = (n = this.$slots).default) == null ? void 0 : o.call(n, {
|
|
424
|
+
invalid: this.invalid,
|
|
425
|
+
formData: (t = this.formProvided) == null ? void 0 : t.modelValue,
|
|
426
|
+
errors: (a = this.formProvided) == null ? void 0 : a.errors,
|
|
427
|
+
fieldsErrors: this.errors
|
|
428
|
+
})) ?? this.$slots.defalut
|
|
429
|
+
) : ((u = (f = this.$slots).default) == null ? void 0 : u.call(f, {
|
|
430
|
+
invalid: this.invalid,
|
|
431
|
+
formData: (i = this.formProvided) == null ? void 0 : i.modelValue,
|
|
432
|
+
errors: (s = this.formProvided) == null ? void 0 : s.errors,
|
|
433
|
+
fieldsErrors: this.errors
|
|
434
|
+
})) ?? this.$slots.defalut;
|
|
435
|
+
}
|
|
436
|
+
}), K = (e, r = {}) => {
|
|
437
|
+
const t = Symbol(), a = Symbol(), n = Symbol(), o = ye(e, t, r), i = pe(
|
|
438
|
+
t,
|
|
439
|
+
a
|
|
440
|
+
), s = te(
|
|
441
|
+
t,
|
|
442
|
+
a,
|
|
443
|
+
n,
|
|
444
|
+
r
|
|
445
|
+
);
|
|
446
|
+
return {
|
|
447
|
+
VvForm: o,
|
|
448
|
+
VvFormWrapper: i,
|
|
449
|
+
VvFormField: s,
|
|
450
|
+
formInjectionKey: t,
|
|
451
|
+
formWrapperInjectionKey: a,
|
|
452
|
+
formFieldInjectionKey: n
|
|
453
|
+
};
|
|
454
|
+
}, z = Symbol(), Se = (e) => {
|
|
455
|
+
let r = {};
|
|
456
|
+
return e.schema && (r = K(e.schema, e)), {
|
|
457
|
+
...r,
|
|
458
|
+
install(t, { global: a = !1 } = {}) {
|
|
459
|
+
t.provide(z, e), a && (t.config.globalProperties.$vvForm = e, r != null && r.VvForm && t.component("VvForm", r.VvForm), r != null && r.VvFormWrapper && t.component("VvFormWrapper", r.VvFormWrapper), r != null && r.VvFormField && t.component("VvFormField", r.VvFormField));
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
}, je = (e, r = {}) => {
|
|
463
|
+
const t = { ...S(z, {}), ...r };
|
|
464
|
+
return K(e, t);
|
|
465
|
+
};
|
|
466
|
+
export {
|
|
467
|
+
l as FormFieldType,
|
|
468
|
+
Se as createForm,
|
|
469
|
+
J as defaultObjectBySchema,
|
|
470
|
+
K as formFactory,
|
|
471
|
+
z as pluginInjectionKey,
|
|
472
|
+
je as useForm
|
|
473
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(b,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],n):(b=typeof globalThis<"u"?globalThis:b||self,n(b["@volverjs/form-vue"]={},b.Vue,b.VueUseCore,b.zod))})(this,function(b,n,D,M){"use strict";function S(e){return Array.isArray(e)}function U(e){return typeof e<"u"}function A(e){return e===null}function E(e){return typeof e=="object"}function C(e){return typeof e=="string"}function O(e){return typeof e>"u"}const W=/^[0-9]+$/,B=["__proto__","prototype","constructor"];function $(e,r,t){const o=U(t)?t:void 0;if(!E(e)||!C(r))return o;const a=I(r);if(a.length!==0){for(const l of a){if(l==="*")continue;const c=function(u){return u.map(d=>O(d)||A(d)?d:S(d)?c(d):d[l])};if(S(e)&&!W.test(l)?e=c(e):e=e[l],O(e)||A(e))break}return O(e)?o:e}}function k(e,r,t){if(!E(e)||!C(r))return;const o=I(r);if(o.length===0)return;const a=o.length;for(let l=0;l<a;l++){const c=o[l];if(l===a-1){e[c]=t;return}if(c==="*"&&S(e)){const u=o.slice(l+1).join(".");for(const d of e)k(d,u,t);return}O(e[c])&&(e[c]={}),e=e[c]}}function I(e){const r=e.split(/[.]|(?:\[(\d|\*)\])/).filter(t=>!!t);return r.some(t=>B.indexOf(t)!==-1)?[]:r}var s=(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))(s||{});const q=(e,r,t,o={})=>n.defineComponent({name:"FieldComponent",props:{type:{type:String,validator:a=>Object.values(s).includes(a),default:s.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(a,{slots:l,emit:c}){const u=n.computed({get(){if(i!=null&&i.modelValue)return $(Object(i.modelValue.value),String(a.name))},set(f){i!=null&&i.modelValue&&(k(Object(i.modelValue.value),String(a.name),f),c("update:modelValue",{newValue:u.value,formData:i==null?void 0:i.modelValue}))}});n.onMounted(()=>{u.value===void 0&&a.defaultValue!==void 0&&(u.value=a.defaultValue)});const d=n.inject(r,void 0);d&&d.fields.value.add(a.name);const i=n.inject(e),{props:m,name:h}=n.toRefs(a),p=n.computed(()=>{if(i!=null&&i.errors.value)return $(i.errors.value,String(a.name))}),v=n.computed(()=>{var f;return(f=p.value)==null?void 0:f._errors}),V=n.computed(()=>p.value!==void 0);n.watch(V,()=>{V.value?(c("invalid",v.value),d&&d.errors.value.set(a.name,{_errors:v.value})):(c("valid",u.value),d&&d.errors.value.delete(a.name))}),n.watch(()=>i==null?void 0:i.modelValue,()=>{c("update:formData",i==null?void 0:i.modelValue)},{deep:!0});const G=f=>{u.value=f},L=n.computed(()=>typeof m.value=="function"?m.value(i==null?void 0:i.modelValue):m.value),le=n.computed(()=>({...L.value,name:L.value.name??a.name,invalid:V.value,valid:a.showValid?Boolean(!V.value&&u.value):void 0,type:(f=>{if([s.text,s.number,s.email,s.password,s.tel,s.url,s.search,s.date,s.time,s.datetimeLocal,s.month,s.week,s.color].includes(f))return f})(a.type),invalidLabel:v.value,modelValue:u.value,errors:a.is?p.value:void 0,"onUpdate:modelValue":G}));return n.provide(t,{name:n.readonly(h),errors:n.readonly(p)}),{component:n.computed(()=>{if(a.type===s.custom)return{render(){var f;return((f=l.default)==null?void 0:f.call(l,{modelValue:u.value,onUpdate:G,invalid:V.value,invalidLabel:v.value,formData:i==null?void 0:i.modelValue.value,formErrors:i==null?void 0:i.errors.value,errors:p.value}))??l.defalut}};if(!o.lazyLoad){let f;switch(a.type){case s.select:f=n.resolveComponent("VvSelect");break;case s.checkbox:f=n.resolveComponent("VvCheckbox");break;case s.radio:f=n.resolveComponent("VvRadio");break;case s.textarea:f=n.resolveComponent("VvTextarea");break;case s.radioGroup:f=n.resolveComponent("VvRadioGroup");break;case s.checkboxGroup:f=n.resolveComponent("VvCheckboxGroup");break;case s.combobox:f=n.resolveComponent("VvCombobox");break;default:f=n.resolveComponent("VvInputText")}if(typeof f!="string")return f;console.warn(`[form-vue warn]: ${f} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return n.defineAsyncComponent(async()=>{switch(o.sideEffects&&await Promise.resolve(o.sideEffects(a.type)),a.type){case s.textarea:return import("@volverjs/ui-vue/vv-textarea");case s.radio:return import("@volverjs/ui-vue/vv-radio");case s.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case s.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case s.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case s.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:le,invalid:V}},render(){return this.is?n.h(this.is,this.hasProps,this.$slots):this.type===s.custom?n.h(this.component,null,this.$slots):n.h(this.component,this.hasProps,this.$slots)}});var z=function(r){return K(r)&&!J(r)};function K(e){return!!e&&typeof e=="object"}function J(e){var r=Object.prototype.toString.call(e);return r==="[object RegExp]"||r==="[object Date]"||Y(e)}var R=typeof Symbol=="function"&&Symbol.for,Z=R?Symbol.for("react.element"):60103;function Y(e){return e.$$typeof===Z}function H(e){return Array.isArray(e)?[]:{}}function g(e,r){return r.clone!==!1&&r.isMergeableObject(e)?y(H(e),e,r):e}function Q(e,r,t){return e.concat(r).map(function(o){return g(o,t)})}function X(e,r){if(!r.customMerge)return y;var t=r.customMerge(e);return typeof t=="function"?t:y}function P(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(function(r){return Object.propertyIsEnumerable.call(e,r)}):[]}function N(e){return Object.keys(e).concat(P(e))}function _(e,r){try{return r in e}catch{return!1}}function F(e,r){return _(e,r)&&!(Object.hasOwnProperty.call(e,r)&&Object.propertyIsEnumerable.call(e,r))}function T(e,r,t){var o={};return t.isMergeableObject(e)&&N(e).forEach(function(a){o[a]=g(e[a],t)}),N(r).forEach(function(a){F(e,a)||(_(e,a)&&t.isMergeableObject(r[a])?o[a]=X(a,t)(e[a],r[a],t):o[a]=g(r[a],t))}),o}function y(e,r,t){t=t||{},t.arrayMerge=t.arrayMerge||Q,t.isMergeableObject=t.isMergeableObject||z,t.cloneUnlessOtherwiseSpecified=g;var o=Array.isArray(r),a=Array.isArray(e),l=o===a;return l?o?t.arrayMerge(e,r,t):T(e,r,t):g(r,t)}y.all=function(r,t){if(!Array.isArray(r))throw new Error("first argument should be an array");return r.reduce(function(o,a){return y(o,a,t)},{})};var ee=y,re=ee;const j=(e,r={})=>re(Object.fromEntries(Object.entries(e.shape).map(([t,o])=>o instanceof M.ZodDefault?[t,o._def.defaultValue()]:o instanceof M.ZodObject?[t,j(o)]:[t,void 0])),r),te=(e,r,t)=>n.defineComponent({name:"FormComponent",props:{modelValue:{type:Object,default:()=>({})}},emits:["invalid","valid","submit","update:modelValue"],expose:["submit","errors","status"],setup(o,{emit:a}){const l=n.ref(j(e,o.modelValue));n.watch(()=>o.modelValue,m=>{if(m){const h=n.isProxy(m)?n.toRaw(m):m;l.value=typeof(h==null?void 0:h.clone)=="function"?h.clone():JSON.parse(JSON.stringify(h))}},{deep:!0}),D.watchThrottled(l,m=>{c.value&&d(),(!m||!o.modelValue||JSON.stringify(m)!==JSON.stringify(o.modelValue))&&a("update:modelValue",m)},{deep:!0,throttle:(t==null?void 0:t.updateThrottle)??500});const c=n.ref(),u=n.ref(),d=(m=l.value)=>{const h=e.safeParse(m);return h.success?(c.value=void 0,u.value="valid",l.value=h.data,a("valid",h.data),!0):(c.value=h.error.format(),u.value="invalid",a("invalid",c.value),!1)},i=()=>d()?(a("submit",l.value),!0):!1;return n.provide(r,{modelValue:l,submit:i,errors:n.readonly(c)}),{submit:i}},render(){return n.h("form",{onSubmit:n.withModifiers(this.submit,["prevent"])},this.$slots)}}),ne=(e,r)=>n.defineComponent({name:"WrapperComponent",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(t,{emit:o}){const a=n.inject(e),l=n.inject(r,void 0),c=n.ref(new Set),u=n.ref(new Map),{name:d}=n.toRefs(t);n.provide(r,{name:n.readonly(d),errors:u,fields:c}),n.watch(c,m=>{l!=null&&l.fields&&m.forEach(h=>{l==null||l.fields.value.add(h)})},{deep:!0}),n.watch(()=>new Map(u.value),(m,h)=>{l!=null&&l.errors&&(Array.from(h.keys()).forEach(p=>{l.errors.value.delete(p)}),Array.from(m.keys()).forEach(p=>{const v=m.get(p);v&&l.errors.value.set(p,v)}))},{deep:!0});const i=n.computed(()=>a!=null&&a.errors.value?u.value.size>0:!1);return n.watch(i,()=>{i.value?o("invalid"):o("valid")}),{formProvided:a,invalid:i,fields:c,errors:u}},render(){var t,o,a,l,c,u,d,i;return this.tag?n.h(this.tag,null,((l=(a=this.$slots).default)==null?void 0:l.call(a,{invalid:this.invalid,formData:(t=this.formProvided)==null?void 0:t.modelValue,errors:(o=this.formProvided)==null?void 0:o.errors,fieldsErrors:this.errors}))??this.$slots.defalut):((i=(d=this.$slots).default)==null?void 0:i.call(d,{invalid:this.invalid,formData:(c=this.formProvided)==null?void 0:c.modelValue,errors:(u=this.formProvided)==null?void 0:u.errors,fieldsErrors:this.errors}))??this.$slots.defalut}}),x=(e,r={})=>{const t=Symbol(),o=Symbol(),a=Symbol(),l=te(e,t,r),c=ne(t,o),u=q(t,o,a,r);return{VvForm:l,VvFormWrapper:c,VvFormField:u,formInjectionKey:t,formWrapperInjectionKey:o,formFieldInjectionKey:a}},w=Symbol(),ae=e=>{let r={};return e.schema&&(r=x(e.schema,e)),{...r,install(t,{global:o=!1}={}){t.provide(w,e),o&&(t.config.globalProperties.$vvForm=e,r!=null&&r.VvForm&&t.component("VvForm",r.VvForm),r!=null&&r.VvFormWrapper&&t.component("VvFormWrapper",r.VvFormWrapper),r!=null&&r.VvFormField&&t.component("VvFormField",r.VvFormField))}}},oe=(e,r={})=>{const t={...n.inject(w,{}),...r};return x(e,t)};b.FormFieldType=s,b.createForm=ae,b.defaultObjectBySchema=j,b.formFactory=x,b.pluginInjectionKey=w,b.useForm=oe,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})});
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Ref } from 'vue'
|
|
2
|
+
import type { ZodFormattedError } from 'zod'
|
|
3
|
+
import type { FormFieldType } from './enums'
|
|
4
|
+
|
|
5
|
+
export type FormComposableOptions = {
|
|
6
|
+
lazyLoad?: boolean
|
|
7
|
+
updateThrottle?: number
|
|
8
|
+
sideEffects?: (type: `${FormFieldType}`) => Promise | void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type FormPluginOptions = {
|
|
12
|
+
schema?: ZodSchema
|
|
13
|
+
} & FormComposableOptions
|
|
14
|
+
|
|
15
|
+
export type InjectedFormData<Type = Recrod<string | number, unknown>> = {
|
|
16
|
+
modelValue: Ref<Type>
|
|
17
|
+
errors: Ref<ZodFormattedError<Type>>
|
|
18
|
+
submit: () => boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type InjectedFormWrapperData = {
|
|
22
|
+
name: Ref<string>
|
|
23
|
+
fields: Ref<Set<string>>
|
|
24
|
+
errors: Ref<Map<string, Record<string, { _errors: string[] }>>>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type InjectedFormFieldData = {
|
|
28
|
+
name: Ref<string>
|
|
29
|
+
errors: Ref<Map<string, Record<string, { _errors: string[] }>>>
|
|
30
|
+
}
|
package/dist/utils.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@volverjs/form-vue",
|
|
3
|
+
"description": "Vue 3 Forms with @volverjs/ui-vue",
|
|
4
|
+
"author": "24/Consulting",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"form",
|
|
8
|
+
"form-field",
|
|
9
|
+
"form-wrapper",
|
|
10
|
+
"vue3",
|
|
11
|
+
"zod",
|
|
12
|
+
"validation"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/volverjs/form-vue",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/volverjs/form-vue"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/volverjs/form-vue/issues"
|
|
21
|
+
},
|
|
22
|
+
"version": "0.0.1",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">= 16.x"
|
|
25
|
+
},
|
|
26
|
+
"packageManager": "pnpm@7.8.0",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"src",
|
|
33
|
+
"node",
|
|
34
|
+
"*.d.ts"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@iconify/tools": "^2.2.6",
|
|
38
|
+
"@iconify/vue": "^4.1.0",
|
|
39
|
+
"@volverjs/ui-vue": "0.0.5-beta.1",
|
|
40
|
+
"@vueuse/core": "^9.13.0",
|
|
41
|
+
"deepmerge": "^4.3.0",
|
|
42
|
+
"ts-dot-prop": "^2.1.2",
|
|
43
|
+
"vue": "^3.2.47",
|
|
44
|
+
"zod": "^3.21.4"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@testing-library/vue": "^6.6.1",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^5.54.1",
|
|
49
|
+
"@typescript-eslint/parser": "^5.54.1",
|
|
50
|
+
"@vitejs/plugin-vue": "^4.0.0",
|
|
51
|
+
"@vue/compiler-sfc": "^3.2.47",
|
|
52
|
+
"@vue/runtime-core": "^3.2.47",
|
|
53
|
+
"@vue/test-utils": "^2.3.1",
|
|
54
|
+
"copy": "^0.3.2",
|
|
55
|
+
"eslint": "^8.35.0",
|
|
56
|
+
"eslint-config-prettier": "^8.7.0",
|
|
57
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
58
|
+
"happy-dom": "^8.9.0",
|
|
59
|
+
"prettier": "^2.8.4",
|
|
60
|
+
"sass": "^1.58.3",
|
|
61
|
+
"typescript": "^4.9.5",
|
|
62
|
+
"vite": "^4.1.4",
|
|
63
|
+
"vite-plugin-eslint": "^1.8.1",
|
|
64
|
+
"vite-plugin-externalize-deps": "^0.5.0",
|
|
65
|
+
"vitest": "^0.29.2",
|
|
66
|
+
"vue-tsc": "^1.2.0"
|
|
67
|
+
},
|
|
68
|
+
"typesVersions": {
|
|
69
|
+
"*": {
|
|
70
|
+
"*": [
|
|
71
|
+
"dist/index.d.ts"
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"exports": {
|
|
76
|
+
".": {
|
|
77
|
+
"types": "./dist/index.d.ts",
|
|
78
|
+
"import": "./dist/index.es.js",
|
|
79
|
+
"default": "./dist/index.umd.cjs"
|
|
80
|
+
},
|
|
81
|
+
"./src/*": "./src/*",
|
|
82
|
+
"./dist/*": "./dist/*"
|
|
83
|
+
},
|
|
84
|
+
"scripts": {
|
|
85
|
+
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
|
|
86
|
+
"type-check": "tsc --noEmit",
|
|
87
|
+
"build": "vite build && vue-tsc --declaration --emitDeclarationOnly && copy src/**/types.d.ts dist",
|
|
88
|
+
"test": "vitest",
|
|
89
|
+
"credits": "npx @opengovsg/credits-generator"
|
|
90
|
+
}
|
|
91
|
+
}
|