/
Form.ts
190 lines (176 loc) · 4.49 KB
/
Form.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { h, defineComponent, toRef, resolveDynamicComponent, PropType, VNode, UnwrapRef } from 'vue';
import { useForm } from './useForm';
import { SubmissionHandler, InvalidSubmissionHandler } from './types';
import { isEvent, normalizeChildren } from './utils';
import { FormContext } from '.';
type FormSlotProps = UnwrapRef<
Pick<
FormContext,
| 'meta'
| 'errors'
| 'values'
| 'isSubmitting'
| 'submitCount'
| 'validate'
| 'validateField'
| 'handleReset'
| 'submitForm'
| 'setErrors'
| 'setFieldError'
| 'setFieldValue'
| 'setValues'
| 'setFieldTouched'
| 'setTouched'
| 'resetForm'
>
> & {
handleSubmit: (evt: Event | SubmissionHandler, onSubmit?: SubmissionHandler) => Promise<unknown>;
};
const FormImpl = defineComponent({
name: 'Form',
inheritAttrs: false,
props: {
as: {
type: String,
default: 'form',
},
validationSchema: {
type: Object,
default: undefined,
},
initialValues: {
type: Object,
default: undefined,
},
initialErrors: {
type: Object,
default: undefined,
},
initialTouched: {
type: Object,
default: undefined,
},
validateOnMount: {
type: Boolean,
default: false,
},
onSubmit: {
type: Function as PropType<SubmissionHandler>,
default: undefined,
},
onInvalidSubmit: {
type: Function as PropType<InvalidSubmissionHandler>,
default: undefined,
},
},
setup(props, ctx) {
const initialValues = toRef(props, 'initialValues');
const validationSchema = toRef(props, 'validationSchema');
const {
errors,
values,
meta,
isSubmitting,
submitCount,
validate,
validateField,
handleReset,
resetForm,
handleSubmit,
submitForm,
setErrors,
setFieldError,
setFieldValue,
setValues,
setFieldTouched,
setTouched,
} = useForm({
validationSchema: validationSchema.value ? validationSchema : undefined,
initialValues,
initialErrors: props.initialErrors,
initialTouched: props.initialTouched,
validateOnMount: props.validateOnMount,
});
const onSubmit = props.onSubmit ? handleSubmit(props.onSubmit, props.onInvalidSubmit) : submitForm;
function handleFormReset(e?: Event) {
if (isEvent(e)) {
// Prevent default form reset behavior
e.preventDefault();
}
handleReset();
if (typeof ctx.attrs.onReset === 'function') {
ctx.attrs.onReset();
}
}
function handleScopedSlotSubmit(evt: Event | SubmissionHandler, onSubmit?: SubmissionHandler) {
const onSuccess = typeof evt === 'function' && !onSubmit ? evt : onSubmit;
return handleSubmit(onSuccess as SubmissionHandler<Record<string, unknown>>, props.onInvalidSubmit)(evt as Event);
}
function slotProps(): FormSlotProps {
return {
meta: meta.value,
errors: errors.value,
values: values,
isSubmitting: isSubmitting.value,
submitCount: submitCount.value,
validate,
validateField,
handleSubmit: handleScopedSlotSubmit,
handleReset,
submitForm,
setErrors,
setFieldError,
setFieldValue,
setValues,
setFieldTouched,
setTouched,
resetForm,
};
}
// expose these functions and methods as part of public API
ctx.expose({
setFieldError,
setErrors,
setFieldValue,
setValues,
setFieldTouched,
setTouched,
resetForm,
validate,
validateField,
});
return function renderForm() {
// avoid resolving the form component as itself
const tag = props.as === 'form' ? props.as : (resolveDynamicComponent(props.as) as string);
const children = normalizeChildren(tag, ctx, slotProps as any);
if (!props.as) {
return children;
}
// Attributes to add on a native `form` tag
const formAttrs =
props.as === 'form'
? {
// Disables native validation as vee-validate will handle it.
novalidate: true,
}
: {};
return h(
tag,
{
...formAttrs,
...ctx.attrs,
onSubmit,
onReset: handleFormReset,
},
children
);
};
},
});
export const Form = FormImpl as typeof FormImpl & {
new (): {
$slots: {
default: (arg: FormSlotProps) => VNode[];
};
};
};