-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
vnode.ts
250 lines (195 loc) · 6.15 KB
/
vnode.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
247
248
249
250
// VNode Utils
import { find, isCallable, isNullOrUndefined, includes, normalizeRules, isSpecified } from './index';
import Vue, { VNode, VNodeDirective } from 'vue';
export const isTextInput = (vnode: VNode): boolean => {
const attrs = (vnode.data && vnode.data.attrs) || vnode.elm;
// it will fallback to being a text input per browsers spec.
if (vnode.tag === 'input' && (!attrs || !attrs.type)) {
return true;
}
if (vnode.tag === 'textarea') {
return true;
}
return includes(['text', 'password', 'search', 'email', 'tel', 'url', 'number'], attrs && attrs.type);
};
// export const isCheckboxOrRadioInput = (vnode: VNode): boolean => {
// const attrs = (vnode.data && vnode.data.attrs) || vnode.elm;
// return includes(['radio', 'checkbox'], attrs && attrs.type);
// };
// Gets the model object on the vnode.
export function findModel(vnode: VNode): VNodeDirective | undefined {
if (!vnode.data) {
return undefined;
}
// Component Model
// THIS IS NOT TYPED IN OFFICIAL VUE TYPINGS
// eslint-disable-next-line
const nonStandardVNodeData = vnode.data as any;
if ('model' in nonStandardVNodeData) {
return nonStandardVNodeData.model;
}
if (!vnode.data.directives) {
return undefined;
}
return find(vnode.data.directives, d => d.name === 'model');
}
function extractChildren(vnode: VNode | VNode[]): VNode[] {
if (Array.isArray(vnode)) {
return vnode;
}
if (Array.isArray(vnode.children)) {
return vnode.children;
}
/* istanbul ignore next */
if (vnode.componentOptions && Array.isArray(vnode.componentOptions.children)) {
return vnode.componentOptions.children;
}
return [];
}
export function extractVNodes(vnode: VNode | VNode[]): VNode[] {
if (!Array.isArray(vnode) && findModel(vnode)) {
return [vnode];
}
const children = extractChildren(vnode);
return children.reduce((nodes: VNode[], node): VNode[] => {
const candidates = extractVNodes(node);
if (candidates.length) {
nodes.push(...candidates);
}
return nodes;
}, []);
}
// Resolves v-model config if exists.
export function findModelConfig(vnode: VNode): { prop: string; event: string } | null {
/* istanbul ignore next */
if (!vnode.componentOptions) return null;
// This is also not typed in the standard Vue TS.
return (vnode.componentOptions.Ctor as any).options.model;
}
// Adds a listener to vnode listener object.
export function mergeVNodeListeners(obj: any, eventName: string, handler: Function): void {
// Has a single listener.
if (isCallable(obj[eventName])) {
const prevHandler = obj[eventName];
obj[eventName] = [prevHandler];
}
// has other listeners.
if (Array.isArray(obj[eventName])) {
obj[eventName].push(handler);
return;
}
// no listener at all.
if (isNullOrUndefined(obj[eventName])) {
obj[eventName] = [handler];
}
}
// Adds a listener to a native HTML vnode.
function addNativeNodeListener(node: VNode, eventName: string, handler: Function): void {
/* istanbul ignore next */
if (!node.data) {
node.data = {};
}
if (isNullOrUndefined(node.data.on)) {
node.data.on = {};
}
mergeVNodeListeners(node.data.on, eventName, handler);
}
// Adds a listener to a Vue component vnode.
function addComponentNodeListener(node: VNode, eventName: string, handler: Function): void {
/* istanbul ignore next */
if (!node.componentOptions) {
return;
}
/* istanbul ignore next */
if (!node.componentOptions.listeners) {
node.componentOptions.listeners = {};
}
mergeVNodeListeners(node.componentOptions.listeners, eventName, handler);
}
export function addVNodeListener(vnode: VNode, eventName: string, handler: Function): void {
if (vnode.componentOptions) {
addComponentNodeListener(vnode, eventName, handler);
return;
}
addNativeNodeListener(vnode, eventName, handler);
}
// Determines if `change` should be used over `input` for listeners.
export function getInputEventName(vnode: VNode, model?: VNodeDirective): string {
// Is a component.
if (vnode.componentOptions) {
const { event } = findModelConfig(vnode) || { event: 'input' };
return event;
}
// Lazy Models typically use change event
if (model && model.modifiers && model.modifiers.lazy) {
return 'change';
}
// is a textual-type input.
if (isTextInput(vnode)) {
return 'input';
}
return 'change';
}
// TODO: Type this one properly.
export function normalizeSlots(slots: any, ctx: Vue): VNode[] {
const acc: VNode[] = [];
return Object.keys(slots).reduce((arr, key): VNode[] => {
slots[key].forEach((vnode: VNode): void => {
if (!vnode.context) {
slots[key].context = ctx;
if (!vnode.data) {
vnode.data = {};
}
vnode.data.slot = key;
}
});
return arr.concat(slots[key]);
}, acc);
}
function resolveTextualRules(vnode: VNode): Record<string, any> {
const attrs = vnode.data && vnode.data.attrs;
const rules: Record<string, any> = {};
if (!attrs) return rules;
if (attrs.type === 'email') {
rules.email = ['multiple' in attrs];
}
if (attrs.pattern) {
rules.regex = attrs.pattern;
}
if (attrs.maxlength >= 0) {
rules.max = attrs.maxlength;
}
if (attrs.minlength >= 0) {
rules.min = attrs.minlength;
}
if (attrs.type === 'number') {
if (isSpecified(attrs.min)) {
rules.min_value = Number(attrs.min);
}
if (isSpecified(attrs.max)) {
rules.max_value = Number(attrs.max);
}
}
return rules;
}
export function resolveRules(vnode: VNode) {
const htmlTags = ['input', 'select'];
const attrs = vnode.data && vnode.data.attrs;
if (!includes(htmlTags, vnode.tag) || !attrs) {
return {};
}
const rules: Record<string, any> = {};
if ('required' in attrs && attrs.required !== false) {
rules.required = attrs.type === 'checkbox' ? [true] : true;
}
if (isTextInput(vnode)) {
return normalizeRules({ ...rules, ...resolveTextualRules(vnode) });
}
return normalizeRules(rules);
}
export function normalizeChildren(context: Vue, slotProps: any): VNode[] {
if (context.$scopedSlots.default) {
return context.$scopedSlots.default(slotProps) || [];
}
return context.$slots.default || [];
}