@volverjs/form-vue 1.0.0-beta.2 → 1.0.0-beta.21

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