@@ -12,15 +12,12 @@ const Badge: React.FC<BadgeProps> = (props) => {
12
12
const [ bem ] = createNamespace ( 'badge' , prefixCls ) ;
13
13
14
14
const hasContent = ( ) => {
15
- // if (props.content) {
16
- // return true;
17
- // }
18
15
return isDef ( content ) && content !== '' && ( showZero || + content !== 0 ) ;
19
16
} ;
20
17
21
18
const renderContent = ( ) => {
22
19
if ( ! dot && hasContent ( ) ) {
23
- if ( isDef ( max ) && isNumeric ( content ?. toString ( ) ) && + content > max ) {
20
+ if ( isDef ( max ) && isNumeric ( content ?. toString ( ) ) && + content > + max ) {
24
21
return `${ max } +` ;
25
22
}
26
23
@@ -29,6 +26,9 @@ const Badge: React.FC<BadgeProps> = (props) => {
29
26
return null ;
30
27
} ;
31
28
29
+ const getOffsetWithMinusString = ( val : string ) =>
30
+ val . startsWith ( '-' ) ? val . replace ( '-' , '' ) : `-${ val } ` ;
31
+
32
32
const renderBadge = ( ) => {
33
33
if ( hasContent ( ) || props . dot ) {
34
34
let style : CSSProperties = {
@@ -37,13 +37,20 @@ const Badge: React.FC<BadgeProps> = (props) => {
37
37
38
38
if ( props . offset ) {
39
39
const [ x , y ] = props . offset ;
40
+ const { position } = props ;
41
+ const [ offsetY , offsetX ] = position . split ( '-' ) as [ 'top' | 'bottom' , 'left' | 'right' ] ;
40
42
41
43
if ( props . children ) {
42
- style . top = addUnit ( y ) ;
44
+ if ( typeof y === 'number' ) {
45
+ style [ offsetY ] = addUnit ( offsetY === 'top' ? y : - y ) ;
46
+ } else {
47
+ style [ offsetY ] = offsetY === 'top' ? addUnit ( y ) : getOffsetWithMinusString ( y ) ;
48
+ }
49
+
43
50
if ( typeof x === 'number' ) {
44
- style . right = addUnit ( - x ) ;
51
+ style [ offsetX ] = addUnit ( offsetX === 'left' ? x : - x ) ;
45
52
} else {
46
- style . right = x . startsWith ( '-' ) ? x . replace ( '-' , '' ) : `- ${ x } ` ;
53
+ style [ offsetX ] = offsetX === 'left' ? addUnit ( x ) : getOffsetWithMinusString ( x ) ;
47
54
}
48
55
} else {
49
56
style . marginTop = addUnit ( y ) ;
@@ -60,7 +67,7 @@ const Badge: React.FC<BadgeProps> = (props) => {
60
67
{
61
68
[ props . className ] : props . className && ! props . children ,
62
69
} ,
63
- bem ( { dot : props . dot , fixed : ! ! props . children } ) ,
70
+ bem ( [ props . position , { dot : props . dot , fixed : ! ! props . children } ] ) ,
64
71
) }
65
72
style = { style }
66
73
>
@@ -91,6 +98,7 @@ const Badge: React.FC<BadgeProps> = (props) => {
91
98
Badge . defaultProps = {
92
99
tag : 'div' ,
93
100
showZero : true ,
101
+ position : 'top-right' ,
94
102
} ;
95
103
96
104
export default Badge ;
0 commit comments