@volverjs/form-vue 0.0.10-beta.10 → 0.0.10-beta.11

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/VvForm.d.ts CHANGED
@@ -70,6 +70,7 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
70
70
  }, {
71
71
  formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
72
72
  submit: () => boolean;
73
+ validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => boolean;
73
74
  errors: Readonly<Ref<DeepReadonly<z.ZodFormattedError<z.TypeOf<Schema>>> | undefined>>;
74
75
  status: Readonly<Ref<FormStatus | undefined>>;
75
76
  invalid: import("vue").ComputedRef<boolean>;
@@ -118,6 +119,7 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
118
119
  } & import("vue").ShallowUnwrapRef<{
119
120
  formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
120
121
  submit: () => boolean;
122
+ validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => boolean;
121
123
  errors: Readonly<Ref<DeepReadonly<z.ZodFormattedError<z.TypeOf<Schema>>> | undefined>>;
122
124
  status: Readonly<Ref<FormStatus | undefined>>;
123
125
  invalid: import("vue").ComputedRef<boolean>;
@@ -146,6 +148,7 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
146
148
  }, {
147
149
  formData: Ref<Partial<z.TypeOf<Schema> | undefined>>;
148
150
  submit: () => boolean;
151
+ validate: (value?: Partial<z.TypeOf<Schema> | undefined>) => boolean;
149
152
  errors: Readonly<Ref<DeepReadonly<z.ZodFormattedError<z.TypeOf<Schema>>> | undefined>>;
150
153
  status: Readonly<Ref<FormStatus | undefined>>;
151
154
  invalid: import("vue").ComputedRef<boolean>;
@@ -158,6 +161,7 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
158
161
  default: (_: {
159
162
  formData: unknown extends Partial<z.TypeOf<Schema>> | undefined ? undefined : Partial<z.TypeOf<Schema>> | undefined;
160
163
  submit: () => boolean;
164
+ validate: () => boolean;
161
165
  errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>>>>;
162
166
  status: Ref<DeepReadonly<`${FormStatus}` | undefined>>;
163
167
  invalid: Ref<DeepReadonly<boolean>>;
package/dist/index.d.ts CHANGED
@@ -68,6 +68,7 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
68
68
  }, {
69
69
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema> | undefined>>;
70
70
  submit: () => boolean;
71
+ validate: (value?: Partial<import("zod").TypeOf<Schema> | undefined>) => boolean;
71
72
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").ZodFormattedError<import("zod").TypeOf<Schema>>> | undefined>>;
72
73
  status: Readonly<import("vue").Ref<import("./enums").FormStatus | undefined>>;
73
74
  invalid: import("vue").ComputedRef<boolean>;
@@ -116,6 +117,7 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
116
117
  } & import("vue").ShallowUnwrapRef<{
117
118
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema> | undefined>>;
118
119
  submit: () => boolean;
120
+ validate: (value?: Partial<import("zod").TypeOf<Schema> | undefined>) => boolean;
119
121
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").ZodFormattedError<import("zod").TypeOf<Schema>>> | undefined>>;
120
122
  status: Readonly<import("vue").Ref<import("./enums").FormStatus | undefined>>;
121
123
  invalid: import("vue").ComputedRef<boolean>;
@@ -144,6 +146,7 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
144
146
  }, {
145
147
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema> | undefined>>;
146
148
  submit: () => boolean;
149
+ validate: (value?: Partial<import("zod").TypeOf<Schema> | undefined>) => boolean;
147
150
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").ZodFormattedError<import("zod").TypeOf<Schema>>> | undefined>>;
148
151
  status: Readonly<import("vue").Ref<import("./enums").FormStatus | undefined>>;
149
152
  invalid: import("vue").ComputedRef<boolean>;
@@ -156,6 +159,7 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
156
159
  default: (_: {
157
160
  formData: unknown extends Partial<import("zod").TypeOf<Schema>> | undefined ? undefined : Partial<import("zod").TypeOf<Schema>> | undefined;
158
161
  submit: () => boolean;
162
+ validate: () => boolean;
159
163
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").inferFormattedError<Schema, string>>>>;
160
164
  status: import("vue").Ref<import("vue").DeepReadonly<"invalid" | "valid" | undefined>>;
161
165
  invalid: import("vue").Ref<import("vue").DeepReadonly<boolean>>;
@@ -477,6 +481,7 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
477
481
  }, {
478
482
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema> | undefined>>;
479
483
  submit: () => boolean;
484
+ validate: (value?: Partial<import("zod").TypeOf<Schema> | undefined>) => boolean;
480
485
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").ZodFormattedError<import("zod").TypeOf<Schema>>> | undefined>>;
481
486
  status: Readonly<import("vue").Ref<import("./enums").FormStatus | undefined>>;
482
487
  invalid: import("vue").ComputedRef<boolean>;
@@ -525,6 +530,7 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
525
530
  } & import("vue").ShallowUnwrapRef<{
526
531
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema> | undefined>>;
527
532
  submit: () => boolean;
533
+ validate: (value?: Partial<import("zod").TypeOf<Schema> | undefined>) => boolean;
528
534
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").ZodFormattedError<import("zod").TypeOf<Schema>>> | undefined>>;
529
535
  status: Readonly<import("vue").Ref<import("./enums").FormStatus | undefined>>;
530
536
  invalid: import("vue").ComputedRef<boolean>;
@@ -553,6 +559,7 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
553
559
  }, {
554
560
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema> | undefined>>;
555
561
  submit: () => boolean;
562
+ validate: (value?: Partial<import("zod").TypeOf<Schema> | undefined>) => boolean;
556
563
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").ZodFormattedError<import("zod").TypeOf<Schema>>> | undefined>>;
557
564
  status: Readonly<import("vue").Ref<import("./enums").FormStatus | undefined>>;
558
565
  invalid: import("vue").ComputedRef<boolean>;
@@ -565,6 +572,7 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
565
572
  default: (_: {
566
573
  formData: unknown extends Partial<import("zod").TypeOf<Schema>> | undefined ? undefined : Partial<import("zod").TypeOf<Schema>> | undefined;
567
574
  submit: () => boolean;
575
+ validate: () => boolean;
568
576
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").inferFormattedError<Schema, string>>>>;
569
577
  status: import("vue").Ref<import("vue").DeepReadonly<"invalid" | "valid" | undefined>>;
570
578
  invalid: import("vue").Ref<import("vue").DeepReadonly<boolean>>;
package/dist/index.es.js CHANGED
@@ -1,13 +1,13 @@
1
- import { defineComponent as G, computed as y, onMounted as Y, onBeforeUnmount as R, inject as O, toRefs as U, watch as w, provide as z, readonly as k, resolveComponent as g, defineAsyncComponent as T, h as x, ref as E, toRaw as M, isProxy as j, withModifiers as F, unref as C, getCurrentInstance as ee } from "vue";
1
+ import { defineComponent as G, computed as y, onMounted as Y, onBeforeUnmount as R, inject as O, toRefs as U, watch as w, provide as z, readonly as k, resolveComponent as g, defineAsyncComponent as T, h as x, ref as E, toRaw as Z, isProxy as j, withModifiers as F, unref as C, getCurrentInstance as ee } from "vue";
2
2
  import { watchThrottled as re } from "@vueuse/core";
3
- import { ZodObject as Z, ZodDefault as te, ZodNullable as ne, ZodSchema as ae, ZodEffects as oe } from "zod";
3
+ import { ZodObject as J, ZodDefault as te, ZodNullable as ae, ZodSchema as ne, ZodEffects as oe } from "zod";
4
4
  function _(e) {
5
5
  return Array.isArray(e);
6
6
  }
7
7
  function se(e) {
8
8
  return typeof e < "u";
9
9
  }
10
- function J(e) {
10
+ function M(e) {
11
11
  return e === null;
12
12
  }
13
13
  function q(e) {
@@ -19,58 +19,58 @@ function P(e) {
19
19
  function $(e) {
20
20
  return typeof e > "u";
21
21
  }
22
- const ue = /^[0-9]+$/, le = ["__proto__", "prototype", "constructor"];
23
- function N(e, o, i) {
24
- const s = se(i) ? i : void 0;
22
+ const ue = /^[0-9]+$/, ie = ["__proto__", "prototype", "constructor"];
23
+ function N(e, o, l) {
24
+ const s = se(l) ? l : void 0;
25
25
  if (!q(e) || !P(o))
26
26
  return s;
27
- const a = H(o);
28
- if (a.length !== 0) {
29
- for (const r of a) {
27
+ const n = H(o);
28
+ if (n.length !== 0) {
29
+ for (const r of n) {
30
30
  if (r === "*")
31
31
  continue;
32
- const u = function(n) {
33
- return n.map((l) => $(l) || J(l) ? l : _(l) ? u(l) : l[r]);
32
+ const u = function(a) {
33
+ return a.map((i) => $(i) || M(i) ? i : _(i) ? u(i) : i[r]);
34
34
  };
35
- if (_(e) && !ue.test(r) ? e = u(e) : e = e[r], $(e) || J(e))
35
+ if (_(e) && !ue.test(r) ? e = u(e) : e = e[r], $(e) || M(e))
36
36
  break;
37
37
  }
38
38
  return $(e) ? s : e;
39
39
  }
40
40
  }
41
- function D(e, o, i) {
41
+ function D(e, o, l) {
42
42
  if (!q(e) || !P(o))
43
43
  return;
44
44
  const s = H(o);
45
45
  if (s.length === 0)
46
46
  return;
47
- const a = s.length;
48
- for (let r = 0; r < a; r++) {
47
+ const n = s.length;
48
+ for (let r = 0; r < n; r++) {
49
49
  const u = s[r];
50
- if (r === a - 1) {
51
- e[u] = i;
50
+ if (r === n - 1) {
51
+ e[u] = l;
52
52
  return;
53
53
  }
54
54
  if (u === "*" && _(e)) {
55
- const n = s.slice(r + 1).join(".");
56
- for (const l of e)
57
- D(l, n, i);
55
+ const a = s.slice(r + 1).join(".");
56
+ for (const i of e)
57
+ D(i, a, l);
58
58
  return;
59
59
  }
60
60
  $(e[u]) && (e[u] = {}), e = e[u];
61
61
  }
62
62
  }
63
63
  function H(e) {
64
- const o = e.split(/[.]|(?:\[(\d|\*)\])/).filter((i) => !!i);
65
- return o.some((i) => le.indexOf(i) !== -1) ? [] : o;
64
+ const o = e.split(/[.]|(?:\[(\d|\*)\])/).filter((l) => !!l);
65
+ return o.some((l) => ie.indexOf(l) !== -1) ? [] : o;
66
66
  }
67
67
  var f = /* @__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))(f || {}), L = /* @__PURE__ */ ((e) => (e.invalid = "invalid", e.valid = "valid", e))(L || {});
68
- const ie = (e, o, i, s) => G({
68
+ const le = (e, o, l, s) => G({
69
69
  name: "FieldComponent",
70
70
  props: {
71
71
  type: {
72
72
  type: String,
73
- validator: (a) => Object.values(f).includes(a),
73
+ validator: (n) => Object.values(f).includes(n),
74
74
  default: f.custom
75
75
  },
76
76
  is: {
@@ -100,47 +100,47 @@ const ie = (e, o, i, s) => G({
100
100
  },
101
101
  emits: ["invalid", "valid", "update:formData", "update:modelValue"],
102
102
  expose: ["invalid", "invalidLabel", "errors"],
103
- setup(a, { slots: r, emit: u }) {
104
- const n = y({
103
+ setup(n, { slots: r, emit: u }) {
104
+ const a = y({
105
105
  get() {
106
106
  if (t != null && t.formData)
107
107
  return N(
108
108
  Object(t.formData.value),
109
- String(a.name)
109
+ String(n.name)
110
110
  );
111
111
  },
112
- set(c) {
112
+ set(d) {
113
113
  t != null && t.formData && (D(
114
114
  Object(t.formData.value),
115
- String(a.name),
116
- c
115
+ String(n.name),
116
+ d
117
117
  ), u("update:modelValue", {
118
- newValue: n.value,
118
+ newValue: a.value,
119
119
  formData: t == null ? void 0 : t.formData
120
120
  }));
121
121
  }
122
122
  });
123
123
  Y(() => {
124
- n.value === void 0 && a.defaultValue !== void 0 && (n.value = a.defaultValue);
124
+ a.value === void 0 && n.defaultValue !== void 0 && (a.value = n.defaultValue);
125
125
  }), R(() => {
126
126
  S(), V();
127
127
  });
128
- const l = O(o, void 0);
129
- l && l.fields.value.add(a.name);
130
- const t = O(e), { props: m, name: p } = U(a), h = y(() => {
128
+ const i = O(o, void 0);
129
+ i && i.fields.value.add(n.name);
130
+ const t = O(e), { props: m, name: h } = U(n), c = y(() => {
131
131
  if (t != null && t.errors.value)
132
- return N(t.errors.value, String(a.name));
133
- }), d = y(() => {
134
- var c;
135
- return (c = h.value) == null ? void 0 : c._errors;
136
- }), v = y(() => h.value !== void 0), S = w(v, () => {
137
- v.value ? (u("invalid", d.value), l && l.errors.value.set(
138
- a.name,
132
+ return N(t.errors.value, String(n.name));
133
+ }), v = y(() => {
134
+ var d;
135
+ return (d = c.value) == null ? void 0 : d._errors;
136
+ }), b = y(() => c.value !== void 0), S = w(b, () => {
137
+ b.value ? (u("invalid", v.value), i && i.errors.value.set(
138
+ n.name,
139
139
  {
140
- _errors: d.value
140
+ _errors: v.value
141
141
  }
142
- )) : (u("valid", n.value), l && l.errors.value.delete(
143
- a.name
142
+ )) : (u("valid", a.value), i && i.errors.value.delete(
143
+ n.name
144
144
  ));
145
145
  }), V = w(
146
146
  () => t == null ? void 0 : t.formData,
@@ -148,14 +148,14 @@ const ie = (e, o, i, s) => G({
148
148
  u("update:formData", t == null ? void 0 : t.formData);
149
149
  },
150
150
  { deep: !0 }
151
- ), b = (c) => {
152
- n.value = c;
151
+ ), p = (d) => {
152
+ a.value = d;
153
153
  }, I = y(() => typeof m.value == "function" ? m.value(t == null ? void 0 : t.formData) : m.value), W = y(() => ({
154
154
  ...I.value,
155
- name: I.value.name ?? a.name,
156
- invalid: v.value,
157
- valid: a.showValid ? !!(!v.value && n.value) : void 0,
158
- type: ((c) => {
155
+ name: I.value.name ?? n.name,
156
+ invalid: b.value,
157
+ valid: n.showValid ? !!(!b.value && a.value) : void 0,
158
+ type: ((d) => {
159
159
  if ([
160
160
  f.text,
161
161
  f.number,
@@ -170,67 +170,67 @@ const ie = (e, o, i, s) => G({
170
170
  f.month,
171
171
  f.week,
172
172
  f.color
173
- ].includes(c))
174
- return c;
175
- })(a.type),
176
- invalidLabel: d.value,
177
- modelValue: n.value,
178
- "onUpdate:modelValue": b
173
+ ].includes(d))
174
+ return d;
175
+ })(n.type),
176
+ invalidLabel: v.value,
177
+ modelValue: a.value,
178
+ "onUpdate:modelValue": p
179
179
  }));
180
- return z(i, {
181
- name: k(p),
182
- errors: k(h)
180
+ return z(l, {
181
+ name: k(h),
182
+ errors: k(c)
183
183
  }), { component: y(() => {
184
- if (a.type === f.custom)
184
+ if (n.type === f.custom)
185
185
  return {
186
186
  render() {
187
- var c;
188
- return ((c = r.default) == null ? void 0 : c.call(r, {
189
- modelValue: n.value,
190
- onUpdate: b,
191
- invalid: v.value,
192
- invalidLabel: d.value,
187
+ var d;
188
+ return ((d = r.default) == null ? void 0 : d.call(r, {
189
+ modelValue: a.value,
190
+ onUpdate: p,
191
+ invalid: b.value,
192
+ invalidLabel: v.value,
193
193
  formData: t == null ? void 0 : t.formData.value,
194
194
  formErrors: t == null ? void 0 : t.errors.value,
195
- errors: h.value
195
+ errors: c.value
196
196
  })) ?? r.defalut;
197
197
  }
198
198
  };
199
- if (!((s == null ? void 0 : s.lazyLoad) ?? a.lazyLoad)) {
200
- let c;
201
- switch (a.type) {
199
+ if (!((s == null ? void 0 : s.lazyLoad) ?? n.lazyLoad)) {
200
+ let d;
201
+ switch (n.type) {
202
202
  case f.select:
203
- c = g("VvSelect");
203
+ d = g("VvSelect");
204
204
  break;
205
205
  case f.checkbox:
206
- c = g("VvCheckbox");
206
+ d = g("VvCheckbox");
207
207
  break;
208
208
  case f.radio:
209
- c = g("VvRadio");
209
+ d = g("VvRadio");
210
210
  break;
211
211
  case f.textarea:
212
- c = g("VvTextarea");
212
+ d = g("VvTextarea");
213
213
  break;
214
214
  case f.radioGroup:
215
- c = g("VvRadioGroup");
215
+ d = g("VvRadioGroup");
216
216
  break;
217
217
  case f.checkboxGroup:
218
- c = g("VvCheckboxGroup");
218
+ d = g("VvCheckboxGroup");
219
219
  break;
220
220
  case f.combobox:
221
- c = g("VvCombobox");
221
+ d = g("VvCombobox");
222
222
  break;
223
223
  default:
224
- c = g("VvInputText");
224
+ d = g("VvInputText");
225
225
  }
226
- if (typeof c != "string")
227
- return c;
226
+ if (typeof d != "string")
227
+ return d;
228
228
  console.warn(
229
- `[form-vue warn]: ${c} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`
229
+ `[form-vue warn]: ${d} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`
230
230
  );
231
231
  }
232
232
  return T(async () => {
233
- switch (s != null && s.sideEffects && await Promise.resolve(s.sideEffects(a.type)), a.type) {
233
+ switch (s != null && s.sideEffects && await Promise.resolve(s.sideEffects(n.type)), n.type) {
234
234
  case f.textarea:
235
235
  return import("@volverjs/ui-vue/vv-textarea");
236
236
  case f.radio:
@@ -246,45 +246,45 @@ const ie = (e, o, i, s) => G({
246
246
  }
247
247
  return import("@volverjs/ui-vue/vv-input-text");
248
248
  });
249
- }), hasProps: W, invalid: v };
249
+ }), hasProps: W, invalid: b };
250
250
  },
251
251
  render() {
252
252
  return this.is ? x(this.is, this.hasProps, this.$slots) : this.type === f.custom ? x(this.component, null, this.$slots) : x(this.component, this.hasProps, this.$slots);
253
253
  }
254
254
  }), Q = (e, o = {}) => {
255
- const i = (r) => {
255
+ const l = (r) => {
256
256
  let u = r;
257
257
  for (; u instanceof oe; )
258
258
  u = u.innerType();
259
259
  return u;
260
- }, s = i(e);
260
+ }, s = l(e);
261
261
  return {
262
- ...(s instanceof Z ? s._def.unknownKeys === "passthrough" : !1) ? o : {},
262
+ ...(s instanceof J ? s._def.unknownKeys === "passthrough" : !1) ? o : {},
263
263
  ...Object.fromEntries(
264
264
  Object.entries(s.shape).map(
265
265
  ([r, u]) => {
266
- const n = o[r], l = i(u);
266
+ const a = o[r], i = l(u);
267
267
  let t;
268
- if (l instanceof te && (t = l._def.defaultValue()), n === null && l instanceof ne)
269
- return [r, n];
270
- if (l instanceof ae) {
271
- const m = u.safeParse(n);
268
+ if (i instanceof te && (t = i._def.defaultValue()), a === null && i instanceof ae)
269
+ return [r, a];
270
+ if (i instanceof ne) {
271
+ const m = u.safeParse(a);
272
272
  if (m.success)
273
273
  return [r, m.data ?? t];
274
274
  }
275
- return l instanceof Z ? [
275
+ return i instanceof J ? [
276
276
  r,
277
277
  Q(
278
- l,
279
- n && typeof n == "object" ? n : {}
278
+ i,
279
+ a && typeof a == "object" ? a : {}
280
280
  )
281
281
  ] : [r, t];
282
282
  }
283
283
  )
284
284
  )
285
285
  };
286
- }, fe = (e, o, i) => {
287
- const s = E(), a = E(), r = E(), u = G({
286
+ }, fe = (e, o, l) => {
287
+ const s = E(), n = E(), r = E(), u = G({
288
288
  name: "FormComponent",
289
289
  props: {
290
290
  modelValue: {
@@ -301,47 +301,47 @@ const ie = (e, o, i, s) => G({
301
301
  }
302
302
  },
303
303
  emits: ["invalid", "valid", "submit", "update:modelValue"],
304
- expose: ["submit", "errors", "status"],
305
- setup(n, { emit: l }) {
304
+ expose: ["submit", "validate", "errors", "status", "valid", "invalid"],
305
+ setup(a, { emit: i }) {
306
306
  r.value = Q(
307
307
  e,
308
- M(n.modelValue)
309
- );
310
- const t = (i == null ? void 0 : i.continuosValidation) ?? n.continuosValidation;
311
- w(
312
- () => n.modelValue,
313
- (d) => {
314
- if (d) {
315
- const v = j(d) ? M(d) : d;
308
+ Z(a.modelValue)
309
+ ), w(
310
+ () => a.modelValue,
311
+ (c) => {
312
+ if (c) {
313
+ const v = j(c) ? Z(c) : c;
316
314
  r.value = typeof (v == null ? void 0 : v.clone) == "function" ? v.clone() : JSON.parse(JSON.stringify(v));
317
315
  }
318
316
  },
319
317
  { deep: !0 }
320
318
  ), re(
321
319
  r,
322
- (d) => {
323
- (s.value || t) && m(), (!d || !n.modelValue || JSON.stringify(d) !== JSON.stringify(n.modelValue)) && l("update:modelValue", d);
320
+ (c) => {
321
+ ((s.value || (l == null ? void 0 : l.continuosValidation)) ?? a.continuosValidation) && t(), (!c || !a.modelValue || JSON.stringify(c) !== JSON.stringify(a.modelValue)) && i("update:modelValue", c);
324
322
  },
325
323
  {
326
324
  deep: !0,
327
- throttle: (i == null ? void 0 : i.updateThrottle) ?? n.updateThrottle
325
+ throttle: (l == null ? void 0 : l.updateThrottle) ?? a.updateThrottle
328
326
  }
329
327
  );
330
- const m = (d = r.value) => {
331
- const v = e.safeParse(d);
332
- return v.success ? (s.value = void 0, a.value = L.valid, r.value = v.data, l("update:modelValue", r.value), l("valid", v.data), !0) : (s.value = v.error.format(), a.value = L.invalid, l("invalid", s.value), !1);
333
- }, p = () => m() ? (l("submit", r.value), !0) : !1, h = y(() => a.value === L.invalid);
328
+ const t = (c = r.value) => {
329
+ const v = e.safeParse(c);
330
+ return v.success ? (s.value = void 0, n.value = L.valid, r.value = v.data, i("update:modelValue", r.value), i("valid", v.data), !0) : (s.value = v.error.format(), n.value = L.invalid, i("invalid", s.value), !1);
331
+ }, m = () => t() ? (i("submit", r.value), !0) : !1, h = y(() => n.value === L.invalid);
334
332
  return z(o, {
335
333
  formData: r,
336
- submit: p,
334
+ submit: m,
335
+ validate: t,
337
336
  errors: k(s),
338
- status: k(a),
337
+ status: k(n),
339
338
  invalid: h
340
339
  }), {
341
340
  formData: r,
342
- submit: p,
341
+ submit: m,
342
+ validate: t,
343
343
  errors: k(s),
344
- status: k(a),
344
+ status: k(n),
345
345
  invalid: h
346
346
  };
347
347
  },
@@ -353,10 +353,11 @@ const ie = (e, o, i, s) => G({
353
353
  },
354
354
  {
355
355
  default: () => {
356
- var n, l;
357
- return ((l = (n = this.$slots) == null ? void 0 : n.default) == null ? void 0 : l.call(n, {
356
+ var a, i;
357
+ return ((i = (a = this.$slots) == null ? void 0 : a.default) == null ? void 0 : i.call(a, {
358
358
  formData: this.formData,
359
359
  submit: this.submit,
360
+ validate: this.validate,
360
361
  errors: this.errors,
361
362
  status: this.status,
362
363
  invalid: this.invalid
@@ -368,14 +369,14 @@ const ie = (e, o, i, s) => G({
368
369
  });
369
370
  return {
370
371
  errors: s,
371
- status: a,
372
+ status: n,
372
373
  formData: r,
373
374
  /**
374
375
  * An hack to add types to the default slot
375
376
  */
376
377
  VvForm: u
377
378
  };
378
- }, ce = (e, o) => G({
379
+ }, de = (e, o) => G({
379
380
  name: "WrapperComponent",
380
381
  props: {
381
382
  name: {
@@ -389,45 +390,45 @@ const ie = (e, o, i, s) => G({
389
390
  },
390
391
  emits: ["invalid", "valid"],
391
392
  expose: ["fields", "invalid"],
392
- setup(s, { emit: a }) {
393
- const r = O(e), u = O(o, void 0), n = E(/* @__PURE__ */ new Set()), l = E(/* @__PURE__ */ new Map()), { name: t } = U(s);
393
+ setup(s, { emit: n }) {
394
+ const r = O(e), u = O(o, void 0), a = E(/* @__PURE__ */ new Set()), i = E(/* @__PURE__ */ new Map()), { name: t } = U(s);
394
395
  z(o, {
395
396
  name: k(t),
396
- errors: l,
397
- fields: n
397
+ errors: i,
398
+ fields: a
398
399
  }), w(
399
- n,
400
- (p) => {
401
- u != null && u.fields && p.forEach((h) => {
402
- u == null || u.fields.value.add(h);
400
+ a,
401
+ (h) => {
402
+ u != null && u.fields && h.forEach((c) => {
403
+ u == null || u.fields.value.add(c);
403
404
  });
404
405
  },
405
406
  { deep: !0 }
406
407
  ), w(
407
- () => new Map(l.value),
408
- (p, h) => {
409
- u != null && u.errors && (Array.from(h.keys()).forEach((d) => {
410
- u.errors.value.delete(d);
411
- }), Array.from(p.keys()).forEach((d) => {
412
- const v = p.get(d);
413
- v && u.errors.value.set(d, v);
408
+ () => new Map(i.value),
409
+ (h, c) => {
410
+ u != null && u.errors && (Array.from(c.keys()).forEach((v) => {
411
+ u.errors.value.delete(v);
412
+ }), Array.from(h.keys()).forEach((v) => {
413
+ const b = h.get(v);
414
+ b && u.errors.value.set(v, b);
414
415
  }));
415
416
  },
416
417
  { deep: !0 }
417
418
  );
418
- const m = y(() => r != null && r.invalid.value ? l.value.size > 0 : !1);
419
+ const m = y(() => r != null && r.invalid.value ? i.value.size > 0 : !1);
419
420
  return w(m, () => {
420
- m.value ? a("invalid") : a("valid");
421
+ m.value ? n("invalid") : n("valid");
421
422
  }), {
422
423
  formData: r == null ? void 0 : r.formData,
423
424
  errors: r == null ? void 0 : r.errors,
424
425
  invalid: m,
425
- fields: n,
426
- fieldsErrors: l
426
+ fields: a,
427
+ fieldsErrors: i
427
428
  };
428
429
  },
429
430
  render() {
430
- var s, a;
431
+ var s, n;
431
432
  return this.tag ? x(this.tag, null, {
432
433
  default: () => {
433
434
  var r, u;
@@ -438,83 +439,83 @@ const ie = (e, o, i, s) => G({
438
439
  fieldsErrors: this.fieldsErrors
439
440
  })) ?? this.$slots.defalut;
440
441
  }
441
- }) : ((a = (s = this.$slots).default) == null ? void 0 : a.call(s, {
442
+ }) : ((n = (s = this.$slots).default) == null ? void 0 : n.call(s, {
442
443
  invalid: this.invalid,
443
444
  formData: this.formData,
444
445
  errors: this.errors,
445
446
  fieldsErrors: this.fieldsErrors
446
447
  })) ?? this.$slots.defalut;
447
448
  }
448
- }), de = (e, o) => {
449
- const i = G({
449
+ }), ce = (e, o) => {
450
+ const l = G({
450
451
  props: {
451
452
  schema: {
452
453
  type: [Array, Function],
453
454
  required: !0
454
455
  }
455
456
  },
456
- setup(s, { slots: a }) {
457
+ setup(s, { slots: n }) {
457
458
  const r = O(e);
458
459
  if (!(r != null && r.formData))
459
460
  return;
460
461
  const u = typeof s.schema == "function" ? s.schema(r) : s.schema;
461
- let n;
462
+ let a;
462
463
  return () => {
463
- var l;
464
+ var i;
464
465
  return u.reduce(
465
466
  (t, m) => {
466
- const p = typeof m == "function" ? m(r) : m, {
467
- vvIs: h,
468
- vvName: d,
469
- vvSlots: v,
467
+ const h = typeof m == "function" ? m(r) : m, {
468
+ vvIs: c,
469
+ vvName: v,
470
+ vvSlots: b,
470
471
  vvChildren: S,
471
472
  vvIf: V,
472
- vvElseIf: b,
473
+ vvElseIf: p,
473
474
  vvType: I,
474
475
  vvDefaultValue: W,
475
476
  vvShowValid: K,
476
- ...c
477
- } = p;
477
+ ...d
478
+ } = h;
478
479
  if (V !== void 0) {
479
- if (typeof V == "string" ? n = !!N(
480
+ if (typeof V == "string" ? a = !!N(
480
481
  Object(r.formData.value),
481
482
  V
482
- ) : typeof V == "function" ? n = C(V(r)) : n = C(V), !n)
483
+ ) : typeof V == "function" ? a = C(V(r)) : a = C(V), !a)
483
484
  return t;
484
- } else if (b !== void 0 && n !== void 0) {
485
- if (n || (typeof b == "string" ? n = !!N(
485
+ } else if (p !== void 0 && a !== void 0) {
486
+ if (a || (typeof p == "string" ? a = !!N(
486
487
  Object(r.formData.value),
487
- b
488
- ) : typeof b == "function" ? n = C(b(r)) : n = C(b), !n))
488
+ p
489
+ ) : typeof p == "function" ? a = C(p(r)) : a = C(p), !a))
489
490
  return t;
490
491
  } else
491
- n = void 0;
492
- const A = S ? x(i, {
492
+ a = void 0;
493
+ const A = S ? x(l, {
493
494
  schema: S
494
495
  }) : void 0;
495
- return d ? (t.push(
496
+ return v ? (t.push(
496
497
  x(
497
498
  o,
498
499
  {
499
- name: d,
500
- is: h,
500
+ name: v,
501
+ is: c,
501
502
  type: I,
502
503
  defaultValue: W,
503
504
  showValid: K,
504
- props: c
505
+ props: d
505
506
  },
506
- v ?? A
507
+ b ?? A
507
508
  )
508
- ), t) : h ? (t.push(
509
+ ), t) : c ? (t.push(
509
510
  x(
510
- h,
511
511
  c,
512
- v ?? A
512
+ d,
513
+ b ?? A
513
514
  )
514
515
  ), t) : (S && t.push(A), t);
515
516
  },
516
517
  [
517
- (l = a == null ? void 0 : a.default) == null ? void 0 : l.call(a, {
518
+ (i = n == null ? void 0 : n.default) == null ? void 0 : i.call(n, {
518
519
  formData: r == null ? void 0 : r.formData.value,
519
520
  submit: r == null ? void 0 : r.submit,
520
521
  errors: r == null ? void 0 : r.errors.value,
@@ -526,50 +527,50 @@ const ie = (e, o, i, s) => G({
526
527
  };
527
528
  }
528
529
  });
529
- return i;
530
+ return l;
530
531
  }, B = (e, o = {}) => {
531
- const i = Symbol(), s = Symbol(), a = Symbol(), { VvForm: r, errors: u, status: n, formData: l } = fe(
532
+ const l = Symbol(), s = Symbol(), n = Symbol(), { VvForm: r, errors: u, status: a, formData: i } = fe(
532
533
  e,
533
- i,
534
+ l,
534
535
  o
535
- ), t = ce(
536
- i,
536
+ ), t = de(
537
+ l,
537
538
  s
538
- ), m = ie(
539
- i,
539
+ ), m = le(
540
+ l,
540
541
  s,
541
- a,
542
+ n,
542
543
  o
543
- ), p = de(i, m);
544
+ ), h = ce(l, m);
544
545
  return {
545
546
  VvForm: r,
546
547
  VvFormWrapper: t,
547
548
  VvFormField: m,
548
- VvFormTemplate: p,
549
- formInjectionKey: i,
549
+ VvFormTemplate: h,
550
+ formInjectionKey: l,
550
551
  formWrapperInjectionKey: s,
551
- formFieldInjectionKey: a,
552
+ formFieldInjectionKey: n,
552
553
  errors: u,
553
- status: n,
554
- formData: l
554
+ status: a,
555
+ formData: i
555
556
  };
556
- }, X = Symbol(), pe = (e) => {
557
+ }, X = Symbol(), be = (e) => {
557
558
  let o = {};
558
559
  return e.schema && (o = B(e.schema, e)), {
559
560
  ...o,
560
- install(i, { global: s = !1 } = {}) {
561
- i.provide(X, e), s && (i.config.globalProperties.$vvForm = e, o != null && o.VvForm && i.component("VvForm", o.VvForm), o != null && o.VvFormWrapper && i.component("VvFormWrapper", o.VvFormWrapper), o != null && o.VvFormField && i.component("VvFormField", o.VvFormField), o != null && o.VvFormTemplate && i.component("VvFormTemplate", o.VvFormTemplate));
561
+ install(l, { global: s = !1 } = {}) {
562
+ l.provide(X, e), s && (l.config.globalProperties.$vvForm = e, o != null && o.VvForm && l.component("VvForm", o.VvForm), o != null && o.VvFormWrapper && l.component("VvFormWrapper", o.VvFormWrapper), o != null && o.VvFormField && l.component("VvFormField", o.VvFormField), o != null && o.VvFormTemplate && l.component("VvFormTemplate", o.VvFormTemplate));
562
563
  }
563
564
  };
564
- }, be = (e, o = {}) => ee() ? B(e, {
565
+ }, pe = (e, o = {}) => ee() ? B(e, {
565
566
  ...O(X, {}),
566
567
  ...o
567
568
  }) : B(e, o), ye = (e, o = {}) => B(e, o);
568
569
  export {
569
570
  f as FormFieldType,
570
- pe as createForm,
571
+ be as createForm,
571
572
  Q as defaultObjectBySchema,
572
573
  ye as formFactory,
573
574
  X as pluginInjectionKey,
574
- be as useForm
575
+ pe as useForm
575
576
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(y,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],r):(y=typeof globalThis<"u"?globalThis:y||self,r(y["@volverjs/form-vue"]={},y.Vue,y.VueUseCore,y.zod))})(this,function(y,r,M,x){"use strict";function I(e){return Array.isArray(e)}function Z(e){return typeof e<"u"}function G(e){return e===null}function W(e){return typeof e=="object"}function A(e){return typeof e=="string"}function k(e){return typeof e>"u"}const z=/^[0-9]+$/,U=["__proto__","prototype","constructor"];function C(e,s,f){const i=Z(f)?f:void 0;if(!W(e)||!A(s))return i;const a=K(s);if(a.length!==0){for(const t of a){if(t==="*")continue;const l=function(o){return o.map(u=>k(u)||G(u)?u:I(u)?l(u):u[t])};if(I(e)&&!z.test(t)?e=l(e):e=e[t],k(e)||G(e))break}return k(e)?i:e}}function _(e,s,f){if(!W(e)||!A(s))return;const i=K(s);if(i.length===0)return;const a=i.length;for(let t=0;t<a;t++){const l=i[t];if(t===a-1){e[l]=f;return}if(l==="*"&&I(e)){const o=i.slice(t+1).join(".");for(const u of e)_(u,o,f);return}k(e[l])&&(e[l]={}),e=e[l]}}function K(e){const s=e.split(/[.]|(?:\[(\d|\*)\])/).filter(f=>!!f);return s.some(f=>U.indexOf(f)!==-1)?[]:s}var c=(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))(c||{}),O=(e=>(e.invalid="invalid",e.valid="valid",e))(O||{});const J=(e,s,f,i)=>r.defineComponent({name:"FieldComponent",props:{type:{type:String,validator:a=>Object.values(c).includes(a),default:c.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}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(a,{slots:t,emit:l}){const o=r.computed({get(){if(n!=null&&n.formData)return C(Object(n.formData.value),String(a.name))},set(d){n!=null&&n.formData&&(_(Object(n.formData.value),String(a.name),d),l("update:modelValue",{newValue:o.value,formData:n==null?void 0:n.formData}))}});r.onMounted(()=>{o.value===void 0&&a.defaultValue!==void 0&&(o.value=a.defaultValue)}),r.onBeforeUnmount(()=>{w(),g()});const u=r.inject(s,void 0);u&&u.fields.value.add(a.name);const n=r.inject(e),{props:h,name:b}=r.toRefs(a),p=r.computed(()=>{if(n!=null&&n.errors.value)return C(n.errors.value,String(a.name))}),m=r.computed(()=>{var d;return(d=p.value)==null?void 0:d._errors}),v=r.computed(()=>p.value!==void 0),w=r.watch(v,()=>{v.value?(l("invalid",m.value),u&&u.errors.value.set(a.name,{_errors:m.value})):(l("valid",o.value),u&&u.errors.value.delete(a.name))}),g=r.watch(()=>n==null?void 0:n.formData,()=>{l("update:formData",n==null?void 0:n.formData)},{deep:!0}),V=d=>{o.value=d},E=r.computed(()=>typeof h.value=="function"?h.value(n==null?void 0:n.formData):h.value),L=r.computed(()=>({...E.value,name:E.value.name??a.name,invalid:v.value,valid:a.showValid?!!(!v.value&&o.value):void 0,type:(d=>{if([c.text,c.number,c.email,c.password,c.tel,c.url,c.search,c.date,c.time,c.datetimeLocal,c.month,c.week,c.color].includes(d))return d})(a.type),invalidLabel:m.value,modelValue:o.value,"onUpdate:modelValue":V}));return r.provide(f,{name:r.readonly(b),errors:r.readonly(p)}),{component:r.computed(()=>{if(a.type===c.custom)return{render(){var d;return((d=t.default)==null?void 0:d.call(t,{modelValue:o.value,onUpdate:V,invalid:v.value,invalidLabel:m.value,formData:n==null?void 0:n.formData.value,formErrors:n==null?void 0:n.errors.value,errors:p.value}))??t.defalut}};if(!((i==null?void 0:i.lazyLoad)??a.lazyLoad)){let d;switch(a.type){case c.select:d=r.resolveComponent("VvSelect");break;case c.checkbox:d=r.resolveComponent("VvCheckbox");break;case c.radio:d=r.resolveComponent("VvRadio");break;case c.textarea:d=r.resolveComponent("VvTextarea");break;case c.radioGroup:d=r.resolveComponent("VvRadioGroup");break;case c.checkboxGroup:d=r.resolveComponent("VvCheckboxGroup");break;case c.combobox:d=r.resolveComponent("VvCombobox");break;default:d=r.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 r.defineAsyncComponent(async()=>{switch(i!=null&&i.sideEffects&&await Promise.resolve(i.sideEffects(a.type)),a.type){case c.textarea:return import("@volverjs/ui-vue/vv-textarea");case c.radio:return import("@volverjs/ui-vue/vv-radio");case c.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case c.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case c.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case c.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:L,invalid:v}},render(){return this.is?r.h(this.is,this.hasProps,this.$slots):this.type===c.custom?r.h(this.component,null,this.$slots):r.h(this.component,this.hasProps,this.$slots)}}),$=(e,s={})=>{const f=t=>{let l=t;for(;l instanceof x.ZodEffects;)l=l.innerType();return l},i=f(e);return{...(i instanceof x.ZodObject?i._def.unknownKeys==="passthrough":!1)?s:{},...Object.fromEntries(Object.entries(i.shape).map(([t,l])=>{const o=s[t],u=f(l);let n;if(u instanceof x.ZodDefault&&(n=u._def.defaultValue()),o===null&&u instanceof x.ZodNullable)return[t,o];if(u instanceof x.ZodSchema){const h=l.safeParse(o);if(h.success)return[t,h.data??n]}return u instanceof x.ZodObject?[t,$(u,o&&typeof o=="object"?o:{})]:[t,n]}))}},P=(e,s,f)=>{const i=r.ref(),a=r.ref(),t=r.ref(),l=r.defineComponent({name:"FormComponent",props:{modelValue:{type:Object,default:()=>({})},updateThrottle:{type:Number,default:500},continuosValidation:{type:Boolean,default:!1}},emits:["invalid","valid","submit","update:modelValue"],expose:["submit","errors","status"],setup(o,{emit:u}){t.value=$(e,r.toRaw(o.modelValue));const n=(f==null?void 0:f.continuosValidation)??o.continuosValidation;r.watch(()=>o.modelValue,m=>{if(m){const v=r.isProxy(m)?r.toRaw(m):m;t.value=typeof(v==null?void 0:v.clone)=="function"?v.clone():JSON.parse(JSON.stringify(v))}},{deep:!0}),M.watchThrottled(t,m=>{(i.value||n)&&h(),(!m||!o.modelValue||JSON.stringify(m)!==JSON.stringify(o.modelValue))&&u("update:modelValue",m)},{deep:!0,throttle:(f==null?void 0:f.updateThrottle)??o.updateThrottle});const h=(m=t.value)=>{const v=e.safeParse(m);return v.success?(i.value=void 0,a.value=O.valid,t.value=v.data,u("update:modelValue",t.value),u("valid",v.data),!0):(i.value=v.error.format(),a.value=O.invalid,u("invalid",i.value),!1)},b=()=>h()?(u("submit",t.value),!0):!1,p=r.computed(()=>a.value===O.invalid);return r.provide(s,{formData:t,submit:b,errors:r.readonly(i),status:r.readonly(a),invalid:p}),{formData:t,submit:b,errors:r.readonly(i),status:r.readonly(a),invalid:p}},render(){return r.h("form",{onSubmit:r.withModifiers(this.submit,["prevent"])},{default:()=>{var o,u;return((u=(o=this.$slots)==null?void 0:o.default)==null?void 0:u.call(o,{formData:this.formData,submit:this.submit,errors:this.errors,status:this.status,invalid:this.invalid}))??this.$slots.default}})}});return{errors:i,status:a,formData:t,VvForm:l}},T=(e,s)=>r.defineComponent({name:"WrapperComponent",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(i,{emit:a}){const t=r.inject(e),l=r.inject(s,void 0),o=r.ref(new Set),u=r.ref(new Map),{name:n}=r.toRefs(i);r.provide(s,{name:r.readonly(n),errors:u,fields:o}),r.watch(o,b=>{l!=null&&l.fields&&b.forEach(p=>{l==null||l.fields.value.add(p)})},{deep:!0}),r.watch(()=>new Map(u.value),(b,p)=>{l!=null&&l.errors&&(Array.from(p.keys()).forEach(m=>{l.errors.value.delete(m)}),Array.from(b.keys()).forEach(m=>{const v=b.get(m);v&&l.errors.value.set(m,v)}))},{deep:!0});const h=r.computed(()=>t!=null&&t.invalid.value?u.value.size>0:!1);return r.watch(h,()=>{h.value?a("invalid"):a("valid")}),{formData:t==null?void 0:t.formData,errors:t==null?void 0:t.errors,invalid:h,fields:o,fieldsErrors:u}},render(){var i,a;return this.tag?r.h(this.tag,null,{default:()=>{var t,l;return((l=(t=this.$slots).default)==null?void 0:l.call(t,{invalid:this.invalid,formData:this.formData,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}):((a=(i=this.$slots).default)==null?void 0:a.call(i,{invalid:this.invalid,formData:this.formData,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}),R=(e,s)=>{const f=r.defineComponent({props:{schema:{type:[Array,Function],required:!0}},setup(i,{slots:a}){const t=r.inject(e);if(!(t!=null&&t.formData))return;const l=typeof i.schema=="function"?i.schema(t):i.schema;let o;return()=>{var u;return l.reduce((n,h)=>{const b=typeof h=="function"?h(t):h,{vvIs:p,vvName:m,vvSlots:v,vvChildren:w,vvIf:g,vvElseIf:V,vvType:E,vvDefaultValue:L,vvShowValid:q,...d}=b;if(g!==void 0){if(typeof g=="string"?o=!!C(Object(t.formData.value),g):typeof g=="function"?o=r.unref(g(t)):o=r.unref(g),!o)return n}else if(V!==void 0&&o!==void 0){if(o||(typeof V=="string"?o=!!C(Object(t.formData.value),V):typeof V=="function"?o=r.unref(V(t)):o=r.unref(V),!o))return n}else o=void 0;const N=w?r.h(f,{schema:w}):void 0;return m?(n.push(r.h(s,{name:m,is:p,type:E,defaultValue:L,showValid:q,props:d},v??N)),n):p?(n.push(r.h(p,d,v??N)),n):(w&&n.push(N),n)},[(u=a==null?void 0:a.default)==null?void 0:u.call(a,{formData:t==null?void 0:t.formData.value,submit:t==null?void 0:t.submit,errors:t==null?void 0:t.errors.value,status:t==null?void 0:t.status.value,invalid:t==null?void 0:t.invalid.value})])}}});return f},S=(e,s={})=>{const f=Symbol(),i=Symbol(),a=Symbol(),{VvForm:t,errors:l,status:o,formData:u}=P(e,f,s),n=T(f,i),h=J(f,i,a,s),b=R(f,h);return{VvForm:t,VvFormWrapper:n,VvFormField:h,VvFormTemplate:b,formInjectionKey:f,formWrapperInjectionKey:i,formFieldInjectionKey:a,errors:l,status:o,formData:u}},B=Symbol(),D=e=>{let s={};return e.schema&&(s=S(e.schema,e)),{...s,install(f,{global:i=!1}={}){f.provide(B,e),i&&(f.config.globalProperties.$vvForm=e,s!=null&&s.VvForm&&f.component("VvForm",s.VvForm),s!=null&&s.VvFormWrapper&&f.component("VvFormWrapper",s.VvFormWrapper),s!=null&&s.VvFormField&&f.component("VvFormField",s.VvFormField),s!=null&&s.VvFormTemplate&&f.component("VvFormTemplate",s.VvFormTemplate))}}},H=(e,s={})=>r.getCurrentInstance()?S(e,{...r.inject(B,{}),...s}):S(e,s),Q=(e,s={})=>S(e,s);y.FormFieldType=c,y.createForm=D,y.defaultObjectBySchema=$,y.formFactory=Q,y.pluginInjectionKey=B,y.useForm=H,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"})});
1
+ (function(p,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],r):(p=typeof globalThis<"u"?globalThis:p||self,r(p["@volverjs/form-vue"]={},p.Vue,p.VueUseCore,p.zod))})(this,function(p,r,Z,x){"use strict";function I(e){return Array.isArray(e)}function z(e){return typeof e<"u"}function G(e){return e===null}function W(e){return typeof e=="object"}function A(e){return typeof e=="string"}function k(e){return typeof e>"u"}const M=/^[0-9]+$/,U=["__proto__","prototype","constructor"];function C(e,s,f){const i=z(f)?f:void 0;if(!W(e)||!A(s))return i;const a=K(s);if(a.length!==0){for(const t of a){if(t==="*")continue;const l=function(o){return o.map(u=>k(u)||G(u)?u:I(u)?l(u):u[t])};if(I(e)&&!M.test(t)?e=l(e):e=e[t],k(e)||G(e))break}return k(e)?i:e}}function _(e,s,f){if(!W(e)||!A(s))return;const i=K(s);if(i.length===0)return;const a=i.length;for(let t=0;t<a;t++){const l=i[t];if(t===a-1){e[l]=f;return}if(l==="*"&&I(e)){const o=i.slice(t+1).join(".");for(const u of e)_(u,o,f);return}k(e[l])&&(e[l]={}),e=e[l]}}function K(e){const s=e.split(/[.]|(?:\[(\d|\*)\])/).filter(f=>!!f);return s.some(f=>U.indexOf(f)!==-1)?[]:s}var d=(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))(d||{}),O=(e=>(e.invalid="invalid",e.valid="valid",e))(O||{});const J=(e,s,f,i)=>r.defineComponent({name:"FieldComponent",props:{type:{type:String,validator:a=>Object.values(d).includes(a),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}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(a,{slots:t,emit:l}){const o=r.computed({get(){if(n!=null&&n.formData)return C(Object(n.formData.value),String(a.name))},set(c){n!=null&&n.formData&&(_(Object(n.formData.value),String(a.name),c),l("update:modelValue",{newValue:o.value,formData:n==null?void 0:n.formData}))}});r.onMounted(()=>{o.value===void 0&&a.defaultValue!==void 0&&(o.value=a.defaultValue)}),r.onBeforeUnmount(()=>{w(),g()});const u=r.inject(s,void 0);u&&u.fields.value.add(a.name);const n=r.inject(e),{props:h,name:y}=r.toRefs(a),m=r.computed(()=>{if(n!=null&&n.errors.value)return C(n.errors.value,String(a.name))}),v=r.computed(()=>{var c;return(c=m.value)==null?void 0:c._errors}),b=r.computed(()=>m.value!==void 0),w=r.watch(b,()=>{b.value?(l("invalid",v.value),u&&u.errors.value.set(a.name,{_errors:v.value})):(l("valid",o.value),u&&u.errors.value.delete(a.name))}),g=r.watch(()=>n==null?void 0:n.formData,()=>{l("update:formData",n==null?void 0:n.formData)},{deep:!0}),V=c=>{o.value=c},E=r.computed(()=>typeof h.value=="function"?h.value(n==null?void 0:n.formData):h.value),L=r.computed(()=>({...E.value,name:E.value.name??a.name,invalid:b.value,valid:a.showValid?!!(!b.value&&o.value):void 0,type:(c=>{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(c))return c})(a.type),invalidLabel:v.value,modelValue:o.value,"onUpdate:modelValue":V}));return r.provide(f,{name:r.readonly(y),errors:r.readonly(m)}),{component:r.computed(()=>{if(a.type===d.custom)return{render(){var c;return((c=t.default)==null?void 0:c.call(t,{modelValue:o.value,onUpdate:V,invalid:b.value,invalidLabel:v.value,formData:n==null?void 0:n.formData.value,formErrors:n==null?void 0:n.errors.value,errors:m.value}))??t.defalut}};if(!((i==null?void 0:i.lazyLoad)??a.lazyLoad)){let c;switch(a.type){case d.select:c=r.resolveComponent("VvSelect");break;case d.checkbox:c=r.resolveComponent("VvCheckbox");break;case d.radio:c=r.resolveComponent("VvRadio");break;case d.textarea:c=r.resolveComponent("VvTextarea");break;case d.radioGroup:c=r.resolveComponent("VvRadioGroup");break;case d.checkboxGroup:c=r.resolveComponent("VvCheckboxGroup");break;case d.combobox:c=r.resolveComponent("VvCombobox");break;default:c=r.resolveComponent("VvInputText")}if(typeof c!="string")return c;console.warn(`[form-vue warn]: ${c} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return r.defineAsyncComponent(async()=>{switch(i!=null&&i.sideEffects&&await Promise.resolve(i.sideEffects(a.type)),a.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.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:L,invalid:b}},render(){return this.is?r.h(this.is,this.hasProps,this.$slots):this.type===d.custom?r.h(this.component,null,this.$slots):r.h(this.component,this.hasProps,this.$slots)}}),$=(e,s={})=>{const f=t=>{let l=t;for(;l instanceof x.ZodEffects;)l=l.innerType();return l},i=f(e);return{...(i instanceof x.ZodObject?i._def.unknownKeys==="passthrough":!1)?s:{},...Object.fromEntries(Object.entries(i.shape).map(([t,l])=>{const o=s[t],u=f(l);let n;if(u instanceof x.ZodDefault&&(n=u._def.defaultValue()),o===null&&u instanceof x.ZodNullable)return[t,o];if(u instanceof x.ZodSchema){const h=l.safeParse(o);if(h.success)return[t,h.data??n]}return u instanceof x.ZodObject?[t,$(u,o&&typeof o=="object"?o:{})]:[t,n]}))}},P=(e,s,f)=>{const i=r.ref(),a=r.ref(),t=r.ref(),l=r.defineComponent({name:"FormComponent",props:{modelValue:{type:Object,default:()=>({})},updateThrottle:{type:Number,default:500},continuosValidation:{type:Boolean,default:!1}},emits:["invalid","valid","submit","update:modelValue"],expose:["submit","validate","errors","status","valid","invalid"],setup(o,{emit:u}){t.value=$(e,r.toRaw(o.modelValue)),r.watch(()=>o.modelValue,m=>{if(m){const v=r.isProxy(m)?r.toRaw(m):m;t.value=typeof(v==null?void 0:v.clone)=="function"?v.clone():JSON.parse(JSON.stringify(v))}},{deep:!0}),Z.watchThrottled(t,m=>{((i.value||(f==null?void 0:f.continuosValidation))??o.continuosValidation)&&n(),(!m||!o.modelValue||JSON.stringify(m)!==JSON.stringify(o.modelValue))&&u("update:modelValue",m)},{deep:!0,throttle:(f==null?void 0:f.updateThrottle)??o.updateThrottle});const n=(m=t.value)=>{const v=e.safeParse(m);return v.success?(i.value=void 0,a.value=O.valid,t.value=v.data,u("update:modelValue",t.value),u("valid",v.data),!0):(i.value=v.error.format(),a.value=O.invalid,u("invalid",i.value),!1)},h=()=>n()?(u("submit",t.value),!0):!1,y=r.computed(()=>a.value===O.invalid);return r.provide(s,{formData:t,submit:h,validate:n,errors:r.readonly(i),status:r.readonly(a),invalid:y}),{formData:t,submit:h,validate:n,errors:r.readonly(i),status:r.readonly(a),invalid:y}},render(){return r.h("form",{onSubmit:r.withModifiers(this.submit,["prevent"])},{default:()=>{var o,u;return((u=(o=this.$slots)==null?void 0:o.default)==null?void 0:u.call(o,{formData:this.formData,submit:this.submit,validate:this.validate,errors:this.errors,status:this.status,invalid:this.invalid}))??this.$slots.default}})}});return{errors:i,status:a,formData:t,VvForm:l}},T=(e,s)=>r.defineComponent({name:"WrapperComponent",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(i,{emit:a}){const t=r.inject(e),l=r.inject(s,void 0),o=r.ref(new Set),u=r.ref(new Map),{name:n}=r.toRefs(i);r.provide(s,{name:r.readonly(n),errors:u,fields:o}),r.watch(o,y=>{l!=null&&l.fields&&y.forEach(m=>{l==null||l.fields.value.add(m)})},{deep:!0}),r.watch(()=>new Map(u.value),(y,m)=>{l!=null&&l.errors&&(Array.from(m.keys()).forEach(v=>{l.errors.value.delete(v)}),Array.from(y.keys()).forEach(v=>{const b=y.get(v);b&&l.errors.value.set(v,b)}))},{deep:!0});const h=r.computed(()=>t!=null&&t.invalid.value?u.value.size>0:!1);return r.watch(h,()=>{h.value?a("invalid"):a("valid")}),{formData:t==null?void 0:t.formData,errors:t==null?void 0:t.errors,invalid:h,fields:o,fieldsErrors:u}},render(){var i,a;return this.tag?r.h(this.tag,null,{default:()=>{var t,l;return((l=(t=this.$slots).default)==null?void 0:l.call(t,{invalid:this.invalid,formData:this.formData,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}):((a=(i=this.$slots).default)==null?void 0:a.call(i,{invalid:this.invalid,formData:this.formData,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}),R=(e,s)=>{const f=r.defineComponent({props:{schema:{type:[Array,Function],required:!0}},setup(i,{slots:a}){const t=r.inject(e);if(!(t!=null&&t.formData))return;const l=typeof i.schema=="function"?i.schema(t):i.schema;let o;return()=>{var u;return l.reduce((n,h)=>{const y=typeof h=="function"?h(t):h,{vvIs:m,vvName:v,vvSlots:b,vvChildren:w,vvIf:g,vvElseIf:V,vvType:E,vvDefaultValue:L,vvShowValid:q,...c}=y;if(g!==void 0){if(typeof g=="string"?o=!!C(Object(t.formData.value),g):typeof g=="function"?o=r.unref(g(t)):o=r.unref(g),!o)return n}else if(V!==void 0&&o!==void 0){if(o||(typeof V=="string"?o=!!C(Object(t.formData.value),V):typeof V=="function"?o=r.unref(V(t)):o=r.unref(V),!o))return n}else o=void 0;const N=w?r.h(f,{schema:w}):void 0;return v?(n.push(r.h(s,{name:v,is:m,type:E,defaultValue:L,showValid:q,props:c},b??N)),n):m?(n.push(r.h(m,c,b??N)),n):(w&&n.push(N),n)},[(u=a==null?void 0:a.default)==null?void 0:u.call(a,{formData:t==null?void 0:t.formData.value,submit:t==null?void 0:t.submit,errors:t==null?void 0:t.errors.value,status:t==null?void 0:t.status.value,invalid:t==null?void 0:t.invalid.value})])}}});return f},S=(e,s={})=>{const f=Symbol(),i=Symbol(),a=Symbol(),{VvForm:t,errors:l,status:o,formData:u}=P(e,f,s),n=T(f,i),h=J(f,i,a,s),y=R(f,h);return{VvForm:t,VvFormWrapper:n,VvFormField:h,VvFormTemplate:y,formInjectionKey:f,formWrapperInjectionKey:i,formFieldInjectionKey:a,errors:l,status:o,formData:u}},B=Symbol(),D=e=>{let s={};return e.schema&&(s=S(e.schema,e)),{...s,install(f,{global:i=!1}={}){f.provide(B,e),i&&(f.config.globalProperties.$vvForm=e,s!=null&&s.VvForm&&f.component("VvForm",s.VvForm),s!=null&&s.VvFormWrapper&&f.component("VvFormWrapper",s.VvFormWrapper),s!=null&&s.VvFormField&&f.component("VvFormField",s.VvFormField),s!=null&&s.VvFormTemplate&&f.component("VvFormTemplate",s.VvFormTemplate))}}},H=(e,s={})=>r.getCurrentInstance()?S(e,{...r.inject(B,{}),...s}):S(e,s),Q=(e,s={})=>S(e,s);p.FormFieldType=d,p.createForm=D,p.defaultObjectBySchema=$,p.formFactory=Q,p.pluginInjectionKey=B,p.useForm=H,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
package/dist/types.d.ts CHANGED
@@ -28,6 +28,7 @@ export type InjectedFormData<Schema extends FormSchema> = {
28
28
  formData: Ref<Partial<z.infer<Schema>> | undefined>
29
29
  errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
30
30
  submit: () => boolean
31
+ validate: () => boolean
31
32
  status: Readonly<Ref<FormStatus | undefined>>
32
33
  invalid: Readonly<Ref<boolean>>
33
34
  }
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.10",
22
+ "version": "0.0.10-beta.11",
23
23
  "engines": {
24
24
  "node": ">= 16.x"
25
25
  },
package/src/VvForm.ts CHANGED
@@ -48,16 +48,14 @@ export const defineForm = <Schema extends FormSchema>(
48
48
  },
49
49
  },
50
50
  emits: ['invalid', 'valid', 'submit', 'update:modelValue'],
51
- expose: ['submit', 'errors', 'status'],
51
+ expose: ['submit', 'validate', 'errors', 'status', 'valid', 'invalid'],
52
52
  setup(props, { emit }) {
53
53
  formData.value = defaultObjectBySchema(
54
54
  schema,
55
55
  toRaw(props.modelValue),
56
56
  )
57
57
 
58
- const keepValidation =
59
- options?.continuosValidation ?? props.continuosValidation
60
-
58
+ // clone modelValue and update formData
61
59
  watch(
62
60
  () => props.modelValue,
63
61
  (newValue) => {
@@ -73,12 +71,16 @@ export const defineForm = <Schema extends FormSchema>(
73
71
  },
74
72
  { deep: true },
75
73
  )
76
- // v-model
74
+
75
+ // emit update:modelValue on formData change
77
76
  watchThrottled(
78
77
  formData,
79
78
  (newValue) => {
80
- if (errors.value || keepValidation) {
81
- parseModelValue()
79
+ if (
80
+ (errors.value || options?.continuosValidation) ??
81
+ props.continuosValidation
82
+ ) {
83
+ validate()
82
84
  }
83
85
  if (
84
86
  !newValue ||
@@ -95,7 +97,8 @@ export const defineForm = <Schema extends FormSchema>(
95
97
  },
96
98
  )
97
99
 
98
- const parseModelValue = (value = formData.value) => {
100
+ // validate formData with safeParse
101
+ const validate = (value = formData.value) => {
99
102
  const parseResult = schema.safeParse(value)
100
103
  if (!parseResult.success) {
101
104
  errors.value =
@@ -114,9 +117,9 @@ export const defineForm = <Schema extends FormSchema>(
114
117
  return true
115
118
  }
116
119
 
117
- // submit
120
+ // emit submit event if form is valid
118
121
  const submit = () => {
119
- if (!parseModelValue()) {
122
+ if (!validate()) {
120
123
  return false
121
124
  }
122
125
  emit('submit', formData.value)
@@ -125,10 +128,11 @@ export const defineForm = <Schema extends FormSchema>(
125
128
 
126
129
  const invalid = computed(() => status.value === FormStatus.invalid)
127
130
 
128
- // provide
131
+ // provide data to children
129
132
  provide(provideKey, {
130
133
  formData,
131
134
  submit,
135
+ validate,
132
136
  errors: readonly(errors),
133
137
  status: readonly(status),
134
138
  invalid,
@@ -137,6 +141,7 @@ export const defineForm = <Schema extends FormSchema>(
137
141
  return {
138
142
  formData,
139
143
  submit,
144
+ validate,
140
145
  errors: readonly(errors),
141
146
  status: readonly(status),
142
147
  invalid,
@@ -153,6 +158,7 @@ export const defineForm = <Schema extends FormSchema>(
153
158
  this.$slots?.default?.({
154
159
  formData: this.formData,
155
160
  submit: this.submit,
161
+ validate: this.validate,
156
162
  errors: this.errors,
157
163
  status: this.status,
158
164
  invalid: this.invalid,
@@ -178,6 +184,7 @@ export const defineForm = <Schema extends FormSchema>(
178
184
  ? undefined
179
185
  : Partial<TypeOf<Schema>> | undefined
180
186
  submit: () => boolean
187
+ validate: () => boolean
181
188
  errors: Readonly<
182
189
  Ref<DeepReadonly<z.inferFormattedError<Schema>>>
183
190
  >
package/src/types.d.ts CHANGED
@@ -28,6 +28,7 @@ export type InjectedFormData<Schema extends FormSchema> = {
28
28
  formData: Ref<Partial<z.infer<Schema>> | undefined>
29
29
  errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
30
30
  submit: () => boolean
31
+ validate: () => boolean
31
32
  status: Readonly<Ref<FormStatus | undefined>>
32
33
  invalid: Readonly<Ref<boolean>>
33
34
  }