@volverjs/form-vue 1.0.0-beta.9 → 1.0.1
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 +412 -245
- package/dist/VvForm.d.ts +135 -0
- package/dist/VvFormField.d.ts +130 -0
- package/dist/VvFormFieldsGroup.d.ts +109 -0
- package/dist/VvFormTemplate.d.ts +40 -0
- package/dist/VvFormWrapper.d.ts +65 -0
- package/dist/{src/enums.d.ts → enums.d.ts} +1 -0
- package/dist/index.d.ts +972 -1
- package/dist/index.es.js +948 -596
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts +93 -0
- package/dist/utils.d.ts +3 -0
- package/package.json +61 -60
- package/src/VvForm.ts +359 -300
- package/src/VvFormField.ts +366 -334
- package/src/VvFormFieldsGroup.ts +382 -0
- package/src/VvFormTemplate.ts +185 -171
- package/src/VvFormWrapper.ts +198 -161
- package/src/enums.ts +27 -26
- package/src/index.ts +157 -134
- package/src/types.ts +162 -125
- package/src/utils.ts +121 -100
- package/dist/src/VvForm.d.ts +0 -202
- package/dist/src/VvFormField.d.ts +0 -116
- package/dist/src/VvFormTemplate.d.ts +0 -60
- package/dist/src/VvFormWrapper.d.ts +0 -107
- package/dist/src/index.d.ts +0 -1498
- package/dist/src/types.d.ts +0 -70
- 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/VvFormWrapper.ts
CHANGED
|
@@ -1,171 +1,208 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type Ref,
|
|
4
|
-
computed,
|
|
5
|
-
defineComponent,
|
|
6
|
-
inject,
|
|
7
|
-
provide,
|
|
8
|
-
readonly,
|
|
9
|
-
ref,
|
|
10
|
-
toRefs,
|
|
11
|
-
watch,
|
|
12
|
-
h,
|
|
13
|
-
type DeepReadonly,
|
|
14
|
-
} from 'vue'
|
|
15
|
-
import type { TypeOf, z } from 'zod'
|
|
1
|
+
import type { DeepReadonly, InjectionKey, Ref, SlotsType } from 'vue'
|
|
2
|
+
import type { z } from 'zod'
|
|
16
3
|
import type {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
4
|
+
FormSchema,
|
|
5
|
+
InjectedFormData,
|
|
6
|
+
InjectedFormWrapperData,
|
|
7
|
+
Path,
|
|
20
8
|
} from './types'
|
|
9
|
+
import {
|
|
10
|
+
computed,
|
|
11
|
+
defineComponent,
|
|
12
|
+
h,
|
|
13
|
+
inject,
|
|
14
|
+
onBeforeUnmount,
|
|
15
|
+
onMounted,
|
|
16
|
+
provide,
|
|
17
|
+
readonly,
|
|
18
|
+
ref,
|
|
19
|
+
toRefs,
|
|
20
|
+
watch,
|
|
21
|
+
} from 'vue'
|
|
22
|
+
|
|
23
|
+
export function defineFormWrapper<Schema extends FormSchema, Type = undefined>(formProvideKey: InjectionKey<InjectedFormData<Schema, Type>>, wrapperProvideKey: InjectionKey<InjectedFormWrapperData<Schema>>) {
|
|
24
|
+
return defineComponent({
|
|
25
|
+
name: 'VvFormWrapper',
|
|
26
|
+
props: {
|
|
27
|
+
name: {
|
|
28
|
+
type: String,
|
|
29
|
+
required: true,
|
|
30
|
+
},
|
|
31
|
+
tag: {
|
|
32
|
+
type: String,
|
|
33
|
+
default: undefined,
|
|
34
|
+
},
|
|
35
|
+
readonly: {
|
|
36
|
+
type: Boolean,
|
|
37
|
+
default: false,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
emits: ['invalid', 'valid'],
|
|
41
|
+
expose: [
|
|
42
|
+
'clear',
|
|
43
|
+
'errors',
|
|
44
|
+
'fields',
|
|
45
|
+
'fieldsErrors',
|
|
46
|
+
'formData',
|
|
47
|
+
'invalid',
|
|
48
|
+
'readonly',
|
|
49
|
+
'reset',
|
|
50
|
+
'submit',
|
|
51
|
+
'tag',
|
|
52
|
+
'validate',
|
|
53
|
+
'validateWrapper',
|
|
54
|
+
],
|
|
55
|
+
slots: Object as SlotsType<{
|
|
56
|
+
default: {
|
|
57
|
+
errors?: DeepReadonly<z.inferFormattedError<Schema>>
|
|
58
|
+
fieldsErrors: Map<string, z.inferFormattedError<Schema>>
|
|
59
|
+
formData?: undefined extends Type ? Partial<z.infer<Schema>> : Type
|
|
60
|
+
formErrors?: DeepReadonly<z.inferFormattedError<Schema>>
|
|
61
|
+
invalid: boolean
|
|
62
|
+
readonly: boolean
|
|
63
|
+
clear?: InjectedFormData<Schema, Type>['clear']
|
|
64
|
+
reset?: InjectedFormData<Schema, Type>['reset']
|
|
65
|
+
submit?: InjectedFormData<Schema, Type>['submit']
|
|
66
|
+
validate?: InjectedFormData<Schema, Type>['validate']
|
|
67
|
+
validateWrapper?: () => Promise<boolean>
|
|
68
|
+
}
|
|
69
|
+
}>,
|
|
70
|
+
setup(props, { emit }) {
|
|
71
|
+
// inject data from parent form
|
|
72
|
+
const injectedFormData = inject(formProvideKey)
|
|
73
|
+
// inject data from parent form wrapper
|
|
74
|
+
const injectedWrapperData = inject(wrapperProvideKey, undefined)
|
|
75
|
+
const fields: Ref<Map<string, Path<z.infer<Schema>>>> = ref(new Map())
|
|
76
|
+
const fieldsErrors: Ref<
|
|
77
|
+
Map<string, z.inferFormattedError<Schema>>
|
|
78
|
+
> = ref(new Map())
|
|
79
|
+
const { name } = toRefs(props)
|
|
80
|
+
|
|
81
|
+
// invalid
|
|
82
|
+
const isInvalid = computed(() => {
|
|
83
|
+
if (!injectedFormData?.invalid.value) {
|
|
84
|
+
return false
|
|
85
|
+
}
|
|
86
|
+
return fieldsErrors.value.size > 0
|
|
87
|
+
})
|
|
88
|
+
watch(isInvalid, (newValue) => {
|
|
89
|
+
if (newValue) {
|
|
90
|
+
emit('invalid')
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
emit('valid')
|
|
94
|
+
})
|
|
21
95
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
wrapperProvideKey: InjectionKey<InjectedFormWrapperData<Schema>>,
|
|
25
|
-
) => {
|
|
26
|
-
const VvFormWrapper = defineComponent({
|
|
27
|
-
name: 'VvFormWrapper',
|
|
28
|
-
props: {
|
|
29
|
-
name: {
|
|
30
|
-
type: String,
|
|
31
|
-
required: true,
|
|
32
|
-
},
|
|
33
|
-
tag: {
|
|
34
|
-
type: String,
|
|
35
|
-
default: undefined,
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
emits: ['invalid', 'valid'],
|
|
39
|
-
expose: ['fields', 'invalid'],
|
|
40
|
-
setup(props, { emit }) {
|
|
41
|
-
const injectedFormData = inject(formProvideKey)
|
|
42
|
-
const wrapperProvided = inject(wrapperProvideKey, undefined)
|
|
43
|
-
const fields = ref(new Set<string>())
|
|
44
|
-
const fieldsErrors: Ref<
|
|
45
|
-
Map<string, z.inferFormattedError<Schema>>
|
|
46
|
-
> = ref(new Map())
|
|
47
|
-
const { name } = toRefs(props)
|
|
96
|
+
// readonly
|
|
97
|
+
const isReadonly = computed(() => injectedFormData?.readonly.value || props.readonly)
|
|
48
98
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
99
|
+
// provide data to child fields
|
|
100
|
+
const providedData = {
|
|
101
|
+
name: readonly(name),
|
|
102
|
+
errors: fieldsErrors,
|
|
103
|
+
invalid: readonly(isInvalid),
|
|
104
|
+
readonly: readonly(isReadonly),
|
|
105
|
+
fields,
|
|
106
|
+
}
|
|
107
|
+
provide(wrapperProvideKey, providedData)
|
|
55
108
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
109
|
+
// add fields to parent wrapper
|
|
110
|
+
const computedFields = computed(() => new Map(fields.value))
|
|
111
|
+
watch(
|
|
112
|
+
computedFields,
|
|
113
|
+
(newValue, oldValue) => {
|
|
114
|
+
if (injectedWrapperData?.fields) {
|
|
115
|
+
oldValue.forEach((_field, key) => {
|
|
116
|
+
if (!newValue.has(key)) {
|
|
117
|
+
injectedWrapperData?.fields.value.delete(key)
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
newValue.forEach((field, key) => {
|
|
121
|
+
if (!injectedWrapperData?.fields.value.has(key)) {
|
|
122
|
+
injectedWrapperData?.fields.value.set(key, field)
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
{ deep: true },
|
|
128
|
+
)
|
|
68
129
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
130
|
+
// add fields errors to parent wrapper
|
|
131
|
+
watch(
|
|
132
|
+
fieldsErrors,
|
|
133
|
+
(newValue) => {
|
|
134
|
+
if (injectedWrapperData?.errors) {
|
|
135
|
+
fields.value.forEach((field) => {
|
|
136
|
+
if (!newValue.has(field)) {
|
|
137
|
+
injectedWrapperData.errors.value.delete(field)
|
|
138
|
+
}
|
|
139
|
+
if (newValue.has(field)) {
|
|
140
|
+
const value = newValue.get(field)
|
|
141
|
+
if (value) {
|
|
142
|
+
injectedWrapperData.errors.value.set(field, value)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
{ deep: true },
|
|
149
|
+
)
|
|
87
150
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
151
|
+
onMounted(() => {
|
|
152
|
+
if (!injectedFormData?.wrappers || !name.value) {
|
|
153
|
+
console.warn('[@volverjs/form-vue]: Invalid wrapper registration state')
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
if (injectedFormData.wrappers.has(name.value)) {
|
|
157
|
+
console.warn(`[@volverjs/form-vue]: wrapper name "${name.value}" is already used`)
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
injectedFormData.wrappers.set(name.value, providedData)
|
|
161
|
+
})
|
|
162
|
+
onBeforeUnmount(() => {
|
|
163
|
+
if (injectedFormData?.wrappers && name.value) {
|
|
164
|
+
injectedFormData.wrappers.delete(name.value)
|
|
165
|
+
}
|
|
166
|
+
})
|
|
94
167
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
} else {
|
|
99
|
-
emit('valid')
|
|
100
|
-
}
|
|
101
|
-
})
|
|
168
|
+
const validateWrapper = () => {
|
|
169
|
+
return injectedFormData?.validate(undefined, { fields: new Set(fields.value.values()) }) ?? Promise.resolve(true)
|
|
170
|
+
}
|
|
102
171
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* An hack to add types to the default slot
|
|
141
|
-
*/
|
|
142
|
-
return VvFormWrapper as typeof VvFormWrapper & {
|
|
143
|
-
new (): {
|
|
144
|
-
$slots: {
|
|
145
|
-
default: (_: {
|
|
146
|
-
invalid: boolean
|
|
147
|
-
formData: unknown extends
|
|
148
|
-
| Partial<TypeOf<Schema>>
|
|
149
|
-
| undefined
|
|
150
|
-
? undefined
|
|
151
|
-
: Partial<TypeOf<Schema>> | undefined
|
|
152
|
-
submit: () => Promise<boolean>
|
|
153
|
-
validate: () => Promise<boolean>
|
|
154
|
-
errors: Readonly<
|
|
155
|
-
Ref<DeepReadonly<z.inferFormattedError<Schema>>>
|
|
156
|
-
>
|
|
157
|
-
fieldsErrors: Map<
|
|
158
|
-
string,
|
|
159
|
-
Record<
|
|
160
|
-
string,
|
|
161
|
-
{
|
|
162
|
-
_errors: string[]
|
|
163
|
-
}
|
|
164
|
-
>
|
|
165
|
-
>
|
|
166
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
167
|
-
}) => any
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
172
|
+
return {
|
|
173
|
+
errors: injectedFormData?.errors,
|
|
174
|
+
fields,
|
|
175
|
+
fieldsErrors,
|
|
176
|
+
formData: injectedFormData?.formData,
|
|
177
|
+
invalid: isInvalid,
|
|
178
|
+
readonly: isReadonly,
|
|
179
|
+
clear: injectedFormData?.clear,
|
|
180
|
+
reset: injectedFormData?.reset,
|
|
181
|
+
submit: injectedFormData?.submit,
|
|
182
|
+
validate: injectedFormData?.validate,
|
|
183
|
+
validateWrapper,
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
render() {
|
|
187
|
+
const defaultSlot = () =>
|
|
188
|
+
this.$slots.default?.({
|
|
189
|
+
errors: this.errors,
|
|
190
|
+
fieldsErrors: this.fieldsErrors,
|
|
191
|
+
formData: this.formData,
|
|
192
|
+
invalid: this.invalid,
|
|
193
|
+
readonly: this.readonly,
|
|
194
|
+
clear: this.clear,
|
|
195
|
+
reset: this.reset,
|
|
196
|
+
submit: this.submit,
|
|
197
|
+
validate: this.validate,
|
|
198
|
+
validateWrapper: this.validateWrapper,
|
|
199
|
+
})
|
|
200
|
+
if (this.tag) {
|
|
201
|
+
return h(this.tag, null, {
|
|
202
|
+
default: defaultSlot,
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
return defaultSlot()
|
|
206
|
+
},
|
|
207
|
+
})
|
|
171
208
|
}
|
package/src/enums.ts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
export enum FormFieldType {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
text = 'text',
|
|
3
|
+
number = 'number',
|
|
4
|
+
email = 'email',
|
|
5
|
+
password = 'password',
|
|
6
|
+
tel = 'tel',
|
|
7
|
+
url = 'url',
|
|
8
|
+
search = 'search',
|
|
9
|
+
date = 'date',
|
|
10
|
+
time = 'time',
|
|
11
|
+
datetimeLocal = 'datetime-local',
|
|
12
|
+
month = 'month',
|
|
13
|
+
week = 'week',
|
|
14
|
+
color = 'color',
|
|
15
|
+
select = 'select',
|
|
16
|
+
checkbox = 'checkbox',
|
|
17
|
+
radio = 'radio',
|
|
18
|
+
textarea = 'textarea',
|
|
19
|
+
radioGroup = 'radioGroup',
|
|
20
|
+
checkboxGroup = 'checkboxGroup',
|
|
21
|
+
combobox = 'combobox',
|
|
22
|
+
custom = 'custom',
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export enum FormStatus {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
invalid = 'invalid',
|
|
27
|
+
valid = 'valid',
|
|
28
|
+
submitting = 'submitting',
|
|
29
|
+
reset = 'reset',
|
|
30
|
+
updated = 'updated',
|
|
31
|
+
unknown = 'unknown',
|
|
31
32
|
}
|