@volverjs/form-vue 0.0.9 → 0.0.10-beta.10
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 +241 -169
- package/dist/VvForm.d.ts +167 -39
- package/dist/VvFormField.d.ts +92 -3
- package/dist/VvFormTemplate.d.ts +20 -0
- package/dist/VvFormWrapper.d.ts +121 -19
- package/dist/enums.d.ts +4 -0
- package/dist/index.d.ts +733 -63
- package/dist/index.es.js +431 -282
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts +110 -11
- package/dist/utils.d.ts +2 -5
- package/package.json +19 -17
- package/src/VvForm.ts +90 -32
- package/src/VvFormField.ts +59 -39
- package/src/VvFormTemplate.ts +179 -0
- package/src/VvFormWrapper.ts +66 -25
- package/src/enums.ts +5 -0
- package/src/index.ts +60 -17
- package/src/types.d.ts +110 -11
- package/src/utils.ts +49 -38
package/src/types.d.ts
CHANGED
|
@@ -1,31 +1,130 @@
|
|
|
1
1
|
import type { Ref } from 'vue'
|
|
2
|
-
import type {
|
|
3
|
-
import type { FormFieldType } from './enums'
|
|
2
|
+
import type { z, AnyZodObject, ZodEffects } from 'zod'
|
|
3
|
+
import type { FormFieldType, FormStatus } from './enums'
|
|
4
4
|
|
|
5
|
-
export type
|
|
5
|
+
export type FormSchema =
|
|
6
|
+
| AnyZodObject
|
|
7
|
+
| ZodEffects<AnyZodObject>
|
|
8
|
+
| ZodEffects<ZodEffects<AnyZodObject>>
|
|
9
|
+
|
|
10
|
+
export type FormFieldComponentOptions = {
|
|
6
11
|
lazyLoad?: boolean
|
|
12
|
+
sideEffects?: (type: `${FormFieldType}`) => Promise | void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type FormComponentOptions = {
|
|
7
16
|
updateThrottle?: number
|
|
8
17
|
continuosValidation?: boolean
|
|
9
|
-
sideEffects?: (type: `${FormFieldType}`) => Promise | void
|
|
10
18
|
}
|
|
11
19
|
|
|
20
|
+
export type FormComposableOptions = FormFieldComponentOptions &
|
|
21
|
+
FormComponentOptions
|
|
22
|
+
|
|
12
23
|
export type FormPluginOptions = {
|
|
13
24
|
schema?: ZodSchema
|
|
14
25
|
} & FormComposableOptions
|
|
15
26
|
|
|
16
|
-
export type InjectedFormData<
|
|
17
|
-
|
|
18
|
-
errors: Ref<
|
|
27
|
+
export type InjectedFormData<Schema extends FormSchema> = {
|
|
28
|
+
formData: Ref<Partial<z.infer<Schema>> | undefined>
|
|
29
|
+
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
|
|
19
30
|
submit: () => boolean
|
|
31
|
+
status: Readonly<Ref<FormStatus | undefined>>
|
|
32
|
+
invalid: Readonly<Ref<boolean>>
|
|
20
33
|
}
|
|
21
34
|
|
|
22
|
-
export type InjectedFormWrapperData = {
|
|
35
|
+
export type InjectedFormWrapperData<Schema extends FormSchema> = {
|
|
23
36
|
name: Ref<string>
|
|
24
37
|
fields: Ref<Set<string>>
|
|
25
|
-
errors: Ref<
|
|
38
|
+
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
|
|
26
39
|
}
|
|
27
40
|
|
|
28
|
-
export type InjectedFormFieldData = {
|
|
41
|
+
export type InjectedFormFieldData<Schema extends FormSchema> = {
|
|
29
42
|
name: Ref<string>
|
|
30
|
-
errors: Ref<
|
|
43
|
+
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
|
|
31
44
|
}
|
|
45
|
+
|
|
46
|
+
export type Primitive =
|
|
47
|
+
| null
|
|
48
|
+
| undefined
|
|
49
|
+
| string
|
|
50
|
+
| number
|
|
51
|
+
| boolean
|
|
52
|
+
| symbol
|
|
53
|
+
| bigint
|
|
54
|
+
|
|
55
|
+
type ArrayKey = number
|
|
56
|
+
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
|
+
type IsTuple<T extends readonly any[]> = number extends T['length']
|
|
59
|
+
? false
|
|
60
|
+
: true
|
|
61
|
+
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
+
type TupleKeys<T extends readonly any[]> = Exclude<keyof T, keyof any[]>
|
|
64
|
+
|
|
65
|
+
export type PathConcat<
|
|
66
|
+
TKey extends string | number,
|
|
67
|
+
TValue,
|
|
68
|
+
> = TValue extends Primitive ? `${TKey}` : `${TKey}` | `${TKey}.${Path<TValue>}`
|
|
69
|
+
|
|
70
|
+
export type Path<T> = T extends readonly (infer V)[]
|
|
71
|
+
? IsTuple<T> extends true
|
|
72
|
+
? {
|
|
73
|
+
[K in TupleKeys<T>]-?: PathConcat<K & string, T[K]>
|
|
74
|
+
}[TupleKeys<T>]
|
|
75
|
+
: PathConcat<ArrayKey, V>
|
|
76
|
+
: {
|
|
77
|
+
[K in keyof T]-?: PathConcat<K & string, T[K]>
|
|
78
|
+
}[keyof T]
|
|
79
|
+
|
|
80
|
+
export type PathValue<T, TPath extends Path<T> | ArrayPath<T>> = T extends any
|
|
81
|
+
? TPath extends `${infer K}.${infer R}`
|
|
82
|
+
? K extends keyof T
|
|
83
|
+
? R extends Path<T[K]>
|
|
84
|
+
? undefined extends T[K]
|
|
85
|
+
? PathValue<T[K], R> | undefined
|
|
86
|
+
: PathValue<T[K], R>
|
|
87
|
+
: never
|
|
88
|
+
: K extends `${ArrayKey}`
|
|
89
|
+
? T extends readonly (infer V)[]
|
|
90
|
+
? PathValue<V, R & Path<V>>
|
|
91
|
+
: never
|
|
92
|
+
: never
|
|
93
|
+
: TPath extends keyof T
|
|
94
|
+
? T[TPath]
|
|
95
|
+
: TPath extends `${ArrayKey}`
|
|
96
|
+
? T extends readonly (infer V)[]
|
|
97
|
+
? V
|
|
98
|
+
: never
|
|
99
|
+
: never
|
|
100
|
+
: never
|
|
101
|
+
|
|
102
|
+
export type AnyBoolean<Schema> =
|
|
103
|
+
| boolean
|
|
104
|
+
| Ref<boolean>
|
|
105
|
+
| ((data?: InjectedFormData<Schema>) => boolean | Ref<boolean>)
|
|
106
|
+
|
|
107
|
+
export type SimpleFormTemplateItem<Schema extends FormSchema> = Record<
|
|
108
|
+
string,
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
110
|
+
any
|
|
111
|
+
> & {
|
|
112
|
+
vvIs?: string | Component
|
|
113
|
+
vvName?: Path<z.infer<Schema>>
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
115
|
+
vvSlots?: Record<string, any>
|
|
116
|
+
vvChildren?: Array<
|
|
117
|
+
| SimpleFormTemplateItem<Schema>
|
|
118
|
+
| ((data?: InjectedFormData<Schema>) => SimpleFormTemplateItem<Schema>)
|
|
119
|
+
>
|
|
120
|
+
vvIf?: AnyBoolean<Schema> | Path<z.infer<Schema>>
|
|
121
|
+
vvElseIf?: AnyBoolean<Schema> | Path<z.infer<Schema>>
|
|
122
|
+
vvType?: `${FormFieldType}`
|
|
123
|
+
vvShowValid?: boolean
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
125
|
+
vvDefaultValue?: any
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type FormTemplateItem<Schema extends FormSchema> =
|
|
129
|
+
| SimpleFormTemplateItem<Schema>
|
|
130
|
+
| ((data?: InjectedFormData<Schema>) => SimpleFormTemplateItem<Schema>)
|
package/src/utils.ts
CHANGED
|
@@ -1,60 +1,71 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type z,
|
|
3
3
|
type AnyZodObject,
|
|
4
|
+
type ZodTypeAny,
|
|
4
5
|
ZodDefault,
|
|
5
6
|
ZodObject,
|
|
6
7
|
ZodEffects,
|
|
7
8
|
ZodSchema,
|
|
8
9
|
ZodNullable,
|
|
9
10
|
} from 'zod'
|
|
11
|
+
import type { FormSchema } from './types'
|
|
10
12
|
|
|
11
|
-
export const defaultObjectBySchema = <
|
|
12
|
-
Schema extends AnyZodObject | ZodEffects<AnyZodObject>,
|
|
13
|
-
>(
|
|
13
|
+
export const defaultObjectBySchema = <Schema extends FormSchema>(
|
|
14
14
|
schema: Schema,
|
|
15
15
|
original: Partial<z.infer<Schema>> = {},
|
|
16
16
|
): Partial<z.infer<Schema>> => {
|
|
17
|
-
const
|
|
18
|
-
schema
|
|
19
|
-
|
|
17
|
+
const getInnerType = <Type extends ZodTypeAny>(
|
|
18
|
+
schema: Type | ZodEffects<Type> | ZodEffects<ZodEffects<Type>>,
|
|
19
|
+
) => {
|
|
20
|
+
let toReturn = schema
|
|
21
|
+
while (toReturn instanceof ZodEffects) {
|
|
22
|
+
toReturn = toReturn.innerType()
|
|
23
|
+
}
|
|
24
|
+
return toReturn
|
|
25
|
+
}
|
|
26
|
+
const innerType = getInnerType<AnyZodObject>(schema)
|
|
20
27
|
const unknownKeys =
|
|
21
|
-
|
|
22
|
-
?
|
|
28
|
+
innerType instanceof ZodObject
|
|
29
|
+
? innerType._def.unknownKeys === 'passthrough'
|
|
23
30
|
: false
|
|
24
31
|
return {
|
|
25
32
|
...(unknownKeys ? original : {}),
|
|
26
33
|
...Object.fromEntries(
|
|
27
|
-
Object.entries(shape)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
defaultValue =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
|
|
34
|
+
(Object.entries(innerType.shape) as [string, ZodTypeAny][]).map(
|
|
35
|
+
([key, subSchema]) => {
|
|
36
|
+
const originalValue = original[key]
|
|
37
|
+
const innerType = getInnerType(subSchema)
|
|
38
|
+
let defaultValue = undefined
|
|
39
|
+
if (innerType instanceof ZodDefault) {
|
|
40
|
+
defaultValue = innerType._def.defaultValue()
|
|
41
|
+
}
|
|
42
|
+
if (
|
|
43
|
+
originalValue === null &&
|
|
44
|
+
innerType instanceof ZodNullable
|
|
45
|
+
) {
|
|
46
|
+
return [key, originalValue]
|
|
47
|
+
}
|
|
48
|
+
if (innerType instanceof ZodSchema) {
|
|
49
|
+
const parse = subSchema.safeParse(originalValue)
|
|
50
|
+
if (parse.success) {
|
|
51
|
+
return [key, parse.data ?? defaultValue]
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (innerType instanceof ZodObject) {
|
|
55
|
+
return [
|
|
56
|
+
key,
|
|
57
|
+
defaultObjectBySchema(
|
|
58
|
+
innerType,
|
|
59
|
+
originalValue &&
|
|
60
|
+
typeof originalValue === 'object'
|
|
61
|
+
? originalValue
|
|
62
|
+
: {},
|
|
63
|
+
),
|
|
64
|
+
]
|
|
43
65
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
key,
|
|
48
|
-
defaultObjectBySchema(
|
|
49
|
-
subSchema,
|
|
50
|
-
originalValue && typeof originalValue === 'object'
|
|
51
|
-
? originalValue
|
|
52
|
-
: {},
|
|
53
|
-
),
|
|
54
|
-
]
|
|
55
|
-
}
|
|
56
|
-
return [key, defaultValue]
|
|
57
|
-
}),
|
|
66
|
+
return [key, defaultValue]
|
|
67
|
+
},
|
|
68
|
+
),
|
|
58
69
|
),
|
|
59
70
|
} as Partial<z.infer<Schema>>
|
|
60
71
|
}
|