@volverjs/form-vue 1.0.0-beta.2 → 1.0.0-beta.20
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 +251 -243
- package/dist/VvForm.d.ts +99 -0
- package/dist/VvFormField.d.ts +130 -0
- package/dist/VvFormTemplate.d.ts +40 -0
- package/dist/VvFormWrapper.d.ts +51 -0
- package/dist/{src/enums.d.ts → enums.d.ts} +1 -0
- package/dist/index.d.ts +1028 -1
- package/dist/index.es.js +680 -529
- package/dist/index.umd.js +1 -1
- package/dist/{src/types.d.ts → types.d.ts} +23 -15
- package/dist/utils.d.ts +3 -0
- package/package.json +56 -60
- package/src/VvForm.ts +325 -245
- package/src/VvFormField.ts +360 -319
- package/src/VvFormTemplate.ts +191 -170
- package/src/VvFormWrapper.ts +154 -159
- package/src/enums.ts +27 -26
- package/src/index.ts +125 -128
- package/src/types.ts +130 -109
- package/src/utils.ts +125 -97
- package/dist/src/VvForm.d.ts +0 -229
- package/dist/src/VvFormField.d.ts +0 -106
- package/dist/src/VvFormTemplate.d.ts +0 -131
- package/dist/src/VvFormWrapper.d.ts +0 -172
- package/dist/src/index.d.ts +0 -1957
- 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/types.ts
CHANGED
|
@@ -1,156 +1,177 @@
|
|
|
1
|
-
import type { Component, DeepReadonly, Ref, WatchStopHandle } from 'vue'
|
|
2
|
-
import type {
|
|
1
|
+
import type { Component, DeepReadonly, Ref, RendererElement, RendererNode, VNode, WatchStopHandle } from 'vue'
|
|
2
|
+
import type { TypeOf, z } from 'zod'
|
|
3
3
|
import type { IgnoredUpdater } from '@vueuse/core'
|
|
4
4
|
import type { FormFieldType, FormStatus } from './enums'
|
|
5
5
|
|
|
6
6
|
export type FormSchema =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
| z.AnyZodObject
|
|
8
|
+
| z.ZodEffects<z.AnyZodObject>
|
|
9
|
+
| z.ZodEffects<z.ZodEffects<z.AnyZodObject>>
|
|
10
10
|
|
|
11
11
|
export type FormFieldComponentOptions = {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
lazyLoad?: boolean
|
|
13
|
+
sideEffects?: (type: `${FormFieldType}`) => Promise<void> | void
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export type FormComponentOptions<Schema> = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
17
|
+
updateThrottle?: number
|
|
18
|
+
continuousValidation?: boolean
|
|
19
|
+
readonly?: boolean
|
|
20
|
+
template?: Schema extends FormSchema ? FormTemplate<Schema> : never
|
|
21
|
+
onUpdate?: Schema extends FormSchema
|
|
22
|
+
? (data?: Partial<z.infer<Schema>>) => void
|
|
23
|
+
: never
|
|
24
|
+
onSubmit?: Schema extends FormSchema
|
|
25
|
+
? (data?: z.infer<Schema>) => void
|
|
26
|
+
: never
|
|
27
|
+
onReset?: Schema extends FormSchema ? (data?: z.infer<Schema>) => void : never
|
|
28
|
+
onInvalid?: Schema extends FormSchema
|
|
29
|
+
? (error?: z.inferFormattedError<Schema>) => void
|
|
30
|
+
: never
|
|
31
|
+
onValid?: Schema extends FormSchema
|
|
32
|
+
? (data?: z.infer<Schema>) => void
|
|
33
|
+
: never
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
export type FormComposableOptions<Schema> = FormFieldComponentOptions &
|
|
35
|
-
|
|
37
|
+
FormComponentOptions<Schema>
|
|
36
38
|
|
|
37
39
|
type FormPluginOptionsSchema = {
|
|
38
|
-
|
|
40
|
+
schema?: FormSchema
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
export type FormPluginOptions = FormPluginOptionsSchema &
|
|
42
|
-
|
|
44
|
+
FormComposableOptions<FormPluginOptionsSchema['schema']>
|
|
43
45
|
|
|
44
46
|
export type InjectedFormData<Schema extends FormSchema> = {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
formData: Ref<Partial<z.infer<Schema>> | undefined>
|
|
48
|
+
errors: Readonly<
|
|
49
|
+
Ref<DeepReadonly<z.inferFormattedError<Schema>> | undefined>
|
|
50
|
+
>
|
|
51
|
+
submit: () => Promise<boolean>
|
|
52
|
+
validate: (formData?: Partial<z.infer<Schema>>, fields?: Set<string>) => Promise<boolean>
|
|
53
|
+
clear: () => void
|
|
54
|
+
reset: () => void
|
|
55
|
+
ignoreUpdates: IgnoredUpdater
|
|
56
|
+
stopUpdatesWatch: WatchStopHandle
|
|
57
|
+
status: Readonly<Ref<FormStatus | undefined>>
|
|
58
|
+
invalid: Readonly<Ref<boolean>>
|
|
59
|
+
readonly: Ref<boolean>
|
|
55
60
|
}
|
|
56
61
|
|
|
57
62
|
export type InjectedFormWrapperData<Schema extends FormSchema> = {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
name: Ref<string>
|
|
64
|
+
fields: Ref<Set<string>>
|
|
65
|
+
errors: Ref<Map<string, z.inferFormattedError<Schema>>>
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
export type InjectedFormFieldData<Schema extends FormSchema> = {
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
name: Ref<string>
|
|
70
|
+
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
|
|
66
71
|
}
|
|
67
72
|
|
|
68
73
|
export type Primitive =
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
| null
|
|
75
|
+
| undefined
|
|
76
|
+
| string
|
|
77
|
+
| number
|
|
78
|
+
| boolean
|
|
79
|
+
| symbol
|
|
80
|
+
| bigint
|
|
81
|
+
|
|
78
82
|
type IsTuple<T extends readonly any[]> = number extends T['length']
|
|
79
|
-
|
|
80
|
-
|
|
83
|
+
? false
|
|
84
|
+
: true
|
|
81
85
|
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
86
|
type TupleKeys<T extends readonly any[]> = Exclude<keyof T, keyof any[]>
|
|
84
87
|
|
|
85
88
|
export type PathConcat<
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
TKey extends string | number,
|
|
90
|
+
TValue,
|
|
88
91
|
> = TValue extends Primitive ? `${TKey}` : `${TKey}` | `${TKey}.${Path<TValue>}`
|
|
89
92
|
|
|
90
93
|
export type Path<T> = T extends readonly (infer V)[]
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
+
? IsTuple<T> extends true
|
|
95
|
+
? {
|
|
96
|
+
[K in TupleKeys<T>]-?: PathConcat<K & string, T[K]>
|
|
97
|
+
}[TupleKeys<T>]
|
|
98
|
+
: PathConcat<number, V>
|
|
99
|
+
: {
|
|
100
|
+
[K in keyof T]-?: PathConcat<K & string, T[K]>
|
|
101
|
+
}[keyof T]
|
|
102
|
+
|
|
101
103
|
export type PathValue<T, TPath extends Path<T> | Path<T>[]> = T extends any
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
104
|
+
? TPath extends `${infer K}.${infer R}`
|
|
105
|
+
? K extends keyof T
|
|
106
|
+
? R extends Path<T[K]>
|
|
107
|
+
? undefined extends T[K]
|
|
108
|
+
? PathValue<T[K], R> | undefined
|
|
109
|
+
: PathValue<T[K], R>
|
|
110
|
+
: never
|
|
111
|
+
: K extends `${number}`
|
|
112
|
+
? T extends readonly (infer V)[]
|
|
113
|
+
? PathValue<V, R & Path<V>>
|
|
114
|
+
: never
|
|
115
|
+
: never
|
|
116
|
+
: TPath extends keyof T
|
|
117
|
+
? T[TPath]
|
|
118
|
+
: TPath extends `${number}`
|
|
119
|
+
? T extends readonly (infer V)[]
|
|
120
|
+
? V
|
|
121
|
+
: never
|
|
122
|
+
: never
|
|
123
|
+
: never
|
|
122
124
|
|
|
123
125
|
export type AnyBoolean<Schema extends FormSchema> =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
126
|
+
| boolean
|
|
127
|
+
| Ref<boolean>
|
|
128
|
+
| ((data?: InjectedFormData<Schema>) => boolean | Ref<boolean>)
|
|
127
129
|
|
|
128
130
|
export type SimpleFormTemplateItem<Schema extends FormSchema> = Record<
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
any
|
|
131
|
+
string,
|
|
132
|
+
any
|
|
132
133
|
> & {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
134
|
+
vvIs?: string | Component
|
|
135
|
+
vvName?: Path<z.infer<Schema>>
|
|
136
|
+
vvSlots?: Record<string, any>
|
|
137
|
+
vvChildren?:
|
|
138
|
+
| Array<
|
|
139
|
+
| SimpleFormTemplateItem<Schema>
|
|
140
|
+
| ((
|
|
141
|
+
data?: InjectedFormData<Schema>,
|
|
142
|
+
scope?: Record<string, unknown>,
|
|
143
|
+
) => SimpleFormTemplateItem<Schema>)
|
|
144
|
+
>
|
|
145
|
+
| ((
|
|
146
|
+
data?: InjectedFormData<Schema>,
|
|
147
|
+
scope?: Record<string, unknown>,
|
|
148
|
+
) => Array<
|
|
149
|
+
| SimpleFormTemplateItem<Schema>
|
|
150
|
+
| ((
|
|
151
|
+
data?: InjectedFormData<Schema>,
|
|
152
|
+
scope?: Record<string, unknown>,
|
|
153
|
+
) => SimpleFormTemplateItem<Schema>)
|
|
154
|
+
>)
|
|
155
|
+
vvIf?: AnyBoolean<Schema> | Path<z.infer<Schema>>
|
|
156
|
+
vvElseIf?: AnyBoolean<Schema> | Path<z.infer<Schema>>
|
|
157
|
+
vvType?: `${FormFieldType}`
|
|
158
|
+
vvShowValid?: boolean
|
|
159
|
+
vvContent?: string
|
|
160
|
+
vvDefaultValue?: any
|
|
148
161
|
}
|
|
149
162
|
|
|
150
163
|
export type FormTemplateItem<Schema extends FormSchema> =
|
|
151
|
-
|
|
152
|
-
|
|
164
|
+
| SimpleFormTemplateItem<Schema>
|
|
165
|
+
| ((
|
|
166
|
+
data?: InjectedFormData<Schema>,
|
|
167
|
+
scope?: Record<string, unknown>,
|
|
168
|
+
) => SimpleFormTemplateItem<Schema>)
|
|
153
169
|
|
|
154
170
|
export type FormTemplate<Schema extends FormSchema> =
|
|
155
|
-
|
|
156
|
-
|
|
171
|
+
| FormTemplateItem<Schema>[]
|
|
172
|
+
| ((
|
|
173
|
+
data?: InjectedFormData<Schema>,
|
|
174
|
+
scope?: Record<string, unknown>,
|
|
175
|
+
) => FormTemplateItem<Schema>[])
|
|
176
|
+
|
|
177
|
+
export type RenderFunctionOutput = VNode<RendererNode, RendererElement, { [key: string]: any }>
|
package/src/utils.ts
CHANGED
|
@@ -1,102 +1,130 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
type z,
|
|
3
|
+
type AnyZodObject,
|
|
4
|
+
type ZodTypeAny,
|
|
5
|
+
ZodDefault,
|
|
6
|
+
ZodObject,
|
|
7
|
+
ZodEffects,
|
|
8
|
+
ZodSchema,
|
|
9
|
+
ZodNullable,
|
|
10
|
+
ZodOptional,
|
|
11
|
+
ZodRecord,
|
|
12
|
+
ZodArray,
|
|
12
13
|
} from 'zod'
|
|
13
14
|
import type { FormSchema } from './types'
|
|
14
15
|
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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
|
-
|
|
16
|
+
export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema, original: Partial<z.infer<Schema>> & Record<string, unknown> = {}): Partial<z.infer<Schema>> {
|
|
17
|
+
const getSchemaInnerType = <Type extends ZodTypeAny>(
|
|
18
|
+
schema:
|
|
19
|
+
| Type
|
|
20
|
+
| ZodEffects<Type>
|
|
21
|
+
| ZodEffects<ZodEffects<Type>>
|
|
22
|
+
| ZodOptional<Type>,
|
|
23
|
+
) => {
|
|
24
|
+
let toReturn = schema
|
|
25
|
+
while (toReturn instanceof ZodEffects) {
|
|
26
|
+
toReturn = toReturn.innerType()
|
|
27
|
+
}
|
|
28
|
+
if (toReturn instanceof ZodOptional) {
|
|
29
|
+
toReturn = toReturn._def.innerType
|
|
30
|
+
}
|
|
31
|
+
return toReturn
|
|
32
|
+
}
|
|
33
|
+
const isSchemaOptional = <Type extends ZodTypeAny>(
|
|
34
|
+
schema:
|
|
35
|
+
| Type
|
|
36
|
+
| ZodEffects<Type>
|
|
37
|
+
| ZodEffects<ZodEffects<Type>>
|
|
38
|
+
| ZodOptional<Type>,
|
|
39
|
+
) => {
|
|
40
|
+
let toReturn = schema
|
|
41
|
+
while (toReturn instanceof ZodEffects) {
|
|
42
|
+
toReturn = toReturn.innerType()
|
|
43
|
+
}
|
|
44
|
+
if (toReturn instanceof ZodOptional) {
|
|
45
|
+
return true
|
|
46
|
+
}
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
const innerType = getSchemaInnerType<AnyZodObject>(schema)
|
|
50
|
+
const unknownKeys
|
|
51
|
+
= innerType instanceof ZodObject
|
|
52
|
+
? innerType._def.unknownKeys === 'passthrough'
|
|
53
|
+
: false
|
|
54
|
+
return {
|
|
55
|
+
...(unknownKeys ? original : {}),
|
|
56
|
+
...Object.fromEntries(
|
|
57
|
+
(Object.entries(innerType.shape) as [string, ZodTypeAny][]).map(
|
|
58
|
+
([key, subSchema]) => {
|
|
59
|
+
const originalValue = original[key]
|
|
60
|
+
const isOptional = isSchemaOptional(subSchema)
|
|
61
|
+
let innerType = getSchemaInnerType(subSchema)
|
|
62
|
+
let defaultValue: Partial<z.infer<Schema>> | undefined
|
|
63
|
+
if (innerType instanceof ZodDefault) {
|
|
64
|
+
defaultValue = innerType._def.defaultValue()
|
|
65
|
+
innerType = innerType._def.innerType
|
|
66
|
+
}
|
|
67
|
+
if (
|
|
68
|
+
originalValue === null
|
|
69
|
+
&& innerType instanceof ZodNullable
|
|
70
|
+
) {
|
|
71
|
+
return [key, originalValue]
|
|
72
|
+
}
|
|
73
|
+
if ((originalValue === undefined || originalValue === null) && isOptional) {
|
|
74
|
+
return [key, defaultValue]
|
|
75
|
+
}
|
|
76
|
+
if (innerType instanceof ZodSchema) {
|
|
77
|
+
const parse = subSchema.safeParse(originalValue)
|
|
78
|
+
if (parse.success) {
|
|
79
|
+
return [key, parse.data ?? defaultValue]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (
|
|
83
|
+
innerType instanceof ZodArray
|
|
84
|
+
&& Array.isArray(originalValue)
|
|
85
|
+
&& originalValue.length
|
|
86
|
+
) {
|
|
87
|
+
const arrayType = getSchemaInnerType(innerType._def.type)
|
|
88
|
+
if (arrayType instanceof ZodObject) {
|
|
89
|
+
return [
|
|
90
|
+
key,
|
|
91
|
+
originalValue.map((element: unknown) =>
|
|
92
|
+
defaultObjectBySchema(
|
|
93
|
+
arrayType,
|
|
94
|
+
(element && typeof element === 'object'
|
|
95
|
+
? element
|
|
96
|
+
: undefined) as Partial<
|
|
97
|
+
typeof arrayType
|
|
98
|
+
>,
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (innerType instanceof ZodRecord && originalValue) {
|
|
105
|
+
const valueType = getSchemaInnerType(innerType._def.valueType)
|
|
106
|
+
if (valueType instanceof ZodObject) {
|
|
107
|
+
return [key, Object.keys(originalValue).reduce((acc: Record<string, unknown>, recordKey: string) => {
|
|
108
|
+
acc[recordKey] = defaultObjectBySchema(valueType, originalValue[recordKey])
|
|
109
|
+
return acc
|
|
110
|
+
}, {})]
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (innerType instanceof ZodObject) {
|
|
114
|
+
return [
|
|
115
|
+
key,
|
|
116
|
+
defaultObjectBySchema(
|
|
117
|
+
innerType,
|
|
118
|
+
originalValue
|
|
119
|
+
&& typeof originalValue === 'object'
|
|
120
|
+
? originalValue
|
|
121
|
+
: defaultValue,
|
|
122
|
+
),
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
return [key, defaultValue]
|
|
126
|
+
},
|
|
127
|
+
),
|
|
128
|
+
),
|
|
129
|
+
} as Partial<z.infer<Schema>>
|
|
102
130
|
}
|