@volverjs/form-vue 1.0.0-beta.9 → 1.0.0

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,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
- 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,
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 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
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
- // 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
- })
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
  }