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

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/README.md CHANGED
@@ -171,7 +171,7 @@ The **default settings** are **inherited** from the plugin (if it's installed).
171
171
  `useForm()` can create a form also outside a Vue 3 Component, plugin settings are **not inherited**.
172
172
 
173
173
  ```ts
174
- import { formFactory } from '@volverjs/form-vue'
174
+ import { useForm } from '@volverjs/form-vue'
175
175
  import { z } from 'zod'
176
176
 
177
177
  const schema = z.object({
@@ -187,7 +187,7 @@ const {
187
187
  formData,
188
188
  status,
189
189
  errors
190
- } = formFactory(schema, {
190
+ } = useForm(schema, {
191
191
  lazyLoad: true
192
192
  })
193
193
 
@@ -12,6 +12,7 @@ export declare const defineFormTemplate: <Schema extends FormSchema>(formProvide
12
12
  default: (_: {
13
13
  formData: unknown extends Partial<TypeOf<Schema>> | undefined ? undefined : Partial<TypeOf<Schema>> | undefined;
14
14
  submit: () => boolean;
15
+ validate: () => boolean;
15
16
  errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>>>>;
16
17
  status: Ref<DeepReadonly<`${FormStatus}` | undefined>>;
17
18
  invalid: Ref<DeepReadonly<boolean>>;
@@ -48,6 +48,8 @@ export declare const defineFormWrapper: <Schema extends FormSchema>(formProvideK
48
48
  }, {
49
49
  formData: Ref<Partial<TypeOf<Schema>> | undefined> | undefined;
50
50
  errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema_1, string>>>> | undefined;
51
+ submit: (() => boolean) | undefined;
52
+ validate: (() => boolean) | undefined;
51
53
  invalid: import("vue").ComputedRef<boolean>;
52
54
  fields: Ref<Set<string>>;
53
55
  fieldsErrors: Ref<Map<string, Record<string, {
@@ -90,6 +92,8 @@ export declare const defineFormWrapper: <Schema extends FormSchema>(formProvideK
90
92
  } & import("vue").ShallowUnwrapRef<{
91
93
  formData: Ref<Partial<TypeOf<Schema>> | undefined> | undefined;
92
94
  errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema_1, string>>>> | undefined;
95
+ submit: (() => boolean) | undefined;
96
+ validate: (() => boolean) | undefined;
93
97
  invalid: import("vue").ComputedRef<boolean>;
94
98
  fields: Ref<Set<string>>;
95
99
  fieldsErrors: Ref<Map<string, Record<string, {
@@ -114,6 +118,8 @@ export declare const defineFormWrapper: <Schema extends FormSchema>(formProvideK
114
118
  }, {
115
119
  formData: Ref<Partial<TypeOf<Schema>> | undefined> | undefined;
116
120
  errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema_1, string>>>> | undefined;
121
+ submit: (() => boolean) | undefined;
122
+ validate: (() => boolean) | undefined;
117
123
  invalid: import("vue").ComputedRef<boolean>;
118
124
  fields: Ref<Set<string>>;
119
125
  fieldsErrors: Ref<Map<string, Record<string, {
@@ -126,6 +132,8 @@ export declare const defineFormWrapper: <Schema extends FormSchema>(formProvideK
126
132
  default: (_: {
127
133
  invalid: boolean;
128
134
  formData: unknown extends Partial<TypeOf<Schema>> | undefined ? undefined : Partial<TypeOf<Schema>> | undefined;
135
+ submit: () => boolean;
136
+ validate: () => boolean;
129
137
  errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema, string>>>>;
130
138
  fieldsErrors: Map<string, Record<string, {
131
139
  _errors: string[];
package/dist/index.d.ts CHANGED
@@ -213,6 +213,8 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
213
213
  }, {
214
214
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
215
215
  errors: Readonly<import("vue").Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
216
+ submit: (() => boolean) | undefined;
217
+ validate: (() => boolean) | undefined;
216
218
  invalid: import("vue").ComputedRef<boolean>;
217
219
  fields: import("vue").Ref<Set<string>>;
218
220
  fieldsErrors: import("vue").Ref<Map<string, Record<string, {
@@ -255,6 +257,8 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
255
257
  } & import("vue").ShallowUnwrapRef<{
256
258
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
257
259
  errors: Readonly<import("vue").Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
260
+ submit: (() => boolean) | undefined;
261
+ validate: (() => boolean) | undefined;
258
262
  invalid: import("vue").ComputedRef<boolean>;
259
263
  fields: import("vue").Ref<Set<string>>;
260
264
  fieldsErrors: import("vue").Ref<Map<string, Record<string, {
@@ -279,6 +283,8 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
279
283
  }, {
280
284
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
281
285
  errors: Readonly<import("vue").Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
286
+ submit: (() => boolean) | undefined;
287
+ validate: (() => boolean) | undefined;
282
288
  invalid: import("vue").ComputedRef<boolean>;
283
289
  fields: import("vue").Ref<Set<string>>;
284
290
  fieldsErrors: import("vue").Ref<Map<string, Record<string, {
@@ -291,6 +297,8 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
291
297
  default: (_: {
292
298
  invalid: boolean;
293
299
  formData: unknown extends Partial<import("zod").TypeOf<Schema>> | undefined ? undefined : Partial<import("zod").TypeOf<Schema>> | undefined;
300
+ submit: () => boolean;
301
+ validate: () => boolean;
294
302
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").inferFormattedError<Schema, string>>>>;
295
303
  fieldsErrors: Map<string, Record<string, {
296
304
  _errors: string[];
@@ -396,6 +404,7 @@ export declare const useForm: <Schema extends FormSchema>(schema: Schema, option
396
404
  default: (_: {
397
405
  formData: unknown extends Partial<import("zod").TypeOf<Schema>> | undefined ? undefined : Partial<import("zod").TypeOf<Schema>> | undefined;
398
406
  submit: () => boolean;
407
+ validate: () => boolean;
399
408
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").inferFormattedError<Schema, string>>>>;
400
409
  status: import("vue").Ref<import("vue").DeepReadonly<"invalid" | "valid" | undefined>>;
401
410
  invalid: import("vue").Ref<import("vue").DeepReadonly<boolean>>;
@@ -626,6 +635,8 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
626
635
  }, {
627
636
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
628
637
  errors: Readonly<import("vue").Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
638
+ submit: (() => boolean) | undefined;
639
+ validate: (() => boolean) | undefined;
629
640
  invalid: import("vue").ComputedRef<boolean>;
630
641
  fields: import("vue").Ref<Set<string>>;
631
642
  fieldsErrors: import("vue").Ref<Map<string, Record<string, {
@@ -668,6 +679,8 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
668
679
  } & import("vue").ShallowUnwrapRef<{
669
680
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
670
681
  errors: Readonly<import("vue").Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
682
+ submit: (() => boolean) | undefined;
683
+ validate: (() => boolean) | undefined;
671
684
  invalid: import("vue").ComputedRef<boolean>;
672
685
  fields: import("vue").Ref<Set<string>>;
673
686
  fieldsErrors: import("vue").Ref<Map<string, Record<string, {
@@ -692,6 +705,8 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
692
705
  }, {
693
706
  formData: import("vue").Ref<Partial<import("zod").TypeOf<Schema>> | undefined> | undefined;
694
707
  errors: Readonly<import("vue").Ref<DeepReadonly<import("zod").inferFormattedError<Schema_1, string>>>> | undefined;
708
+ submit: (() => boolean) | undefined;
709
+ validate: (() => boolean) | undefined;
695
710
  invalid: import("vue").ComputedRef<boolean>;
696
711
  fields: import("vue").Ref<Set<string>>;
697
712
  fieldsErrors: import("vue").Ref<Map<string, Record<string, {
@@ -704,6 +719,8 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
704
719
  default: (_: {
705
720
  invalid: boolean;
706
721
  formData: unknown extends Partial<import("zod").TypeOf<Schema>> | undefined ? undefined : Partial<import("zod").TypeOf<Schema>> | undefined;
722
+ submit: () => boolean;
723
+ validate: () => boolean;
707
724
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").inferFormattedError<Schema, string>>>>;
708
725
  fieldsErrors: Map<string, Record<string, {
709
726
  _errors: string[];
@@ -809,6 +826,7 @@ export declare const formFactory: <Schema extends FormSchema>(schema: Schema, op
809
826
  default: (_: {
810
827
  formData: unknown extends Partial<import("zod").TypeOf<Schema>> | undefined ? undefined : Partial<import("zod").TypeOf<Schema>> | undefined;
811
828
  submit: () => boolean;
829
+ validate: () => boolean;
812
830
  errors: Readonly<import("vue").Ref<import("vue").DeepReadonly<import("zod").inferFormattedError<Schema, string>>>>;
813
831
  status: import("vue").Ref<import("vue").DeepReadonly<"invalid" | "valid" | undefined>>;
814
832
  invalid: import("vue").Ref<import("vue").DeepReadonly<boolean>>;
package/dist/index.es.js CHANGED
@@ -1,10 +1,10 @@
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";
1
+ import { defineComponent as G, computed as y, onMounted as R, onBeforeUnmount as T, inject as O, toRefs as U, watch as w, provide as z, readonly as k, resolveComponent as g, defineAsyncComponent as D, 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 J, ZodDefault as te, ZodNullable as ae, ZodSchema as ne, ZodEffects as oe } from "zod";
3
+ import { ZodObject as J, ZodDefault as te, ZodNullable as ae, ZodSchema as ne, ZodEffects as se } from "zod";
4
4
  function _(e) {
5
5
  return Array.isArray(e);
6
6
  }
7
- function se(e) {
7
+ function oe(e) {
8
8
  return typeof e < "u";
9
9
  }
10
10
  function M(e) {
@@ -20,11 +20,11 @@ function $(e) {
20
20
  return typeof e > "u";
21
21
  }
22
22
  const ue = /^[0-9]+$/, ie = ["__proto__", "prototype", "constructor"];
23
- function N(e, o, l) {
24
- const s = se(l) ? l : void 0;
25
- if (!q(e) || !P(o))
26
- return s;
27
- const n = H(o);
23
+ function N(e, s, l) {
24
+ const o = oe(l) ? l : void 0;
25
+ if (!q(e) || !P(s))
26
+ return o;
27
+ const n = Q(s);
28
28
  if (n.length !== 0) {
29
29
  for (const r of n) {
30
30
  if (r === "*")
@@ -35,37 +35,37 @@ function N(e, o, l) {
35
35
  if (_(e) && !ue.test(r) ? e = u(e) : e = e[r], $(e) || M(e))
36
36
  break;
37
37
  }
38
- return $(e) ? s : e;
38
+ return $(e) ? o : e;
39
39
  }
40
40
  }
41
- function D(e, o, l) {
42
- if (!q(e) || !P(o))
41
+ function H(e, s, l) {
42
+ if (!q(e) || !P(s))
43
43
  return;
44
- const s = H(o);
45
- if (s.length === 0)
44
+ const o = Q(s);
45
+ if (o.length === 0)
46
46
  return;
47
- const n = s.length;
47
+ const n = o.length;
48
48
  for (let r = 0; r < n; r++) {
49
- const u = s[r];
49
+ const u = o[r];
50
50
  if (r === n - 1) {
51
51
  e[u] = l;
52
52
  return;
53
53
  }
54
54
  if (u === "*" && _(e)) {
55
- const a = s.slice(r + 1).join(".");
55
+ const a = o.slice(r + 1).join(".");
56
56
  for (const i of e)
57
- D(i, a, l);
57
+ H(i, a, l);
58
58
  return;
59
59
  }
60
60
  $(e[u]) && (e[u] = {}), e = e[u];
61
61
  }
62
62
  }
63
- function H(e) {
64
- const o = e.split(/[.]|(?:\[(\d|\*)\])/).filter((l) => !!l);
65
- return o.some((l) => ie.indexOf(l) !== -1) ? [] : o;
63
+ function Q(e) {
64
+ const s = e.split(/[.]|(?:\[(\d|\*)\])/).filter((l) => !!l);
65
+ return s.some((l) => ie.indexOf(l) !== -1) ? [] : s;
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 le = (e, o, l, s) => G({
68
+ const le = (e, s, l, o) => G({
69
69
  name: "FieldComponent",
70
70
  props: {
71
71
  type: {
@@ -109,35 +109,35 @@ const le = (e, o, l, s) => G({
109
109
  String(n.name)
110
110
  );
111
111
  },
112
- set(d) {
113
- t != null && t.formData && (D(
112
+ set(v) {
113
+ t != null && t.formData && (H(
114
114
  Object(t.formData.value),
115
115
  String(n.name),
116
- d
116
+ v
117
117
  ), u("update:modelValue", {
118
118
  newValue: a.value,
119
119
  formData: t == null ? void 0 : t.formData
120
120
  }));
121
121
  }
122
122
  });
123
- Y(() => {
123
+ R(() => {
124
124
  a.value === void 0 && n.defaultValue !== void 0 && (a.value = n.defaultValue);
125
- }), R(() => {
125
+ }), T(() => {
126
126
  S(), V();
127
127
  });
128
- const i = O(o, void 0);
128
+ const i = O(s, void 0);
129
129
  i && i.fields.value.add(n.name);
130
- const t = O(e), { props: m, name: h } = U(n), c = y(() => {
130
+ const t = O(e), { props: m, name: h } = U(n), d = y(() => {
131
131
  if (t != null && t.errors.value)
132
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(
133
+ }), c = y(() => {
134
+ var v;
135
+ return (v = d.value) == null ? void 0 : v._errors;
136
+ }), b = y(() => d.value !== void 0), S = w(b, () => {
137
+ b.value ? (u("invalid", c.value), i && i.errors.value.set(
138
138
  n.name,
139
139
  {
140
- _errors: v.value
140
+ _errors: c.value
141
141
  }
142
142
  )) : (u("valid", a.value), i && i.errors.value.delete(
143
143
  n.name
@@ -148,14 +148,14 @@ const le = (e, o, l, s) => G({
148
148
  u("update:formData", t == null ? void 0 : t.formData);
149
149
  },
150
150
  { deep: !0 }
151
- ), p = (d) => {
152
- a.value = d;
151
+ ), p = (v) => {
152
+ a.value = v;
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
155
  name: I.value.name ?? n.name,
156
156
  invalid: b.value,
157
157
  valid: n.showValid ? !!(!b.value && a.value) : void 0,
158
- type: ((d) => {
158
+ type: ((v) => {
159
159
  if ([
160
160
  f.text,
161
161
  f.number,
@@ -170,67 +170,69 @@ const le = (e, o, l, s) => G({
170
170
  f.month,
171
171
  f.week,
172
172
  f.color
173
- ].includes(d))
174
- return d;
173
+ ].includes(v))
174
+ return v;
175
175
  })(n.type),
176
- invalidLabel: v.value,
176
+ invalidLabel: c.value,
177
177
  modelValue: a.value,
178
178
  "onUpdate:modelValue": p
179
179
  }));
180
180
  return z(l, {
181
181
  name: k(h),
182
- errors: k(c)
182
+ errors: k(d)
183
183
  }), { component: y(() => {
184
184
  if (n.type === f.custom)
185
185
  return {
186
186
  render() {
187
- var d;
188
- return ((d = r.default) == null ? void 0 : d.call(r, {
187
+ var v;
188
+ return ((v = r.default) == null ? void 0 : v.call(r, {
189
189
  modelValue: a.value,
190
190
  onUpdate: p,
191
+ submit: t == null ? void 0 : t.submit,
192
+ validate: t == null ? void 0 : t.validate,
191
193
  invalid: b.value,
192
- invalidLabel: v.value,
194
+ invalidLabel: c.value,
193
195
  formData: t == null ? void 0 : t.formData.value,
194
196
  formErrors: t == null ? void 0 : t.errors.value,
195
- errors: c.value
197
+ errors: d.value
196
198
  })) ?? r.defalut;
197
199
  }
198
200
  };
199
- if (!((s == null ? void 0 : s.lazyLoad) ?? n.lazyLoad)) {
200
- let d;
201
+ if (!((o == null ? void 0 : o.lazyLoad) ?? n.lazyLoad)) {
202
+ let v;
201
203
  switch (n.type) {
202
204
  case f.select:
203
- d = g("VvSelect");
205
+ v = g("VvSelect");
204
206
  break;
205
207
  case f.checkbox:
206
- d = g("VvCheckbox");
208
+ v = g("VvCheckbox");
207
209
  break;
208
210
  case f.radio:
209
- d = g("VvRadio");
211
+ v = g("VvRadio");
210
212
  break;
211
213
  case f.textarea:
212
- d = g("VvTextarea");
214
+ v = g("VvTextarea");
213
215
  break;
214
216
  case f.radioGroup:
215
- d = g("VvRadioGroup");
217
+ v = g("VvRadioGroup");
216
218
  break;
217
219
  case f.checkboxGroup:
218
- d = g("VvCheckboxGroup");
220
+ v = g("VvCheckboxGroup");
219
221
  break;
220
222
  case f.combobox:
221
- d = g("VvCombobox");
223
+ v = g("VvCombobox");
222
224
  break;
223
225
  default:
224
- d = g("VvInputText");
226
+ v = g("VvInputText");
225
227
  }
226
- if (typeof d != "string")
227
- return d;
228
+ if (typeof v != "string")
229
+ return v;
228
230
  console.warn(
229
- `[form-vue warn]: ${d} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`
231
+ `[form-vue warn]: ${v} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`
230
232
  );
231
233
  }
232
- return T(async () => {
233
- switch (s != null && s.sideEffects && await Promise.resolve(s.sideEffects(n.type)), n.type) {
234
+ return D(async () => {
235
+ switch (o != null && o.sideEffects && await Promise.resolve(o.sideEffects(n.type)), n.type) {
234
236
  case f.textarea:
235
237
  return import("@volverjs/ui-vue/vv-textarea");
236
238
  case f.radio:
@@ -251,19 +253,19 @@ const le = (e, o, l, s) => G({
251
253
  render() {
252
254
  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
255
  }
254
- }), Q = (e, o = {}) => {
256
+ }), X = (e, s = {}) => {
255
257
  const l = (r) => {
256
258
  let u = r;
257
- for (; u instanceof oe; )
259
+ for (; u instanceof se; )
258
260
  u = u.innerType();
259
261
  return u;
260
- }, s = l(e);
262
+ }, o = l(e);
261
263
  return {
262
- ...(s instanceof J ? s._def.unknownKeys === "passthrough" : !1) ? o : {},
264
+ ...(o instanceof J ? o._def.unknownKeys === "passthrough" : !1) ? s : {},
263
265
  ...Object.fromEntries(
264
- Object.entries(s.shape).map(
266
+ Object.entries(o.shape).map(
265
267
  ([r, u]) => {
266
- const a = o[r], i = l(u);
268
+ const a = s[r], i = l(u);
267
269
  let t;
268
270
  if (i instanceof te && (t = i._def.defaultValue()), a === null && i instanceof ae)
269
271
  return [r, a];
@@ -274,7 +276,7 @@ const le = (e, o, l, s) => G({
274
276
  }
275
277
  return i instanceof J ? [
276
278
  r,
277
- Q(
279
+ X(
278
280
  i,
279
281
  a && typeof a == "object" ? a : {}
280
282
  )
@@ -283,8 +285,8 @@ const le = (e, o, l, s) => G({
283
285
  )
284
286
  )
285
287
  };
286
- }, fe = (e, o, l) => {
287
- const s = E(), n = E(), r = E(), u = G({
288
+ }, fe = (e, s, l) => {
289
+ const o = E(), n = E(), r = E(), u = G({
288
290
  name: "FormComponent",
289
291
  props: {
290
292
  modelValue: {
@@ -303,44 +305,44 @@ const le = (e, o, l, s) => G({
303
305
  emits: ["invalid", "valid", "submit", "update:modelValue"],
304
306
  expose: ["submit", "validate", "errors", "status", "valid", "invalid"],
305
307
  setup(a, { emit: i }) {
306
- r.value = Q(
308
+ r.value = X(
307
309
  e,
308
310
  Z(a.modelValue)
309
311
  ), w(
310
312
  () => a.modelValue,
311
- (c) => {
312
- if (c) {
313
- const v = j(c) ? Z(c) : c;
314
- r.value = typeof (v == null ? void 0 : v.clone) == "function" ? v.clone() : JSON.parse(JSON.stringify(v));
313
+ (d) => {
314
+ if (d) {
315
+ const c = j(d) ? Z(d) : d;
316
+ r.value = typeof (c == null ? void 0 : c.clone) == "function" ? c.clone() : JSON.parse(JSON.stringify(c));
315
317
  }
316
318
  },
317
319
  { deep: !0 }
318
320
  ), re(
319
321
  r,
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);
322
+ (d) => {
323
+ ((o.value || (l == null ? void 0 : l.continuosValidation)) ?? a.continuosValidation) && t(), (!d || !a.modelValue || JSON.stringify(d) !== JSON.stringify(a.modelValue)) && i("update:modelValue", d);
322
324
  },
323
325
  {
324
326
  deep: !0,
325
327
  throttle: (l == null ? void 0 : l.updateThrottle) ?? a.updateThrottle
326
328
  }
327
329
  );
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);
330
+ const t = (d = r.value) => {
331
+ const c = e.safeParse(d);
332
+ return c.success ? (o.value = void 0, n.value = L.valid, r.value = c.data, i("update:modelValue", r.value), i("valid", c.data), !0) : (o.value = c.error.format(), n.value = L.invalid, i("invalid", o.value), !1);
331
333
  }, m = () => t() ? (i("submit", r.value), !0) : !1, h = y(() => n.value === L.invalid);
332
- return z(o, {
334
+ return z(s, {
333
335
  formData: r,
334
336
  submit: m,
335
337
  validate: t,
336
- errors: k(s),
338
+ errors: k(o),
337
339
  status: k(n),
338
340
  invalid: h
339
341
  }), {
340
342
  formData: r,
341
343
  submit: m,
342
344
  validate: t,
343
- errors: k(s),
345
+ errors: k(o),
344
346
  status: k(n),
345
347
  invalid: h
346
348
  };
@@ -368,7 +370,7 @@ const le = (e, o, l, s) => G({
368
370
  }
369
371
  });
370
372
  return {
371
- errors: s,
373
+ errors: o,
372
374
  status: n,
373
375
  formData: r,
374
376
  /**
@@ -376,7 +378,7 @@ const le = (e, o, l, s) => G({
376
378
  */
377
379
  VvForm: u
378
380
  };
379
- }, de = (e, o) => G({
381
+ }, ve = (e, s) => G({
380
382
  name: "WrapperComponent",
381
383
  props: {
382
384
  name: {
@@ -390,28 +392,28 @@ const le = (e, o, l, s) => G({
390
392
  },
391
393
  emits: ["invalid", "valid"],
392
394
  expose: ["fields", "invalid"],
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);
395
- z(o, {
395
+ setup(o, { emit: n }) {
396
+ const r = O(e), u = O(s, void 0), a = E(/* @__PURE__ */ new Set()), i = E(/* @__PURE__ */ new Map()), { name: t } = U(o);
397
+ z(s, {
396
398
  name: k(t),
397
399
  errors: i,
398
400
  fields: a
399
401
  }), w(
400
402
  a,
401
403
  (h) => {
402
- u != null && u.fields && h.forEach((c) => {
403
- u == null || u.fields.value.add(c);
404
+ u != null && u.fields && h.forEach((d) => {
405
+ u == null || u.fields.value.add(d);
404
406
  });
405
407
  },
406
408
  { deep: !0 }
407
409
  ), w(
408
410
  () => 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);
411
+ (h, d) => {
412
+ u != null && u.errors && (Array.from(d.keys()).forEach((c) => {
413
+ u.errors.value.delete(c);
414
+ }), Array.from(h.keys()).forEach((c) => {
415
+ const b = h.get(c);
416
+ b && u.errors.value.set(c, b);
415
417
  }));
416
418
  },
417
419
  { deep: !0 }
@@ -422,31 +424,37 @@ const le = (e, o, l, s) => G({
422
424
  }), {
423
425
  formData: r == null ? void 0 : r.formData,
424
426
  errors: r == null ? void 0 : r.errors,
427
+ submit: r == null ? void 0 : r.submit,
428
+ validate: r == null ? void 0 : r.validate,
425
429
  invalid: m,
426
430
  fields: a,
427
431
  fieldsErrors: i
428
432
  };
429
433
  },
430
434
  render() {
431
- var s, n;
435
+ var o, n;
432
436
  return this.tag ? x(this.tag, null, {
433
437
  default: () => {
434
438
  var r, u;
435
439
  return ((u = (r = this.$slots).default) == null ? void 0 : u.call(r, {
436
440
  invalid: this.invalid,
437
441
  formData: this.formData,
442
+ submit: this.submit,
443
+ validate: this.validate,
438
444
  errors: this.errors,
439
445
  fieldsErrors: this.fieldsErrors
440
446
  })) ?? this.$slots.defalut;
441
447
  }
442
- }) : ((n = (s = this.$slots).default) == null ? void 0 : n.call(s, {
448
+ }) : ((n = (o = this.$slots).default) == null ? void 0 : n.call(o, {
443
449
  invalid: this.invalid,
444
450
  formData: this.formData,
451
+ submit: this.submit,
452
+ validate: this.validate,
445
453
  errors: this.errors,
446
454
  fieldsErrors: this.fieldsErrors
447
455
  })) ?? this.$slots.defalut;
448
456
  }
449
- }), ce = (e, o) => {
457
+ }), de = (e, s) => {
450
458
  const l = G({
451
459
  props: {
452
460
  schema: {
@@ -454,19 +462,19 @@ const le = (e, o, l, s) => G({
454
462
  required: !0
455
463
  }
456
464
  },
457
- setup(s, { slots: n }) {
465
+ setup(o, { slots: n }) {
458
466
  const r = O(e);
459
467
  if (!(r != null && r.formData))
460
468
  return;
461
- const u = typeof s.schema == "function" ? s.schema(r) : s.schema;
469
+ const u = typeof o.schema == "function" ? o.schema(r) : o.schema;
462
470
  let a;
463
471
  return () => {
464
472
  var i;
465
473
  return u.reduce(
466
474
  (t, m) => {
467
475
  const h = typeof m == "function" ? m(r) : m, {
468
- vvIs: c,
469
- vvName: v,
476
+ vvIs: d,
477
+ vvName: c,
470
478
  vvSlots: b,
471
479
  vvChildren: S,
472
480
  vvIf: V,
@@ -474,7 +482,7 @@ const le = (e, o, l, s) => G({
474
482
  vvType: I,
475
483
  vvDefaultValue: W,
476
484
  vvShowValid: K,
477
- ...d
485
+ ...v
478
486
  } = h;
479
487
  if (V !== void 0) {
480
488
  if (typeof V == "string" ? a = !!N(
@@ -493,23 +501,23 @@ const le = (e, o, l, s) => G({
493
501
  const A = S ? x(l, {
494
502
  schema: S
495
503
  }) : void 0;
496
- return v ? (t.push(
504
+ return c ? (t.push(
497
505
  x(
498
- o,
506
+ s,
499
507
  {
500
- name: v,
501
- is: c,
508
+ name: c,
509
+ is: d,
502
510
  type: I,
503
511
  defaultValue: W,
504
512
  showValid: K,
505
- props: d
513
+ props: v
506
514
  },
507
515
  b ?? A
508
516
  )
509
- ), t) : c ? (t.push(
517
+ ), t) : d ? (t.push(
510
518
  x(
511
- c,
512
519
  d,
520
+ v,
513
521
  b ?? A
514
522
  )
515
523
  ), t) : (S && t.push(A), t);
@@ -518,6 +526,7 @@ const le = (e, o, l, s) => G({
518
526
  (i = n == null ? void 0 : n.default) == null ? void 0 : i.call(n, {
519
527
  formData: r == null ? void 0 : r.formData.value,
520
528
  submit: r == null ? void 0 : r.submit,
529
+ validate: r == null ? void 0 : r.validate,
521
530
  errors: r == null ? void 0 : r.errors.value,
522
531
  status: r == null ? void 0 : r.status.value,
523
532
  invalid: r == null ? void 0 : r.invalid.value
@@ -528,49 +537,49 @@ const le = (e, o, l, s) => G({
528
537
  }
529
538
  });
530
539
  return l;
531
- }, B = (e, o = {}) => {
532
- const l = Symbol(), s = Symbol(), n = Symbol(), { VvForm: r, errors: u, status: a, formData: i } = fe(
540
+ }, B = (e, s = {}) => {
541
+ const l = Symbol(), o = Symbol(), n = Symbol(), { VvForm: r, errors: u, status: a, formData: i } = fe(
533
542
  e,
534
- l,
535
- o
536
- ), t = de(
537
543
  l,
538
544
  s
545
+ ), t = ve(
546
+ l,
547
+ o
539
548
  ), m = le(
540
549
  l,
541
- s,
550
+ o,
542
551
  n,
543
- o
544
- ), h = ce(l, m);
552
+ s
553
+ ), h = de(l, m);
545
554
  return {
546
555
  VvForm: r,
547
556
  VvFormWrapper: t,
548
557
  VvFormField: m,
549
558
  VvFormTemplate: h,
550
559
  formInjectionKey: l,
551
- formWrapperInjectionKey: s,
560
+ formWrapperInjectionKey: o,
552
561
  formFieldInjectionKey: n,
553
562
  errors: u,
554
563
  status: a,
555
564
  formData: i
556
565
  };
557
- }, X = Symbol(), be = (e) => {
558
- let o = {};
559
- return e.schema && (o = B(e.schema, e)), {
560
- ...o,
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));
566
+ }, Y = Symbol(), be = (e) => {
567
+ let s = {};
568
+ return e.schema && (s = B(e.schema, e)), {
569
+ ...s,
570
+ install(l, { global: o = !1 } = {}) {
571
+ l.provide(Y, e), o && (l.config.globalProperties.$vvForm = e, s != null && s.VvForm && l.component("VvForm", s.VvForm), s != null && s.VvFormWrapper && l.component("VvFormWrapper", s.VvFormWrapper), s != null && s.VvFormField && l.component("VvFormField", s.VvFormField), s != null && s.VvFormTemplate && l.component("VvFormTemplate", s.VvFormTemplate));
563
572
  }
564
573
  };
565
- }, pe = (e, o = {}) => ee() ? B(e, {
566
- ...O(X, {}),
567
- ...o
568
- }) : B(e, o), ye = (e, o = {}) => B(e, o);
574
+ }, pe = (e, s = {}) => ee() ? B(e, {
575
+ ...O(Y, {}),
576
+ ...s
577
+ }) : B(e, s), ye = (e, s = {}) => B(e, s);
569
578
  export {
570
579
  f as FormFieldType,
571
580
  be as createForm,
572
- Q as defaultObjectBySchema,
581
+ X as defaultObjectBySchema,
573
582
  ye as formFactory,
574
- X as pluginInjectionKey,
583
+ Y as pluginInjectionKey,
575
584
  pe as useForm
576
585
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
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"})});
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 o=K(s);if(o.length!==0){for(const t of o){if(t==="*")continue;const l=function(a){return a.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 o=i.length;for(let t=0;t<o;t++){const l=i[t];if(t===o-1){e[l]=f;return}if(l==="*"&&I(e)){const a=i.slice(t+1).join(".");for(const u of e)_(u,a,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:o=>Object.values(d).includes(o),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(o,{slots:t,emit:l}){const a=r.computed({get(){if(n!=null&&n.formData)return C(Object(n.formData.value),String(o.name))},set(c){n!=null&&n.formData&&(_(Object(n.formData.value),String(o.name),c),l("update:modelValue",{newValue:a.value,formData:n==null?void 0:n.formData}))}});r.onMounted(()=>{a.value===void 0&&o.defaultValue!==void 0&&(a.value=o.defaultValue)}),r.onBeforeUnmount(()=>{w(),g()});const u=r.inject(s,void 0);u&&u.fields.value.add(o.name);const n=r.inject(e),{props:h,name:y}=r.toRefs(o),m=r.computed(()=>{if(n!=null&&n.errors.value)return C(n.errors.value,String(o.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(o.name,{_errors:v.value})):(l("valid",a.value),u&&u.errors.value.delete(o.name))}),g=r.watch(()=>n==null?void 0:n.formData,()=>{l("update:formData",n==null?void 0:n.formData)},{deep:!0}),V=c=>{a.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??o.name,invalid:b.value,valid:o.showValid?!!(!b.value&&a.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})(o.type),invalidLabel:v.value,modelValue:a.value,"onUpdate:modelValue":V}));return r.provide(f,{name:r.readonly(y),errors:r.readonly(m)}),{component:r.computed(()=>{if(o.type===d.custom)return{render(){var c;return((c=t.default)==null?void 0:c.call(t,{modelValue:a.value,onUpdate:V,submit:n==null?void 0:n.submit,validate:n==null?void 0:n.validate,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)??o.lazyLoad)){let c;switch(o.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(o.type)),o.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 a=s[t],u=f(l);let n;if(u instanceof x.ZodDefault&&(n=u._def.defaultValue()),a===null&&u instanceof x.ZodNullable)return[t,a];if(u instanceof x.ZodSchema){const h=l.safeParse(a);if(h.success)return[t,h.data??n]}return u instanceof x.ZodObject?[t,$(u,a&&typeof a=="object"?a:{})]:[t,n]}))}},P=(e,s,f)=>{const i=r.ref(),o=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(a,{emit:u}){t.value=$(e,r.toRaw(a.modelValue)),r.watch(()=>a.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))??a.continuosValidation)&&n(),(!m||!a.modelValue||JSON.stringify(m)!==JSON.stringify(a.modelValue))&&u("update:modelValue",m)},{deep:!0,throttle:(f==null?void 0:f.updateThrottle)??a.updateThrottle});const n=(m=t.value)=>{const v=e.safeParse(m);return v.success?(i.value=void 0,o.value=O.valid,t.value=v.data,u("update:modelValue",t.value),u("valid",v.data),!0):(i.value=v.error.format(),o.value=O.invalid,u("invalid",i.value),!1)},h=()=>n()?(u("submit",t.value),!0):!1,y=r.computed(()=>o.value===O.invalid);return r.provide(s,{formData:t,submit:h,validate:n,errors:r.readonly(i),status:r.readonly(o),invalid:y}),{formData:t,submit:h,validate:n,errors:r.readonly(i),status:r.readonly(o),invalid:y}},render(){return r.h("form",{onSubmit:r.withModifiers(this.submit,["prevent"])},{default:()=>{var a,u;return((u=(a=this.$slots)==null?void 0:a.default)==null?void 0:u.call(a,{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:o,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:o}){const t=r.inject(e),l=r.inject(s,void 0),a=r.ref(new Set),u=r.ref(new Map),{name:n}=r.toRefs(i);r.provide(s,{name:r.readonly(n),errors:u,fields:a}),r.watch(a,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?o("invalid"):o("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:h,fields:a,fieldsErrors:u}},render(){var i,o;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,submit:this.submit,validate:this.validate,errors:this.errors,fieldsErrors:this.fieldsErrors}))??this.$slots.defalut}}):((o=(i=this.$slots).default)==null?void 0:o.call(i,{invalid:this.invalid,formData:this.formData,submit:this.submit,validate:this.validate,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:o}){const t=r.inject(e);if(!(t!=null&&t.formData))return;const l=typeof i.schema=="function"?i.schema(t):i.schema;let a;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"?a=!!C(Object(t.formData.value),g):typeof g=="function"?a=r.unref(g(t)):a=r.unref(g),!a)return n}else if(V!==void 0&&a!==void 0){if(a||(typeof V=="string"?a=!!C(Object(t.formData.value),V):typeof V=="function"?a=r.unref(V(t)):a=r.unref(V),!a))return n}else a=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=o==null?void 0:o.default)==null?void 0:u.call(o,{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})])}}});return f},S=(e,s={})=>{const f=Symbol(),i=Symbol(),o=Symbol(),{VvForm:t,errors:l,status:a,formData:u}=P(e,f,s),n=T(f,i),h=J(f,i,o,s),y=R(f,h);return{VvForm:t,VvFormWrapper:n,VvFormField:h,VvFormTemplate:y,formInjectionKey:f,formWrapperInjectionKey:i,formFieldInjectionKey:o,errors:l,status:a,formData:u}},B=Symbol(),H=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))}}},Q=(e,s={})=>r.getCurrentInstance()?S(e,{...r.inject(B,{}),...s}):S(e,s),X=(e,s={})=>S(e,s);p.FormFieldType=d,p.createForm=H,p.defaultObjectBySchema=$,p.formFactory=X,p.pluginInjectionKey=B,p.useForm=Q,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "bugs": {
20
20
  "url": "https://github.com/volverjs/form-vue/issues"
21
21
  },
22
- "version": "0.0.10-beta.11",
22
+ "version": "0.0.10-beta.13",
23
23
  "engines": {
24
24
  "node": ">= 16.x"
25
25
  },
@@ -225,6 +225,8 @@ export const defineFormField = <Schema extends FormSchema>(
225
225
  slots.default?.({
226
226
  modelValue: modelValue.value,
227
227
  onUpdate,
228
+ submit: injectedFormData?.submit,
229
+ validate: injectedFormData?.validate,
228
230
  invalid: invalid.value,
229
231
  invalidLabel: invalidLabel.value,
230
232
  formData: injectedFormData?.formData.value,
@@ -144,6 +144,7 @@ export const defineFormTemplate = <Schema extends FormSchema>(
144
144
  templateSlots?.default?.({
145
145
  formData: injectedFormData?.formData.value,
146
146
  submit: injectedFormData?.submit,
147
+ validate: injectedFormData?.validate,
147
148
  errors: injectedFormData?.errors.value,
148
149
  status: injectedFormData?.status.value,
149
150
  invalid: injectedFormData?.invalid.value,
@@ -166,6 +167,7 @@ export const defineFormTemplate = <Schema extends FormSchema>(
166
167
  ? undefined
167
168
  : Partial<TypeOf<Schema>> | undefined
168
169
  submit: () => boolean
170
+ validate: () => boolean
169
171
  errors: Readonly<
170
172
  Ref<DeepReadonly<z.inferFormattedError<Schema>>>
171
173
  >
@@ -103,6 +103,8 @@ export const defineFormWrapper = <Schema extends FormSchema>(
103
103
  return {
104
104
  formData: injectedFormData?.formData,
105
105
  errors: injectedFormData?.errors,
106
+ submit: injectedFormData?.submit,
107
+ validate: injectedFormData?.validate,
106
108
  invalid,
107
109
  fields,
108
110
  fieldsErrors,
@@ -115,6 +117,8 @@ export const defineFormWrapper = <Schema extends FormSchema>(
115
117
  this.$slots.default?.({
116
118
  invalid: this.invalid,
117
119
  formData: this.formData,
120
+ submit: this.submit,
121
+ validate: this.validate,
118
122
  errors: this.errors,
119
123
  fieldsErrors: this.fieldsErrors,
120
124
  }) ?? this.$slots.defalut,
@@ -124,6 +128,8 @@ export const defineFormWrapper = <Schema extends FormSchema>(
124
128
  this.$slots.default?.({
125
129
  invalid: this.invalid,
126
130
  formData: this.formData,
131
+ submit: this.submit,
132
+ validate: this.validate,
127
133
  errors: this.errors,
128
134
  fieldsErrors: this.fieldsErrors,
129
135
  }) ?? this.$slots.defalut
@@ -143,6 +149,8 @@ export const defineFormWrapper = <Schema extends FormSchema>(
143
149
  | undefined
144
150
  ? undefined
145
151
  : Partial<TypeOf<Schema>> | undefined
152
+ submit: () => boolean
153
+ validate: () => boolean
146
154
  errors: Readonly<
147
155
  Ref<DeepReadonly<z.inferFormattedError<Schema>>>
148
156
  >