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