@volverjs/form-vue 1.0.0-beta.21 → 1.0.0-beta.23
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 +150 -13
- package/dist/VvForm.d.ts +5 -5
- package/dist/VvFormField.d.ts +7 -7
- package/dist/VvFormFieldsGroup.d.ts +109 -0
- package/dist/VvFormWrapper.d.ts +1 -1
- package/dist/index.d.ts +385 -58
- package/dist/index.es.js +747 -551
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts +10 -3
- package/package.json +13 -13
- package/src/VvFormField.ts +23 -22
- package/src/VvFormFieldsGroup.ts +375 -0
- package/src/VvFormTemplate.ts +1 -1
- package/src/index.ts +13 -2
- package/src/types.ts +17 -6
- package/src/utils.ts +8 -12
package/src/index.ts
CHANGED
|
@@ -6,14 +6,16 @@ import {
|
|
|
6
6
|
type Plugin,
|
|
7
7
|
} from 'vue'
|
|
8
8
|
import type { AnyZodObject } from 'zod'
|
|
9
|
-
import { defineFormField } from './VvFormField'
|
|
10
9
|
import { defineForm } from './VvForm'
|
|
10
|
+
import { defineFormField } from './VvFormField'
|
|
11
|
+
import { defineFormFieldsGroup } from './VvFormFieldsGroup'
|
|
11
12
|
import { defineFormWrapper } from './VvFormWrapper'
|
|
12
13
|
import { defineFormTemplate } from './VvFormTemplate'
|
|
13
14
|
import type {
|
|
14
15
|
InjectedFormData,
|
|
15
16
|
InjectedFormWrapperData,
|
|
16
17
|
InjectedFormFieldData,
|
|
18
|
+
InjectedFormFieldsGroupData,
|
|
17
19
|
FormComposableOptions,
|
|
18
20
|
FormPluginOptions,
|
|
19
21
|
FormTemplateItem,
|
|
@@ -23,7 +25,7 @@ import type {
|
|
|
23
25
|
FormTemplate,
|
|
24
26
|
} from './types'
|
|
25
27
|
|
|
26
|
-
function _formFactory<Schema extends FormSchema>(schema: Schema,
|
|
28
|
+
function _formFactory<Schema extends FormSchema>(schema: Schema, options: FormComposableOptions<Schema> = {}) {
|
|
27
29
|
// create injection keys
|
|
28
30
|
const formInjectionKey = Symbol('formInjectionKey') as InjectionKey<InjectedFormData<Schema>>
|
|
29
31
|
const formWrapperInjectionKey = Symbol('formWrapperInjectionKey') as InjectionKey<
|
|
@@ -32,6 +34,9 @@ function _formFactory<Schema extends FormSchema>(schema: Schema, options: FormCo
|
|
|
32
34
|
const formFieldInjectionKey = Symbol('formFieldInjectionKey') as InjectionKey<
|
|
33
35
|
InjectedFormFieldData<Schema>
|
|
34
36
|
>
|
|
37
|
+
const formFieldsGroupInjectionKey = Symbol('formFieldsGroupInjectionKey') as InjectionKey<
|
|
38
|
+
InjectedFormFieldsGroupData<Schema>
|
|
39
|
+
>
|
|
35
40
|
|
|
36
41
|
// create components
|
|
37
42
|
const VvFormWrapper = defineFormWrapper(
|
|
@@ -44,6 +49,11 @@ function _formFactory<Schema extends FormSchema>(schema: Schema, options: FormCo
|
|
|
44
49
|
formFieldInjectionKey,
|
|
45
50
|
options,
|
|
46
51
|
)
|
|
52
|
+
const VvFormFieldsGroup = defineFormFieldsGroup(
|
|
53
|
+
formInjectionKey,
|
|
54
|
+
formWrapperInjectionKey,
|
|
55
|
+
formFieldsGroupInjectionKey,
|
|
56
|
+
)
|
|
47
57
|
const VvFormTemplate = defineFormTemplate(formInjectionKey, VvFormField)
|
|
48
58
|
const {
|
|
49
59
|
clear,
|
|
@@ -77,6 +87,7 @@ function _formFactory<Schema extends FormSchema>(schema: Schema, options: FormCo
|
|
|
77
87
|
validate,
|
|
78
88
|
VvForm,
|
|
79
89
|
VvFormField,
|
|
90
|
+
VvFormFieldsGroup,
|
|
80
91
|
VvFormTemplate,
|
|
81
92
|
VvFormWrapper,
|
|
82
93
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import type { Component, DeepReadonly, Ref, RendererElement, RendererNode, VNode, WatchStopHandle } from 'vue'
|
|
2
|
-
import type {
|
|
2
|
+
import type { z, AnyZodObject, ZodEffects, ZodOptional, ZodTypeAny } from 'zod'
|
|
3
3
|
import type { IgnoredUpdater } from '@vueuse/core'
|
|
4
4
|
import type { FormFieldType, FormStatus } from './enums'
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
type Depth = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // Adjust the depth limit as needed
|
|
7
|
+
|
|
8
|
+
type DecrementDepth<D extends Depth[number]> = Depth[D]
|
|
9
|
+
|
|
10
|
+
export type EffectType<T extends ZodTypeAny, D extends Depth[number] = 10> =
|
|
11
|
+
D extends 0
|
|
12
|
+
? T
|
|
13
|
+
: T | ZodOptional<T> | ZodEffects<EffectType<T, DecrementDepth<D>>>
|
|
14
|
+
|
|
15
|
+
export type FormSchema = EffectType<AnyZodObject>
|
|
10
16
|
|
|
11
17
|
export type FormFieldComponentOptions = {
|
|
12
18
|
lazyLoad?: boolean
|
|
@@ -66,10 +72,15 @@ export type InjectedFormWrapperData<Schema extends FormSchema> = {
|
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
export type InjectedFormFieldData<Schema extends FormSchema> = {
|
|
69
|
-
name: Ref<
|
|
75
|
+
name: Readonly<Ref<Path<z.infer<Schema>>>>
|
|
70
76
|
errors: Readonly<Ref<DeepReadonly<z.inferFormattedError<Schema>>>>
|
|
71
77
|
}
|
|
72
78
|
|
|
79
|
+
export type InjectedFormFieldsGroupData<Schema extends FormSchema> = {
|
|
80
|
+
names: DeepReadonly<Ref<Path<z.infer<Schema>>[]>>
|
|
81
|
+
errors: Readonly<Ref<DeepReadonly<Record<string, z.inferFormattedError<Schema>> | undefined>>>
|
|
82
|
+
}
|
|
83
|
+
|
|
73
84
|
export type Primitive =
|
|
74
85
|
| null
|
|
75
86
|
| undefined
|
package/src/utils.ts
CHANGED
|
@@ -11,15 +11,11 @@ import {
|
|
|
11
11
|
ZodRecord,
|
|
12
12
|
ZodArray,
|
|
13
13
|
} from 'zod'
|
|
14
|
-
import type { FormSchema } from './types'
|
|
14
|
+
import type { EffectType, FormSchema } from './types'
|
|
15
15
|
|
|
16
16
|
export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema, original: Partial<z.infer<Schema>> & Record<string, unknown> = {}): Partial<z.infer<Schema>> {
|
|
17
17
|
const getSchemaInnerType = <Type extends ZodTypeAny>(
|
|
18
|
-
schema:
|
|
19
|
-
| Type
|
|
20
|
-
| ZodEffects<Type>
|
|
21
|
-
| ZodEffects<ZodEffects<Type>>
|
|
22
|
-
| ZodOptional<Type>,
|
|
18
|
+
schema: EffectType<Type>,
|
|
23
19
|
) => {
|
|
24
20
|
let toReturn = schema
|
|
25
21
|
while (toReturn instanceof ZodEffects) {
|
|
@@ -48,9 +44,9 @@ export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema,
|
|
|
48
44
|
}
|
|
49
45
|
const innerType = getSchemaInnerType<AnyZodObject>(schema)
|
|
50
46
|
const unknownKeys
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
= innerType instanceof ZodObject
|
|
48
|
+
? innerType._def.unknownKeys === 'passthrough'
|
|
49
|
+
: false
|
|
54
50
|
return {
|
|
55
51
|
...(unknownKeys ? original : {}),
|
|
56
52
|
...Object.fromEntries(
|
|
@@ -94,7 +90,7 @@ export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema,
|
|
|
94
90
|
(element && typeof element === 'object'
|
|
95
91
|
? element
|
|
96
92
|
: undefined) as Partial<
|
|
97
|
-
|
|
93
|
+
typeof arrayType
|
|
98
94
|
>,
|
|
99
95
|
),
|
|
100
96
|
),
|
|
@@ -105,7 +101,7 @@ export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema,
|
|
|
105
101
|
const valueType = getSchemaInnerType(innerType._def.valueType)
|
|
106
102
|
if (valueType instanceof ZodObject) {
|
|
107
103
|
return [key, Object.keys(originalValue).reduce((acc: Record<string, unknown>, recordKey: string) => {
|
|
108
|
-
acc[recordKey] = defaultObjectBySchema(valueType, originalValue[recordKey])
|
|
104
|
+
acc[recordKey] = defaultObjectBySchema(valueType, (originalValue as Record<string, unknown>)[recordKey] as Partial<any> & Record<string, unknown>)
|
|
109
105
|
return acc
|
|
110
106
|
}, {})]
|
|
111
107
|
}
|
|
@@ -117,7 +113,7 @@ export function defaultObjectBySchema<Schema extends FormSchema>(schema: Schema,
|
|
|
117
113
|
innerType,
|
|
118
114
|
originalValue
|
|
119
115
|
&& typeof originalValue === 'object'
|
|
120
|
-
? originalValue
|
|
116
|
+
? (originalValue as Partial<z.infer<Schema>> & Record<string, unknown>)
|
|
121
117
|
: defaultValue,
|
|
122
118
|
),
|
|
123
119
|
]
|