index.js
8.29 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = _interopRequireWildcard(require("react"));
var _rcTooltip = _interopRequireDefault(require("rc-tooltip"));
var _classnames = _interopRequireDefault(require("classnames"));
var _placements = _interopRequireDefault(require("./placements"));
var _reactNode = require("../_util/reactNode");
var _configProvider = require("../config-provider");
var _colors = require("../_util/colors");
var splitObject = function splitObject(obj, keys) {
var picked = {};
var omitted = (0, _extends2["default"])({}, obj);
keys.forEach(function (key) {
if (obj && key in obj) {
picked[key] = obj[key];
delete omitted[key];
}
});
return {
picked: picked,
omitted: omitted
};
};
var PresetColorRegex = new RegExp("^(".concat(_colors.PresetColorTypes.join('|'), ")(-inverse)?$")); // Fix Tooltip won't hide at disabled button
// mouse events don't trigger at disabled button in Chrome
// https://github.com/react-component/tooltip/issues/18
function getDisabledCompatibleChildren(element, prefixCls) {
var elementType = element.type;
if ((elementType.__ANT_BUTTON === true || elementType.__ANT_SWITCH === true || elementType.__ANT_CHECKBOX === true || element.type === 'button') && element.props.disabled) {
// Pick some layout related style properties up to span
// Prevent layout bugs like https://github.com/ant-design/ant-design/issues/5254
var _splitObject = splitObject(element.props.style, ['position', 'left', 'right', 'top', 'bottom', 'float', 'display', 'zIndex']),
picked = _splitObject.picked,
omitted = _splitObject.omitted;
var spanStyle = (0, _extends2["default"])((0, _extends2["default"])({
display: 'inline-block'
}, picked), {
cursor: 'not-allowed',
width: element.props.block ? '100%' : null
});
var buttonStyle = (0, _extends2["default"])((0, _extends2["default"])({}, omitted), {
pointerEvents: 'none'
});
var child = (0, _reactNode.cloneElement)(element, {
style: buttonStyle,
className: null
});
return /*#__PURE__*/React.createElement("span", {
style: spanStyle,
className: (0, _classnames["default"])(element.props.className, "".concat(prefixCls, "-disabled-compatible-wrapper"))
}, child);
}
return element;
}
var Tooltip = /*#__PURE__*/React.forwardRef(function (props, ref) {
var _classNames2;
var _React$useContext = React.useContext(_configProvider.ConfigContext),
getContextPopupContainer = _React$useContext.getPopupContainer,
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var _React$useState = React.useState(!!props.visible || !!props.defaultVisible),
_React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2),
visible = _React$useState2[0],
setVisible = _React$useState2[1];
React.useEffect(function () {
if ('visible' in props) {
setVisible(props.visible);
}
}, [props.visible]);
var isNoTitle = function isNoTitle() {
var title = props.title,
overlay = props.overlay;
return !title && !overlay && title !== 0; // overlay for old version compatibility
};
var onVisibleChange = function onVisibleChange(vis) {
if (!('visible' in props)) {
setVisible(isNoTitle() ? false : vis);
}
if (props.onVisibleChange && !isNoTitle()) {
props.onVisibleChange(vis);
}
};
var getTooltipPlacements = function getTooltipPlacements() {
var builtinPlacements = props.builtinPlacements,
arrowPointAtCenter = props.arrowPointAtCenter,
autoAdjustOverflow = props.autoAdjustOverflow;
return builtinPlacements || (0, _placements["default"])({
arrowPointAtCenter: arrowPointAtCenter,
autoAdjustOverflow: autoAdjustOverflow
});
}; // 动态设置动画点
var onPopupAlign = function onPopupAlign(domNode, align) {
var placements = getTooltipPlacements(); // 当前返回的位置
var placement = Object.keys(placements).filter(function (key) {
return placements[key].points[0] === align.points[0] && placements[key].points[1] === align.points[1];
})[0];
if (!placement) {
return;
} // 根据当前坐标设置动画点
var rect = domNode.getBoundingClientRect();
var transformOrigin = {
top: '50%',
left: '50%'
};
if (placement.indexOf('top') >= 0 || placement.indexOf('Bottom') >= 0) {
transformOrigin.top = "".concat(rect.height - align.offset[1], "px");
} else if (placement.indexOf('Top') >= 0 || placement.indexOf('bottom') >= 0) {
transformOrigin.top = "".concat(-align.offset[1], "px");
}
if (placement.indexOf('left') >= 0 || placement.indexOf('Right') >= 0) {
transformOrigin.left = "".concat(rect.width - align.offset[0], "px");
} else if (placement.indexOf('right') >= 0 || placement.indexOf('Left') >= 0) {
transformOrigin.left = "".concat(-align.offset[0], "px");
}
domNode.style.transformOrigin = "".concat(transformOrigin.left, " ").concat(transformOrigin.top);
};
var getOverlay = function getOverlay() {
var title = props.title,
overlay = props.overlay;
if (title === 0) {
return title;
}
return overlay || title || '';
};
var customizePrefixCls = props.prefixCls,
openClassName = props.openClassName,
getPopupContainer = props.getPopupContainer,
getTooltipContainer = props.getTooltipContainer,
overlayClassName = props.overlayClassName,
color = props.color,
overlayInnerStyle = props.overlayInnerStyle,
children = props.children;
var prefixCls = getPrefixCls('tooltip', customizePrefixCls);
var tempVisible = visible; // Hide tooltip when there is no title
if (!('visible' in props) && isNoTitle()) {
tempVisible = false;
}
var child = getDisabledCompatibleChildren((0, _reactNode.isValidElement)(children) ? children : /*#__PURE__*/React.createElement("span", null, children), prefixCls);
var childProps = child.props;
var childCls = (0, _classnames["default"])(childProps.className, (0, _defineProperty2["default"])({}, openClassName || "".concat(prefixCls, "-open"), true));
var customOverlayClassName = (0, _classnames["default"])(overlayClassName, (_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-rtl"), direction === 'rtl'), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-").concat(color), color && PresetColorRegex.test(color)), _classNames2));
var formattedOverlayInnerStyle = overlayInnerStyle;
var arrowContentStyle;
if (color && !PresetColorRegex.test(color)) {
formattedOverlayInnerStyle = (0, _extends2["default"])((0, _extends2["default"])({}, overlayInnerStyle), {
background: color
});
arrowContentStyle = {
background: color
};
}
return /*#__PURE__*/React.createElement(_rcTooltip["default"], (0, _extends2["default"])({}, props, {
prefixCls: prefixCls,
overlayClassName: customOverlayClassName,
getTooltipContainer: getPopupContainer || getTooltipContainer || getContextPopupContainer,
ref: ref,
builtinPlacements: getTooltipPlacements(),
overlay: getOverlay(),
visible: tempVisible,
onVisibleChange: onVisibleChange,
onPopupAlign: onPopupAlign,
overlayInnerStyle: formattedOverlayInnerStyle,
arrowContent: /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-arrow-content"),
style: arrowContentStyle
})
}), tempVisible ? (0, _reactNode.cloneElement)(child, {
className: childCls
}) : child);
});
Tooltip.displayName = 'Tooltip';
Tooltip.defaultProps = {
placement: 'top',
transitionName: 'zoom-big-fast',
mouseEnterDelay: 0.1,
mouseLeaveDelay: 0.1,
arrowPointAtCenter: false,
autoAdjustOverflow: true
};
var _default = Tooltip;
exports["default"] = _default;