@volverjs/form-vue 0.0.1 → 0.0.3
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 +42 -15
- package/dist/VvForm.d.ts +3 -2
- package/dist/index.d.ts +10 -4
- package/dist/index.es.js +102 -99
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/utils.d.ts +5 -1
- package/package.json +1 -3
- package/src/VvForm.ts +4 -3
- package/src/index.ts +10 -3
- package/src/types.d.ts +1 -0
- package/src/utils.ts +13 -3
package/README.md
CHANGED
|
@@ -73,10 +73,13 @@ A `valid` or `invalid` event is emitted when the form status changes.
|
|
|
73
73
|
const onSubmit = (formData) => {
|
|
74
74
|
// Do something with the form data
|
|
75
75
|
}
|
|
76
|
+
const onInvalid = (errors) => {
|
|
77
|
+
// Do something with the errors
|
|
78
|
+
}
|
|
76
79
|
</script>
|
|
77
80
|
|
|
78
81
|
<template>
|
|
79
|
-
<VvForm @submit="onSubmit">
|
|
82
|
+
<VvForm @submit="onSubmit" @invalid="onInvalid">
|
|
80
83
|
<!-- form fields -->
|
|
81
84
|
<button type="submit">Submit</button>
|
|
82
85
|
</VvForm>
|
|
@@ -87,7 +90,10 @@ The submit can be triggered programmatically with the `submit()` method.
|
|
|
87
90
|
|
|
88
91
|
```vue
|
|
89
92
|
<script lang="ts" setup>
|
|
90
|
-
|
|
93
|
+
import { ref } from 'vue'
|
|
94
|
+
import type { FormComponent } from '@volverjs/form-vue'
|
|
95
|
+
|
|
96
|
+
const formEl = ref<InstanceType<FormComponent>>(null)
|
|
91
97
|
const onSubmit = (formData) => {
|
|
92
98
|
// Do something with the form data
|
|
93
99
|
}
|
|
@@ -121,7 +127,6 @@ The throttle can be changed with the `updateThrottle` option.
|
|
|
121
127
|
<template>
|
|
122
128
|
<VvForm v-model="formData">
|
|
123
129
|
<!-- form fields -->
|
|
124
|
-
<button type="submit">Submit</button>
|
|
125
130
|
</VvForm>
|
|
126
131
|
</template>
|
|
127
132
|
```
|
|
@@ -134,13 +139,13 @@ The wrapper status is invalid if at least one of the fields inside it is invalid
|
|
|
134
139
|
```vue
|
|
135
140
|
<template>
|
|
136
141
|
<VvForm>
|
|
137
|
-
<VvFormWrapper
|
|
142
|
+
<VvFormWrapper v-slot="{ invalid }">
|
|
138
143
|
<div class="form-section-1">
|
|
139
144
|
<span v-if="invalid">There is a validation error</span>
|
|
140
145
|
<!-- form fields of section 1 -->
|
|
141
146
|
</div>
|
|
142
147
|
</VvFormWrapper>
|
|
143
|
-
<VvFormWrapper
|
|
148
|
+
<VvFormWrapper v-slot="{ invalid }">
|
|
144
149
|
<div class="form-section-2">
|
|
145
150
|
<span v-if="invalid">There is a validation error</span>
|
|
146
151
|
<!-- form fields of the section 2 -->
|
|
@@ -150,6 +155,27 @@ The wrapper status is invalid if at least one of the fields inside it is invalid
|
|
|
150
155
|
</template>
|
|
151
156
|
```
|
|
152
157
|
|
|
158
|
+
`VvFormWrapper` can be used recursively to create a validation tree. The wrapper status is invalid if at least one of the fields inside it or one of its children is invalid.
|
|
159
|
+
|
|
160
|
+
```vue
|
|
161
|
+
<template>
|
|
162
|
+
<VvForm>
|
|
163
|
+
<VvFormWrapper v-slot="{ invalid }">
|
|
164
|
+
<div class="form-section">
|
|
165
|
+
<span v-if="invalid">There is a validation error</span>
|
|
166
|
+
<!-- form fields of section -->
|
|
167
|
+
<VvFormWrapper v-slot="{ invalid: groupInvalid }">
|
|
168
|
+
<div class="form-section__group">
|
|
169
|
+
<span v-if="groupInvalid">There is a validation error</span>
|
|
170
|
+
<!-- form fields of the group -->
|
|
171
|
+
</div>
|
|
172
|
+
</VvFormWrapper>
|
|
173
|
+
</div>
|
|
174
|
+
</VvFormWrapper>
|
|
175
|
+
</VvForm>
|
|
176
|
+
</template>
|
|
177
|
+
```
|
|
178
|
+
|
|
153
179
|
### VvFormField
|
|
154
180
|
|
|
155
181
|
`VvFormField` allow you to render a [`@volverjs/ui-vue`](https://github.com/volverjs/ui-vue) input component inside a form. It automatically bind the form data through the `name` attribute.
|
|
@@ -176,33 +202,33 @@ For nested objects, use the `name` attribute with dot notation.
|
|
|
176
202
|
The type of input component is defined by the `type` attribute.
|
|
177
203
|
All the available input types are listed in the [VvFormField documentation](/docs/VvFormField.md).
|
|
178
204
|
|
|
179
|
-
You can also use the `VvFormField` component to render a default slot without .
|
|
205
|
+
You can also use the `VvFormField` component to render a default slot without a `type` (default `type` is `custom`).
|
|
180
206
|
|
|
181
207
|
```vue
|
|
182
208
|
<template>
|
|
183
209
|
<VvForm>
|
|
184
210
|
<VvFormField
|
|
185
|
-
|
|
186
|
-
#default="{
|
|
211
|
+
v-slot="{
|
|
187
212
|
modelValue,
|
|
188
213
|
invalid,
|
|
189
214
|
invalidLabel,
|
|
190
215
|
formData,
|
|
191
216
|
formErrors,
|
|
192
|
-
|
|
217
|
+
errors,
|
|
193
218
|
onUpdate
|
|
194
219
|
}"
|
|
220
|
+
name="surname"
|
|
195
221
|
>
|
|
196
|
-
<label for="
|
|
222
|
+
<label for="surname">Surname</label>
|
|
197
223
|
<input
|
|
198
|
-
id="
|
|
224
|
+
id="surname"
|
|
199
225
|
type="text"
|
|
200
226
|
:value="modelValue"
|
|
201
227
|
:aria-invalid="invalid"
|
|
202
|
-
:aria-errormessage="invalid ? '
|
|
228
|
+
:aria-errormessage="invalid ? 'surname-alert' : undefined"
|
|
203
229
|
@input="onUpdate"
|
|
204
230
|
/>
|
|
205
|
-
<small v-if="invalid" role="alert" id="
|
|
231
|
+
<small v-if="invalid" role="alert" id="surname-alert">
|
|
206
232
|
{{ invalidLabel }}
|
|
207
233
|
</small>
|
|
208
234
|
</VvFormField>
|
|
@@ -219,7 +245,7 @@ Or a custom component.
|
|
|
219
245
|
|
|
220
246
|
<template>
|
|
221
247
|
<VvForm>
|
|
222
|
-
<VvFormField name="
|
|
248
|
+
<VvFormField name="surname" :is="MyInput" />
|
|
223
249
|
</VvForm>
|
|
224
250
|
</template>
|
|
225
251
|
```
|
|
@@ -227,7 +253,7 @@ Or a custom component.
|
|
|
227
253
|
## Composable
|
|
228
254
|
|
|
229
255
|
`useForm` can be used to create a form programmatically inside a Vue 3 Component.
|
|
230
|
-
|
|
256
|
+
The default settings are inherited from the plugin (if it was defined).
|
|
231
257
|
|
|
232
258
|
```vue
|
|
233
259
|
<script lang="ts" setup>
|
|
@@ -257,6 +283,7 @@ If the plugin is defined globally, the settings are inherited but can be customi
|
|
|
257
283
|
## Outside a Vue 3 Component
|
|
258
284
|
|
|
259
285
|
`formFactory` can be used to create a form outside a Vue 3 Component.
|
|
286
|
+
No settings are inherited.
|
|
260
287
|
|
|
261
288
|
```ts
|
|
262
289
|
import { formFactory } from '@volverjs/form-vue'
|
package/dist/VvForm.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { type InjectionKey } from 'vue';
|
|
2
|
-
import type { AnyZodObject } from 'zod';
|
|
2
|
+
import type { AnyZodObject, ZodEffects } from 'zod';
|
|
3
3
|
import type { InjectedFormData } from './types';
|
|
4
4
|
export declare enum FormStatus {
|
|
5
5
|
invalid = "invalid",
|
|
6
6
|
valid = "valid"
|
|
7
7
|
}
|
|
8
|
-
export declare const defineForm: (schema: AnyZodObject
|
|
8
|
+
export declare const defineForm: (schema: AnyZodObject | ZodEffects<AnyZodObject>, provideKey: InjectionKey<InjectedFormData>, options?: {
|
|
9
9
|
updateThrottle?: number;
|
|
10
|
+
continuosValidation?: boolean;
|
|
10
11
|
}) => import("vue").DefineComponent<{
|
|
11
12
|
modelValue: {
|
|
12
13
|
type: ObjectConstructor;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { type InjectionKey, type Plugin } from 'vue';
|
|
2
|
-
import type { AnyZodObject } from 'zod';
|
|
2
|
+
import type { AnyZodObject, ZodEffects } from 'zod';
|
|
3
|
+
import { defineFormField } from './VvFormField';
|
|
4
|
+
import { defineForm } from './VvForm';
|
|
5
|
+
import { defineFormWrapper } from './VvFormWrapper';
|
|
3
6
|
import type { InjectedFormData, InjectedFormWrapperData, InjectedFormFieldData, FormComposableOptions, FormPluginOptions } from './types';
|
|
4
|
-
export declare const formFactory: (schema: AnyZodObject
|
|
7
|
+
export declare const formFactory: (schema: AnyZodObject | ZodEffects<AnyZodObject>, options?: FormComposableOptions) => {
|
|
5
8
|
VvForm: import("vue").DefineComponent<{
|
|
6
9
|
modelValue: {
|
|
7
10
|
type: ObjectConstructor;
|
|
@@ -60,7 +63,7 @@ export declare const formFactory: (schema: AnyZodObject, options?: FormComposabl
|
|
|
60
63
|
};
|
|
61
64
|
export declare const pluginInjectionKey: InjectionKey<FormPluginOptions>;
|
|
62
65
|
export declare const createForm: (options: FormPluginOptions) => Plugin & Partial<ReturnType<typeof useForm>>;
|
|
63
|
-
export declare const useForm: (schema: AnyZodObject
|
|
66
|
+
export declare const useForm: (schema: AnyZodObject | ZodEffects<AnyZodObject>, options?: FormComposableOptions) => {
|
|
64
67
|
VvForm: import("vue").DefineComponent<{
|
|
65
68
|
modelValue: {
|
|
66
69
|
type: ObjectConstructor;
|
|
@@ -119,4 +122,7 @@ export declare const useForm: (schema: AnyZodObject, options?: FormComposableOpt
|
|
|
119
122
|
};
|
|
120
123
|
export { FormFieldType } from './enums';
|
|
121
124
|
export { defaultObjectBySchema } from './utils';
|
|
122
|
-
|
|
125
|
+
type FormComponent = ReturnType<typeof defineForm>;
|
|
126
|
+
type FormWrapperComponent = ReturnType<typeof defineFormWrapper>;
|
|
127
|
+
type FormFieldComponent = ReturnType<typeof defineFormField>;
|
|
128
|
+
export type { InjectedFormData, InjectedFormWrapperData, InjectedFormFieldData, FormComposableOptions, FormPluginOptions, FormComponent, FormWrapperComponent, FormFieldComponent, };
|
package/dist/index.es.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { defineComponent as w, computed as h, onMounted as
|
|
1
|
+
import { defineComponent as w, computed as h, onMounted as Z, inject as S, toRefs as G, watch as V, provide as $, readonly as M, resolveComponent as b, defineAsyncComponent as q, h as O, ref as x, isProxy as Y, toRaw as H, withModifiers as Q } from "vue";
|
|
2
2
|
import { watchThrottled as X } from "@vueuse/core";
|
|
3
|
-
import {
|
|
4
|
-
function
|
|
3
|
+
import { ZodEffects as P, ZodDefault as T, ZodObject as F } from "zod";
|
|
4
|
+
function A(e) {
|
|
5
5
|
return Array.isArray(e);
|
|
6
6
|
}
|
|
7
|
-
function
|
|
7
|
+
function ee(e) {
|
|
8
8
|
return typeof e < "u";
|
|
9
9
|
}
|
|
10
10
|
function N(e) {
|
|
@@ -16,26 +16,26 @@ function L(e) {
|
|
|
16
16
|
function D(e) {
|
|
17
17
|
return typeof e == "string";
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function E(e) {
|
|
20
20
|
return typeof e > "u";
|
|
21
21
|
}
|
|
22
|
-
const
|
|
22
|
+
const re = /^[0-9]+$/, te = ["__proto__", "prototype", "constructor"];
|
|
23
23
|
function _(e, r, t) {
|
|
24
|
-
const a =
|
|
24
|
+
const a = ee(t) ? t : void 0;
|
|
25
25
|
if (!L(e) || !D(r))
|
|
26
26
|
return a;
|
|
27
27
|
const n = W(r);
|
|
28
28
|
if (n.length !== 0) {
|
|
29
|
-
for (const
|
|
30
|
-
if (
|
|
29
|
+
for (const u of n) {
|
|
30
|
+
if (u === "*")
|
|
31
31
|
continue;
|
|
32
32
|
const i = function(s) {
|
|
33
|
-
return s.map((f) =>
|
|
33
|
+
return s.map((f) => E(f) || N(f) ? f : A(f) ? i(f) : f[u]);
|
|
34
34
|
};
|
|
35
|
-
if (
|
|
35
|
+
if (A(e) && !re.test(u) ? e = i(e) : e = e[u], E(e) || N(e))
|
|
36
36
|
break;
|
|
37
37
|
}
|
|
38
|
-
return
|
|
38
|
+
return E(e) ? a : e;
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
function U(e, r, t) {
|
|
@@ -45,27 +45,27 @@ function U(e, r, t) {
|
|
|
45
45
|
if (a.length === 0)
|
|
46
46
|
return;
|
|
47
47
|
const n = a.length;
|
|
48
|
-
for (let
|
|
49
|
-
const i = a[
|
|
50
|
-
if (
|
|
48
|
+
for (let u = 0; u < n; u++) {
|
|
49
|
+
const i = a[u];
|
|
50
|
+
if (u === n - 1) {
|
|
51
51
|
e[i] = t;
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
|
-
if (i === "*" &&
|
|
55
|
-
const s = a.slice(
|
|
54
|
+
if (i === "*" && A(e)) {
|
|
55
|
+
const s = a.slice(u + 1).join(".");
|
|
56
56
|
for (const f of e)
|
|
57
57
|
U(f, s, t);
|
|
58
58
|
return;
|
|
59
59
|
}
|
|
60
|
-
|
|
60
|
+
E(e[i]) && (e[i] = {}), e = e[i];
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
function W(e) {
|
|
64
64
|
const r = e.split(/[.]|(?:\[(\d|\*)\])/).filter((t) => !!t);
|
|
65
|
-
return r.some((t) =>
|
|
65
|
+
return r.some((t) => te.indexOf(t) !== -1) ? [] : r;
|
|
66
66
|
}
|
|
67
67
|
var l = /* @__PURE__ */ ((e) => (e.text = "text", e.number = "number", e.email = "email", e.password = "password", e.tel = "tel", e.url = "url", e.search = "search", e.date = "date", e.time = "time", e.datetimeLocal = "datetimeLocal", e.month = "month", e.week = "week", e.color = "color", e.select = "select", e.checkbox = "checkbox", e.radio = "radio", e.textarea = "textarea", e.radioGroup = "radioGroup", e.checkboxGroup = "checkboxGroup", e.combobox = "combobox", e.custom = "custom", e))(l || {});
|
|
68
|
-
const
|
|
68
|
+
const ne = (e, r, t, a = {}) => w({
|
|
69
69
|
name: "FieldComponent",
|
|
70
70
|
props: {
|
|
71
71
|
type: {
|
|
@@ -96,34 +96,34 @@ const te = (e, r, t, a = {}) => w({
|
|
|
96
96
|
},
|
|
97
97
|
emits: ["invalid", "valid", "update:formData", "update:modelValue"],
|
|
98
98
|
expose: ["invalid", "invalidLabel", "errors"],
|
|
99
|
-
setup(n, { slots:
|
|
99
|
+
setup(n, { slots: u, emit: i }) {
|
|
100
100
|
const s = h({
|
|
101
101
|
get() {
|
|
102
|
-
if (
|
|
102
|
+
if (o != null && o.modelValue)
|
|
103
103
|
return _(
|
|
104
|
-
Object(
|
|
104
|
+
Object(o.modelValue.value),
|
|
105
105
|
String(n.name)
|
|
106
106
|
);
|
|
107
107
|
},
|
|
108
108
|
set(c) {
|
|
109
|
-
|
|
110
|
-
Object(
|
|
109
|
+
o != null && o.modelValue && (U(
|
|
110
|
+
Object(o.modelValue.value),
|
|
111
111
|
String(n.name),
|
|
112
112
|
c
|
|
113
113
|
), i("update:modelValue", {
|
|
114
114
|
newValue: s.value,
|
|
115
|
-
formData:
|
|
115
|
+
formData: o == null ? void 0 : o.modelValue
|
|
116
116
|
}));
|
|
117
117
|
}
|
|
118
118
|
});
|
|
119
|
-
|
|
119
|
+
Z(() => {
|
|
120
120
|
s.value === void 0 && n.defaultValue !== void 0 && (s.value = n.defaultValue);
|
|
121
121
|
});
|
|
122
122
|
const f = S(r, void 0);
|
|
123
123
|
f && f.fields.value.add(n.name);
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
126
|
-
return _(
|
|
124
|
+
const o = S(e), { props: d, name: m } = G(n), v = h(() => {
|
|
125
|
+
if (o != null && o.errors.value)
|
|
126
|
+
return _(o.errors.value, String(n.name));
|
|
127
127
|
}), y = h(() => {
|
|
128
128
|
var c;
|
|
129
129
|
return (c = v.value) == null ? void 0 : c._errors;
|
|
@@ -135,15 +135,15 @@ const te = (e, r, t, a = {}) => w({
|
|
|
135
135
|
n.name
|
|
136
136
|
));
|
|
137
137
|
}), V(
|
|
138
|
-
() =>
|
|
138
|
+
() => o == null ? void 0 : o.modelValue,
|
|
139
139
|
() => {
|
|
140
|
-
i("update:formData",
|
|
140
|
+
i("update:formData", o == null ? void 0 : o.modelValue);
|
|
141
141
|
},
|
|
142
142
|
{ deep: !0 }
|
|
143
143
|
);
|
|
144
144
|
const k = (c) => {
|
|
145
145
|
s.value = c;
|
|
146
|
-
}, I = h(() => typeof d.value == "function" ? d.value(
|
|
146
|
+
}, I = h(() => typeof d.value == "function" ? d.value(o == null ? void 0 : o.modelValue) : d.value), R = h(() => ({
|
|
147
147
|
...I.value,
|
|
148
148
|
name: I.value.name ?? n.name,
|
|
149
149
|
invalid: p.value,
|
|
@@ -172,22 +172,22 @@ const te = (e, r, t, a = {}) => w({
|
|
|
172
172
|
"onUpdate:modelValue": k
|
|
173
173
|
}));
|
|
174
174
|
return $(t, {
|
|
175
|
-
name:
|
|
176
|
-
errors:
|
|
175
|
+
name: M(m),
|
|
176
|
+
errors: M(v)
|
|
177
177
|
}), { component: h(() => {
|
|
178
178
|
if (n.type === l.custom)
|
|
179
179
|
return {
|
|
180
180
|
render() {
|
|
181
181
|
var c;
|
|
182
|
-
return ((c =
|
|
182
|
+
return ((c = u.default) == null ? void 0 : c.call(u, {
|
|
183
183
|
modelValue: s.value,
|
|
184
184
|
onUpdate: k,
|
|
185
185
|
invalid: p.value,
|
|
186
186
|
invalidLabel: y.value,
|
|
187
|
-
formData:
|
|
188
|
-
formErrors:
|
|
187
|
+
formData: o == null ? void 0 : o.modelValue.value,
|
|
188
|
+
formErrors: o == null ? void 0 : o.errors.value,
|
|
189
189
|
errors: v.value
|
|
190
|
-
})) ??
|
|
190
|
+
})) ?? u.defalut;
|
|
191
191
|
}
|
|
192
192
|
};
|
|
193
193
|
if (!a.lazyLoad) {
|
|
@@ -223,7 +223,7 @@ const te = (e, r, t, a = {}) => w({
|
|
|
223
223
|
`[form-vue warn]: ${c} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`
|
|
224
224
|
);
|
|
225
225
|
}
|
|
226
|
-
return
|
|
226
|
+
return q(async () => {
|
|
227
227
|
switch (a.sideEffects && await Promise.resolve(a.sideEffects(n.type)), n.type) {
|
|
228
228
|
case l.textarea:
|
|
229
229
|
return import("@volverjs/ui-vue/vv-textarea");
|
|
@@ -246,44 +246,44 @@ const te = (e, r, t, a = {}) => w({
|
|
|
246
246
|
return this.is ? O(this.is, this.hasProps, this.$slots) : this.type === l.custom ? O(this.component, null, this.$slots) : O(this.component, this.hasProps, this.$slots);
|
|
247
247
|
}
|
|
248
248
|
});
|
|
249
|
-
var
|
|
250
|
-
return
|
|
249
|
+
var ae = function(r) {
|
|
250
|
+
return ue(r) && !oe(r);
|
|
251
251
|
};
|
|
252
|
-
function
|
|
252
|
+
function ue(e) {
|
|
253
253
|
return !!e && typeof e == "object";
|
|
254
254
|
}
|
|
255
255
|
function oe(e) {
|
|
256
256
|
var r = Object.prototype.toString.call(e);
|
|
257
|
-
return r === "[object RegExp]" || r === "[object Date]" ||
|
|
258
|
-
}
|
|
259
|
-
var ue = typeof Symbol == "function" && Symbol.for, le = ue ? Symbol.for("react.element") : 60103;
|
|
260
|
-
function ie(e) {
|
|
261
|
-
return e.$$typeof === le;
|
|
257
|
+
return r === "[object RegExp]" || r === "[object Date]" || se(e);
|
|
262
258
|
}
|
|
259
|
+
var le = typeof Symbol == "function" && Symbol.for, ie = le ? Symbol.for("react.element") : 60103;
|
|
263
260
|
function se(e) {
|
|
261
|
+
return e.$$typeof === ie;
|
|
262
|
+
}
|
|
263
|
+
function ce(e) {
|
|
264
264
|
return Array.isArray(e) ? [] : {};
|
|
265
265
|
}
|
|
266
266
|
function j(e, r) {
|
|
267
|
-
return r.clone !== !1 && r.isMergeableObject(e) ? g(
|
|
267
|
+
return r.clone !== !1 && r.isMergeableObject(e) ? g(ce(e), e, r) : e;
|
|
268
268
|
}
|
|
269
|
-
function
|
|
269
|
+
function fe(e, r, t) {
|
|
270
270
|
return e.concat(r).map(function(a) {
|
|
271
271
|
return j(a, t);
|
|
272
272
|
});
|
|
273
273
|
}
|
|
274
|
-
function
|
|
274
|
+
function de(e, r) {
|
|
275
275
|
if (!r.customMerge)
|
|
276
276
|
return g;
|
|
277
277
|
var t = r.customMerge(e);
|
|
278
278
|
return typeof t == "function" ? t : g;
|
|
279
279
|
}
|
|
280
|
-
function
|
|
280
|
+
function me(e) {
|
|
281
281
|
return Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(e).filter(function(r) {
|
|
282
282
|
return Object.propertyIsEnumerable.call(e, r);
|
|
283
283
|
}) : [];
|
|
284
284
|
}
|
|
285
285
|
function C(e) {
|
|
286
|
-
return Object.keys(e).concat(
|
|
286
|
+
return Object.keys(e).concat(me(e));
|
|
287
287
|
}
|
|
288
288
|
function B(e, r) {
|
|
289
289
|
try {
|
|
@@ -292,21 +292,21 @@ function B(e, r) {
|
|
|
292
292
|
return !1;
|
|
293
293
|
}
|
|
294
294
|
}
|
|
295
|
-
function
|
|
295
|
+
function ve(e, r) {
|
|
296
296
|
return B(e, r) && !(Object.hasOwnProperty.call(e, r) && Object.propertyIsEnumerable.call(e, r));
|
|
297
297
|
}
|
|
298
|
-
function
|
|
298
|
+
function be(e, r, t) {
|
|
299
299
|
var a = {};
|
|
300
300
|
return t.isMergeableObject(e) && C(e).forEach(function(n) {
|
|
301
301
|
a[n] = j(e[n], t);
|
|
302
302
|
}), C(r).forEach(function(n) {
|
|
303
|
-
|
|
303
|
+
ve(e, n) || (B(e, n) && t.isMergeableObject(r[n]) ? a[n] = de(n, t)(e[n], r[n], t) : a[n] = j(r[n], t));
|
|
304
304
|
}), a;
|
|
305
305
|
}
|
|
306
306
|
function g(e, r, t) {
|
|
307
|
-
t = t || {}, t.arrayMerge = t.arrayMerge ||
|
|
308
|
-
var a = Array.isArray(r), n = Array.isArray(e),
|
|
309
|
-
return
|
|
307
|
+
t = t || {}, t.arrayMerge = t.arrayMerge || fe, t.isMergeableObject = t.isMergeableObject || ae, t.cloneUnlessOtherwiseSpecified = j;
|
|
308
|
+
var a = Array.isArray(r), n = Array.isArray(e), u = a === n;
|
|
309
|
+
return u ? a ? t.arrayMerge(e, r, t) : be(e, r, t) : j(r, t);
|
|
310
310
|
}
|
|
311
311
|
g.all = function(r, t) {
|
|
312
312
|
if (!Array.isArray(r))
|
|
@@ -315,13 +315,16 @@ g.all = function(r, t) {
|
|
|
315
315
|
return g(a, n, t);
|
|
316
316
|
}, {});
|
|
317
317
|
};
|
|
318
|
-
var
|
|
319
|
-
const J = (e, r = {}) =>
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
),
|
|
318
|
+
var he = g, ye = he;
|
|
319
|
+
const J = (e, r = {}) => {
|
|
320
|
+
const t = e instanceof P ? e.innerType().shape : e.shape;
|
|
321
|
+
return ye(
|
|
322
|
+
Object.fromEntries(
|
|
323
|
+
Object.entries(t).map(([a, n]) => n instanceof T ? [a, n._def.defaultValue()] : n instanceof F ? [a, J(n)] : [a, void 0])
|
|
324
|
+
),
|
|
325
|
+
r
|
|
326
|
+
);
|
|
327
|
+
}, pe = (e, r, t) => w({
|
|
325
328
|
name: "FormComponent",
|
|
326
329
|
props: {
|
|
327
330
|
modelValue: {
|
|
@@ -332,7 +335,7 @@ const J = (e, r = {}) => he(
|
|
|
332
335
|
emits: ["invalid", "valid", "submit", "update:modelValue"],
|
|
333
336
|
expose: ["submit", "errors", "status"],
|
|
334
337
|
setup(a, { emit: n }) {
|
|
335
|
-
const
|
|
338
|
+
const u = x(
|
|
336
339
|
J(e, a.modelValue)
|
|
337
340
|
);
|
|
338
341
|
V(
|
|
@@ -340,26 +343,26 @@ const J = (e, r = {}) => he(
|
|
|
340
343
|
(d) => {
|
|
341
344
|
if (d) {
|
|
342
345
|
const m = Y(d) ? H(d) : d;
|
|
343
|
-
|
|
346
|
+
u.value = typeof (m == null ? void 0 : m.clone) == "function" ? m.clone() : JSON.parse(JSON.stringify(m));
|
|
344
347
|
}
|
|
345
348
|
},
|
|
346
349
|
{ deep: !0 }
|
|
347
350
|
), X(
|
|
348
|
-
|
|
351
|
+
u,
|
|
349
352
|
(d) => {
|
|
350
|
-
i.value && f(), (!d || !a.modelValue || JSON.stringify(d) !== JSON.stringify(a.modelValue)) && n("update:modelValue", d);
|
|
353
|
+
(i.value || t != null && t.continuosValidation) && f(), (!d || !a.modelValue || JSON.stringify(d) !== JSON.stringify(a.modelValue)) && n("update:modelValue", d);
|
|
351
354
|
},
|
|
352
355
|
{ deep: !0, throttle: (t == null ? void 0 : t.updateThrottle) ?? 500 }
|
|
353
356
|
);
|
|
354
|
-
const i = x(), s = x(), f = (d =
|
|
357
|
+
const i = x(), s = x(), f = (d = u.value) => {
|
|
355
358
|
const m = e.safeParse(d);
|
|
356
|
-
return m.success ? (i.value = void 0, s.value = "valid",
|
|
357
|
-
},
|
|
359
|
+
return m.success ? (i.value = void 0, s.value = "valid", u.value = m.data, n("valid", m.data), !0) : (i.value = m.error.format(), s.value = "invalid", n("invalid", i.value), !1);
|
|
360
|
+
}, o = () => f() ? (n("submit", u.value), !0) : !1;
|
|
358
361
|
return $(r, {
|
|
359
|
-
modelValue:
|
|
360
|
-
submit:
|
|
361
|
-
errors:
|
|
362
|
-
}), { submit:
|
|
362
|
+
modelValue: u,
|
|
363
|
+
submit: o,
|
|
364
|
+
errors: M(i)
|
|
365
|
+
}), { submit: o };
|
|
363
366
|
},
|
|
364
367
|
render() {
|
|
365
368
|
return O(
|
|
@@ -370,7 +373,7 @@ const J = (e, r = {}) => he(
|
|
|
370
373
|
this.$slots
|
|
371
374
|
);
|
|
372
375
|
}
|
|
373
|
-
}),
|
|
376
|
+
}), Ve = (e, r) => w({
|
|
374
377
|
name: "WrapperComponent",
|
|
375
378
|
props: {
|
|
376
379
|
name: {
|
|
@@ -385,48 +388,48 @@ const J = (e, r = {}) => he(
|
|
|
385
388
|
emits: ["invalid", "valid"],
|
|
386
389
|
expose: ["fields", "invalid"],
|
|
387
390
|
setup(t, { emit: a }) {
|
|
388
|
-
const n = S(e),
|
|
391
|
+
const n = S(e), u = S(r, void 0), i = x(/* @__PURE__ */ new Set()), s = x(/* @__PURE__ */ new Map()), { name: f } = G(t);
|
|
389
392
|
$(r, {
|
|
390
|
-
name:
|
|
393
|
+
name: M(f),
|
|
391
394
|
errors: s,
|
|
392
395
|
fields: i
|
|
393
396
|
}), V(
|
|
394
397
|
i,
|
|
395
398
|
(d) => {
|
|
396
|
-
|
|
397
|
-
|
|
399
|
+
u != null && u.fields && d.forEach((m) => {
|
|
400
|
+
u == null || u.fields.value.add(m);
|
|
398
401
|
});
|
|
399
402
|
},
|
|
400
403
|
{ deep: !0 }
|
|
401
404
|
), V(
|
|
402
405
|
() => new Map(s.value),
|
|
403
406
|
(d, m) => {
|
|
404
|
-
|
|
405
|
-
|
|
407
|
+
u != null && u.errors && (Array.from(m.keys()).forEach((v) => {
|
|
408
|
+
u.errors.value.delete(v);
|
|
406
409
|
}), Array.from(d.keys()).forEach((v) => {
|
|
407
410
|
const y = d.get(v);
|
|
408
|
-
y &&
|
|
411
|
+
y && u.errors.value.set(v, y);
|
|
409
412
|
}));
|
|
410
413
|
},
|
|
411
414
|
{ deep: !0 }
|
|
412
415
|
);
|
|
413
|
-
const
|
|
414
|
-
return V(
|
|
415
|
-
|
|
416
|
-
}), { formProvided: n, invalid:
|
|
416
|
+
const o = h(() => n != null && n.errors.value ? s.value.size > 0 : !1);
|
|
417
|
+
return V(o, () => {
|
|
418
|
+
o.value ? a("invalid") : a("valid");
|
|
419
|
+
}), { formProvided: n, invalid: o, fields: i, errors: s };
|
|
417
420
|
},
|
|
418
421
|
render() {
|
|
419
|
-
var t, a, n,
|
|
422
|
+
var t, a, n, u, i, s, f, o;
|
|
420
423
|
return this.tag ? O(
|
|
421
424
|
this.tag,
|
|
422
425
|
null,
|
|
423
|
-
((
|
|
426
|
+
((u = (n = this.$slots).default) == null ? void 0 : u.call(n, {
|
|
424
427
|
invalid: this.invalid,
|
|
425
428
|
formData: (t = this.formProvided) == null ? void 0 : t.modelValue,
|
|
426
429
|
errors: (a = this.formProvided) == null ? void 0 : a.errors,
|
|
427
430
|
fieldsErrors: this.errors
|
|
428
431
|
})) ?? this.$slots.defalut
|
|
429
|
-
) : ((
|
|
432
|
+
) : ((o = (f = this.$slots).default) == null ? void 0 : o.call(f, {
|
|
430
433
|
invalid: this.invalid,
|
|
431
434
|
formData: (i = this.formProvided) == null ? void 0 : i.modelValue,
|
|
432
435
|
errors: (s = this.formProvided) == null ? void 0 : s.errors,
|
|
@@ -434,24 +437,24 @@ const J = (e, r = {}) => he(
|
|
|
434
437
|
})) ?? this.$slots.defalut;
|
|
435
438
|
}
|
|
436
439
|
}), K = (e, r = {}) => {
|
|
437
|
-
const t = Symbol(), a = Symbol(), n = Symbol(),
|
|
440
|
+
const t = Symbol(), a = Symbol(), n = Symbol(), u = pe(e, t, r), i = Ve(
|
|
438
441
|
t,
|
|
439
442
|
a
|
|
440
|
-
), s =
|
|
443
|
+
), s = ne(
|
|
441
444
|
t,
|
|
442
445
|
a,
|
|
443
446
|
n,
|
|
444
447
|
r
|
|
445
448
|
);
|
|
446
449
|
return {
|
|
447
|
-
VvForm:
|
|
450
|
+
VvForm: u,
|
|
448
451
|
VvFormWrapper: i,
|
|
449
452
|
VvFormField: s,
|
|
450
453
|
formInjectionKey: t,
|
|
451
454
|
formWrapperInjectionKey: a,
|
|
452
455
|
formFieldInjectionKey: n
|
|
453
456
|
};
|
|
454
|
-
}, z = Symbol(),
|
|
457
|
+
}, z = Symbol(), je = (e) => {
|
|
455
458
|
let r = {};
|
|
456
459
|
return e.schema && (r = K(e.schema, e)), {
|
|
457
460
|
...r,
|
|
@@ -459,15 +462,15 @@ const J = (e, r = {}) => he(
|
|
|
459
462
|
t.provide(z, e), a && (t.config.globalProperties.$vvForm = e, r != null && r.VvForm && t.component("VvForm", r.VvForm), r != null && r.VvFormWrapper && t.component("VvFormWrapper", r.VvFormWrapper), r != null && r.VvFormField && t.component("VvFormField", r.VvFormField));
|
|
460
463
|
}
|
|
461
464
|
};
|
|
462
|
-
},
|
|
465
|
+
}, Ee = (e, r = {}) => {
|
|
463
466
|
const t = { ...S(z, {}), ...r };
|
|
464
467
|
return K(e, t);
|
|
465
468
|
};
|
|
466
469
|
export {
|
|
467
470
|
l as FormFieldType,
|
|
468
|
-
|
|
471
|
+
je as createForm,
|
|
469
472
|
J as defaultObjectBySchema,
|
|
470
473
|
K as formFactory,
|
|
471
474
|
z as pluginInjectionKey,
|
|
472
|
-
|
|
475
|
+
Ee as useForm
|
|
473
476
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(b,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],n):(b=typeof globalThis<"u"?globalThis:b||self,n(b["@volverjs/form-vue"]={},b.Vue,b.VueUseCore,b.zod))})(this,function(b,n,D,M){"use strict";function S(e){return Array.isArray(e)}function U(e){return typeof e<"u"}function A(e){return e===null}function E(e){return typeof e=="object"}function C(e){return typeof e=="string"}function O(e){return typeof e>"u"}const W=/^[0-9]+$/,B=["__proto__","prototype","constructor"];function $(e,r,t){const o=U(t)?t:void 0;if(!E(e)||!C(r))return o;const a=I(r);if(a.length!==0){for(const l of a){if(l==="*")continue;const c=function(u){return u.map(d=>O(d)||A(d)?d:S(d)?c(d):d[l])};if(S(e)&&!W.test(l)?e=c(e):e=e[l],O(e)||A(e))break}return O(e)?o:e}}function k(e,r,t){if(!E(e)||!C(r))return;const o=I(r);if(o.length===0)return;const a=o.length;for(let l=0;l<a;l++){const c=o[l];if(l===a-1){e[c]=t;return}if(c==="*"&&S(e)){const u=o.slice(l+1).join(".");for(const d of e)k(d,u,t);return}O(e[c])&&(e[c]={}),e=e[c]}}function I(e){const r=e.split(/[.]|(?:\[(\d|\*)\])/).filter(t=>!!t);return r.some(t=>B.indexOf(t)!==-1)?[]:r}var s=(e=>(e.text="text",e.number="number",e.email="email",e.password="password",e.tel="tel",e.url="url",e.search="search",e.date="date",e.time="time",e.datetimeLocal="datetimeLocal",e.month="month",e.week="week",e.color="color",e.select="select",e.checkbox="checkbox",e.radio="radio",e.textarea="textarea",e.radioGroup="radioGroup",e.checkboxGroup="checkboxGroup",e.combobox="combobox",e.custom="custom",e))(s||{});const q=(e,r,t,o={})=>n.defineComponent({name:"FieldComponent",props:{type:{type:String,validator:a=>Object.values(s).includes(a),default:s.custom},is:{type:[Object,String],default:void 0},name:{type:[String,Number,Boolean,Symbol],required:!0},props:{type:[Object,Function],default:()=>({})},showValid:{type:Boolean,default:!1},defaultValue:{type:[String,Number,Boolean,Array,Object],default:void 0}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(a,{slots:l,emit:c}){const u=n.computed({get(){if(i!=null&&i.modelValue)return $(Object(i.modelValue.value),String(a.name))},set(f){i!=null&&i.modelValue&&(k(Object(i.modelValue.value),String(a.name),f),c("update:modelValue",{newValue:u.value,formData:i==null?void 0:i.modelValue}))}});n.onMounted(()=>{u.value===void 0&&a.defaultValue!==void 0&&(u.value=a.defaultValue)});const d=n.inject(r,void 0);d&&d.fields.value.add(a.name);const i=n.inject(e),{props:m,name:h}=n.toRefs(a),p=n.computed(()=>{if(i!=null&&i.errors.value)return $(i.errors.value,String(a.name))}),v=n.computed(()=>{var f;return(f=p.value)==null?void 0:f._errors}),V=n.computed(()=>p.value!==void 0);n.watch(V,()=>{V.value?(c("invalid",v.value),d&&d.errors.value.set(a.name,{_errors:v.value})):(c("valid",u.value),d&&d.errors.value.delete(a.name))}),n.watch(()=>i==null?void 0:i.modelValue,()=>{c("update:formData",i==null?void 0:i.modelValue)},{deep:!0});const G=f=>{u.value=f},L=n.computed(()=>typeof m.value=="function"?m.value(i==null?void 0:i.modelValue):m.value),le=n.computed(()=>({...L.value,name:L.value.name??a.name,invalid:V.value,valid:a.showValid?Boolean(!V.value&&u.value):void 0,type:(f=>{if([s.text,s.number,s.email,s.password,s.tel,s.url,s.search,s.date,s.time,s.datetimeLocal,s.month,s.week,s.color].includes(f))return f})(a.type),invalidLabel:v.value,modelValue:u.value,errors:a.is?p.value:void 0,"onUpdate:modelValue":G}));return n.provide(t,{name:n.readonly(h),errors:n.readonly(p)}),{component:n.computed(()=>{if(a.type===s.custom)return{render(){var f;return((f=l.default)==null?void 0:f.call(l,{modelValue:u.value,onUpdate:G,invalid:V.value,invalidLabel:v.value,formData:i==null?void 0:i.modelValue.value,formErrors:i==null?void 0:i.errors.value,errors:p.value}))??l.defalut}};if(!o.lazyLoad){let f;switch(a.type){case s.select:f=n.resolveComponent("VvSelect");break;case s.checkbox:f=n.resolveComponent("VvCheckbox");break;case s.radio:f=n.resolveComponent("VvRadio");break;case s.textarea:f=n.resolveComponent("VvTextarea");break;case s.radioGroup:f=n.resolveComponent("VvRadioGroup");break;case s.checkboxGroup:f=n.resolveComponent("VvCheckboxGroup");break;case s.combobox:f=n.resolveComponent("VvCombobox");break;default:f=n.resolveComponent("VvInputText")}if(typeof f!="string")return f;console.warn(`[form-vue warn]: ${f} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return n.defineAsyncComponent(async()=>{switch(o.sideEffects&&await Promise.resolve(o.sideEffects(a.type)),a.type){case s.textarea:return import("@volverjs/ui-vue/vv-textarea");case s.radio:return import("@volverjs/ui-vue/vv-radio");case s.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case s.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case s.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case s.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:le,invalid:V}},render(){return this.is?n.h(this.is,this.hasProps,this.$slots):this.type===s.custom?n.h(this.component,null,this.$slots):n.h(this.component,this.hasProps,this.$slots)}});var z=function(r){return K(r)&&!J(r)};function K(e){return!!e&&typeof e=="object"}function J(e){var r=Object.prototype.toString.call(e);return r==="[object RegExp]"||r==="[object Date]"||Y(e)}var R=typeof Symbol=="function"&&Symbol.for,Z=R?Symbol.for("react.element"):60103;function Y(e){return e.$$typeof===Z}function H(e){return Array.isArray(e)?[]:{}}function g(e,r){return r.clone!==!1&&r.isMergeableObject(e)?y(H(e),e,r):e}function Q(e,r,t){return e.concat(r).map(function(o){return g(o,t)})}function X(e,r){if(!r.customMerge)return y;var t=r.customMerge(e);return typeof t=="function"?t:y}function P(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(function(r){return Object.propertyIsEnumerable.call(e,r)}):[]}function N(e){return Object.keys(e).concat(P(e))}function _(e,r){try{return r in e}catch{return!1}}function F(e,r){return _(e,r)&&!(Object.hasOwnProperty.call(e,r)&&Object.propertyIsEnumerable.call(e,r))}function T(e,r,t){var o={};return t.isMergeableObject(e)&&N(e).forEach(function(a){o[a]=g(e[a],t)}),N(r).forEach(function(a){F(e,a)||(_(e,a)&&t.isMergeableObject(r[a])?o[a]=X(a,t)(e[a],r[a],t):o[a]=g(r[a],t))}),o}function y(e,r,t){t=t||{},t.arrayMerge=t.arrayMerge||Q,t.isMergeableObject=t.isMergeableObject||z,t.cloneUnlessOtherwiseSpecified=g;var o=Array.isArray(r),a=Array.isArray(e),l=o===a;return l?o?t.arrayMerge(e,r,t):T(e,r,t):g(r,t)}y.all=function(r,t){if(!Array.isArray(r))throw new Error("first argument should be an array");return r.reduce(function(o,a){return y(o,a,t)},{})};var ee=y,re=ee;const j=(e,r={})=>re(Object.fromEntries(Object.entries(e.shape).map(([t,o])=>o instanceof M.ZodDefault?[t,o._def.defaultValue()]:o instanceof M.ZodObject?[t,j(o)]:[t,void 0])),r),te=(e,r,t)=>n.defineComponent({name:"FormComponent",props:{modelValue:{type:Object,default:()=>({})}},emits:["invalid","valid","submit","update:modelValue"],expose:["submit","errors","status"],setup(o,{emit:a}){const l=n.ref(j(e,o.modelValue));n.watch(()=>o.modelValue,m=>{if(m){const h=n.isProxy(m)?n.toRaw(m):m;l.value=typeof(h==null?void 0:h.clone)=="function"?h.clone():JSON.parse(JSON.stringify(h))}},{deep:!0}),D.watchThrottled(l,m=>{c.value&&d(),(!m||!o.modelValue||JSON.stringify(m)!==JSON.stringify(o.modelValue))&&a("update:modelValue",m)},{deep:!0,throttle:(t==null?void 0:t.updateThrottle)??500});const c=n.ref(),u=n.ref(),d=(m=l.value)=>{const h=e.safeParse(m);return h.success?(c.value=void 0,u.value="valid",l.value=h.data,a("valid",h.data),!0):(c.value=h.error.format(),u.value="invalid",a("invalid",c.value),!1)},i=()=>d()?(a("submit",l.value),!0):!1;return n.provide(r,{modelValue:l,submit:i,errors:n.readonly(c)}),{submit:i}},render(){return n.h("form",{onSubmit:n.withModifiers(this.submit,["prevent"])},this.$slots)}}),ne=(e,r)=>n.defineComponent({name:"WrapperComponent",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(t,{emit:o}){const a=n.inject(e),l=n.inject(r,void 0),c=n.ref(new Set),u=n.ref(new Map),{name:d}=n.toRefs(t);n.provide(r,{name:n.readonly(d),errors:u,fields:c}),n.watch(c,m=>{l!=null&&l.fields&&m.forEach(h=>{l==null||l.fields.value.add(h)})},{deep:!0}),n.watch(()=>new Map(u.value),(m,h)=>{l!=null&&l.errors&&(Array.from(h.keys()).forEach(p=>{l.errors.value.delete(p)}),Array.from(m.keys()).forEach(p=>{const v=m.get(p);v&&l.errors.value.set(p,v)}))},{deep:!0});const i=n.computed(()=>a!=null&&a.errors.value?u.value.size>0:!1);return n.watch(i,()=>{i.value?o("invalid"):o("valid")}),{formProvided:a,invalid:i,fields:c,errors:u}},render(){var t,o,a,l,c,u,d,i;return this.tag?n.h(this.tag,null,((l=(a=this.$slots).default)==null?void 0:l.call(a,{invalid:this.invalid,formData:(t=this.formProvided)==null?void 0:t.modelValue,errors:(o=this.formProvided)==null?void 0:o.errors,fieldsErrors:this.errors}))??this.$slots.defalut):((i=(d=this.$slots).default)==null?void 0:i.call(d,{invalid:this.invalid,formData:(c=this.formProvided)==null?void 0:c.modelValue,errors:(u=this.formProvided)==null?void 0:u.errors,fieldsErrors:this.errors}))??this.$slots.defalut}}),x=(e,r={})=>{const t=Symbol(),o=Symbol(),a=Symbol(),l=te(e,t,r),c=ne(t,o),u=q(t,o,a,r);return{VvForm:l,VvFormWrapper:c,VvFormField:u,formInjectionKey:t,formWrapperInjectionKey:o,formFieldInjectionKey:a}},w=Symbol(),ae=e=>{let r={};return e.schema&&(r=x(e.schema,e)),{...r,install(t,{global:o=!1}={}){t.provide(w,e),o&&(t.config.globalProperties.$vvForm=e,r!=null&&r.VvForm&&t.component("VvForm",r.VvForm),r!=null&&r.VvFormWrapper&&t.component("VvFormWrapper",r.VvFormWrapper),r!=null&&r.VvFormField&&t.component("VvFormField",r.VvFormField))}}},oe=(e,r={})=>{const t={...n.inject(w,{}),...r};return x(e,t)};b.FormFieldType=s,b.createForm=ae,b.defaultObjectBySchema=j,b.formFactory=x,b.pluginInjectionKey=w,b.useForm=oe,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(b,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("vue"),require("@vueuse/core"),require("zod")):typeof define=="function"&&define.amd?define(["exports","vue","@vueuse/core","zod"],a):(b=typeof globalThis<"u"?globalThis:b||self,a(b["@volverjs/form-vue"]={},b.Vue,b.VueUseCore,b.zod))})(this,function(b,a,D,S){"use strict";function j(e){return Array.isArray(e)}function U(e){return typeof e<"u"}function E(e){return e===null}function A(e){return typeof e=="object"}function C(e){return typeof e=="string"}function O(e){return typeof e>"u"}const W=/^[0-9]+$/,B=["__proto__","prototype","constructor"];function $(e,r,t){const o=U(t)?t:void 0;if(!A(e)||!C(r))return o;const n=I(r);if(n.length!==0){for(const l of n){if(l==="*")continue;const c=function(u){return u.map(d=>O(d)||E(d)?d:j(d)?c(d):d[l])};if(j(e)&&!W.test(l)?e=c(e):e=e[l],O(e)||E(e))break}return O(e)?o:e}}function k(e,r,t){if(!A(e)||!C(r))return;const o=I(r);if(o.length===0)return;const n=o.length;for(let l=0;l<n;l++){const c=o[l];if(l===n-1){e[c]=t;return}if(c==="*"&&j(e)){const u=o.slice(l+1).join(".");for(const d of e)k(d,u,t);return}O(e[c])&&(e[c]={}),e=e[c]}}function I(e){const r=e.split(/[.]|(?:\[(\d|\*)\])/).filter(t=>!!t);return r.some(t=>B.indexOf(t)!==-1)?[]:r}var s=(e=>(e.text="text",e.number="number",e.email="email",e.password="password",e.tel="tel",e.url="url",e.search="search",e.date="date",e.time="time",e.datetimeLocal="datetimeLocal",e.month="month",e.week="week",e.color="color",e.select="select",e.checkbox="checkbox",e.radio="radio",e.textarea="textarea",e.radioGroup="radioGroup",e.checkboxGroup="checkboxGroup",e.combobox="combobox",e.custom="custom",e))(s||{});const q=(e,r,t,o={})=>a.defineComponent({name:"FieldComponent",props:{type:{type:String,validator:n=>Object.values(s).includes(n),default:s.custom},is:{type:[Object,String],default:void 0},name:{type:[String,Number,Boolean,Symbol],required:!0},props:{type:[Object,Function],default:()=>({})},showValid:{type:Boolean,default:!1},defaultValue:{type:[String,Number,Boolean,Array,Object],default:void 0}},emits:["invalid","valid","update:formData","update:modelValue"],expose:["invalid","invalidLabel","errors"],setup(n,{slots:l,emit:c}){const u=a.computed({get(){if(i!=null&&i.modelValue)return $(Object(i.modelValue.value),String(n.name))},set(f){i!=null&&i.modelValue&&(k(Object(i.modelValue.value),String(n.name),f),c("update:modelValue",{newValue:u.value,formData:i==null?void 0:i.modelValue}))}});a.onMounted(()=>{u.value===void 0&&n.defaultValue!==void 0&&(u.value=n.defaultValue)});const d=a.inject(r,void 0);d&&d.fields.value.add(n.name);const i=a.inject(e),{props:m,name:h}=a.toRefs(n),p=a.computed(()=>{if(i!=null&&i.errors.value)return $(i.errors.value,String(n.name))}),y=a.computed(()=>{var f;return(f=p.value)==null?void 0:f._errors}),V=a.computed(()=>p.value!==void 0);a.watch(V,()=>{V.value?(c("invalid",y.value),d&&d.errors.value.set(n.name,{_errors:y.value})):(c("valid",u.value),d&&d.errors.value.delete(n.name))}),a.watch(()=>i==null?void 0:i.modelValue,()=>{c("update:formData",i==null?void 0:i.modelValue)},{deep:!0});const G=f=>{u.value=f},L=a.computed(()=>typeof m.value=="function"?m.value(i==null?void 0:i.modelValue):m.value),le=a.computed(()=>({...L.value,name:L.value.name??n.name,invalid:V.value,valid:n.showValid?Boolean(!V.value&&u.value):void 0,type:(f=>{if([s.text,s.number,s.email,s.password,s.tel,s.url,s.search,s.date,s.time,s.datetimeLocal,s.month,s.week,s.color].includes(f))return f})(n.type),invalidLabel:y.value,modelValue:u.value,errors:n.is?p.value:void 0,"onUpdate:modelValue":G}));return a.provide(t,{name:a.readonly(h),errors:a.readonly(p)}),{component:a.computed(()=>{if(n.type===s.custom)return{render(){var f;return((f=l.default)==null?void 0:f.call(l,{modelValue:u.value,onUpdate:G,invalid:V.value,invalidLabel:y.value,formData:i==null?void 0:i.modelValue.value,formErrors:i==null?void 0:i.errors.value,errors:p.value}))??l.defalut}};if(!o.lazyLoad){let f;switch(n.type){case s.select:f=a.resolveComponent("VvSelect");break;case s.checkbox:f=a.resolveComponent("VvCheckbox");break;case s.radio:f=a.resolveComponent("VvRadio");break;case s.textarea:f=a.resolveComponent("VvTextarea");break;case s.radioGroup:f=a.resolveComponent("VvRadioGroup");break;case s.checkboxGroup:f=a.resolveComponent("VvCheckboxGroup");break;case s.combobox:f=a.resolveComponent("VvCombobox");break;default:f=a.resolveComponent("VvInputText")}if(typeof f!="string")return f;console.warn(`[form-vue warn]: ${f} not found, the component will be loaded asynchronously. To avoid this warning, please set "lazyLoad" option.`)}return a.defineAsyncComponent(async()=>{switch(o.sideEffects&&await Promise.resolve(o.sideEffects(n.type)),n.type){case s.textarea:return import("@volverjs/ui-vue/vv-textarea");case s.radio:return import("@volverjs/ui-vue/vv-radio");case s.radioGroup:return import("@volverjs/ui-vue/vv-radio-group");case s.checkbox:return import("@volverjs/ui-vue/vv-checkbox");case s.checkboxGroup:return import("@volverjs/ui-vue/vv-checkbox-group");case s.combobox:return import("@volverjs/ui-vue/vv-combobox")}return import("@volverjs/ui-vue/vv-input-text")})}),hasProps:le,invalid:V}},render(){return this.is?a.h(this.is,this.hasProps,this.$slots):this.type===s.custom?a.h(this.component,null,this.$slots):a.h(this.component,this.hasProps,this.$slots)}});var K=function(r){return z(r)&&!J(r)};function z(e){return!!e&&typeof e=="object"}function J(e){var r=Object.prototype.toString.call(e);return r==="[object RegExp]"||r==="[object Date]"||Y(e)}var R=typeof Symbol=="function"&&Symbol.for,Z=R?Symbol.for("react.element"):60103;function Y(e){return e.$$typeof===Z}function H(e){return Array.isArray(e)?[]:{}}function g(e,r){return r.clone!==!1&&r.isMergeableObject(e)?v(H(e),e,r):e}function Q(e,r,t){return e.concat(r).map(function(o){return g(o,t)})}function X(e,r){if(!r.customMerge)return v;var t=r.customMerge(e);return typeof t=="function"?t:v}function P(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(function(r){return Object.propertyIsEnumerable.call(e,r)}):[]}function N(e){return Object.keys(e).concat(P(e))}function _(e,r){try{return r in e}catch{return!1}}function T(e,r){return _(e,r)&&!(Object.hasOwnProperty.call(e,r)&&Object.propertyIsEnumerable.call(e,r))}function F(e,r,t){var o={};return t.isMergeableObject(e)&&N(e).forEach(function(n){o[n]=g(e[n],t)}),N(r).forEach(function(n){T(e,n)||(_(e,n)&&t.isMergeableObject(r[n])?o[n]=X(n,t)(e[n],r[n],t):o[n]=g(r[n],t))}),o}function v(e,r,t){t=t||{},t.arrayMerge=t.arrayMerge||Q,t.isMergeableObject=t.isMergeableObject||K,t.cloneUnlessOtherwiseSpecified=g;var o=Array.isArray(r),n=Array.isArray(e),l=o===n;return l?o?t.arrayMerge(e,r,t):F(e,r,t):g(r,t)}v.all=function(r,t){if(!Array.isArray(r))throw new Error("first argument should be an array");return r.reduce(function(o,n){return v(o,n,t)},{})};var ee=v,re=ee;const x=(e,r={})=>{const t=e instanceof S.ZodEffects?e.innerType().shape:e.shape;return re(Object.fromEntries(Object.entries(t).map(([o,n])=>n instanceof S.ZodDefault?[o,n._def.defaultValue()]:n instanceof S.ZodObject?[o,x(n)]:[o,void 0])),r)},te=(e,r,t)=>a.defineComponent({name:"FormComponent",props:{modelValue:{type:Object,default:()=>({})}},emits:["invalid","valid","submit","update:modelValue"],expose:["submit","errors","status"],setup(o,{emit:n}){const l=a.ref(x(e,o.modelValue));a.watch(()=>o.modelValue,m=>{if(m){const h=a.isProxy(m)?a.toRaw(m):m;l.value=typeof(h==null?void 0:h.clone)=="function"?h.clone():JSON.parse(JSON.stringify(h))}},{deep:!0}),D.watchThrottled(l,m=>{(c.value||t!=null&&t.continuosValidation)&&d(),(!m||!o.modelValue||JSON.stringify(m)!==JSON.stringify(o.modelValue))&&n("update:modelValue",m)},{deep:!0,throttle:(t==null?void 0:t.updateThrottle)??500});const c=a.ref(),u=a.ref(),d=(m=l.value)=>{const h=e.safeParse(m);return h.success?(c.value=void 0,u.value="valid",l.value=h.data,n("valid",h.data),!0):(c.value=h.error.format(),u.value="invalid",n("invalid",c.value),!1)},i=()=>d()?(n("submit",l.value),!0):!1;return a.provide(r,{modelValue:l,submit:i,errors:a.readonly(c)}),{submit:i}},render(){return a.h("form",{onSubmit:a.withModifiers(this.submit,["prevent"])},this.$slots)}}),ne=(e,r)=>a.defineComponent({name:"WrapperComponent",props:{name:{type:String,required:!0},tag:{type:String,default:void 0}},emits:["invalid","valid"],expose:["fields","invalid"],setup(t,{emit:o}){const n=a.inject(e),l=a.inject(r,void 0),c=a.ref(new Set),u=a.ref(new Map),{name:d}=a.toRefs(t);a.provide(r,{name:a.readonly(d),errors:u,fields:c}),a.watch(c,m=>{l!=null&&l.fields&&m.forEach(h=>{l==null||l.fields.value.add(h)})},{deep:!0}),a.watch(()=>new Map(u.value),(m,h)=>{l!=null&&l.errors&&(Array.from(h.keys()).forEach(p=>{l.errors.value.delete(p)}),Array.from(m.keys()).forEach(p=>{const y=m.get(p);y&&l.errors.value.set(p,y)}))},{deep:!0});const i=a.computed(()=>n!=null&&n.errors.value?u.value.size>0:!1);return a.watch(i,()=>{i.value?o("invalid"):o("valid")}),{formProvided:n,invalid:i,fields:c,errors:u}},render(){var t,o,n,l,c,u,d,i;return this.tag?a.h(this.tag,null,((l=(n=this.$slots).default)==null?void 0:l.call(n,{invalid:this.invalid,formData:(t=this.formProvided)==null?void 0:t.modelValue,errors:(o=this.formProvided)==null?void 0:o.errors,fieldsErrors:this.errors}))??this.$slots.defalut):((i=(d=this.$slots).default)==null?void 0:i.call(d,{invalid:this.invalid,formData:(c=this.formProvided)==null?void 0:c.modelValue,errors:(u=this.formProvided)==null?void 0:u.errors,fieldsErrors:this.errors}))??this.$slots.defalut}}),w=(e,r={})=>{const t=Symbol(),o=Symbol(),n=Symbol(),l=te(e,t,r),c=ne(t,o),u=q(t,o,n,r);return{VvForm:l,VvFormWrapper:c,VvFormField:u,formInjectionKey:t,formWrapperInjectionKey:o,formFieldInjectionKey:n}},M=Symbol(),ae=e=>{let r={};return e.schema&&(r=w(e.schema,e)),{...r,install(t,{global:o=!1}={}){t.provide(M,e),o&&(t.config.globalProperties.$vvForm=e,r!=null&&r.VvForm&&t.component("VvForm",r.VvForm),r!=null&&r.VvFormWrapper&&t.component("VvFormWrapper",r.VvFormWrapper),r!=null&&r.VvFormField&&t.component("VvFormField",r.VvFormField))}}},oe=(e,r={})=>{const t={...a.inject(M,{}),...r};return w(e,t)};b.FormFieldType=s,b.createForm=ae,b.defaultObjectBySchema=x,b.formFactory=w,b.pluginInjectionKey=M,b.useForm=oe,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})});
|
package/dist/types.d.ts
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import { type z } from 'zod';
|
|
2
|
-
export declare const defaultObjectBySchema: <Schema extends z.AnyZodObject
|
|
2
|
+
export declare const defaultObjectBySchema: <Schema extends z.AnyZodObject | z.ZodEffects<z.AnyZodObject, {
|
|
3
|
+
[x: string]: any;
|
|
4
|
+
}, {
|
|
5
|
+
[x: string]: any;
|
|
6
|
+
}>>(schema: Schema, original?: Partial<z.TypeOf<Schema>>) => Partial<z.TypeOf<Schema>>;
|
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.
|
|
22
|
+
"version": "0.0.3",
|
|
23
23
|
"engines": {
|
|
24
24
|
"node": ">= 16.x"
|
|
25
25
|
},
|
|
@@ -34,8 +34,6 @@
|
|
|
34
34
|
"*.d.ts"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@iconify/tools": "^2.2.6",
|
|
38
|
-
"@iconify/vue": "^4.1.0",
|
|
39
37
|
"@volverjs/ui-vue": "0.0.5-beta.1",
|
|
40
38
|
"@vueuse/core": "^9.13.0",
|
|
41
39
|
"deepmerge": "^4.3.0",
|
package/src/VvForm.ts
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
} from 'vue'
|
|
13
13
|
import { watchThrottled } from '@vueuse/core'
|
|
14
14
|
|
|
15
|
-
import type { AnyZodObject } from 'zod'
|
|
15
|
+
import type { AnyZodObject, ZodEffects } from 'zod'
|
|
16
16
|
import type { InjectedFormData } from './types'
|
|
17
17
|
import { defaultObjectBySchema } from './utils'
|
|
18
18
|
|
|
@@ -22,10 +22,11 @@ export enum FormStatus {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export const defineForm = (
|
|
25
|
-
schema: AnyZodObject
|
|
25
|
+
schema: AnyZodObject | ZodEffects<AnyZodObject>,
|
|
26
26
|
provideKey: InjectionKey<InjectedFormData>,
|
|
27
27
|
options?: {
|
|
28
28
|
updateThrottle?: number
|
|
29
|
+
continuosValidation?: boolean
|
|
29
30
|
},
|
|
30
31
|
) => {
|
|
31
32
|
return defineComponent({
|
|
@@ -61,7 +62,7 @@ export const defineForm = (
|
|
|
61
62
|
watchThrottled(
|
|
62
63
|
localModelValue,
|
|
63
64
|
(newValue) => {
|
|
64
|
-
if (errors.value) {
|
|
65
|
+
if (errors.value || options?.continuosValidation) {
|
|
65
66
|
parseModelValue()
|
|
66
67
|
}
|
|
67
68
|
if (
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type App, inject, type InjectionKey, type Plugin } from 'vue'
|
|
2
|
-
import type { AnyZodObject } from 'zod'
|
|
2
|
+
import type { AnyZodObject, ZodEffects } from 'zod'
|
|
3
3
|
import { defineFormField } from './VvFormField'
|
|
4
4
|
import { defineForm } from './VvForm'
|
|
5
5
|
import { defineFormWrapper } from './VvFormWrapper'
|
|
@@ -12,7 +12,7 @@ import type {
|
|
|
12
12
|
} from './types'
|
|
13
13
|
|
|
14
14
|
export const formFactory = (
|
|
15
|
-
schema: AnyZodObject
|
|
15
|
+
schema: AnyZodObject | ZodEffects<AnyZodObject>,
|
|
16
16
|
options: FormComposableOptions = {},
|
|
17
17
|
) => {
|
|
18
18
|
// create injection keys form provide/inject
|
|
@@ -78,7 +78,7 @@ export const createForm = (
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
export const useForm = (
|
|
81
|
-
schema: AnyZodObject
|
|
81
|
+
schema: AnyZodObject | ZodEffects<AnyZodObject>,
|
|
82
82
|
options: FormComposableOptions = {},
|
|
83
83
|
) => {
|
|
84
84
|
const hasOptions = { ...inject(pluginInjectionKey, {}), ...options }
|
|
@@ -88,10 +88,17 @@ export const useForm = (
|
|
|
88
88
|
export { FormFieldType } from './enums'
|
|
89
89
|
export { defaultObjectBySchema } from './utils'
|
|
90
90
|
|
|
91
|
+
type FormComponent = ReturnType<typeof defineForm>
|
|
92
|
+
type FormWrapperComponent = ReturnType<typeof defineFormWrapper>
|
|
93
|
+
type FormFieldComponent = ReturnType<typeof defineFormField>
|
|
94
|
+
|
|
91
95
|
export type {
|
|
92
96
|
InjectedFormData,
|
|
93
97
|
InjectedFormWrapperData,
|
|
94
98
|
InjectedFormFieldData,
|
|
95
99
|
FormComposableOptions,
|
|
96
100
|
FormPluginOptions,
|
|
101
|
+
FormComponent,
|
|
102
|
+
FormWrapperComponent,
|
|
103
|
+
FormFieldComponent,
|
|
97
104
|
}
|
package/src/types.d.ts
CHANGED
package/src/utils.ts
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
import merge from 'deepmerge'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
type z,
|
|
4
|
+
type AnyZodObject,
|
|
5
|
+
ZodDefault,
|
|
6
|
+
ZodObject,
|
|
7
|
+
ZodEffects,
|
|
8
|
+
} from 'zod'
|
|
3
9
|
|
|
4
|
-
export const defaultObjectBySchema = <
|
|
10
|
+
export const defaultObjectBySchema = <
|
|
11
|
+
Schema extends AnyZodObject | ZodEffects<AnyZodObject>,
|
|
12
|
+
>(
|
|
5
13
|
schema: Schema,
|
|
6
14
|
original: Partial<z.infer<Schema>> = {},
|
|
7
15
|
): Partial<z.infer<Schema>> => {
|
|
16
|
+
const shape =
|
|
17
|
+
schema instanceof ZodEffects ? schema.innerType().shape : schema.shape
|
|
8
18
|
return merge(
|
|
9
19
|
Object.fromEntries(
|
|
10
|
-
Object.entries(
|
|
20
|
+
Object.entries(shape).map(([key, value]) => {
|
|
11
21
|
if (value instanceof ZodDefault) {
|
|
12
22
|
return [key, value._def.defaultValue()]
|
|
13
23
|
}
|