1+ /* eslint-disable react/no-access-state-in-setstate */
12import React , { Component } from 'react' ;
3+ import ReactDOM from 'react-dom' ;
24import PropTypes from 'prop-types' ;
35import { getTrEle } from '../util' ;
46import { getTooltipPositionInBody } from '../../tooltip/toolView' ;
57import '../css/business.less' ;
68import { tablePrefixCls } from '../constant' ;
7- import ReactDOM from 'react-dom' ;
89import { getRootWindow , prefixCls } from '../../../utils' ;
910
1011class RowTooltip extends Component {
1112 constructor ( props ) {
1213 super ( props ) ;
1314 this . state = {
1415 tooltipStyle : { visibility : 'hidden' } ,
15- tooltipMsg : this . props . tooltipConfigs [ 0 ] . tooltipMsg
16+ tooltipMsg : this . props . tooltipConfigs [ 0 ] . tooltipMsg ,
1617 } ;
1718 }
1819
1920 componentDidMount ( ) {
2021 document . body . addEventListener ( 'mouseover' , this . onBodyMouseOver ) ;
2122 setTimeout ( ( ) => {
22- const ele = this . props . tableContainerRef ?. current ?. querySelector ( `.${ tablePrefixCls } -container` ) ;
23+ const ele = this . props . tableContainerRef ?. current ?. querySelector (
24+ `.${ tablePrefixCls } -container` ,
25+ ) ;
2326 if ( ele ) {
24- ele . addEventListener ( 'mouseover' , this . onMouseOver )
27+ ele . addEventListener ( 'mouseover' , this . onMouseOver ) ;
2528 }
26- } )
29+ } ) ;
2730 }
2831
2932 componentWillUnmount ( ) {
3033 document . body . removeEventListener ( 'mouseover' , this . onBodyMouseOver ) ;
31- const ele = this . props . tableContainerRef ?. current ?. querySelector ( `.${ tablePrefixCls } -container` ) ;
34+ const ele = this . props . tableContainerRef ?. current ?. querySelector (
35+ `.${ tablePrefixCls } -container` ,
36+ ) ;
3237 if ( ele ) {
3338 ele . removeEventListener ( 'mouseover' , this . onMouseOver ) ;
3439 }
3540 }
3641
3742 timeStamp = new Date ( ) . getTime ( ) ;
3843
39- onMouseOver = evt => {
44+ onMouseOver = ( evt ) => {
4045 const targetRow = getTrEle ( evt . target ) ;
4146
42- const targetTooltipConfig = this . props . tooltipConfigs . find ( item => targetRow ?. classList ?. contains ( item . tooltipRowCls ) ) ;
47+ const targetTooltipConfig = this . props . tooltipConfigs . find ( ( item ) => targetRow ?. classList ?. contains ( item . tooltipRowCls ) ) ;
4348 if ( targetTooltipConfig ) {
44- const tooltipEles = document . querySelectorAll ( `.${ tablePrefixCls } -row-tooltip` ) ;
45- const tooltipEle = Array . from ( tooltipEles ) . find ( ele => ele . dataset . id === String ( this . timeStamp ) ) ;
49+ const tooltipEles = document . querySelectorAll (
50+ `.${ tablePrefixCls } -row-tooltip` ,
51+ ) ;
52+ const tooltipEle = Array . from ( tooltipEles ) . find (
53+ ( ele ) => ele . dataset . id === String ( this . timeStamp ) ,
54+ ) ;
4655
4756 if ( tooltipEle ) {
48- const tooltipStyle = getTooltipPositionInBody ( tooltipEle , targetRow . querySelector ( `.${ prefixCls } -checkbox-disabled` ) , 'top-left' ) ;
57+ const tooltipStyle = getTooltipPositionInBody (
58+ tooltipEle ,
59+ targetRow . querySelector ( `.${ prefixCls } -checkbox-disabled` ) ,
60+ 'top-left' ,
61+ ) ;
4962
5063 const isInModal = document . querySelector ( `.${ prefixCls } -modal-mask` ) ;
5164
52- this . setState ( {
53- tooltipMsg : targetTooltipConfig . tooltipMsg
54- } , ( ) => {
55- const _window = this . props . useRootWindow ? getRootWindow ( ) : window ;
56- this . setState ( {
57- tooltipStyle : {
58- ...this . state . tooltipStyle ,
59- left : tooltipStyle . left - 7 ,
60- top : isInModal ? tooltipStyle . top + _window . pageYOffset - 8 : tooltipStyle . top - 8 ,
61- visibility : 'visible'
62- }
63- } )
64- } )
65+ this . setState (
66+ {
67+ tooltipMsg : targetTooltipConfig . tooltipMsg ,
68+ } ,
69+ ( ) => {
70+ const _window = this . props . useRootWindow ? getRootWindow ( ) : window ;
71+ this . setState ( {
72+ tooltipStyle : {
73+ ...this . state . tooltipStyle ,
74+ left : tooltipStyle . left - 7 ,
75+ top : isInModal
76+ ? tooltipStyle . top + _window . pageYOffset - 8
77+ : tooltipStyle . top - 8 ,
78+ visibility : 'visible' ,
79+ } ,
80+ } ) ;
81+ } ,
82+ ) ;
6583 }
6684 } else {
6785 this . setState ( {
6886 tooltipStyle : {
6987 ...this . state . tooltipStyle ,
70- visibility : 'hidden'
71- }
72- } )
88+ visibility : 'hidden' ,
89+ } ,
90+ } ) ;
7391 }
74- }
92+ } ;
7593
76- onBodyMouseOver = evt => {
94+ onBodyMouseOver = ( evt ) => {
7795 const targetRow = getTrEle ( evt . target ) ;
78- const targetTooltipConfig = this . props . tooltipConfigs . find ( item => targetRow ?. classList ?. contains ( item . tooltipRowCls ) ) ;
96+ const targetTooltipConfig = this . props . tooltipConfigs . find ( ( item ) => targetRow ?. classList ?. contains ( item . tooltipRowCls ) ) ;
7997 if ( ! targetTooltipConfig ) {
8098 this . setState ( {
8199 tooltipStyle : {
82100 ...this . state . tooltipStyle ,
83- visibility : 'hidden'
84- }
85- } )
101+ visibility : 'hidden' ,
102+ } ,
103+ } ) ;
86104 }
87105 } ;
88106
89107 render ( ) {
90- return (
91- ReactDOM . createPortal (
92- < span data-id = { this . timeStamp } className = { `${ tablePrefixCls } -row-tooltip` } style = { this . state . tooltipStyle } >
108+ return this . state . tooltipMsg
109+ ? ReactDOM . createPortal (
110+ < span
111+ data-id = { this . timeStamp }
112+ className = { `${ tablePrefixCls } -row-tooltip` }
113+ style = { this . state . tooltipStyle }
114+ >
93115 { this . state . tooltipMsg }
94116 </ span > ,
95- document . body
117+ document . body ,
96118 )
97- )
119+ : null ;
98120 }
99121}
100122
@@ -104,10 +126,12 @@ RowTooltip.propTypes = {
104126} ;
105127
106128RowTooltip . defaultProps = {
107- tooltipConfigs : [ {
108- tooltipMsg : '不可选' ,
109- tooltipRowCls : `${ tablePrefixCls } -row-disabled` , // 需要展示 tooltip 行的类名,
110- } ]
129+ tooltipConfigs : [
130+ {
131+ tooltipMsg : '不可选' ,
132+ tooltipRowCls : `${ tablePrefixCls } -row-disabled` , // 需要展示 tooltip 行的类名,
133+ } ,
134+ ] ,
111135} ;
112136
113137export default RowTooltip ;
0 commit comments