-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
56 lines (53 loc) · 1.52 KB
/
index.ts
File metadata and controls
56 lines (53 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
import { Ref } from 'vue'
import { BaseComponentBinds, ComponentBindsConfig, GenericObject, LazyComponentBindsConfig, MaybeRefOrLazy, Path, PathValue } from './vee-validate.types'
import { ObjectKeys } from './utils'
/**
* @example
<script setup lang="ts">
const { handleSubmit, values, resetForm, defineComponentBinds } = useForm<ZodSchemaType>({
validationSchema,
})
const { formBinds } = useBinds(values, defineComponentBinds);
</script>
<template>
<v-form>
<v-text-field
v-bind="formBinds.phone.value"
label="Phone"
/>
<v-text-field
v-bind="formBinds.password.value"
label="Password"
/>
</v-form>
</template>
*/
export function useBinds<
TValues extends GenericObject = GenericObject
>(
values: TValues,
defineComponentBinds: <
TPath extends Path<TValues>,
TValue = PathValue<TValues, TPath>,
TExtras extends GenericObject = GenericObject
>(
path: MaybeRefOrLazy<TPath>,
config?:
| Partial<ComponentBindsConfig<TValue, TExtras>>
| LazyComponentBindsConfig<TValue, TExtras>
) => Ref<BaseComponentBinds<TValue> & TExtras>
) {
//
type FormType = {
[k in keyof TValues]: Ref<
BaseComponentBinds<any> & { 'error-messages':string[] } & GenericObject
>
}
const formBinds: FormType = {} as FormType
ObjectKeys(values).forEach((field) => {
formBinds[field] = defineComponentBinds(field as Path<TValues>, {
mapProps: (state) => ({ 'error-messages': state.errors })
})
})
return { formBinds }
}