@volverjs/form-vue 1.0.0-beta.9 → 1.0.1
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/LICENSE +1 -1
- package/README.md +412 -245
- package/dist/VvForm.d.ts +135 -0
- package/dist/VvFormField.d.ts +130 -0
- package/dist/VvFormFieldsGroup.d.ts +109 -0
- package/dist/VvFormTemplate.d.ts +40 -0
- package/dist/VvFormWrapper.d.ts +65 -0
- package/dist/{src/enums.d.ts → enums.d.ts} +1 -0
- package/dist/index.d.ts +972 -1
- package/dist/index.es.js +948 -596
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts +93 -0
- package/dist/utils.d.ts +3 -0
- package/package.json +61 -60
- package/src/VvForm.ts +359 -300
- package/src/VvFormField.ts +366 -334
- package/src/VvFormFieldsGroup.ts +382 -0
- package/src/VvFormTemplate.ts +185 -171
- package/src/VvFormWrapper.ts +198 -161
- package/src/enums.ts +27 -26
- package/src/index.ts +157 -134
- package/src/types.ts +162 -125
- package/src/utils.ts +121 -100
- package/dist/src/VvForm.d.ts +0 -202
- package/dist/src/VvFormField.d.ts +0 -116
- package/dist/src/VvFormTemplate.d.ts +0 -60
- package/dist/src/VvFormWrapper.d.ts +0 -107
- package/dist/src/index.d.ts +0 -1498
- package/dist/src/types.d.ts +0 -70
- package/dist/src/utils.d.ts +0 -3
- package/dist/test-playwright/VvForm.spec.d.ts +0 -1
- package/dist/test-playwright/VvFormField.spec.d.ts +0 -1
- package/dist/test-playwright/VvFormWrapper.spec.d.ts +0 -1
- package/dist/test-vitest/defaultObjectBySchema.test.d.ts +0 -1
- package/dist/test-vitest/useForm.test.d.ts +0 -1
package/src/VvFormTemplate.ts
CHANGED
|
@@ -1,178 +1,192 @@
|
|
|
1
|
+
import type { Component, DeepReadonly, InjectionKey, PropType, SlotsType, VNode } from 'vue'
|
|
2
|
+
import type { FormSchema, InjectedFormData, FormTemplate, RenderFunctionOutput } from './types'
|
|
3
|
+
import type { z } from 'zod'
|
|
4
|
+
import type { FormStatus } from './enums'
|
|
1
5
|
import { get } from 'ts-dot-prop'
|
|
2
6
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type Ref,
|
|
8
|
-
type VNode,
|
|
9
|
-
defineComponent,
|
|
10
|
-
h,
|
|
11
|
-
inject,
|
|
12
|
-
unref,
|
|
7
|
+
defineComponent,
|
|
8
|
+
h,
|
|
9
|
+
inject,
|
|
10
|
+
unref,
|
|
13
11
|
} from 'vue'
|
|
14
|
-
import type { TypeOf, z } from 'zod'
|
|
15
|
-
import type { FormSchema, InjectedFormData, FormTemplate } from './types'
|
|
16
|
-
import type { FormStatus } from './enums'
|
|
17
12
|
|
|
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
|
-
|
|
13
|
+
export function defineFormTemplate<Schema extends FormSchema, Type = undefined>(formProvideKey: InjectionKey<InjectedFormData<Schema, Type>>, VvFormField: Component) {
|
|
14
|
+
const VvFormTemplate = defineComponent({
|
|
15
|
+
name: 'VvFormTemplate',
|
|
16
|
+
props: {
|
|
17
|
+
schema: {
|
|
18
|
+
type: [Array, Function] as PropType<FormTemplate<Schema, Type>>,
|
|
19
|
+
required: true,
|
|
20
|
+
},
|
|
21
|
+
scope: {
|
|
22
|
+
type: Object as PropType<Record<string, unknown>>,
|
|
23
|
+
default: () => ({}),
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
slots: Object as SlotsType<{
|
|
27
|
+
default: {
|
|
28
|
+
errors?: DeepReadonly<z.inferFormattedError<Schema>>
|
|
29
|
+
formData?: undefined extends Type ? Partial<z.infer<Schema>> : Type
|
|
30
|
+
invalid: boolean
|
|
31
|
+
status?: FormStatus
|
|
32
|
+
submit?: InjectedFormData<Schema, Type>['submit']
|
|
33
|
+
validate?: InjectedFormData<Schema, Type>['validate']
|
|
34
|
+
clear?: InjectedFormData<Schema, Type>['clear']
|
|
35
|
+
reset?: InjectedFormData<Schema, Type>['reset']
|
|
36
|
+
}
|
|
37
|
+
}>,
|
|
38
|
+
setup(templateProps, { slots: templateSlots }) {
|
|
39
|
+
const injectedFormData = inject(formProvideKey)
|
|
40
|
+
if (!injectedFormData?.formData)
|
|
41
|
+
return
|
|
42
|
+
return () => {
|
|
43
|
+
const normalizedSchema = typeof templateProps.schema === 'function'
|
|
44
|
+
? templateProps.schema(
|
|
45
|
+
injectedFormData,
|
|
46
|
+
templateProps.scope,
|
|
47
|
+
)
|
|
48
|
+
: templateProps.schema
|
|
49
|
+
let lastIf: boolean | undefined
|
|
50
|
+
const toReturn = normalizedSchema.reduce<
|
|
51
|
+
(VNode | VNode[] | undefined)[]
|
|
52
|
+
>((acc, field) => {
|
|
53
|
+
const normalizedField = typeof field === 'function'
|
|
54
|
+
? field(injectedFormData, templateProps.scope)
|
|
55
|
+
: field
|
|
56
|
+
const {
|
|
57
|
+
vvIs,
|
|
58
|
+
vvName,
|
|
59
|
+
vvSlots,
|
|
60
|
+
vvChildren,
|
|
61
|
+
vvIf,
|
|
62
|
+
vvElseIf,
|
|
63
|
+
vvType,
|
|
64
|
+
vvDefaultValue,
|
|
65
|
+
vvShowValid,
|
|
66
|
+
vvContent,
|
|
67
|
+
...props
|
|
68
|
+
} = normalizedField
|
|
69
|
+
|
|
70
|
+
// conditions
|
|
71
|
+
if (vvIf !== undefined) {
|
|
72
|
+
if (typeof vvIf === 'string') {
|
|
73
|
+
lastIf = Boolean(
|
|
74
|
+
get(
|
|
75
|
+
new Object(injectedFormData.formData.value),
|
|
76
|
+
vvIf,
|
|
77
|
+
),
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
else if (typeof vvIf === 'function') {
|
|
81
|
+
lastIf = unref(vvIf(injectedFormData))
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
lastIf = unref(vvIf)
|
|
85
|
+
}
|
|
86
|
+
if (!lastIf) {
|
|
87
|
+
return acc
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else if (vvElseIf !== undefined && lastIf !== undefined) {
|
|
91
|
+
if (lastIf) {
|
|
92
|
+
return acc
|
|
93
|
+
}
|
|
94
|
+
if (typeof vvElseIf === 'string') {
|
|
95
|
+
lastIf = Boolean(
|
|
96
|
+
get(
|
|
97
|
+
new Object(injectedFormData.formData.value),
|
|
98
|
+
vvElseIf,
|
|
99
|
+
),
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
else if (typeof vvElseIf === 'function') {
|
|
103
|
+
lastIf = unref(vvElseIf(injectedFormData))
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
lastIf = unref(vvElseIf)
|
|
107
|
+
}
|
|
108
|
+
if (!lastIf) {
|
|
109
|
+
return acc
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
lastIf = undefined
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// children
|
|
117
|
+
let hChildren: RenderFunctionOutput | { default: (scope: Record<string, unknown>) => RenderFunctionOutput } | undefined
|
|
118
|
+
if (vvChildren) {
|
|
119
|
+
if (typeof vvIs === 'string') {
|
|
120
|
+
hChildren = h(VvFormTemplate, {
|
|
121
|
+
schema: vvChildren,
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
hChildren = {
|
|
126
|
+
default: (scope: Record<string, unknown>) =>
|
|
127
|
+
h(VvFormTemplate, {
|
|
128
|
+
schema: vvChildren,
|
|
129
|
+
scope,
|
|
130
|
+
}),
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
59
134
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
showValid: vvShowValid,
|
|
116
|
-
props,
|
|
117
|
-
},
|
|
118
|
-
vvSlots ?? hChildren ?? vvContent,
|
|
119
|
-
),
|
|
120
|
-
)
|
|
121
|
-
return acc
|
|
122
|
-
}
|
|
123
|
-
if (vvIs) {
|
|
124
|
-
acc.push(
|
|
125
|
-
h(
|
|
126
|
-
vvIs as Component,
|
|
127
|
-
props,
|
|
128
|
-
vvSlots ?? hChildren ?? vvContent,
|
|
129
|
-
),
|
|
130
|
-
)
|
|
131
|
-
return acc
|
|
132
|
-
}
|
|
133
|
-
if (vvChildren) {
|
|
134
|
-
acc.push(hChildren)
|
|
135
|
-
return acc
|
|
136
|
-
}
|
|
137
|
-
return acc
|
|
138
|
-
}, [])
|
|
139
|
-
toReturn.push(
|
|
140
|
-
templateSlots?.default?.({
|
|
141
|
-
formData: injectedFormData?.formData.value,
|
|
142
|
-
submit: injectedFormData?.submit,
|
|
143
|
-
validate: injectedFormData?.validate,
|
|
144
|
-
errors: injectedFormData?.errors.value,
|
|
145
|
-
status: injectedFormData?.status.value,
|
|
146
|
-
invalid: injectedFormData?.invalid.value,
|
|
147
|
-
}),
|
|
148
|
-
)
|
|
149
|
-
return toReturn
|
|
150
|
-
}
|
|
151
|
-
},
|
|
152
|
-
})
|
|
135
|
+
// render
|
|
136
|
+
if (vvName) {
|
|
137
|
+
acc.push(
|
|
138
|
+
h(
|
|
139
|
+
VvFormField,
|
|
140
|
+
{
|
|
141
|
+
name: vvName,
|
|
142
|
+
is: vvIs,
|
|
143
|
+
type: vvType,
|
|
144
|
+
defaultValue: vvDefaultValue,
|
|
145
|
+
showValid: vvShowValid,
|
|
146
|
+
props,
|
|
147
|
+
},
|
|
148
|
+
vvSlots ?? hChildren ?? vvContent,
|
|
149
|
+
),
|
|
150
|
+
)
|
|
151
|
+
return acc
|
|
152
|
+
}
|
|
153
|
+
if (vvIs) {
|
|
154
|
+
acc.push(
|
|
155
|
+
h(
|
|
156
|
+
vvIs as Component,
|
|
157
|
+
props,
|
|
158
|
+
vvSlots ?? hChildren ?? vvContent,
|
|
159
|
+
),
|
|
160
|
+
)
|
|
161
|
+
return acc
|
|
162
|
+
}
|
|
163
|
+
if (hChildren) {
|
|
164
|
+
if ('default' in hChildren) {
|
|
165
|
+
acc.push(hChildren.default(templateProps.scope))
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
acc.push(hChildren)
|
|
169
|
+
}
|
|
170
|
+
return acc
|
|
171
|
+
}
|
|
172
|
+
return acc
|
|
173
|
+
}, [])
|
|
174
|
+
toReturn.push(
|
|
175
|
+
templateSlots?.default?.({
|
|
176
|
+
errors: injectedFormData?.errors.value,
|
|
177
|
+
formData: injectedFormData?.formData.value,
|
|
178
|
+
invalid: injectedFormData?.invalid.value,
|
|
179
|
+
status: injectedFormData?.status.value,
|
|
180
|
+
submit: injectedFormData?.submit,
|
|
181
|
+
validate: injectedFormData?.validate,
|
|
182
|
+
clear: injectedFormData?.clear,
|
|
183
|
+
reset: injectedFormData?.reset,
|
|
184
|
+
}),
|
|
185
|
+
)
|
|
186
|
+
return toReturn
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
})
|
|
153
190
|
|
|
154
|
-
|
|
155
|
-
* An hack to add types to the default slot
|
|
156
|
-
*/
|
|
157
|
-
return VvFormTemplate as typeof VvFormTemplate & {
|
|
158
|
-
new (): {
|
|
159
|
-
$slots: {
|
|
160
|
-
default: (_: {
|
|
161
|
-
formData: unknown extends
|
|
162
|
-
| Partial<TypeOf<Schema>>
|
|
163
|
-
| undefined
|
|
164
|
-
? undefined
|
|
165
|
-
: Partial<TypeOf<Schema>> | undefined
|
|
166
|
-
submit: () => Promise<boolean>
|
|
167
|
-
validate: () => Promise<boolean>
|
|
168
|
-
errors: Readonly<
|
|
169
|
-
Ref<DeepReadonly<z.inferFormattedError<Schema>>>
|
|
170
|
-
>
|
|
171
|
-
status: Ref<DeepReadonly<`${FormStatus}` | undefined>>
|
|
172
|
-
invalid: Ref<DeepReadonly<boolean>>
|
|
173
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
174
|
-
}) => any
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
191
|
+
return VvFormTemplate
|
|
178
192
|
}
|