@volverjs/form-vue 1.0.0-beta.12 → 1.0.0-beta.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +247 -239
- package/dist/VvForm.d.ts +5 -5
- package/dist/VvFormField.d.ts +7 -7
- package/dist/VvFormTemplate.d.ts +4 -4
- package/dist/VvFormWrapper.d.ts +3 -3
- package/dist/index.d.ts +17 -17
- package/dist/index.es.js +475 -453
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/utils.d.ts +2 -2
- package/package.json +52 -56
- package/src/VvForm.ts +287 -291
- package/src/VvFormField.ts +323 -325
- package/src/VvFormTemplate.ts +193 -188
- package/src/VvFormWrapper.ts +144 -147
- package/src/enums.ts +26 -26
- package/src/index.ts +117 -128
- package/src/types.ts +111 -117
- package/src/utils.ts +93 -97
package/src/VvFormTemplate.ts
CHANGED
|
@@ -1,202 +1,207 @@
|
|
|
1
1
|
import { get } from 'ts-dot-prop'
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
type Component,
|
|
4
|
+
type PropType,
|
|
5
|
+
type InjectionKey,
|
|
6
|
+
type DeepReadonly,
|
|
7
|
+
type Ref,
|
|
8
|
+
type VNode,
|
|
9
|
+
defineComponent,
|
|
10
|
+
h,
|
|
11
|
+
inject,
|
|
12
|
+
unref,
|
|
13
13
|
} from 'vue'
|
|
14
14
|
import type { TypeOf, z } from 'zod'
|
|
15
15
|
import type { FormSchema, InjectedFormData, FormTemplate, RenderFunctionOutput } from './types'
|
|
16
16
|
import type { FormStatus } from './enums'
|
|
17
17
|
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
...props
|
|
65
|
-
} = normalizedField
|
|
18
|
+
export function defineFormTemplate<Schema extends FormSchema>(formProvideKey: InjectionKey<InjectedFormData<Schema>>, VvFormField: Component) {
|
|
19
|
+
const VvFormTemplate = defineComponent({
|
|
20
|
+
name: 'VvFormTemplate',
|
|
21
|
+
props: {
|
|
22
|
+
schema: {
|
|
23
|
+
type: [Array, Function] as PropType<FormTemplate<Schema>>,
|
|
24
|
+
required: true,
|
|
25
|
+
},
|
|
26
|
+
scope: {
|
|
27
|
+
type: Object as PropType<Record<string, unknown>>,
|
|
28
|
+
default: () => ({}),
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
setup(templateProps, { slots: templateSlots }) {
|
|
32
|
+
const injectedFormData = inject(formProvideKey)
|
|
33
|
+
if (!injectedFormData?.formData)
|
|
34
|
+
return
|
|
35
|
+
return () => {
|
|
36
|
+
const normalizedSchema
|
|
37
|
+
= typeof templateProps.schema === 'function'
|
|
38
|
+
? templateProps.schema(
|
|
39
|
+
injectedFormData,
|
|
40
|
+
templateProps.scope,
|
|
41
|
+
)
|
|
42
|
+
: templateProps.schema
|
|
43
|
+
let lastIf: boolean | undefined
|
|
44
|
+
const toReturn = normalizedSchema.reduce<
|
|
45
|
+
(VNode | VNode[] | undefined)[]
|
|
46
|
+
>((acc, field) => {
|
|
47
|
+
const normalizedField
|
|
48
|
+
= typeof field === 'function'
|
|
49
|
+
? field(injectedFormData, templateProps.scope)
|
|
50
|
+
: field
|
|
51
|
+
const {
|
|
52
|
+
vvIs,
|
|
53
|
+
vvName,
|
|
54
|
+
vvSlots,
|
|
55
|
+
vvChildren,
|
|
56
|
+
vvIf,
|
|
57
|
+
vvElseIf,
|
|
58
|
+
vvType,
|
|
59
|
+
vvDefaultValue,
|
|
60
|
+
vvShowValid,
|
|
61
|
+
vvContent,
|
|
62
|
+
...props
|
|
63
|
+
} = normalizedField
|
|
66
64
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
65
|
+
// conditions
|
|
66
|
+
if (vvIf !== undefined) {
|
|
67
|
+
if (typeof vvIf === 'string') {
|
|
68
|
+
lastIf = Boolean(
|
|
69
|
+
get(
|
|
70
|
+
Object(injectedFormData.formData.value),
|
|
71
|
+
vvIf,
|
|
72
|
+
),
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
else if (typeof vvIf === 'function') {
|
|
76
|
+
lastIf = unref(vvIf(injectedFormData))
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
lastIf = unref(vvIf)
|
|
80
|
+
}
|
|
81
|
+
if (!lastIf) {
|
|
82
|
+
return acc
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else if (vvElseIf !== undefined && lastIf !== undefined) {
|
|
86
|
+
if (lastIf) {
|
|
87
|
+
return acc
|
|
88
|
+
}
|
|
89
|
+
if (typeof vvElseIf === 'string') {
|
|
90
|
+
lastIf = Boolean(
|
|
91
|
+
get(
|
|
92
|
+
Object(injectedFormData.formData.value),
|
|
93
|
+
vvElseIf,
|
|
94
|
+
),
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
else if (typeof vvElseIf === 'function') {
|
|
98
|
+
lastIf = unref(vvElseIf(injectedFormData))
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
lastIf = unref(vvElseIf)
|
|
102
|
+
}
|
|
103
|
+
if (!lastIf) {
|
|
104
|
+
return acc
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
lastIf = undefined
|
|
109
|
+
}
|
|
106
110
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
111
|
+
// children
|
|
112
|
+
let hChildren: RenderFunctionOutput | { default: (scope: Record<string, unknown>) => RenderFunctionOutput } | undefined
|
|
113
|
+
if (vvChildren) {
|
|
114
|
+
if (typeof vvIs === 'string') {
|
|
115
|
+
hChildren = h(VvFormTemplate, {
|
|
116
|
+
schema: vvChildren,
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
hChildren = {
|
|
121
|
+
default: (scope: Record<string, unknown>) =>
|
|
122
|
+
h(VvFormTemplate, {
|
|
123
|
+
schema: vvChildren,
|
|
124
|
+
scope,
|
|
125
|
+
}),
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
124
129
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
130
|
+
// render
|
|
131
|
+
if (vvName) {
|
|
132
|
+
acc.push(
|
|
133
|
+
h(
|
|
134
|
+
VvFormField,
|
|
135
|
+
{
|
|
136
|
+
name: vvName,
|
|
137
|
+
is: vvIs,
|
|
138
|
+
type: vvType,
|
|
139
|
+
defaultValue: vvDefaultValue,
|
|
140
|
+
showValid: vvShowValid,
|
|
141
|
+
props,
|
|
142
|
+
},
|
|
143
|
+
vvSlots ?? hChildren ?? vvContent,
|
|
144
|
+
),
|
|
145
|
+
)
|
|
146
|
+
return acc
|
|
147
|
+
}
|
|
148
|
+
if (vvIs) {
|
|
149
|
+
acc.push(
|
|
150
|
+
h(
|
|
151
|
+
vvIs as Component,
|
|
152
|
+
props,
|
|
153
|
+
vvSlots ?? hChildren ?? vvContent,
|
|
154
|
+
),
|
|
155
|
+
)
|
|
156
|
+
return acc
|
|
157
|
+
}
|
|
158
|
+
if (hChildren) {
|
|
159
|
+
if ('default' in hChildren) {
|
|
160
|
+
acc.push(hChildren.default(templateProps.scope))
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
acc.push(hChildren)
|
|
164
|
+
}
|
|
165
|
+
return acc
|
|
166
|
+
}
|
|
167
|
+
return acc
|
|
168
|
+
}, [])
|
|
169
|
+
toReturn.push(
|
|
170
|
+
templateSlots?.default?.({
|
|
171
|
+
formData: injectedFormData?.formData.value,
|
|
172
|
+
submit: injectedFormData?.submit,
|
|
173
|
+
validate: injectedFormData?.validate,
|
|
174
|
+
errors: injectedFormData?.errors.value,
|
|
175
|
+
status: injectedFormData?.status.value,
|
|
176
|
+
invalid: injectedFormData?.invalid.value,
|
|
177
|
+
}),
|
|
178
|
+
)
|
|
179
|
+
return toReturn
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
})
|
|
177
183
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
184
|
+
/**
|
|
185
|
+
* An hack to add types to the default slot
|
|
186
|
+
*/
|
|
187
|
+
return VvFormTemplate as typeof VvFormTemplate & {
|
|
188
|
+
new (): {
|
|
189
|
+
$slots: {
|
|
190
|
+
default: (_: {
|
|
191
|
+
formData: unknown extends
|
|
192
|
+
| Partial<TypeOf<Schema>>
|
|
193
|
+
| undefined
|
|
194
|
+
? undefined
|
|
195
|
+
: Partial<TypeOf<Schema>> | undefined
|
|
196
|
+
submit: () => Promise<boolean>
|
|
197
|
+
validate: () => Promise<boolean>
|
|
198
|
+
errors: Readonly<
|
|
193
199
|
Ref<DeepReadonly<z.inferFormattedError<Schema>>>
|
|
194
200
|
>
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
201
|
+
status: Ref<DeepReadonly<`${FormStatus}` | undefined>>
|
|
202
|
+
invalid: Ref<DeepReadonly<boolean>>
|
|
203
|
+
}) => any
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
202
207
|
}
|