@@ -3,12 +3,14 @@ import classNames from 'classnames';
3
3
import {
4
4
getSlotOptions ,
5
5
getComponentFromProp ,
6
- isEmptyElement ,
6
+ isStringElement ,
7
7
getListeners ,
8
+ isEmptyElement ,
8
9
} from '../_util/props-util' ;
9
10
import { Col } from '../grid' ;
10
11
import { ConfigConsumerProps } from '../config-provider' ;
11
12
import { ListGridType } from './index' ;
13
+ import { cloneElement } from '../_util/vnode' ;
12
14
13
15
export const ListItemProps = {
14
16
prefixCls : PropTypes . string ,
@@ -68,59 +70,68 @@ export default {
68
70
listContext : { default : ( ) => ( { } ) } ,
69
71
configProvider : { default : ( ) => ConfigConsumerProps } ,
70
72
} ,
73
+ methods : {
74
+ isItemContainsTextNodeAndNotSingular ( ) {
75
+ const { $slots } = this ;
76
+ let result ;
77
+ const children = $slots . default || [ ] ;
78
+ children . forEach ( element => {
79
+ if ( isStringElement ( element ) && ! isEmptyElement ( element ) ) {
80
+ result = true ;
81
+ }
82
+ } ) ;
83
+ return result && children . length > 1 ;
84
+ } ,
85
+
86
+ isFlexMode ( ) {
87
+ const extra = getComponentFromProp ( this , 'extra' ) ;
88
+ const { itemLayout } = this . listContext ;
89
+ if ( itemLayout === 'vertical' ) {
90
+ return ! ! extra ;
91
+ }
92
+ return ! this . isItemContainsTextNodeAndNotSingular ( ) ;
93
+ } ,
94
+ } ,
71
95
render ( ) {
72
- const { grid } = this . listContext ;
96
+ const { grid, itemLayout } = this . listContext ;
73
97
const { prefixCls : customizePrefixCls , $slots } = this ;
74
98
const listeners = getListeners ( this ) ;
75
99
const getPrefixCls = this . configProvider . getPrefixCls ;
76
100
const prefixCls = getPrefixCls ( 'list' , customizePrefixCls ) ;
77
-
78
- const classString = `${ prefixCls } -item` ;
79
101
const extra = getComponentFromProp ( this , 'extra' ) ;
80
102
const actions = getComponentFromProp ( this , 'actions' ) ;
81
- const metaContent = [ ] ;
82
- const otherContent = [ ] ;
83
103
84
- ( $slots . default || [ ] ) . forEach ( element => {
85
- if ( ! isEmptyElement ( element ) ) {
86
- if ( getSlotOptions ( element ) . __ANT_LIST_ITEM_META ) {
87
- metaContent . push ( element ) ;
88
- } else {
89
- otherContent . push ( element ) ;
90
- }
91
- }
92
- } ) ;
93
-
94
- const contentClassString = classNames ( `${ prefixCls } -item-content` , {
95
- [ `${ prefixCls } -item-content-single` ] : metaContent . length < 1 ,
96
- } ) ;
97
- const content =
98
- otherContent . length > 0 ? < div class = { contentClassString } > { otherContent } </ div > : null ;
99
-
100
- let actionsContent ;
101
- if ( actions && actions . length > 0 ) {
102
- const actionsContentItem = ( action , i ) => (
103
- < li key = { `${ prefixCls } -item-action-${ i } ` } >
104
- { action }
105
- { i !== actions . length - 1 && < em class = { `${ prefixCls } -item-action-split` } /> }
106
- </ li >
107
- ) ;
108
- actionsContent = (
109
- < ul class = { `${ prefixCls } -item-action` } >
110
- { actions . map ( ( action , i ) => actionsContentItem ( action , i ) ) }
111
- </ ul >
112
- ) ;
113
- }
104
+ const actionsContent = actions && actions . length > 0 && (
105
+ < ul class = { `${ prefixCls } -item-action` } key = "actions" >
106
+ { actions . map ( ( action , i ) => (
107
+ < li key = { `${ prefixCls } -item-action-${ i } ` } >
108
+ { action }
109
+ { i !== actions . length - 1 && < em class = { `${ prefixCls } -item-action-split` } /> }
110
+ </ li >
111
+ ) ) }
112
+ </ ul >
113
+ ) ;
114
114
115
- const extraContent = (
116
- < div class = { `${ prefixCls } -item-extra-wrap` } >
117
- < div class = { `${ prefixCls } -item-main` } >
118
- { metaContent }
119
- { content }
120
- { actionsContent }
121
- </ div >
122
- < div class = { `${ prefixCls } -item-extra` } > { extra } </ div >
123
- </ div >
115
+ const Tag = grid ? 'div' : 'li' ;
116
+ const itemChildren = (
117
+ < Tag
118
+ { ...{ on : listeners } }
119
+ class = { classNames ( `${ prefixCls } -item` , {
120
+ [ `${ prefixCls } -item-no-flex` ] : ! this . isFlexMode ( ) ,
121
+ } ) }
122
+ >
123
+ { itemLayout === 'vertical' && extra
124
+ ? [
125
+ < div class = { `${ prefixCls } -item-main` } key = "content" >
126
+ { $slots . default }
127
+ { actionsContent }
128
+ </ div > ,
129
+ < div class = { `${ prefixCls } -item-extra` } key = "extra" >
130
+ { extra }
131
+ </ div > ,
132
+ ]
133
+ : [ $slots . default , actionsContent , cloneElement ( extra , { key : 'extra' } ) ] }
134
+ </ Tag >
124
135
) ;
125
136
126
137
const mainContent = grid ? (
@@ -133,20 +144,10 @@ export default {
133
144
xl = { getGrid ( grid , 'xl' ) }
134
145
xxl = { getGrid ( grid , 'xxl' ) }
135
146
>
136
- < div { ...{ on : listeners } } class = { classString } >
137
- { extra && extraContent }
138
- { ! extra && metaContent }
139
- { ! extra && content }
140
- { ! extra && actionsContent }
141
- </ div >
147
+ { itemChildren }
142
148
</ Col >
143
149
) : (
144
- < div { ...{ on : listeners } } class = { classString } >
145
- { extra && extraContent }
146
- { ! extra && metaContent }
147
- { ! extra && content }
148
- { ! extra && actionsContent }
149
- </ div >
150
+ itemChildren
150
151
) ;
151
152
152
153
return mainContent ;
0 commit comments