@@ -69,10 +69,10 @@ const getSlots = ele => {
69
69
} ;
70
70
const getSlot = ( self , name = 'default' , options = { } ) => {
71
71
let res = self . $slots [ name ] && self . $slots [ name ] ( options ) ;
72
- while ( res && res . length === 1 && res [ 0 ] . type === Fragment ) {
73
- res = res [ 0 ] . children ;
72
+ while ( res && res . length === 1 && ( res [ 0 ] . type === Fragment || Array . isArray ( res [ 0 ] ) ) ) {
73
+ res = res [ 0 ] . children || res [ 0 ] ;
74
74
}
75
- return res ;
75
+ return res && res . __v_isVNode ? [ res ] : res ;
76
76
} ;
77
77
78
78
const getAllChildren = ele => {
@@ -93,6 +93,13 @@ const getSlotOptions = ele => {
93
93
}
94
94
return componentOptions ? componentOptions . Ctor . options || { } : { } ;
95
95
} ;
96
+ const findDOMNode = instance => {
97
+ let node = instance . $el ;
98
+ while ( ! node . tagName ) {
99
+ node = node . nextSibling ;
100
+ }
101
+ return node ;
102
+ } ;
96
103
const getOptionProps = instance => {
97
104
const res = { } ;
98
105
if ( instance . $ && instance . $ . vnode ) {
@@ -105,27 +112,42 @@ const getOptionProps = instance => {
105
112
}
106
113
} ) ;
107
114
} else if ( isVNode ( instance ) && typeof instance . type === 'object' ) {
108
- const props = instance . props || { } ;
115
+ const originProps = instance . props || { } ;
116
+ const props = { } ;
117
+ Object . keys ( originProps ) . forEach ( key => {
118
+ props [ camelize ( key ) ] = originProps [ key ] ;
119
+ } ) ;
109
120
const options = instance . type . props ;
110
121
Object . keys ( options ) . forEach ( k => {
111
- const hyphenateKey = hyphenate ( k ) ;
112
- const v = resolvePropValue ( options , props , k , props [ hyphenateKey ] , hyphenateKey ) ;
113
- if ( v !== undefined || hyphenateKey in props ) {
122
+ const v = resolvePropValue ( options , props , k , props [ k ] ) ;
123
+ if ( v !== undefined || k in props ) {
114
124
res [ k ] = v ;
115
125
}
116
126
} ) ;
117
127
}
118
128
return res ;
119
129
} ;
120
130
const getComponent = ( instance , prop , options = instance , execute = true ) => {
121
- const temp = instance [ prop ] ;
122
- if ( temp !== undefined ) {
123
- return typeof temp === 'function' && execute ? temp ( options ) : temp ;
124
- } else {
125
- let com = instance . $slots [ prop ] ;
126
- com = execute && com ? com ( options ) : com ;
127
- return Array . isArray ( com ) && com . length === 1 ? com [ 0 ] : com ;
131
+ if ( instance . $ ) {
132
+ const temp = instance [ prop ] ;
133
+ if ( temp !== undefined ) {
134
+ return typeof temp === 'function' && execute ? temp ( options ) : temp ;
135
+ } else {
136
+ let com = instance . $slots [ prop ] ;
137
+ com = execute && com ? com ( options ) : com ;
138
+ return Array . isArray ( com ) && com . length === 1 ? com [ 0 ] : com ;
139
+ }
140
+ } else if ( isVNode ( instance ) ) {
141
+ const temp = instance . props && instance . props [ prop ] ;
142
+ if ( temp !== undefined ) {
143
+ return typeof temp === 'function' && execute ? temp ( options ) : temp ;
144
+ } else if ( instance . children && instance . children [ prop ] ) {
145
+ let com = instance . children [ prop ] ;
146
+ com = execute && com ? com ( options ) : com ;
147
+ return Array . isArray ( com ) && com . length === 1 ? com [ 0 ] : com ;
148
+ }
128
149
}
150
+ return undefined ;
129
151
} ;
130
152
const getComponentFromProp = ( instance , prop , options = instance , execute = true ) => {
131
153
if ( instance . $createElement ) {
@@ -169,19 +191,31 @@ const getComponentFromProp = (instance, prop, options = instance, execute = true
169
191
} ;
170
192
171
193
const getAllProps = ele => {
172
- let data = ele . data || { } ;
173
- let componentOptions = ele . componentOptions || { } ;
174
- if ( ele . $vnode ) {
175
- data = ele . $vnode . data || { } ;
176
- componentOptions = ele . $vnode . componentOptions || { } ;
194
+ let props = getOptionProps ( ele ) ;
195
+ if ( ele . $ ) {
196
+ props = { ... props , ... this . $attrs } ;
197
+ } else {
198
+ props = { ... ele . props , ... props } ;
177
199
}
178
- return { ... data . props , ... data . attrs , ... componentOptions . propsData } ;
200
+ return props ;
179
201
} ;
180
202
181
- // 使用 getOptionProps 替换 ,待测试
182
- const getPropsData = ele => {
183
- return getOptionProps ( ele ) ;
184
- //return ele.props || {};
203
+ const getPropsData = vnode => {
204
+ const res = { } ;
205
+ const originProps = vnode . props || { } ;
206
+ const props = { } ;
207
+ Object . keys ( originProps ) . forEach ( key => {
208
+ props [ camelize ( key ) ] = originProps [ key ] ;
209
+ } ) ;
210
+ const options = isPlainObject ( vnode . type ) ? vnode . type . props : { } ;
211
+ Object . keys ( options ) . forEach ( k => {
212
+ const v = resolvePropValue ( options , props , k , props [ k ] ) ;
213
+ if ( k in props ) {
214
+ // 仅包含 props,不包含默认值
215
+ res [ k ] = v ;
216
+ }
217
+ } ) ;
218
+ return { ...props , ...res } ; // 合并事件、未声明属性等
185
219
} ;
186
220
const getValueByProp = ( ele , prop ) => {
187
221
return getPropsData ( ele ) [ prop ] ;
@@ -203,18 +237,16 @@ const getKey = ele => {
203
237
return key ;
204
238
} ;
205
239
206
- export function getEvents ( child ) {
207
- const { $attrs } = child ;
208
- return splitAttrs ( $attrs ) . events ;
209
-
210
- // let events = {};
211
- // if (child.componentOptions && child.componentOptions.listeners) {
212
- // events = child.componentOptions.listeners;
213
- // } else if (child.data && child.data.on) {
214
- // events = child.data.on;
215
- // }
216
- // return { ...events };
240
+ export function getEvents ( ele , on = true ) {
241
+ let props = { } ;
242
+ if ( ele . $ ) {
243
+ props = { ...props , ...ele . $attrs } ;
244
+ } else {
245
+ props = { ...props , ...ele . props } ;
246
+ }
247
+ return splitAttrs ( props ) [ on ? 'onEvents' : 'events' ] ;
217
248
}
249
+
218
250
export function getEvent ( child , event ) {
219
251
return child . props && child . props [ event ] ;
220
252
}
@@ -270,6 +302,10 @@ export function getComponentName(opts) {
270
302
return opts && ( opts . Ctor . options . name || opts . tag ) ;
271
303
}
272
304
305
+ export function isFragment ( c ) {
306
+ return c . length === 1 && c [ 0 ] . type === Fragment ;
307
+ }
308
+
273
309
export function isEmptyElement ( c ) {
274
310
return c . type === Comment || ( c . type === Text && c . children . trim ( ) === '' ) ;
275
311
}
@@ -279,6 +315,9 @@ export function isStringElement(c) {
279
315
}
280
316
281
317
export function filterEmpty ( children = [ ] ) {
318
+ if ( isFragment ( children ) ) {
319
+ return children [ 0 ] . children . filter ( c => ! isEmptyElement ( c ) ) ;
320
+ }
282
321
return children . filter ( c => ! isEmptyElement ( c ) ) ;
283
322
}
284
323
const initDefaultProps = ( propTypes , defaultProps ) => {
@@ -332,5 +371,6 @@ export {
332
371
getSlot ,
333
372
getAllProps ,
334
373
getAllChildren ,
374
+ findDOMNode ,
335
375
} ;
336
376
export default hasProp ;
0 commit comments