-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTooltip.jsx
190 lines (180 loc) · 5.64 KB
/
Tooltip.jsx
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
import { cloneElement } from '../_util/vnode'
import VcTooltip from '../vc-tooltip'
import getPlacements from './placements'
import PropTypes from '../_util/vue-types'
import { hasProp, getComponentFromProp, getClass, getStyle, isValidElement } from '../_util/props-util'
import abstractTooltipProps from './abstractTooltipProps'
const splitObject = (obj, keys) => {
const picked = {}
const omited = { ...obj }
keys.forEach(key => {
if (obj && key in obj) {
picked[key] = obj[key]
delete omited[key]
}
})
return { picked, omited }
}
const props = abstractTooltipProps()
export default {
name: 'ATooltip',
props: {
...props,
title: PropTypes.any,
},
model: {
prop: 'visible',
event: 'visibleChange',
},
data () {
return {
sVisible: !!this.$props.visible,
}
},
watch: {
visible (val) {
this.sVisible = val
},
},
methods: {
onVisibleChange (visible) {
if (!hasProp(this, 'visible')) {
this.sVisible = this.isNoTitle() ? false : visible
}
if (!this.isNoTitle()) {
this.$emit('visibleChange', visible)
}
},
getPopupDomNode () {
return this.$refs.tooltip.getPopupDomNode()
},
getPlacements () {
const { builtinPlacements, arrowPointAtCenter, autoAdjustOverflow } = this.$props
return builtinPlacements || getPlacements({
arrowPointAtCenter,
verticalArrowShift: 8,
autoAdjustOverflow,
})
},
isHoverTrigger () {
const { trigger } = this.$props
if (!trigger || trigger === 'hover') {
return true
}
if (Array.isArray(trigger)) {
return trigger.indexOf('hover') >= 0
}
return false
},
// 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
getDisabledCompatibleChildren (ele) {
const isAntBtn = ele.componentOptions && ele.componentOptions.Ctor.options.__ANT_BUTTON
if (((isAntBtn && (ele.componentOptions.propsData.disabled || ele.componentOptions.propsData.disabled === '')) ||
(ele.tag === 'button' && ele.data && ele.data.attrs.disabled !== false)) &&
this.isHoverTrigger()) {
// Pick some layout related style properties up to span
// Prevent layout bugs like https://github.com/ant-design/ant-design/issues/5254
const { picked, omited } = splitObject(
getStyle(ele),
['position', 'left', 'right', 'top', 'bottom', 'float', 'display', 'zIndex'],
)
const spanStyle = {
display: 'inline-block', // default inline-block is important
...picked,
cursor: 'not-allowed',
}
const buttonStyle = {
...omited,
pointerEvents: 'none',
}
const spanCls = getClass(ele)
const child = cloneElement(ele, {
style: buttonStyle,
class: null,
})
return (
<span style={spanStyle} class={spanCls}>
{child}
</span>
)
}
return ele
},
isNoTitle () {
const { $slots, title } = this
return !$slots.title && !title
},
// 动态设置动画点
onPopupAlign (domNode, align) {
const placements = this.getPlacements()
// 当前返回的位置
const placement = Object.keys(placements).filter(
key => (
placements[key].points[0] === align.points[0] &&
placements[key].points[1] === align.points[1]
),
)[0]
if (!placement) {
return
}
// 根据当前坐标设置动画点
const rect = domNode.getBoundingClientRect()
const transformOrigin = {
top: '50%',
left: '50%',
}
if (placement.indexOf('top') >= 0 || placement.indexOf('Bottom') >= 0) {
transformOrigin.top = `${rect.height - align.offset[1]}px`
} else if (placement.indexOf('Top') >= 0 || placement.indexOf('bottom') >= 0) {
transformOrigin.top = `${-align.offset[1]}px`
}
if (placement.indexOf('left') >= 0 || placement.indexOf('Right') >= 0) {
transformOrigin.left = `${rect.width - align.offset[0]}px`
} else if (placement.indexOf('right') >= 0 || placement.indexOf('Left') >= 0) {
transformOrigin.left = `${-align.offset[0]}px`
}
domNode.style.transformOrigin = `${transformOrigin.left} ${transformOrigin.top}`
},
},
render (h) {
const { $props, $data, $slots } = this
const { prefixCls, openClassName, getPopupContainer } = $props
let children = ($slots.default || []).filter(c => c.tag || c.text.trim() !== '')
children = children.length === 1 ? children[0] : children
let sVisible = $data.sVisible
// Hide tooltip when there is no title
if (!hasProp(this, 'visible') && this.isNoTitle()) {
sVisible = false
}
if (!children) {
return null
}
const child = this.getDisabledCompatibleChildren(isValidElement(children) ? children : <span>{children}</span>)
const childCls = {
[openClassName || `${prefixCls}-open`]: true,
}
const tooltipProps = {
props: {
...$props,
getTooltipContainer: getPopupContainer,
builtinPlacements: this.getPlacements(),
visible: sVisible,
},
ref: 'tooltip',
on: {
visibleChange: this.onVisibleChange,
popupAlign: this.onPopupAlign,
},
}
return (
<VcTooltip {...tooltipProps}>
<template slot='overlay'>
{getComponentFromProp(this, 'title')}
</template>
{sVisible ? cloneElement(child, { class: childCls }) : child}
</VcTooltip>
)
},
}