@@ -35,6 +35,22 @@ export const FormItemProps = {
35
35
fieldDecoratorId : PropTypes . string ,
36
36
fieldDecoratorOptions : PropTypes . object ,
37
37
} ;
38
+ function comeFromSlot ( vnodes = [ ] , itemVnode ) {
39
+ let isSlot = false ;
40
+ for ( let i = 0 , len = vnodes . length ; i < len ; i ++ ) {
41
+ const vnode = vnodes [ i ] ;
42
+ if ( vnode && ( vnode === itemVnode || vnode . $vnode === itemVnode ) ) {
43
+ isSlot = true ;
44
+ } else {
45
+ const children = vnode . componentOptions ? vnode . componentOptions . children : vnode . children ;
46
+ isSlot = comeFromSlot ( children , itemVnode ) ;
47
+ }
48
+ if ( isSlot ) {
49
+ break ;
50
+ }
51
+ }
52
+ return isSlot ;
53
+ }
38
54
39
55
export default {
40
56
name : 'AFormItem' ,
@@ -51,7 +67,20 @@ export default {
51
67
collectFormItemContext : { default : ( ) => noop } ,
52
68
} ,
53
69
data ( ) {
54
- this . collectFormItemContext ( this . $vnode . context ) ;
70
+ const { templateContext = { } } = this . FormProps . form || { } ;
71
+ const vnodes = Object . values ( templateContext . $slots || { } ) . reduce ( ( a , b ) => {
72
+ return [ ...a , ...b ] ;
73
+ } , [ ] ) ;
74
+ const isSlot = comeFromSlot ( vnodes , this . $vnode ) ;
75
+ warning ( ! isSlot , 'You can not set FormItem from slot, please use slot-scope instead slot' ) ;
76
+ let isSlotScope = false ;
77
+ // 进一步判断是否是通过slot-scope传递
78
+ if ( ! isSlot && this . $vnode . context !== templateContext ) {
79
+ isSlotScope = comeFromSlot ( this . $vnode . context . $children , templateContext . $vnode ) ;
80
+ }
81
+ if ( ! isSlotScope && ! isSlot ) {
82
+ this . collectFormItemContext ( this . $vnode . context ) ;
83
+ }
55
84
return { helpShow : false } ;
56
85
} ,
57
86
beforeDestroy ( ) {
0 commit comments