-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrowTooltip.js
More file actions
141 lines (127 loc) · 3.93 KB
/
rowTooltip.js
File metadata and controls
141 lines (127 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* eslint-disable react/no-access-state-in-setstate */
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { getTrEle } from '../util';
import { getTooltipPositionInBody } from '../../tooltip/toolView';
import '../css/business.less';
import { tablePrefixCls } from '../constant';
import { getRootWindow, prefixCls } from '../../../utils';
class RowTooltip extends Component {
constructor(props) {
super(props);
this.state = {
tooltipStyle: { visibility: 'hidden' },
tooltipMsg: this.props.tooltipConfigs[0].tooltipMsg,
};
}
componentDidMount() {
document.body.addEventListener('mouseover', this.onBodyMouseOver);
setTimeout(() => {
const ele = this.props.tableContainerRef?.current?.querySelector(
`.${tablePrefixCls}-container`,
);
if (ele) {
ele.addEventListener('mouseover', this.onMouseOver);
}
});
}
componentWillUnmount() {
document.body.removeEventListener('mouseover', this.onBodyMouseOver);
const ele = this.props.tableContainerRef?.current?.querySelector(
`.${tablePrefixCls}-container`,
);
if (ele) {
ele.removeEventListener('mouseover', this.onMouseOver);
}
}
timeStamp = new Date().getTime();
onMouseOver = (evt) => {
const targetRow = getTrEle(evt.target);
const targetTooltipConfig = this.props.tooltipConfigs.find((item) => targetRow?.classList?.contains(item.tooltipRowCls));
if (targetTooltipConfig) {
const tooltipEles = document.querySelectorAll(
`.${tablePrefixCls}-row-tooltip`,
);
const tooltipEle = Array.from(tooltipEles).find(
(ele) => ele.dataset.id === String(this.timeStamp),
);
if (tooltipEle) {
const checkboxEle = targetRow.querySelector(`.${prefixCls}-checkbox-disabled`);
const radioEle = targetRow.querySelector(`.${prefixCls}-radio-input[disabled]`);
const tooltipStyle = getTooltipPositionInBody(
tooltipEle,
checkboxEle || radioEle,
'top-left',
undefined,
'top',
);
const isInModal = document.querySelector(`.${prefixCls}-modal-mask`);
this.setState(
{
tooltipMsg: targetTooltipConfig.tooltipMsg,
},
() => {
const _window = this.props.useRootWindow ? getRootWindow() : window;
this.setState({
tooltipStyle: {
...this.state.tooltipStyle,
left: tooltipStyle.left - 7,
top: isInModal
? tooltipStyle.top + _window.pageYOffset - 8
: tooltipStyle.top - 8,
visibility: 'visible',
},
});
},
);
}
} else {
this.setState({
tooltipStyle: {
...this.state.tooltipStyle,
visibility: 'hidden',
},
});
}
};
onBodyMouseOver = (evt) => {
const targetRow = getTrEle(evt.target);
const targetTooltipConfig = this.props.tooltipConfigs.find((item) => targetRow?.classList?.contains(item.tooltipRowCls));
if (!targetTooltipConfig) {
this.setState({
tooltipStyle: {
...this.state.tooltipStyle,
visibility: 'hidden',
},
});
}
};
render() {
return this.state.tooltipMsg
? ReactDOM.createPortal(
<span
data-id={this.timeStamp}
className={`${tablePrefixCls}-row-tooltip`}
style={this.state.tooltipStyle}
>
{this.state.tooltipMsg}
</span>,
document.body,
)
: null;
}
}
RowTooltip.propTypes = {
tableContainerRef: PropTypes.object.isRequired,
tooltipConfigs: PropTypes.array,
};
RowTooltip.defaultProps = {
tooltipConfigs: [
{
tooltipMsg: '不可选',
tooltipRowCls: `${tablePrefixCls}-row-disabled`, // 需要展示 tooltip 行的类名,
},
],
};
export default RowTooltip;