@volverjs/form-vue 1.0.0-beta.13 → 1.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -1,10 +1,10 @@
1
- import { defineComponent as R, inject as B, toRefs as H, computed as w, onMounted as P, watch as I, onBeforeUnmount as F, unref as C, provide as M, readonly as E, resolveComponent as S, defineAsyncComponent as ee, h as O, ref as $, toRaw as A, isProxy as re, withModifiers as te, getCurrentInstance as ae } from "vue";
1
+ import { defineComponent as z, inject as $, toRefs as H, computed as w, onMounted as P, watch as I, onBeforeUnmount as F, unref as C, provide as M, readonly as E, resolveComponent as S, defineAsyncComponent as ee, h as O, ref as _, toRaw as A, isProxy as re, withModifiers as te, getCurrentInstance as ae } from "vue";
2
2
  import { watchIgnorable as ne, throttleFilter as ue } from "@vueuse/core";
3
- import { ZodObject as z, ZodDefault as le, ZodNullable as ie, ZodSchema as se, ZodArray as fe, ZodEffects as oe, ZodOptional as de } from "zod";
4
- function Z(e) {
3
+ import { ZodObject as G, ZodDefault as le, ZodNullable as ie, ZodSchema as se, ZodArray as fe, ZodRecord as oe, ZodEffects as de, ZodOptional as ve } from "zod";
4
+ function J(e) {
5
5
  return Array.isArray(e);
6
6
  }
7
- function ve(e) {
7
+ function ce(e) {
8
8
  return typeof e < "u";
9
9
  }
10
10
  function q(e) {
@@ -13,35 +13,35 @@ function q(e) {
13
13
  function Q(e) {
14
14
  return typeof e == "object";
15
15
  }
16
- function X(e) {
16
+ function T(e) {
17
17
  return typeof e == "string";
18
18
  }
19
- function G(e) {
19
+ function L(e) {
20
20
  return typeof e > "u";
21
21
  }
22
- const ce = /^[0-9]+$/, me = ["__proto__", "prototype", "constructor"];
23
- function K(e, i, a) {
24
- const f = ve(a) ? a : void 0;
25
- if (!Q(e) || !X(i))
22
+ const me = /^[0-9]+$/, he = ["__proto__", "prototype", "constructor"];
23
+ function R(e, i, a) {
24
+ const f = ce(a) ? a : void 0;
25
+ if (!Q(e) || !T(i))
26
26
  return f;
27
- const u = T(i);
27
+ const u = Y(i);
28
28
  if (u.length !== 0) {
29
29
  for (const r of u) {
30
30
  if (r === "*")
31
31
  continue;
32
32
  const l = function(n) {
33
- return n.map((t) => G(t) || q(t) ? t : Z(t) ? l(t) : t[r]);
33
+ return n.map((t) => L(t) || q(t) ? t : J(t) ? l(t) : t[r]);
34
34
  };
35
- if (Z(e) && !ce.test(r) ? e = l(e) : e = e[r], G(e) || q(e))
35
+ if (J(e) && !me.test(r) ? e = l(e) : e = e[r], L(e) || q(e))
36
36
  break;
37
37
  }
38
- return G(e) ? f : e;
38
+ return L(e) ? f : e;
39
39
  }
40
40
  }
41
- function Y(e, i, a) {
42
- if (!Q(e) || !X(i))
41
+ function X(e, i, a) {
42
+ if (!Q(e) || !T(i))
43
43
  return;
44
- const f = T(i);
44
+ const f = Y(i);
45
45
  if (f.length === 0)
46
46
  return;
47
47
  const u = f.length;
@@ -51,28 +51,28 @@ function Y(e, i, a) {
51
51
  e[l] = a;
52
52
  return;
53
53
  }
54
- if (l === "*" && Z(e)) {
54
+ if (l === "*" && J(e)) {
55
55
  const n = f.slice(r + 1).join(".");
56
56
  for (const t of e)
57
- Y(t, n, a);
57
+ X(t, n, a);
58
58
  return;
59
59
  }
60
- G(e[l]) && (e[l] = {}), e = e[l];
60
+ L(e[l]) && (e[l] = {}), e = e[l];
61
61
  }
62
62
  }
63
- function T(e) {
63
+ function Y(e) {
64
64
  const i = e.split(/[.]|(?:\[(\d|\*)\])/).filter((a) => !!a);
65
- return i.some((a) => me.indexOf(a) !== -1) ? [] : i;
65
+ return i.some((a) => he.indexOf(a) !== -1) ? [] : i;
66
66
  }
67
- var o = /* @__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 = "datetime-local", 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))(o || {}), p = /* @__PURE__ */ ((e) => (e.invalid = "invalid", e.valid = "valid", e.submitting = "submitting", e.updated = "updated", e.unknown = "unknown", e))(p || {});
68
- function he(e, i, a, f) {
69
- return R({
67
+ var d = /* @__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 = "datetime-local", 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))(d || {}), p = /* @__PURE__ */ ((e) => (e.invalid = "invalid", e.valid = "valid", e.submitting = "submitting", e.updated = "updated", e.unknown = "unknown", e))(p || {});
68
+ function ye(e, i, a, f) {
69
+ return z({
70
70
  name: "VvFormField",
71
71
  props: {
72
72
  type: {
73
73
  type: String,
74
- validator: (u) => Object.values(o).includes(u),
75
- default: o.custom
74
+ validator: (u) => Object.values(d).includes(u),
75
+ default: d.custom
76
76
  },
77
77
  is: {
78
78
  type: [Object, String],
@@ -106,43 +106,43 @@ function he(e, i, a, f) {
106
106
  emits: ["invalid", "valid", "update:formData", "update:modelValue"],
107
107
  expose: ["invalid", "invalidLabel", "errors"],
108
108
  setup(u, { slots: r, emit: l }) {
109
- const n = B(i, void 0);
109
+ const n = $(i, void 0);
110
110
  n && n.fields.value.add(u.name);
111
- const t = B(e), { props: b, name: d } = H(u), c = w({
111
+ const t = $(e), { props: b, name: o } = H(u), v = w({
112
112
  get() {
113
113
  if (t != null && t.formData)
114
- return K(
114
+ return R(
115
115
  Object(t.formData.value),
116
116
  String(u.name)
117
117
  );
118
118
  },
119
119
  set(s) {
120
- t != null && t.formData && (Y(
120
+ t != null && t.formData && (X(
121
121
  Object(t.formData.value),
122
122
  String(u.name),
123
123
  s
124
124
  ), l("update:modelValue", {
125
- newValue: c.value,
125
+ newValue: v.value,
126
126
  formData: t == null ? void 0 : t.formData
127
127
  }));
128
128
  }
129
129
  });
130
130
  P(() => {
131
- c.value === void 0 && u.defaultValue !== void 0 && (c.value = u.defaultValue);
131
+ v.value === void 0 && u.defaultValue !== void 0 && (v.value = u.defaultValue);
132
132
  });
133
- const g = w(() => {
133
+ const V = w(() => {
134
134
  if (t != null && t.errors.value)
135
- return K(t.errors.value, String(u.name));
136
- }), V = w(() => {
135
+ return R(t.errors.value, String(u.name));
136
+ }), g = w(() => {
137
137
  var s;
138
- return (s = g.value) == null ? void 0 : s._errors;
139
- }), v = w(() => g.value !== void 0), h = I(v, () => {
140
- v.value ? (l("invalid", V.value), n && n.errors.value.set(
138
+ return (s = V.value) == null ? void 0 : s._errors;
139
+ }), c = w(() => V.value !== void 0), h = I(c, () => {
140
+ c.value ? (l("invalid", g.value), n && n.errors.value.set(
141
141
  u.name,
142
142
  {
143
- _errors: V.value
143
+ _errors: g.value
144
144
  }
145
- )) : (l("valid", c.value), n && n.errors.value.delete(
145
+ )) : (l("valid", v.value), n && n.errors.value.delete(
146
146
  u.name
147
147
  ));
148
148
  }), m = I(
@@ -156,59 +156,59 @@ function he(e, i, a, f) {
156
156
  h(), m();
157
157
  });
158
158
  const y = (s) => {
159
- c.value = s;
159
+ v.value = s;
160
160
  }, x = w(() => {
161
161
  let s = b.value;
162
162
  return typeof s == "function" && (s = s(t == null ? void 0 : t.formData)), Object.keys(s).reduce(
163
- (_, k) => (_[k] = C(s[k]), _),
163
+ (N, k) => (N[k] = C(s[k]), N),
164
164
  {}
165
165
  );
166
166
  }), W = w(() => t != null && t.readonly.value ? !0 : x.value.readonly ?? u.readonly), U = w(() => ({
167
167
  ...x.value,
168
168
  name: x.value.name ?? u.name,
169
- invalid: v.value,
170
- valid: u.showValid ? !!(!v.value && c.value) : void 0,
169
+ invalid: c.value,
170
+ valid: u.showValid ? !!(!c.value && v.value) : void 0,
171
171
  type: ((s) => {
172
172
  if ([
173
- o.text,
174
- o.number,
175
- o.email,
176
- o.password,
177
- o.tel,
178
- o.url,
179
- o.search,
180
- o.date,
181
- o.time,
182
- o.datetimeLocal,
183
- o.month,
184
- o.week,
185
- o.color
173
+ d.text,
174
+ d.number,
175
+ d.email,
176
+ d.password,
177
+ d.tel,
178
+ d.url,
179
+ d.search,
180
+ d.date,
181
+ d.time,
182
+ d.datetimeLocal,
183
+ d.month,
184
+ d.week,
185
+ d.color
186
186
  ].includes(s))
187
187
  return s;
188
188
  })(u.type),
189
- invalidLabel: V.value,
190
- modelValue: c.value,
189
+ invalidLabel: g.value,
190
+ modelValue: v.value,
191
191
  readonly: W.value,
192
192
  "onUpdate:modelValue": y
193
193
  }));
194
194
  return M(a, {
195
- name: E(d),
196
- errors: E(g)
195
+ name: E(o),
196
+ errors: E(V)
197
197
  }), { component: w(() => {
198
- if (u.type === o.custom)
198
+ if (u.type === d.custom)
199
199
  return {
200
200
  render() {
201
201
  var s;
202
202
  return ((s = r.default) == null ? void 0 : s.call(r, {
203
- modelValue: c.value,
203
+ modelValue: v.value,
204
204
  onUpdate: y,
205
205
  submit: t == null ? void 0 : t.submit,
206
206
  validate: t == null ? void 0 : t.validate,
207
- invalid: v.value,
208
- invalidLabel: V.value,
207
+ invalid: c.value,
208
+ invalidLabel: g.value,
209
209
  formData: t == null ? void 0 : t.formData.value,
210
210
  formErrors: t == null ? void 0 : t.errors.value,
211
- errors: g.value,
211
+ errors: V.value,
212
212
  readonly: W.value
213
213
  })) ?? r.defalut;
214
214
  }
@@ -216,25 +216,25 @@ function he(e, i, a, f) {
216
216
  if (!((f == null ? void 0 : f.lazyLoad) ?? u.lazyLoad)) {
217
217
  let s;
218
218
  switch (u.type) {
219
- case o.select:
219
+ case d.select:
220
220
  s = S("VvSelect");
221
221
  break;
222
- case o.checkbox:
222
+ case d.checkbox:
223
223
  s = S("VvCheckbox");
224
224
  break;
225
- case o.radio:
225
+ case d.radio:
226
226
  s = S("VvRadio");
227
227
  break;
228
- case o.textarea:
228
+ case d.textarea:
229
229
  s = S("VvTextarea");
230
230
  break;
231
- case o.radioGroup:
231
+ case d.radioGroup:
232
232
  s = S("VvRadioGroup");
233
233
  break;
234
- case o.checkboxGroup:
234
+ case d.checkboxGroup:
235
235
  s = S("VvCheckboxGroup");
236
236
  break;
237
- case o.combobox:
237
+ case d.combobox:
238
238
  s = S("VvCombobox");
239
239
  break;
240
240
  default:
@@ -248,39 +248,39 @@ function he(e, i, a, f) {
248
248
  }
249
249
  return ee(async () => {
250
250
  switch (f != null && f.sideEffects && await Promise.resolve(f.sideEffects(u.type)), u.type) {
251
- case o.textarea:
251
+ case d.textarea:
252
252
  return import("@volverjs/ui-vue/vv-textarea");
253
- case o.radio:
253
+ case d.radio:
254
254
  return import("@volverjs/ui-vue/vv-radio");
255
- case o.radioGroup:
255
+ case d.radioGroup:
256
256
  return import("@volverjs/ui-vue/vv-radio-group");
257
- case o.checkbox:
257
+ case d.checkbox:
258
258
  return import("@volverjs/ui-vue/vv-checkbox");
259
- case o.checkboxGroup:
259
+ case d.checkboxGroup:
260
260
  return import("@volverjs/ui-vue/vv-checkbox-group");
261
- case o.select:
261
+ case d.select:
262
262
  return import("@volverjs/ui-vue/vv-select");
263
- case o.combobox:
263
+ case d.combobox:
264
264
  return import("@volverjs/ui-vue/vv-combobox");
265
265
  }
266
266
  return import("@volverjs/ui-vue/vv-input-text");
267
267
  });
268
- }), hasProps: U, invalid: v };
268
+ }), hasProps: U, invalid: c };
269
269
  },
270
270
  render() {
271
- return this.is ? O(this.is, this.hasProps, this.$slots) : this.type === o.custom ? O(this.component, null, this.$slots) : O(this.component, this.hasProps, this.$slots);
271
+ return this.is ? O(this.is, this.hasProps, this.$slots) : this.type === d.custom ? O(this.component, null, this.$slots) : O(this.component, this.hasProps, this.$slots);
272
272
  }
273
273
  });
274
274
  }
275
- function J(e, i = {}) {
275
+ function K(e, i = {}) {
276
276
  const a = (r) => {
277
277
  let l = r;
278
- for (; l instanceof oe; )
278
+ for (; l instanceof de; )
279
279
  l = l.innerType();
280
- return l instanceof de && (l = l._def.innerType), l;
280
+ return l instanceof ve && (l = l._def.innerType), l;
281
281
  }, f = a(e);
282
282
  return {
283
- ...(f instanceof z ? f._def.unknownKeys === "passthrough" : !1) ? i : {},
283
+ ...(f instanceof G ? f._def.unknownKeys === "passthrough" : !1) ? i : {},
284
284
  ...Object.fromEntries(
285
285
  Object.entries(f.shape).map(
286
286
  ([r, l]) => {
@@ -289,26 +289,31 @@ function J(e, i = {}) {
289
289
  if (t instanceof le && (b = t._def.defaultValue(), t = t._def.innerType), n === null && t instanceof ie)
290
290
  return [r, n];
291
291
  if (t instanceof se) {
292
- const d = l.safeParse(n);
293
- if (d.success)
294
- return [r, d.data ?? b];
292
+ const o = l.safeParse(n);
293
+ if (o.success)
294
+ return [r, o.data ?? b];
295
295
  }
296
296
  if (t instanceof fe && Array.isArray(n) && n.length) {
297
- const d = a(t._def.type);
298
- if (d instanceof z)
297
+ const o = a(t._def.type);
298
+ if (o instanceof G)
299
299
  return [
300
300
  r,
301
301
  n.map(
302
- (c) => J(
303
- d,
304
- c && typeof c == "object" ? c : void 0
302
+ (v) => K(
303
+ o,
304
+ v && typeof v == "object" ? v : void 0
305
305
  )
306
- ) ?? b
306
+ )
307
307
  ];
308
308
  }
309
- return t instanceof z ? [
309
+ if (t instanceof oe && n) {
310
+ const o = a(t._def.valueType);
311
+ if (o instanceof G)
312
+ return [r, Object.keys(n).reduce((v, V) => (v[V] = K(o, n[V]), v), {})];
313
+ }
314
+ return t instanceof G ? [
310
315
  r,
311
- J(
316
+ K(
312
317
  t,
313
318
  n && typeof n == "object" ? n : b
314
319
  )
@@ -318,13 +323,13 @@ function J(e, i = {}) {
318
323
  )
319
324
  };
320
325
  }
321
- function ye(e, i, a, f) {
322
- const u = $(), r = $(), l = w(() => r.value === p.invalid), n = $(), t = $(!1), b = async (v = n.value) => {
326
+ function be(e, i, a, f) {
327
+ const u = _(), r = _(), l = w(() => r.value === p.invalid), n = _(), t = _(!1), b = async (c = n.value) => {
323
328
  if (t.value)
324
329
  return !0;
325
- const h = await e.safeParseAsync(v);
330
+ const h = await e.safeParseAsync(c);
326
331
  return h.success ? (u.value = void 0, r.value = p.valid, n.value = h.data, !0) : (u.value = h.error.format(), r.value = p.invalid, !1);
327
- }, d = async () => t.value || !await b() ? !1 : (r.value = p.submitting, !0), { ignoreUpdates: c, stop: g } = ne(
332
+ }, o = async () => t.value || !await b() ? !1 : (r.value = p.submitting, !0), { ignoreUpdates: v, stop: V } = ne(
328
333
  n,
329
334
  () => {
330
335
  r.value = p.updated;
@@ -333,7 +338,7 @@ function ye(e, i, a, f) {
333
338
  deep: !0,
334
339
  eventFilter: ue((a == null ? void 0 : a.updateThrottle) ?? 500)
335
340
  }
336
- ), V = R({
341
+ ), g = z({
337
342
  name: "VvForm",
338
343
  props: {
339
344
  continuousValidation: {
@@ -373,12 +378,12 @@ function ye(e, i, a, f) {
373
378
  "invalid",
374
379
  "readonly"
375
380
  ],
376
- setup(v, { emit: h }) {
377
- return n.value = J(
381
+ setup(c, { emit: h }) {
382
+ return n.value = K(
378
383
  e,
379
- A(v.modelValue)
384
+ A(c.modelValue)
380
385
  ), I(
381
- () => v.modelValue,
386
+ () => c.modelValue,
382
387
  (m) => {
383
388
  if (m) {
384
389
  const y = re(m) ? A(m) : m;
@@ -389,7 +394,7 @@ function ye(e, i, a, f) {
389
394
  },
390
395
  { deep: !0 }
391
396
  ), I(r, async (m) => {
392
- var y, x, W, U, N;
397
+ var y, x, W, U, B;
393
398
  if (m === p.invalid) {
394
399
  const s = A(u.value);
395
400
  h("invalid", s), (y = a == null ? void 0 : a.onInvalid) == null || y.call(
@@ -408,37 +413,37 @@ function ye(e, i, a, f) {
408
413
  h("submit", s), (U = a == null ? void 0 : a.onSubmit) == null || U.call(a, s);
409
414
  }
410
415
  if (m === p.updated) {
411
- if ((u.value || a != null && a.continuousValidation || v.continuousValidation) && await b(), !n.value || !v.modelValue || JSON.stringify(n.value) !== JSON.stringify(v.modelValue)) {
416
+ if ((u.value || a != null && a.continuousValidation || c.continuousValidation) && await b(), !n.value || !c.modelValue || JSON.stringify(n.value) !== JSON.stringify(c.modelValue)) {
412
417
  const s = A(n.value);
413
- h("update:modelValue", s), (N = a == null ? void 0 : a.onUpdate) == null || N.call(a, s);
418
+ h("update:modelValue", s), (B = a == null ? void 0 : a.onUpdate) == null || B.call(a, s);
414
419
  }
415
420
  r.value === p.updated && (r.value = p.unknown);
416
421
  }
417
422
  }), P(() => {
418
- t.value = v.readonly;
423
+ t.value = c.readonly;
419
424
  }), I(
420
- () => v.readonly,
425
+ () => c.readonly,
421
426
  (m) => {
422
427
  t.value = m;
423
428
  }
424
429
  ), I(t, (m) => {
425
- m !== v.readonly && h("update:readonly", t.value);
430
+ m !== c.readonly && h("update:readonly", t.value);
426
431
  }), M(i, {
427
432
  formData: n,
428
- submit: d,
433
+ submit: o,
429
434
  validate: b,
430
- ignoreUpdates: c,
431
- stopUpdatesWatch: g,
435
+ ignoreUpdates: v,
436
+ stopUpdatesWatch: V,
432
437
  errors: E(u),
433
438
  status: E(r),
434
439
  invalid: l,
435
440
  readonly: t
436
441
  }), {
437
442
  formData: n,
438
- submit: d,
443
+ submit: o,
439
444
  validate: b,
440
- ignoreUpdates: c,
441
- stopUpdatesWatch: g,
445
+ ignoreUpdates: v,
446
+ stopUpdatesWatch: V,
442
447
  errors: E(u),
443
448
  status: E(r),
444
449
  invalid: l,
@@ -446,7 +451,7 @@ function ye(e, i, a, f) {
446
451
  };
447
452
  },
448
453
  render() {
449
- const v = () => {
454
+ const c = () => {
450
455
  var h, m;
451
456
  return ((m = (h = this.$slots) == null ? void 0 : h.default) == null ? void 0 : m.call(h, {
452
457
  formData: this.formData,
@@ -472,11 +477,11 @@ function ye(e, i, a, f) {
472
477
  schema: this.template ?? (a == null ? void 0 : a.template)
473
478
  },
474
479
  {
475
- default: v
480
+ default: c
476
481
  }
477
482
  )
478
483
  ] : {
479
- default: v
484
+ default: c
480
485
  }
481
486
  );
482
487
  }
@@ -488,17 +493,17 @@ function ye(e, i, a, f) {
488
493
  readonly: t,
489
494
  formData: n,
490
495
  validate: b,
491
- submit: d,
492
- ignoreUpdates: c,
493
- stopUpdatesWatch: g,
496
+ submit: o,
497
+ ignoreUpdates: v,
498
+ stopUpdatesWatch: V,
494
499
  /**
495
500
  * An hack to add types to the default slot
496
501
  */
497
- VvForm: V
502
+ VvForm: g
498
503
  };
499
504
  }
500
- function be(e, i) {
501
- return R({
505
+ function Ve(e, i) {
506
+ return z({
502
507
  name: "VvFormWrapper",
503
508
  props: {
504
509
  name: {
@@ -513,40 +518,40 @@ function be(e, i) {
513
518
  emits: ["invalid", "valid"],
514
519
  expose: ["fields", "invalid"],
515
520
  setup(f, { emit: u }) {
516
- const r = B(e), l = B(i, void 0), n = $(/* @__PURE__ */ new Set()), t = $(/* @__PURE__ */ new Map()), { name: b } = H(f);
521
+ const r = $(e), l = $(i, void 0), n = _(/* @__PURE__ */ new Set()), t = _(/* @__PURE__ */ new Map()), { name: b } = H(f);
517
522
  M(i, {
518
523
  name: E(b),
519
524
  errors: t,
520
525
  fields: n
521
526
  }), I(
522
527
  n,
523
- (c) => {
524
- l != null && l.fields && c.forEach((g) => {
525
- l == null || l.fields.value.add(g);
528
+ (v) => {
529
+ l != null && l.fields && v.forEach((V) => {
530
+ l == null || l.fields.value.add(V);
526
531
  });
527
532
  },
528
533
  { deep: !0 }
529
534
  ), I(
530
535
  () => new Map(t.value),
531
- (c, g) => {
532
- l != null && l.errors && (Array.from(g.keys()).forEach((V) => {
533
- l.errors.value.delete(V);
534
- }), Array.from(c.keys()).forEach((V) => {
535
- const v = c.get(V);
536
- v && l.errors.value.set(V, v);
536
+ (v, V) => {
537
+ l != null && l.errors && (Array.from(V.keys()).forEach((g) => {
538
+ l.errors.value.delete(g);
539
+ }), Array.from(v.keys()).forEach((g) => {
540
+ const c = v.get(g);
541
+ c && l.errors.value.set(g, c);
537
542
  }));
538
543
  },
539
544
  { deep: !0 }
540
545
  );
541
- const d = w(() => r != null && r.invalid.value ? t.value.size > 0 : !1);
542
- return I(d, () => {
543
- d.value ? u("invalid") : u("valid");
546
+ const o = w(() => r != null && r.invalid.value ? t.value.size > 0 : !1);
547
+ return I(o, () => {
548
+ o.value ? u("invalid") : u("valid");
544
549
  }), {
545
550
  formData: r == null ? void 0 : r.formData,
546
551
  errors: r == null ? void 0 : r.errors,
547
552
  submit: r == null ? void 0 : r.submit,
548
553
  validate: r == null ? void 0 : r.validate,
549
- invalid: d,
554
+ invalid: o,
550
555
  fields: n,
551
556
  fieldsErrors: t
552
557
  };
@@ -576,8 +581,8 @@ function be(e, i) {
576
581
  }
577
582
  });
578
583
  }
579
- function Ve(e, i) {
580
- const a = R({
584
+ function ge(e, i) {
585
+ const a = z({
581
586
  name: "VvFormTemplate",
582
587
  props: {
583
588
  schema: {
@@ -590,7 +595,7 @@ function Ve(e, i) {
590
595
  }
591
596
  },
592
597
  setup(f, { slots: u }) {
593
- const r = B(e);
598
+ const r = $(e);
594
599
  if (r != null && r.formData)
595
600
  return () => {
596
601
  var b;
@@ -599,62 +604,62 @@ function Ve(e, i) {
599
604
  f.scope
600
605
  ) : f.schema;
601
606
  let n;
602
- const t = l.reduce((d, c) => {
603
- const g = typeof c == "function" ? c(r, f.scope) : c, {
604
- vvIs: V,
605
- vvName: v,
607
+ const t = l.reduce((o, v) => {
608
+ const V = typeof v == "function" ? v(r, f.scope) : v, {
609
+ vvIs: g,
610
+ vvName: c,
606
611
  vvSlots: h,
607
612
  vvChildren: m,
608
613
  vvIf: y,
609
614
  vvElseIf: x,
610
615
  vvType: W,
611
616
  vvDefaultValue: U,
612
- vvShowValid: N,
617
+ vvShowValid: B,
613
618
  vvContent: s,
614
- ..._
615
- } = g;
619
+ ...N
620
+ } = V;
616
621
  if (y !== void 0) {
617
- if (typeof y == "string" ? n = !!K(
622
+ if (typeof y == "string" ? n = !!R(
618
623
  Object(r.formData.value),
619
624
  y
620
625
  ) : typeof y == "function" ? n = C(y(r)) : n = C(y), !n)
621
- return d;
626
+ return o;
622
627
  } else if (x !== void 0 && n !== void 0) {
623
- if (n || (typeof x == "string" ? n = !!K(
628
+ if (n || (typeof x == "string" ? n = !!R(
624
629
  Object(r.formData.value),
625
630
  x
626
631
  ) : typeof x == "function" ? n = C(x(r)) : n = C(x), !n))
627
- return d;
632
+ return o;
628
633
  } else
629
634
  n = void 0;
630
635
  let k;
631
- return m && (typeof V == "string" ? k = O(a, {
636
+ return m && (typeof g == "string" ? k = O(a, {
632
637
  schema: m
633
638
  }) : k = {
634
639
  default: (j) => O(a, {
635
640
  schema: m,
636
641
  scope: j
637
642
  })
638
- }), v ? (d.push(
643
+ }), c ? (o.push(
639
644
  O(
640
645
  i,
641
646
  {
642
- name: v,
643
- is: V,
647
+ name: c,
648
+ is: g,
644
649
  type: W,
645
650
  defaultValue: U,
646
- showValid: N,
647
- props: _
651
+ showValid: B,
652
+ props: N
648
653
  },
649
654
  h ?? k ?? s
650
655
  )
651
- ), d) : V ? (d.push(
656
+ ), o) : g ? (o.push(
652
657
  O(
653
- V,
654
- _,
658
+ g,
659
+ N,
655
660
  h ?? k ?? s
656
661
  )
657
- ), d) : (k && ("default" in k ? d.push(k.default(f.scope)) : d.push(k)), d);
662
+ ), o) : (k && ("default" in k ? o.push(k.default(f.scope)) : o.push(k)), o);
658
663
  }, []);
659
664
  return t.push(
660
665
  (b = u == null ? void 0 : u.default) == null ? void 0 : b.call(u, {
@@ -671,27 +676,27 @@ function Ve(e, i) {
671
676
  });
672
677
  return a;
673
678
  }
674
- function L(e, i = {}) {
675
- const a = Symbol("formInjectionKey"), f = Symbol("formWrapperInjectionKey"), u = Symbol("formFieldInjectionKey"), r = be(
679
+ function Z(e, i = {}) {
680
+ const a = Symbol("formInjectionKey"), f = Symbol("formWrapperInjectionKey"), u = Symbol("formFieldInjectionKey"), r = Ve(
676
681
  a,
677
682
  f
678
- ), l = he(
683
+ ), l = ye(
679
684
  a,
680
685
  f,
681
686
  u,
682
687
  i
683
- ), n = Ve(a, l), {
688
+ ), n = ge(a, l), {
684
689
  VvForm: t,
685
690
  errors: b,
686
- status: d,
687
- invalid: c,
688
- readonly: g,
689
- formData: V,
690
- validate: v,
691
+ status: o,
692
+ invalid: v,
693
+ readonly: V,
694
+ formData: g,
695
+ validate: c,
691
696
  submit: h,
692
697
  ignoreUpdates: m,
693
698
  stopUpdatesWatch: y
694
- } = ye(e, a, i, n);
699
+ } = be(e, a, i, n);
695
700
  return {
696
701
  VvForm: t,
697
702
  VvFormWrapper: r,
@@ -701,43 +706,43 @@ function L(e, i = {}) {
701
706
  formWrapperInjectionKey: f,
702
707
  formFieldInjectionKey: u,
703
708
  errors: b,
704
- status: d,
705
- invalid: c,
706
- readonly: g,
707
- formData: V,
708
- validate: v,
709
+ status: o,
710
+ invalid: v,
711
+ readonly: V,
712
+ formData: g,
713
+ validate: c,
709
714
  submit: h,
710
715
  ignoreUpdates: m,
711
716
  stopUpdatesWatch: y
712
717
  };
713
718
  }
714
719
  const D = Symbol("pluginInjectionKey");
715
- function ke(e) {
720
+ function we(e) {
716
721
  let i = {};
717
- return e.schema && (i = L(e.schema, e)), {
722
+ return e.schema && (i = Z(e.schema, e)), {
718
723
  ...i,
719
724
  install(a, { global: f = !1 } = {}) {
720
725
  a.provide(D, e), f && (a.config.globalProperties.$vvForm = e, i != null && i.VvForm && a.component("VvForm", i.VvForm), i != null && i.VvFormWrapper && a.component("VvFormWrapper", i.VvFormWrapper), i != null && i.VvFormField && a.component("VvFormField", i.VvFormField), i != null && i.VvFormTemplate && a.component("VvFormTemplate", i.VvFormTemplate));
721
726
  }
722
727
  };
723
728
  }
724
- function we(e, i = {}) {
725
- return ae() ? L(
729
+ function Oe(e, i = {}) {
730
+ return ae() ? Z(
726
731
  e,
727
732
  {
728
- ...B(D, {}),
733
+ ...$(D, {}),
729
734
  ...i
730
735
  }
731
- ) : L(e, i);
736
+ ) : Z(e, i);
732
737
  }
733
- function Oe(e, i = {}) {
734
- return L(e, i);
738
+ function Ie(e, i = {}) {
739
+ return Z(e, i);
735
740
  }
736
741
  export {
737
- o as FormFieldType,
738
- ke as createForm,
739
- J as defaultObjectBySchema,
740
- Oe as formFactory,
742
+ d as FormFieldType,
743
+ we as createForm,
744
+ K as defaultObjectBySchema,
745
+ Ie as formFactory,
741
746
  D as pluginInjectionKey,
742
- we as useForm
747
+ Oe as useForm
743
748
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(w,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],e):(w=typeof globalThis<"u"?globalThis:w||self,e(w["@volverjs/form-vue"]={},w.Vue,w.VueUseCore,w.zod))})(this,function(w,e,$,S){"use strict";function N(r){return Array.isArray(r)}function J(r){return typeof r<"u"}function K(r){return r===null}function G(r){return typeof r=="object"}function L(r){return typeof r=="string"}function W(r){return typeof r>"u"}const M=/^[0-9]+$/,z=["__proto__","prototype","constructor"];function A(r,s,n){const f=J(n)?n:void 0;if(!G(r)||!L(s))return f;const i=q(s);if(i.length!==0){for(const t of i){if(t==="*")continue;const u=function(l){return l.map(a=>W(a)||K(a)?a:N(a)?u(a):a[t])};if(N(r)&&!M.test(t)?r=u(r):r=r[t],W(r)||K(r))break}return W(r)?f:r}}function Z(r,s,n){if(!G(r)||!L(s))return;const f=q(s);if(f.length===0)return;const i=f.length;for(let t=0;t<i;t++){const u=f[t];if(t===i-1){r[u]=n;return}if(u==="*"&&N(r)){const l=f.slice(t+1).join(".");for(const a of r)Z(a,l,n);return}W(r[u])&&(r[u]={}),r=r[u]}}function q(r){const s=r.split(/[.]|(?:\[(\d|\*)\])/).filter(n=>!!n);return s.some(n=>z.indexOf(n)!==-1)?[]:s}var d=(r=>(r.text="text",r.number="number",r.email="email",r.password="password",r.tel="tel",r.url="url",r.search="search",r.date="date",r.time="time",r.datetimeLocal="datetime-local",r.month="month",r.week="week",r.color="color",r.select="select",r.checkbox="checkbox",r.radio="radio",r.textarea="textarea",r.radioGroup="radioGroup",r.checkboxGroup="checkboxGroup",r.combobox="combobox",r.custom="custom",r))(d||{}),O=(r=>(r.invalid="invalid",r.valid="valid",r.submitting="submitting",r.updated="updated",r.unknown="unknown",r))(O||{});function T(r,s,n,f){return e.defineComponent({name:"VvFormField",props:{type:{type:String,validator:i=>Object.values(d).includes(i),default:d.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},lazyLoad:{type:Boolean,default:!1},readonly:{type:Boolean,default:void 0}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(i,{slots:t,emit:u}){const l=e.inject(s,void 0);l&&l.fields.value.add(i.name);const a=e.inject(r),{props:V,name:c}=e.toRefs(i),v=e.computed({get(){if(a!=null&&a.formData)return A(Object(a.formData.value),String(i.name))},set(o){a!=null&&a.formData&&(Z(Object(a.formData.value),String(i.name),o),u("update:modelValue",{newValue:v.value,formData:a==null?void 0:a.formData}))}});e.onMounted(()=>{v.value===void 0&&i.defaultValue!==void 0&&(v.value=i.defaultValue)});const p=e.computed(()=>{if(a!=null&&a.errors.value)return A(a.errors.value,String(i.name))}),g=e.computed(()=>{var o;return(o=p.value)==null?void 0:o._errors}),m=e.computed(()=>p.value!==void 0),y=e.watch(m,()=>{m.value?(u("invalid",g.value),l&&l.errors.value.set(i.name,{_errors:g.value})):(u("valid",v.value),l&&l.errors.value.delete(i.name))}),h=e.watch(()=>a==null?void 0:a.formData,()=>{u("update:formData",a==null?void 0:a.formData)},{deep:!0});e.onBeforeUnmount(()=>{y(),h()});const b=o=>{v.value=o},x=e.computed(()=>{let o=V.value;return typeof o=="function"&&(o=o(a==null?void 0:a.formData)),Object.keys(o).reduce((U,k)=>(U[k]=e.unref(o[k]),U),{})}),C=e.computed(()=>a!=null&&a.readonly.value?!0:x.value.readonly??i.readonly),I=e.computed(()=>({...x.value,name:x.value.name??i.name,invalid:m.value,valid:i.showValid?!!(!m.value&&v.value):void 0,type:(o=>{if([d.text,d.number,d.email,d.password,d.tel,d.url,d.search,d.date,d.time,d.datetimeLocal,d.month,d.week,d.color].includes(o))return o})(i.type),invalidLabel:g.value,modelValue:v.value,readonly:C.value,"onUpdate:modelValue":b}));return e.provide(n,{name:e.readonly(c),errors:e.readonly(p)}),{component:e.computed(()=>{if(i.type===d.custom)return{render(){var o;return((o=t.default)==null?void 0:o.call(t,{modelValue:v.value,onUpdate:b,submit:a==null?void 0:a.submit,validate:a==null?void 0:a.validate,invalid:m.value,invalidLabel:g.value,formData:a==null?void 0:a.formData.value,formErrors:a==null?void 0:a.errors.value,errors:p.value,readonly:C.value}))??t.defalut}};if(!((f==null?void 0:f.lazyLoad)??i.lazyLoad)){let o;switch(i.type){case d.select:o=e.resolveComponent("VvSelect");break;case d.checkbox:o=e.resolveComponent("VvCheckbox");break;case d.radio:o=e.resolveComponent("VvRadio");break;case d.textarea:o=e.resolveComponent("VvTextarea");break;case d.radioGroup:o=e.resolveComponent("VvRadioGroup");break;case d.checkboxGroup:o=e.resolveComponent("VvCheckboxGroup");break;case d.combobox:o=e.resolveComponent("VvCombobox");break;default:o=e.resolveComponent("VvInputText")}if(typeof o!="string")return o;console.warn(`[form-vue warn]: ${o} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return e.defineAsyncComponent(async()=>{switch(f!=null&&f.sideEffects&&await Promise.resolve(f.sideEffects(i.type)),i.type){case d.textarea:return import("@volverjs/ui-vue/vv-textarea");case d.radio:return import("@volverjs/ui-vue/vv-radio");case d.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case d.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case d.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case d.select:return import("@volverjs/ui-vue/vv-select");case d.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:I,invalid:m}},render(){return this.is?e.h(this.is,this.hasProps,this.$slots):this.type===d.custom?e.h(this.component,null,this.$slots):e.h(this.component,this.hasProps,this.$slots)}})}function E(r,s={}){const n=t=>{let u=t;for(;u instanceof S.ZodEffects;)u=u.innerType();return u instanceof S.ZodOptional&&(u=u._def.innerType),u},f=n(r);return{...(f instanceof S.ZodObject?f._def.unknownKeys==="passthrough":!1)?s:{},...Object.fromEntries(Object.entries(f.shape).map(([t,u])=>{const l=s[t];let a=n(u),V;if(a instanceof S.ZodDefault&&(V=a._def.defaultValue(),a=a._def.innerType),l===null&&a instanceof S.ZodNullable)return[t,l];if(a instanceof S.ZodSchema){const c=u.safeParse(l);if(c.success)return[t,c.data??V]}if(a instanceof S.ZodArray&&Array.isArray(l)&&l.length){const c=n(a._def.type);if(c instanceof S.ZodObject)return[t,l.map(v=>E(c,v&&typeof v=="object"?v:void 0))??V]}return a instanceof S.ZodObject?[t,E(a,l&&typeof l=="object"?l:V)]:[t,V]}))}}function P(r,s,n,f){const i=e.ref(),t=e.ref(),u=e.computed(()=>t.value===O.invalid),l=e.ref(),a=e.ref(!1),V=async(m=l.value)=>{if(a.value)return!0;const y=await r.safeParseAsync(m);return y.success?(i.value=void 0,t.value=O.valid,l.value=y.data,!0):(i.value=y.error.format(),t.value=O.invalid,!1)},c=async()=>a.value||!await V()?!1:(t.value=O.submitting,!0),{ignoreUpdates:v,stop:p}=$.watchIgnorable(l,()=>{t.value=O.updated},{deep:!0,eventFilter:$.throttleFilter((n==null?void 0:n.updateThrottle)??500)}),g=e.defineComponent({name:"VvForm",props:{continuousValidation:{type:Boolean,default:!1},modelValue:{type:Object,default:()=>({})},readonly:{type:Boolean,default:(n==null?void 0:n.readonly)??!1},tag:{type:String,default:"form"},template:{type:[Array,Function],default:void 0}},emits:["invalid","valid","submit","update:modelValue","update:readonly"],expose:["submit","validate","errors","status","valid","invalid","readonly"],setup(m,{emit:y}){return l.value=E(r,e.toRaw(m.modelValue)),e.watch(()=>m.modelValue,h=>{if(h){const b=e.isProxy(h)?e.toRaw(h):h;if(JSON.stringify(b)===JSON.stringify(e.toRaw(l.value)))return;l.value=typeof(b==null?void 0:b.clone)=="function"?b.clone():JSON.parse(JSON.stringify(b))}},{deep:!0}),e.watch(t,async h=>{var b,x,C,I,R;if(h===O.invalid){const o=e.toRaw(i.value);y("invalid",o),(b=n==null?void 0:n.onInvalid)==null||b.call(n,o);return}if(h===O.valid){const o=e.toRaw(l.value);y("valid",o),(x=n==null?void 0:n.onValid)==null||x.call(n,o),y("update:modelValue",o),(C=n==null?void 0:n.onUpdate)==null||C.call(n,o);return}if(h===O.submitting){const o=e.toRaw(l.value);y("submit",o),(I=n==null?void 0:n.onSubmit)==null||I.call(n,o)}if(h===O.updated){if((i.value||n!=null&&n.continuousValidation||m.continuousValidation)&&await V(),!l.value||!m.modelValue||JSON.stringify(l.value)!==JSON.stringify(m.modelValue)){const o=e.toRaw(l.value);y("update:modelValue",o),(R=n==null?void 0:n.onUpdate)==null||R.call(n,o)}t.value===O.updated&&(t.value=O.unknown)}}),e.onMounted(()=>{a.value=m.readonly}),e.watch(()=>m.readonly,h=>{a.value=h}),e.watch(a,h=>{h!==m.readonly&&y("update:readonly",a.value)}),e.provide(s,{formData:l,submit:c,validate:V,ignoreUpdates:v,stopUpdatesWatch:p,errors:e.readonly(i),status:e.readonly(t),invalid:u,readonly:a}),{formData:l,submit:c,validate:V,ignoreUpdates:v,stopUpdatesWatch:p,errors:e.readonly(i),status:e.readonly(t),invalid:u,isReadonly:a}},render(){const m=()=>{var y,h;return((h=(y=this.$slots)==null?void 0:y.default)==null?void 0:h.call(y,{formData:this.formData,submit:this.submit,validate:this.validate,ignoreUpdates:this.ignoreUpdates,stopUpdatesWatch:this.stopUpdatesWatch,errors:this.errors,status:this.status,invalid:this.invalid,readonly:this.isReadonly}))??this.$slots.default};return e.h(this.tag,{onSubmit:e.withModifiers(this.submit,["prevent"])},(this.template??(n==null?void 0:n.template))&&f?[e.h(f,{schema:this.template??(n==null?void 0:n.template)},{default:m})]:{default:m})}});return{errors:i,status:t,invalid:u,readonly:a,formData:l,validate:V,submit:c,ignoreUpdates:v,stopUpdatesWatch:p,VvForm:g}}function H(r,s){return e.defineComponent({name:"VvFormWrapper",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(f,{emit:i}){const t=e.inject(r),u=e.inject(s,void 0),l=e.ref(new Set),a=e.ref(new Map),{name:V}=e.toRefs(f);e.provide(s,{name:e.readonly(V),errors:a,fields:l}),e.watch(l,v=>{u!=null&&u.fields&&v.forEach(p=>{u==null||u.fields.value.add(p)})},{deep:!0}),e.watch(()=>new Map(a.value),(v,p)=>{u!=null&&u.errors&&(Array.from(p.keys()).forEach(g=>{u.errors.value.delete(g)}),Array.from(v.keys()).forEach(g=>{const m=v.get(g);m&&u.errors.value.set(g,m)}))},{deep:!0});const c=e.computed(()=>t!=null&&t.invalid.value?a.value.size>0:!1);return e.watch(c,()=>{c.value?i("invalid"):i("valid")}),{formData:t==null?void 0:t.formData,errors:t==null?void 0:t.errors,submit:t==null?void 0:t.submit,validate:t==null?void 0:t.validate,invalid:c,fields:l,fieldsErrors:a}},render(){var f,i;return this.tag?e.h(this.tag,null,{default:()=>{var t,u;return((u=(t=this.$slots).default)==null?void 0:u.call(t,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}):((i=(f=this.$slots).default)==null?void 0:i.call(f,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}})}function Q(r,s){const n=e.defineComponent({name:"VvFormTemplate",props:{schema:{type:[Array,Function],required:!0},scope:{type:Object,default:()=>({})}},setup(f,{slots:i}){const t=e.inject(r);if(t!=null&&t.formData)return()=>{var V;const u=typeof f.schema=="function"?f.schema(t,f.scope):f.schema;let l;const a=u.reduce((c,v)=>{const p=typeof v=="function"?v(t,f.scope):v,{vvIs:g,vvName:m,vvSlots:y,vvChildren:h,vvIf:b,vvElseIf:x,vvType:C,vvDefaultValue:I,vvShowValid:R,vvContent:o,...U}=p;if(b!==void 0){if(typeof b=="string"?l=!!A(Object(t.formData.value),b):typeof b=="function"?l=e.unref(b(t)):l=e.unref(b),!l)return c}else if(x!==void 0&&l!==void 0){if(l||(typeof x=="string"?l=!!A(Object(t.formData.value),x):typeof x=="function"?l=e.unref(x(t)):l=e.unref(x),!l))return c}else l=void 0;let k;return h&&(typeof g=="string"?k=e.h(n,{schema:h}):k={default:j=>e.h(n,{schema:h,scope:j})}),m?(c.push(e.h(s,{name:m,is:g,type:C,defaultValue:I,showValid:R,props:U},y??k??o)),c):g?(c.push(e.h(g,U,y??k??o)),c):(k&&("default"in k?c.push(k.default(f.scope)):c.push(k)),c)},[]);return a.push((V=i==null?void 0:i.default)==null?void 0:V.call(i,{formData:t==null?void 0:t.formData.value,submit:t==null?void 0:t.submit,validate:t==null?void 0:t.validate,errors:t==null?void 0:t.errors.value,status:t==null?void 0:t.status.value,invalid:t==null?void 0:t.invalid.value})),a}}});return n}function B(r,s={}){const n=Symbol("formInjectionKey"),f=Symbol("formWrapperInjectionKey"),i=Symbol("formFieldInjectionKey"),t=H(n,f),u=T(n,f,i,s),l=Q(n,u),{VvForm:a,errors:V,status:c,invalid:v,readonly:p,formData:g,validate:m,submit:y,ignoreUpdates:h,stopUpdatesWatch:b}=P(r,n,s,l);return{VvForm:a,VvFormWrapper:t,VvFormField:u,VvFormTemplate:l,formInjectionKey:n,formWrapperInjectionKey:f,formFieldInjectionKey:i,errors:V,status:c,invalid:v,readonly:p,formData:g,validate:m,submit:y,ignoreUpdates:h,stopUpdatesWatch:b}}const _=Symbol("pluginInjectionKey");function X(r){let s={};return r.schema&&(s=B(r.schema,r)),{...s,install(n,{global:f=!1}={}){n.provide(_,r),f&&(n.config.globalProperties.$vvForm=r,s!=null&&s.VvForm&&n.component("VvForm",s.VvForm),s!=null&&s.VvFormWrapper&&n.component("VvFormWrapper",s.VvFormWrapper),s!=null&&s.VvFormField&&n.component("VvFormField",s.VvFormField),s!=null&&s.VvFormTemplate&&n.component("VvFormTemplate",s.VvFormTemplate))}}}function Y(r,s={}){return e.getCurrentInstance()?B(r,{...e.inject(_,{}),...s}):B(r,s)}function D(r,s={}){return B(r,s)}w.FormFieldType=d,w.createForm=X,w.defaultObjectBySchema=E,w.formFactory=D,w.pluginInjectionKey=_,w.useForm=Y,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
1
+ (function(w,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],e):(w=typeof globalThis<"u"?globalThis:w||self,e(w["@volverjs/form-vue"]={},w.Vue,w.VueUseCore,w.zod))})(this,function(w,e,$,k){"use strict";function _(r){return Array.isArray(r)}function J(r){return typeof r<"u"}function Z(r){return r===null}function G(r){return typeof r=="object"}function K(r){return typeof r=="string"}function A(r){return typeof r>"u"}const M=/^[0-9]+$/,T=["__proto__","prototype","constructor"];function E(r,s,n){const f=J(n)?n:void 0;if(!G(r)||!K(s))return f;const i=q(s);if(i.length!==0){for(const t of i){if(t==="*")continue;const u=function(l){return l.map(a=>A(a)||Z(a)?a:_(a)?u(a):a[t])};if(_(r)&&!M.test(t)?r=u(r):r=r[t],A(r)||Z(r))break}return A(r)?f:r}}function L(r,s,n){if(!G(r)||!K(s))return;const f=q(s);if(f.length===0)return;const i=f.length;for(let t=0;t<i;t++){const u=f[t];if(t===i-1){r[u]=n;return}if(u==="*"&&_(r)){const l=f.slice(t+1).join(".");for(const a of r)L(a,l,n);return}A(r[u])&&(r[u]={}),r=r[u]}}function q(r){const s=r.split(/[.]|(?:\[(\d|\*)\])/).filter(n=>!!n);return s.some(n=>T.indexOf(n)!==-1)?[]:s}var d=(r=>(r.text="text",r.number="number",r.email="email",r.password="password",r.tel="tel",r.url="url",r.search="search",r.date="date",r.time="time",r.datetimeLocal="datetime-local",r.month="month",r.week="week",r.color="color",r.select="select",r.checkbox="checkbox",r.radio="radio",r.textarea="textarea",r.radioGroup="radioGroup",r.checkboxGroup="checkboxGroup",r.combobox="combobox",r.custom="custom",r))(d||{}),x=(r=>(r.invalid="invalid",r.valid="valid",r.submitting="submitting",r.updated="updated",r.unknown="unknown",r))(x||{});function P(r,s,n,f){return e.defineComponent({name:"VvFormField",props:{type:{type:String,validator:i=>Object.values(d).includes(i),default:d.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},lazyLoad:{type:Boolean,default:!1},readonly:{type:Boolean,default:void 0}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(i,{slots:t,emit:u}){const l=e.inject(s,void 0);l&&l.fields.value.add(i.name);const a=e.inject(r),{props:V,name:c}=e.toRefs(i),m=e.computed({get(){if(a!=null&&a.formData)return E(Object(a.formData.value),String(i.name))},set(o){a!=null&&a.formData&&(L(Object(a.formData.value),String(i.name),o),u("update:modelValue",{newValue:m.value,formData:a==null?void 0:a.formData}))}});e.onMounted(()=>{m.value===void 0&&i.defaultValue!==void 0&&(m.value=i.defaultValue)});const g=e.computed(()=>{if(a!=null&&a.errors.value)return E(a.errors.value,String(i.name))}),p=e.computed(()=>{var o;return(o=g.value)==null?void 0:o._errors}),v=e.computed(()=>g.value!==void 0),y=e.watch(v,()=>{v.value?(u("invalid",p.value),l&&l.errors.value.set(i.name,{_errors:p.value})):(u("valid",m.value),l&&l.errors.value.delete(i.name))}),h=e.watch(()=>a==null?void 0:a.formData,()=>{u("update:formData",a==null?void 0:a.formData)},{deep:!0});e.onBeforeUnmount(()=>{y(),h()});const b=o=>{m.value=o},O=e.computed(()=>{let o=V.value;return typeof o=="function"&&(o=o(a==null?void 0:a.formData)),Object.keys(o).reduce((W,S)=>(W[S]=e.unref(o[S]),W),{})}),C=e.computed(()=>a!=null&&a.readonly.value?!0:O.value.readonly??i.readonly),I=e.computed(()=>({...O.value,name:O.value.name??i.name,invalid:v.value,valid:i.showValid?!!(!v.value&&m.value):void 0,type:(o=>{if([d.text,d.number,d.email,d.password,d.tel,d.url,d.search,d.date,d.time,d.datetimeLocal,d.month,d.week,d.color].includes(o))return o})(i.type),invalidLabel:p.value,modelValue:m.value,readonly:C.value,"onUpdate:modelValue":b}));return e.provide(n,{name:e.readonly(c),errors:e.readonly(g)}),{component:e.computed(()=>{if(i.type===d.custom)return{render(){var o;return((o=t.default)==null?void 0:o.call(t,{modelValue:m.value,onUpdate:b,submit:a==null?void 0:a.submit,validate:a==null?void 0:a.validate,invalid:v.value,invalidLabel:p.value,formData:a==null?void 0:a.formData.value,formErrors:a==null?void 0:a.errors.value,errors:g.value,readonly:C.value}))??t.defalut}};if(!((f==null?void 0:f.lazyLoad)??i.lazyLoad)){let o;switch(i.type){case d.select:o=e.resolveComponent("VvSelect");break;case d.checkbox:o=e.resolveComponent("VvCheckbox");break;case d.radio:o=e.resolveComponent("VvRadio");break;case d.textarea:o=e.resolveComponent("VvTextarea");break;case d.radioGroup:o=e.resolveComponent("VvRadioGroup");break;case d.checkboxGroup:o=e.resolveComponent("VvCheckboxGroup");break;case d.combobox:o=e.resolveComponent("VvCombobox");break;default:o=e.resolveComponent("VvInputText")}if(typeof o!="string")return o;console.warn(`[form-vue warn]: ${o} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return e.defineAsyncComponent(async()=>{switch(f!=null&&f.sideEffects&&await Promise.resolve(f.sideEffects(i.type)),i.type){case d.textarea:return import("@volverjs/ui-vue/vv-textarea");case d.radio:return import("@volverjs/ui-vue/vv-radio");case d.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case d.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case d.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case d.select:return import("@volverjs/ui-vue/vv-select");case d.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:I,invalid:v}},render(){return this.is?e.h(this.is,this.hasProps,this.$slots):this.type===d.custom?e.h(this.component,null,this.$slots):e.h(this.component,this.hasProps,this.$slots)}})}function R(r,s={}){const n=t=>{let u=t;for(;u instanceof k.ZodEffects;)u=u.innerType();return u instanceof k.ZodOptional&&(u=u._def.innerType),u},f=n(r);return{...(f instanceof k.ZodObject?f._def.unknownKeys==="passthrough":!1)?s:{},...Object.fromEntries(Object.entries(f.shape).map(([t,u])=>{const l=s[t];let a=n(u),V;if(a instanceof k.ZodDefault&&(V=a._def.defaultValue(),a=a._def.innerType),l===null&&a instanceof k.ZodNullable)return[t,l];if(a instanceof k.ZodSchema){const c=u.safeParse(l);if(c.success)return[t,c.data??V]}if(a instanceof k.ZodArray&&Array.isArray(l)&&l.length){const c=n(a._def.type);if(c instanceof k.ZodObject)return[t,l.map(m=>R(c,m&&typeof m=="object"?m:void 0))]}if(a instanceof k.ZodRecord&&l){const c=n(a._def.valueType);if(c instanceof k.ZodObject)return[t,Object.keys(l).reduce((m,g)=>(m[g]=R(c,l[g]),m),{})]}return a instanceof k.ZodObject?[t,R(a,l&&typeof l=="object"?l:V)]:[t,V]}))}}function z(r,s,n,f){const i=e.ref(),t=e.ref(),u=e.computed(()=>t.value===x.invalid),l=e.ref(),a=e.ref(!1),V=async(v=l.value)=>{if(a.value)return!0;const y=await r.safeParseAsync(v);return y.success?(i.value=void 0,t.value=x.valid,l.value=y.data,!0):(i.value=y.error.format(),t.value=x.invalid,!1)},c=async()=>a.value||!await V()?!1:(t.value=x.submitting,!0),{ignoreUpdates:m,stop:g}=$.watchIgnorable(l,()=>{t.value=x.updated},{deep:!0,eventFilter:$.throttleFilter((n==null?void 0:n.updateThrottle)??500)}),p=e.defineComponent({name:"VvForm",props:{continuousValidation:{type:Boolean,default:!1},modelValue:{type:Object,default:()=>({})},readonly:{type:Boolean,default:(n==null?void 0:n.readonly)??!1},tag:{type:String,default:"form"},template:{type:[Array,Function],default:void 0}},emits:["invalid","valid","submit","update:modelValue","update:readonly"],expose:["submit","validate","errors","status","valid","invalid","readonly"],setup(v,{emit:y}){return l.value=R(r,e.toRaw(v.modelValue)),e.watch(()=>v.modelValue,h=>{if(h){const b=e.isProxy(h)?e.toRaw(h):h;if(JSON.stringify(b)===JSON.stringify(e.toRaw(l.value)))return;l.value=typeof(b==null?void 0:b.clone)=="function"?b.clone():JSON.parse(JSON.stringify(b))}},{deep:!0}),e.watch(t,async h=>{var b,O,C,I,U;if(h===x.invalid){const o=e.toRaw(i.value);y("invalid",o),(b=n==null?void 0:n.onInvalid)==null||b.call(n,o);return}if(h===x.valid){const o=e.toRaw(l.value);y("valid",o),(O=n==null?void 0:n.onValid)==null||O.call(n,o),y("update:modelValue",o),(C=n==null?void 0:n.onUpdate)==null||C.call(n,o);return}if(h===x.submitting){const o=e.toRaw(l.value);y("submit",o),(I=n==null?void 0:n.onSubmit)==null||I.call(n,o)}if(h===x.updated){if((i.value||n!=null&&n.continuousValidation||v.continuousValidation)&&await V(),!l.value||!v.modelValue||JSON.stringify(l.value)!==JSON.stringify(v.modelValue)){const o=e.toRaw(l.value);y("update:modelValue",o),(U=n==null?void 0:n.onUpdate)==null||U.call(n,o)}t.value===x.updated&&(t.value=x.unknown)}}),e.onMounted(()=>{a.value=v.readonly}),e.watch(()=>v.readonly,h=>{a.value=h}),e.watch(a,h=>{h!==v.readonly&&y("update:readonly",a.value)}),e.provide(s,{formData:l,submit:c,validate:V,ignoreUpdates:m,stopUpdatesWatch:g,errors:e.readonly(i),status:e.readonly(t),invalid:u,readonly:a}),{formData:l,submit:c,validate:V,ignoreUpdates:m,stopUpdatesWatch:g,errors:e.readonly(i),status:e.readonly(t),invalid:u,isReadonly:a}},render(){const v=()=>{var y,h;return((h=(y=this.$slots)==null?void 0:y.default)==null?void 0:h.call(y,{formData:this.formData,submit:this.submit,validate:this.validate,ignoreUpdates:this.ignoreUpdates,stopUpdatesWatch:this.stopUpdatesWatch,errors:this.errors,status:this.status,invalid:this.invalid,readonly:this.isReadonly}))??this.$slots.default};return e.h(this.tag,{onSubmit:e.withModifiers(this.submit,["prevent"])},(this.template??(n==null?void 0:n.template))&&f?[e.h(f,{schema:this.template??(n==null?void 0:n.template)},{default:v})]:{default:v})}});return{errors:i,status:t,invalid:u,readonly:a,formData:l,validate:V,submit:c,ignoreUpdates:m,stopUpdatesWatch:g,VvForm:p}}function H(r,s){return e.defineComponent({name:"VvFormWrapper",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(f,{emit:i}){const t=e.inject(r),u=e.inject(s,void 0),l=e.ref(new Set),a=e.ref(new Map),{name:V}=e.toRefs(f);e.provide(s,{name:e.readonly(V),errors:a,fields:l}),e.watch(l,m=>{u!=null&&u.fields&&m.forEach(g=>{u==null||u.fields.value.add(g)})},{deep:!0}),e.watch(()=>new Map(a.value),(m,g)=>{u!=null&&u.errors&&(Array.from(g.keys()).forEach(p=>{u.errors.value.delete(p)}),Array.from(m.keys()).forEach(p=>{const v=m.get(p);v&&u.errors.value.set(p,v)}))},{deep:!0});const c=e.computed(()=>t!=null&&t.invalid.value?a.value.size>0:!1);return e.watch(c,()=>{c.value?i("invalid"):i("valid")}),{formData:t==null?void 0:t.formData,errors:t==null?void 0:t.errors,submit:t==null?void 0:t.submit,validate:t==null?void 0:t.validate,invalid:c,fields:l,fieldsErrors:a}},render(){var f,i;return this.tag?e.h(this.tag,null,{default:()=>{var t,u;return((u=(t=this.$slots).default)==null?void 0:u.call(t,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}):((i=(f=this.$slots).default)==null?void 0:i.call(f,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}})}function Q(r,s){const n=e.defineComponent({name:"VvFormTemplate",props:{schema:{type:[Array,Function],required:!0},scope:{type:Object,default:()=>({})}},setup(f,{slots:i}){const t=e.inject(r);if(t!=null&&t.formData)return()=>{var V;const u=typeof f.schema=="function"?f.schema(t,f.scope):f.schema;let l;const a=u.reduce((c,m)=>{const g=typeof m=="function"?m(t,f.scope):m,{vvIs:p,vvName:v,vvSlots:y,vvChildren:h,vvIf:b,vvElseIf:O,vvType:C,vvDefaultValue:I,vvShowValid:U,vvContent:o,...W}=g;if(b!==void 0){if(typeof b=="string"?l=!!E(Object(t.formData.value),b):typeof b=="function"?l=e.unref(b(t)):l=e.unref(b),!l)return c}else if(O!==void 0&&l!==void 0){if(l||(typeof O=="string"?l=!!E(Object(t.formData.value),O):typeof O=="function"?l=e.unref(O(t)):l=e.unref(O),!l))return c}else l=void 0;let S;return h&&(typeof p=="string"?S=e.h(n,{schema:h}):S={default:j=>e.h(n,{schema:h,scope:j})}),v?(c.push(e.h(s,{name:v,is:p,type:C,defaultValue:I,showValid:U,props:W},y??S??o)),c):p?(c.push(e.h(p,W,y??S??o)),c):(S&&("default"in S?c.push(S.default(f.scope)):c.push(S)),c)},[]);return a.push((V=i==null?void 0:i.default)==null?void 0:V.call(i,{formData:t==null?void 0:t.formData.value,submit:t==null?void 0:t.submit,validate:t==null?void 0:t.validate,errors:t==null?void 0:t.errors.value,status:t==null?void 0:t.status.value,invalid:t==null?void 0:t.invalid.value})),a}}});return n}function B(r,s={}){const n=Symbol("formInjectionKey"),f=Symbol("formWrapperInjectionKey"),i=Symbol("formFieldInjectionKey"),t=H(n,f),u=P(n,f,i,s),l=Q(n,u),{VvForm:a,errors:V,status:c,invalid:m,readonly:g,formData:p,validate:v,submit:y,ignoreUpdates:h,stopUpdatesWatch:b}=z(r,n,s,l);return{VvForm:a,VvFormWrapper:t,VvFormField:u,VvFormTemplate:l,formInjectionKey:n,formWrapperInjectionKey:f,formFieldInjectionKey:i,errors:V,status:c,invalid:m,readonly:g,formData:p,validate:v,submit:y,ignoreUpdates:h,stopUpdatesWatch:b}}const N=Symbol("pluginInjectionKey");function X(r){let s={};return r.schema&&(s=B(r.schema,r)),{...s,install(n,{global:f=!1}={}){n.provide(N,r),f&&(n.config.globalProperties.$vvForm=r,s!=null&&s.VvForm&&n.component("VvForm",s.VvForm),s!=null&&s.VvFormWrapper&&n.component("VvFormWrapper",s.VvFormWrapper),s!=null&&s.VvFormField&&n.component("VvFormField",s.VvFormField),s!=null&&s.VvFormTemplate&&n.component("VvFormTemplate",s.VvFormTemplate))}}}function Y(r,s={}){return e.getCurrentInstance()?B(r,{...e.inject(N,{}),...s}):B(r,s)}function D(r,s={}){return B(r,s)}w.FormFieldType=d,w.createForm=X,w.defaultObjectBySchema=R,w.formFactory=D,w.pluginInjectionKey=N,w.useForm=Y,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@volverjs/form-vue",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.13",
4
+ "version": "1.0.0-beta.14",
5
5
  "description": "Vue 3 Forms with @volverjs/ui-vue",
6
6
  "author": "8 Wave S.r.l.",
7
7
  "license": "MIT",
@@ -49,28 +49,28 @@
49
49
  "engines": {
50
50
  "node": ">= 16.x"
51
51
  },
52
- "dependencies": {
52
+ "peerDependencies": {
53
53
  "@volverjs/ui-vue": "0.0.10-beta.36",
54
- "@vueuse/core": "^10.10.0",
54
+ "@vueuse/core": "^10.11.0",
55
55
  "ts-dot-prop": "^2.1.4",
56
- "vue": "^3.4.27",
56
+ "vue": "^3.4.29",
57
57
  "zod": "^3.23.8"
58
58
  },
59
59
  "devDependencies": {
60
- "@antfu/eslint-config": "^2.20.0",
60
+ "@antfu/eslint-config": "^2.21.1",
61
61
  "@nabla/vite-plugin-eslint": "^2.0.4",
62
62
  "@playwright/experimental-ct-vue": "1.44.1",
63
63
  "@testing-library/vue": "^8.1.0",
64
64
  "@vitejs/plugin-vue": "^5.0.5",
65
65
  "@volverjs/style": "0.1.12-beta.15",
66
- "@vue/compiler-sfc": "^3.4.27",
67
- "@vue/runtime-core": "^3.4.27",
66
+ "@vue/compiler-sfc": "^3.4.29",
67
+ "@vue/runtime-core": "^3.4.29",
68
68
  "@vue/test-utils": "^2.4.6",
69
69
  "copy": "^0.3.2",
70
- "eslint": "^9.4.0",
70
+ "eslint": "^9.5.0",
71
71
  "happy-dom": "^14.12.0",
72
72
  "typescript": "^5.4.5",
73
- "vite": "^5.2.12",
73
+ "vite": "^5.3.1",
74
74
  "vite-plugin-dts": "^3.9.1",
75
75
  "vite-plugin-externalize-deps": "^0.8.0",
76
76
  "vitest": "^1.6.0"
package/src/utils.ts CHANGED
@@ -8,11 +8,12 @@ import {
8
8
  ZodSchema,
9
9
  ZodNullable,
10
10
  ZodOptional,
11
+ ZodRecord,
11
12
  ZodArray,
12
13
  } from 'zod'
13
14
  import type { FormSchema } from './types'
14
15
 
15
- export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema, original: Partial<z.infer<Schema>> & Record<string, unknown> = {}): Partial<z.infer<Schema>> {
16
+ export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema, original: Partial<z.infer<Schema>> & Record<string, unknown> = {}): Partial<z.infer<Schema>> {
16
17
  const getInnerType = <Type extends ZodTypeAny>(
17
18
  schema:
18
19
  | Type
@@ -76,10 +77,19 @@ export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema,
76
77
  typeof arrayType
77
78
  >,
78
79
  ),
79
- ) ?? defaultValue,
80
+ ),
80
81
  ]
81
82
  }
82
83
  }
84
+ if (innerType instanceof ZodRecord && originalValue) {
85
+ const valueType = getInnerType(innerType._def.valueType)
86
+ if (valueType instanceof ZodObject) {
87
+ return [key, Object.keys(originalValue).reduce((acc: Record<string, unknown>, recordKey: string) => {
88
+ acc[recordKey] = defaultObjectBySchema(valueType, originalValue[recordKey])
89
+ return acc
90
+ }, {})]
91
+ }
92
+ }
83
93
  if (innerType instanceof ZodObject) {
84
94
  return [
85
95
  key,