@volverjs/form-vue 0.0.10-beta.2 → 0.0.10-beta.4

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