/
Field.ts
246 lines (220 loc) · 6.63 KB
/
Field.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { h, defineComponent, toRef, SetupContext, resolveDynamicComponent, computed, watch, PropType } from 'vue';
import { getConfig } from './config';
import { useField } from './useField';
import { normalizeChildren, hasCheckedAttr, shouldHaveValueBinding, isPropPresent } from './utils';
import { toNumber } from '../../shared';
import { EMPTY_VALUE } from './symbols';
interface ValidationTriggersProps {
validateOnMount: boolean;
validateOnBlur: boolean;
validateOnChange: boolean;
validateOnInput: boolean;
validateOnModelUpdate: boolean;
}
export const Field = defineComponent({
name: 'Field',
inheritAttrs: false,
props: {
as: {
type: [String, Object],
default: undefined,
},
name: {
type: String,
required: true,
},
rules: {
type: [Object, String, Function],
default: undefined,
},
validateOnMount: {
type: Boolean,
default: false,
},
validateOnBlur: {
type: Boolean,
default: undefined,
},
validateOnChange: {
type: Boolean,
default: undefined,
},
validateOnInput: {
type: Boolean,
default: undefined,
},
validateOnModelUpdate: {
type: Boolean,
default: undefined,
},
bails: {
type: Boolean,
default: () => getConfig().bails,
},
label: {
type: String,
default: undefined,
},
uncheckedValue: {
type: null,
default: undefined,
},
modelValue: {
type: null,
default: EMPTY_VALUE,
},
modelModifiers: {
type: null,
default: () => ({}),
},
'onUpdate:modelValue': {
type: null as unknown as PropType<((e: any) => unknown) | undefined>,
default: undefined,
},
standalone: {
type: Boolean,
default: false,
},
},
setup(props, ctx) {
const rules = toRef(props, 'rules');
const name = toRef(props, 'name');
const label = toRef(props, 'label');
const uncheckedValue = toRef(props, 'uncheckedValue');
const hasModelEvents = isPropPresent(props, 'onUpdate:modelValue');
const {
errors,
value,
errorMessage,
validate: validateField,
handleChange,
handleBlur,
handleInput,
setTouched,
resetField,
handleReset,
meta,
checked,
setErrors,
} = useField(name, rules, {
validateOnMount: props.validateOnMount,
bails: props.bails,
standalone: props.standalone,
type: ctx.attrs.type as string,
initialValue: resolveInitialValue(props, ctx),
// Only for checkboxes and radio buttons
checkedValue: ctx.attrs.value,
uncheckedValue,
label,
validateOnValueUpdate: false,
});
// If there is a v-model applied on the component we need to emit the `update:modelValue` whenever the value binding changes
const onChangeHandler = hasModelEvents
? function handleChangeWithModel(e: unknown, shouldValidate = true) {
handleChange(e, shouldValidate);
ctx.emit('update:modelValue', value.value);
}
: handleChange;
const onInputHandler = hasModelEvents
? function handleChangeWithModel(e: any) {
handleInput(e);
ctx.emit('update:modelValue', value.value);
}
: handleInput;
const fieldProps = computed(() => {
const { validateOnInput, validateOnChange, validateOnBlur, validateOnModelUpdate } =
resolveValidationTriggers(props);
const baseOnBlur = [handleBlur, ctx.attrs.onBlur, validateOnBlur ? validateField : undefined].filter(Boolean);
const baseOnInput = [(e: unknown) => onChangeHandler(e, validateOnInput), ctx.attrs.onInput].filter(Boolean);
const baseOnChange = [(e: unknown) => onChangeHandler(e, validateOnChange), ctx.attrs.onChange].filter(Boolean);
const attrs: Record<string, any> = {
name: props.name,
onBlur: baseOnBlur,
onInput: baseOnInput,
onChange: baseOnChange,
};
if (validateOnModelUpdate) {
attrs['onUpdate:modelValue'] = [onChangeHandler];
}
if (hasCheckedAttr(ctx.attrs.type) && checked) {
attrs.checked = checked.value;
} else {
attrs.value = value.value;
}
const tag = resolveTag(props, ctx);
if (shouldHaveValueBinding(tag, ctx.attrs)) {
delete attrs.value;
}
return attrs;
});
const modelValue = toRef(props, 'modelValue');
watch(modelValue, newModelValue => {
if (newModelValue !== applyModifiers(value.value, props.modelModifiers)) {
value.value = newModelValue;
validateField();
}
});
function slotProps() {
return {
field: fieldProps.value,
value: value.value,
meta,
errors: errors.value,
errorMessage: errorMessage.value,
validate: validateField,
resetField,
handleChange: onChangeHandler,
handleInput: onInputHandler,
handleReset,
handleBlur,
setTouched,
setErrors,
};
}
return () => {
const tag = resolveDynamicComponent(resolveTag(props, ctx)) as string;
const children = normalizeChildren(tag, ctx, slotProps);
if (tag) {
return h(
tag,
{
...ctx.attrs,
...fieldProps.value,
},
children
);
}
return children;
};
},
});
function resolveTag(props: Record<string, any>, ctx: SetupContext<any>) {
let tag: string = props.as || '';
if (!props.as && !ctx.slots.default) {
tag = 'input';
}
return tag;
}
function resolveValidationTriggers(props: Partial<ValidationTriggersProps>) {
const { validateOnInput, validateOnChange, validateOnBlur, validateOnModelUpdate } = getConfig();
return {
validateOnInput: props.validateOnInput ?? validateOnInput,
validateOnChange: props.validateOnChange ?? validateOnChange,
validateOnBlur: props.validateOnBlur ?? validateOnBlur,
validateOnModelUpdate: props.validateOnModelUpdate ?? validateOnModelUpdate,
};
}
function applyModifiers(value: unknown, modifiers: Record<string, boolean>) {
if (modifiers.number) {
return toNumber(value as string);
}
return value;
}
function resolveInitialValue(props: Record<string, unknown>, ctx: SetupContext<any>) {
// Gets the initial value either from `value` prop/attr or `v-model` binding (modelValue)
// For checkboxes and radio buttons it will always be the model value not the `value` attribute
if (!hasCheckedAttr(ctx.attrs.type)) {
return isPropPresent(props, 'modelValue') ? props.modelValue : ctx.attrs.value;
}
return isPropPresent(props, 'modelValue') ? props.modelValue : undefined;
}