1
+ import { Tooltip } from "antd" ;
2
+ import { useEffect , useRef , useState } from "react" ;
3
+
4
+ export default ( props ) => {
5
+ const { height = 21 , width = '100%' , children, showTooltip = true } = props ;
6
+ const [ textHeight , setTextHeight ] = useState ( height ) ;
7
+ const textRef = useRef ( null ) ;
8
+
9
+ const handleResize = ( ) => {
10
+ if ( textRef . current ) {
11
+ setTextHeight ( textRef . current . offsetHeight ) ;
12
+ }
13
+ } ;
14
+
15
+ useEffect ( ( ) => {
16
+ const resizeObserver = new ResizeObserver ( entries => {
17
+ entries . forEach ( entry => {
18
+ const { width, height } = entry . contentRect ;
19
+ setTextHeight ( height ) ;
20
+ } ) ;
21
+ } ) ;
22
+
23
+ if ( textRef . current && showTooltip ) {
24
+ resizeObserver . observe ( textRef . current ) ;
25
+ }
26
+
27
+ return ( ) => {
28
+ if ( textRef . current && showTooltip ) {
29
+ resizeObserver . unobserve ( textRef . current ) ;
30
+ }
31
+ } ;
32
+ } , [ showTooltip ] ) ;
33
+
34
+ return (
35
+ < div style = { { position : 'relative' , width, height } } onClick = { ( e ) => e . stopPropagation ( ) } >
36
+ < div ref = { textRef } style = { { visibility : 'hidden' } } >
37
+ { children }
38
+ </ div >
39
+ {
40
+ showTooltip && textHeight > height ? (
41
+ < Tooltip title = { children } overlayStyle = { { maxWidth : 'unset !important' } } >
42
+ < div style = { { position : 'absolute' , left : 0 , top : 0 , overflow : 'hidden' , height, width : '100%' , textOverflow : 'ellipsis' , whiteSpace : 'nowrap' } } >
43
+ { children }
44
+ </ div >
45
+ </ Tooltip >
46
+ ) : (
47
+ < div style = { { position : 'absolute' , left : 0 , top : 0 } } >
48
+ { children }
49
+ </ div >
50
+ )
51
+ }
52
+ </ div >
53
+ )
54
+ }
0 commit comments