ant-design-vue/components/tooltip/Tooltip.jsx
tangjinzhou ff25efe185
update to antd3.8.3 (#159)
* refactor: align

* feat:  update align to 2.4.3

* feat: update trigger 2.5.4

* feat: update tooltip 3.7.2

* fix: align

* feat: update vc-calendar to 9.6.2

* feat: update vc-checkbox to 2.1.5

* feat: update vc-dialog to 7.1.8

* feat: update vc-from to 2.2.1

* feat: update vc-notification to 3.1.1

* test: update snapshots

* feat: update vc-tree to 1.12.6

* feat: update vc-table to 6.2.8

* feat: update vc-upload to 2.5.1

* feat: update vc-input-number to 4.0.12

* feat: update vc-tabs to 9.2.6

* refactor: vc-menu

* refactor: update vc-menu to 7.0.5

* style: remove unused

* feat: update pagination to 1.16.5

* feat: add vc-progress 2.2.5 tag

* feat: add vc-rate 2.4.0 tag

* feat: update vc-slider to 8.6.1

* fix: tooltip error

* style: delete conosle

* feat: update vc-steps to 3.1.1

* add vc-switch tag 1.6.0

* feat: update upload to 2.5.1

* fix: update vc-menu

* fix: update store

* fix: add ref dir

* fix: trigger mock shouldComponentUpdate

* fix: update vc-select

* revert: trigger lazyrenderbox

* fix: update vc-select

* fix: update vc-select

* fix: update vc-select

* fix: update vc-menu

* fix: update vc-slick ref

* update style to 3.8.2

* test: update snapshots

* update vc-select

* update util & affix

* feat: add drawer

* fix: support title add slot mode

* test: update affix test

* update alert

* update anchor

* update snapshots

* fix: doc and vc-drawer

* update select & auto-complete

* update back-top & grid

* feractor: avatar

* test: add drawer test

* update badge

* update button

* update card

* update divider

* feat: update vc-tabs to 9.3.6 and tabs

* add afterEnter callback

* update form

* fix: update drawer

* test: update snapshots

* update modal & notification

* test: update snapshots

* update message

* update locale-provider

* update dropdown

* update layout popconfirm popover

* update time-picker

* update menu

* update date-picker

* docs: update input docs

* update input

* update snapshots

* update table

* update test snapshots

* feat: update progress

* update checkbox

* feat: update spin

* update radio

* docs: slider steps timeline

* update list

* update transfer

* update collapse

* update cascader

* update upload
2018-09-05 21:28:54 +08:00

192 lines
5.7 KiB
Vue

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 omitted = { ...obj }
keys.forEach(key => {
if (obj && key in obj) {
picked[key] = obj[key]
delete omitted[key]
}
})
return { picked, omitted }
}
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, omitted } = 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 = {
...omitted,
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, $listeners } = 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: {
...$listeners,
visibleChange: this.onVisibleChange,
popupAlign: this.onPopupAlign,
},
}
return (
<VcTooltip {...tooltipProps}>
<template slot='overlay'>
{getComponentFromProp(this, 'title')}
</template>
{sVisible ? cloneElement(child, { class: childCls }) : child}
</VcTooltip>
)
},
}