2
2
import PropTypes from '../_util/vue-types'
3
3
import ScrollNumber from './ScrollNumber'
4
4
import classNames from 'classnames'
5
- import { initDefaultProps , filterEmpty } from '../_util/props-util'
5
+ import { initDefaultProps , filterEmpty , getComponentFromProp } from '../_util/props-util'
6
+ import { cloneElement } from '../_util/vnode'
6
7
import getTransitionProps from '../_util/getTransitionProps'
8
+ import isNumeric from '../_util/isNumeric'
7
9
8
10
export const BadgeProps = {
9
11
/** Number to show in badge */
10
- count : PropTypes . oneOfType ( [ PropTypes . number , PropTypes . string , null ] ) ,
12
+ count : PropTypes . any ,
11
13
showZero : PropTypes . bool ,
12
14
/** Max count to show */
13
15
overflowCount : PropTypes . number ,
@@ -27,85 +29,162 @@ export default {
27
29
props : initDefaultProps ( BadgeProps , {
28
30
prefixCls : 'ant-badge' ,
29
31
scrollNumberPrefixCls : 'ant-scroll-number' ,
30
- count : null ,
31
32
showZero : false ,
32
33
dot : false ,
33
34
overflowCount : 99 ,
34
35
} ) ,
36
+ methods : {
37
+ getBadgeClassName ( ) {
38
+ const { prefixCls, status } = this . $props
39
+ const children = filterEmpty ( this . $slots . default )
40
+ return classNames ( prefixCls , {
41
+ [ `${ prefixCls } -status` ] : ! ! status ,
42
+ [ `${ prefixCls } -not-a-wrapper` ] : ! children . length ,
43
+ } )
44
+ } ,
45
+
46
+ isZero ( ) {
47
+ const numberedDispayCount = this . getNumberedDispayCount ( )
48
+ return numberedDispayCount === '0' || numberedDispayCount === 0
49
+ } ,
50
+
51
+ isDot ( ) {
52
+ const { dot, status } = this . $props
53
+ const isZero = this . isZero ( )
54
+ return ( dot && ! isZero ) || status
55
+ } ,
56
+
57
+ isHidden ( ) {
58
+ const { showZero } = this . $props
59
+ const displayCount = this . getDispayCount ( )
60
+ const isZero = this . isZero ( )
61
+ const isDot = this . isDot ( )
62
+ const isEmpty = displayCount === null || displayCount === undefined || displayCount === ''
63
+ return ( isEmpty || ( isZero && ! showZero ) ) && ! isDot
64
+ } ,
65
+
66
+ getNumberedDispayCount ( ) {
67
+ const { overflowCount } = this . $props
68
+ const count = this . badgeCount
69
+ const displayCount =
70
+ count > overflowCount ? `${ overflowCount } +` : count
71
+ return displayCount
72
+ } ,
73
+
74
+ getDispayCount ( ) {
75
+ const isDot = this . isDot ( )
76
+ // dot mode don't need count
77
+ if ( isDot ) {
78
+ return ''
79
+ }
80
+ return this . getNumberedDispayCount ( )
81
+ } ,
82
+
83
+ getScollNumberTitle ( ) {
84
+ const { title } = this . $props
85
+ const count = this . badgeCount
86
+ if ( title ) {
87
+ return title
88
+ }
89
+ return typeof count === 'string' || typeof count === 'number' ? count : undefined
90
+ } ,
91
+
92
+ getStyleWithOffset ( ) {
93
+ const { offset, numberStyle } = this . $props
94
+ return offset
95
+ ? {
96
+ right : `${ - parseInt ( offset [ 0 ] , 10 ) } px` ,
97
+ marginTop : isNumeric ( offset [ 1 ] ) ? `${ offset [ 1 ] } px` : offset [ 1 ] ,
98
+ ...numberStyle ,
99
+ }
100
+ : numberStyle
101
+ } ,
102
+
103
+ renderStatusText ( ) {
104
+ const { prefixCls, text } = this . $props
105
+ const hidden = this . isHidden ( )
106
+ return hidden || ! text ? null : < span class = { `${ prefixCls } -status-text` } > { text } </ span >
107
+ } ,
108
+
109
+ renderDispayComponent ( ) {
110
+ const count = this . badgeCount
111
+ const customNode = count
112
+ if ( ! customNode || typeof customNode !== 'object' ) {
113
+ return undefined
114
+ }
115
+ return cloneElement ( customNode , {
116
+ style : this . getStyleWithOffset ( ) ,
117
+ } )
118
+ } ,
119
+
120
+ renderBadgeNumber ( ) {
121
+ const { prefixCls, scrollNumberPrefixCls, status } = this . $props
122
+ const count = this . badgeCount
123
+ const displayCount = this . getDispayCount ( )
124
+ const isDot = this . isDot ( )
125
+ const hidden = this . isHidden ( )
126
+
127
+ const scrollNumberCls = {
128
+ [ `${ prefixCls } -dot` ] : isDot ,
129
+ [ `${ prefixCls } -count` ] : ! isDot ,
130
+ [ `${ prefixCls } -multiple-words` ] :
131
+ ! isDot && count && count . toString && count . toString ( ) . length > 1 ,
132
+ [ `${ prefixCls } -status-${ status } ` ] : ! ! status ,
133
+ }
134
+
135
+ return hidden ? null : (
136
+ < ScrollNumber
137
+ prefixCls = { scrollNumberPrefixCls }
138
+ data-show = { ! hidden }
139
+ v-show = { ! hidden }
140
+ className = { scrollNumberCls }
141
+ count = { displayCount }
142
+ displayComponent = { this . renderDispayComponent ( ) } // <Badge status="success" count={<Icon type="xxx" />}></Badge>
143
+ title = { this . getScollNumberTitle ( ) }
144
+ style = { this . getStyleWithOffset ( ) }
145
+ key = 'scrollNumber'
146
+ />
147
+ )
148
+ } ,
149
+ } ,
35
150
36
151
render ( ) {
37
152
const {
38
- count,
39
- showZero,
40
153
prefixCls,
41
- scrollNumberPrefixCls,
42
- overflowCount,
43
- dot,
44
154
status,
45
155
text,
46
- offset,
47
156
$slots,
48
- numberStyle,
49
- title,
50
157
} = this
51
- let displayCount = count > overflowCount ? `${ overflowCount } +` : count
52
- const isZero = displayCount === '0' || displayCount === 0
53
- const isDot = ( dot && ! isZero ) || status
54
- // dot mode don't need count
55
- if ( isDot ) {
56
- displayCount = ''
57
- }
58
158
const children = filterEmpty ( $slots . default )
59
- const isEmpty = displayCount === null || displayCount === undefined || displayCount === ''
60
- const hidden = ( isEmpty || ( isZero && ! showZero ) ) && ! isDot
159
+ let count = getComponentFromProp ( this , 'count' )
160
+ if ( Array . isArray ( count ) ) {
161
+ count = count [ 0 ]
162
+ }
163
+ this . badgeCount = count
164
+ const scrollNumber = this . renderBadgeNumber ( )
165
+ const statusText = this . renderStatusText ( )
61
166
const statusCls = classNames ( {
62
167
[ `${ prefixCls } -status-dot` ] : ! ! status ,
63
168
[ `${ prefixCls } -status-${ status } ` ] : ! ! status ,
64
169
} )
65
- const scrollNumberCls = classNames ( {
66
- [ `${ prefixCls } -dot` ] : isDot ,
67
- [ `${ prefixCls } -count` ] : ! isDot ,
68
- [ `${ prefixCls } -multiple-words` ] : ! isDot && count && count . toString && count . toString ( ) . length > 1 ,
69
- [ `${ prefixCls } -status-${ status } ` ] : ! ! status ,
70
- } )
71
- const badgeCls = classNames ( prefixCls , {
72
- [ `${ prefixCls } -status` ] : ! ! status ,
73
- [ `${ prefixCls } -not-a-wrapper` ] : ! children . length ,
74
- } )
75
- const styleWithOffset = offset ? {
76
- right : - parseInt ( offset [ 0 ] , 10 ) ,
77
- marginTop : typeof offset [ 1 ] === 'number' ? `${ offset [ 1 ] } px` : offset [ 1 ] ,
78
- ...numberStyle ,
79
- } : numberStyle
80
- // <Badge status="success" />
81
170
171
+ // <Badge status="success" />
82
172
if ( ! children . length && status ) {
83
173
return (
84
- < span { ...{ on : this . $listeners } } class = { badgeCls } style = { styleWithOffset } >
174
+ < span
175
+ { ...{ on : this . $listeners } }
176
+ class = { this . getBadgeClassName ( ) }
177
+ style = { this . getStyleWithOffset ( ) }
178
+ >
85
179
< span class = { statusCls } />
86
180
< span class = { `${ prefixCls } -status-text` } > { text } </ span >
87
181
</ span >
88
182
)
89
183
}
90
184
91
- const scrollNumber = hidden ? null : (
92
- < ScrollNumber
93
- prefixCls = { scrollNumberPrefixCls }
94
- v-show = { ! hidden }
95
- class = { scrollNumberCls }
96
- count = { displayCount }
97
- title = { title || count }
98
- style = { styleWithOffset }
99
- key = 'scrollNumber'
100
- />
101
- )
102
-
103
- const statusText = ( hidden || ! text ) ? null : (
104
- < span class = { `${ prefixCls } -status-text` } > { text } </ span >
105
- )
106
185
const transitionProps = getTransitionProps ( children . length ? `${ prefixCls } -zoom` : '' )
107
186
108
- return ( < span { ...{ on : this . $listeners } } class = { badgeCls } >
187
+ return ( < span { ...{ on : this . $listeners } } class = { this . getBadgeClassName ( ) } >
109
188
{ children }
110
189
< transition { ...transitionProps } >
111
190
{ scrollNumber }
0 commit comments