1
1
import React , { PureComponent } from "react" ;
2
2
import { Tooltip } from "antd" ;
3
+ import Resize from '../resize' ;
3
4
4
5
export interface Props {
5
- value : string ;
6
- title ?: string ;
6
+ value : string | number ;
7
+ title ?: string | number ;
7
8
className ?: string ;
8
9
maxWidth ?: string | number ;
9
10
[ propName : string ] : any ;
@@ -28,18 +29,7 @@ export default class EllipsisText extends PureComponent<Props, State> {
28
29
} ;
29
30
30
31
componentDidMount ( ) {
31
- const { maxWidth } = this . props ;
32
32
this . onResize ( ) ;
33
- if ( ! maxWidth ) {
34
- window . addEventListener ( "resize" , this . onResize ) ;
35
- }
36
- }
37
-
38
- componentWillUnmount ( ) {
39
- const { maxWidth } = this . props ;
40
- if ( ! maxWidth ) {
41
- window . removeEventListener ( "resize" , this . onResize ) ;
42
- }
43
33
}
44
34
45
35
getRangeWidth = ( ele : HTMLElement ) => {
@@ -51,7 +41,8 @@ export default class EllipsisText extends PureComponent<Props, State> {
51
41
52
42
getStyle = ( dom : NewHTMLElement , attr : string ) => {
53
43
// 兼容IE8
54
- const stylePadding = window ?. getComputedStyle ( dom ) [ attr ] || dom . currentStyle [ attr ]
44
+ const stylePadding =
45
+ window ?. getComputedStyle ( dom ) [ attr ] || dom . currentStyle [ attr ] ;
55
46
56
47
return stylePadding . slice ( 0 , - 2 ) ;
57
48
} ;
@@ -62,18 +53,17 @@ export default class EllipsisText extends PureComponent<Props, State> {
62
53
const { scrollWidth, offsetWidth, parentElement } = ele ;
63
54
if ( scrollWidth === 0 ) {
64
55
return this . getMaxWidth ( parentElement ) ;
65
- } else {
66
- const ellipsisNode = this . ellipsisRef ;
67
- ellipsisNode . style . display = "none" ;
68
- const rangeWidth = this . getRangeWidth ( ele ) ;
69
- const ellipsisWidth =
70
- offsetWidth -
71
- rangeWidth -
72
- this . getStyle ( ele , "paddingRight" ) -
73
- this . getStyle ( ele , "paddingLeft" ) ;
74
-
75
- return ellipsisWidth < 0 ? 0 : ellipsisWidth ;
76
56
}
57
+ const ellipsisNode = this . ellipsisRef ;
58
+ ellipsisNode . style . display = "none" ;
59
+ const rangeWidth = this . getRangeWidth ( ele ) ;
60
+ const ellipsisWidth =
61
+ offsetWidth -
62
+ rangeWidth -
63
+ this . getStyle ( ele , "paddingRight" ) -
64
+ this . getStyle ( ele , "paddingLeft" ) ;
65
+
66
+ return ellipsisWidth < 0 ? 0 : ellipsisWidth ;
77
67
} ;
78
68
79
69
onResize = ( ) => {
@@ -87,7 +77,6 @@ export default class EllipsisText extends PureComponent<Props, State> {
87
77
actMaxWidth : ellipsisWidth ,
88
78
isEllipsis : rangeWidth > ( maxWidth || ellipsisWidth )
89
79
} ) ;
90
-
91
80
} ;
92
81
93
82
handleVisibleChange = ( visible : boolean ) => {
@@ -103,29 +92,31 @@ export default class EllipsisText extends PureComponent<Props, State> {
103
92
const { title, value, className, maxWidth, ...other } = this . props ;
104
93
105
94
return (
106
- < Tooltip
107
- title = { title || value }
108
- visible = { visible }
109
- onVisibleChange = { this . handleVisibleChange }
110
- { ...other }
111
- >
112
- < span
113
- ref = { ( ref ) => ( this . ellipsisRef = ref ) }
114
- className = { className }
115
- style = { {
116
- whiteSpace : "nowrap" ,
117
- overflow : "hidden" ,
118
- textOverflow : "ellipsis" ,
119
- display : "inline-block" ,
120
- verticalAlign : "bottom" ,
121
- minWidth : "2em" ,
122
- maxWidth : maxWidth || actMaxWidth ,
123
- cursor : isEllipsis ? 'pointer' :'default'
124
- } }
95
+ < Resize onResize = { maxWidth ? null :this . onResize } >
96
+ < Tooltip
97
+ title = { title || value }
98
+ visible = { visible }
99
+ onVisibleChange = { this . handleVisibleChange }
100
+ { ...other }
125
101
>
126
- { value }
127
- </ span >
128
- </ Tooltip >
102
+ < span
103
+ ref = { ( ref ) => ( this . ellipsisRef = ref ) }
104
+ className = { className }
105
+ style = { {
106
+ whiteSpace : "nowrap" ,
107
+ overflow : "hidden" ,
108
+ textOverflow : "ellipsis" ,
109
+ display : "inline-block" ,
110
+ verticalAlign : "bottom" ,
111
+ minWidth : "2em" ,
112
+ maxWidth : maxWidth || actMaxWidth ,
113
+ cursor : isEllipsis ? "pointer" : "default"
114
+ } }
115
+ >
116
+ { value }
117
+ </ span >
118
+ </ Tooltip >
119
+ </ Resize >
129
120
) ;
130
121
}
131
122
}
0 commit comments