You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/trigger/Popup.jsx

231 lines
6.3 KiB

7 years ago
import PropTypes from '../_util/vue-types'
7 years ago
import Align from '../align'
import PopupInner from './PopupInner'
import LazyRenderBox from './LazyRenderBox'
7 years ago
import { noop } from './utils'
import animate from '../_util/css-animation'
7 years ago
export default {
props: {
visible: PropTypes.bool,
getClassNameFromAlign: PropTypes.func,
getRootDomNode: PropTypes.func,
align: PropTypes.any,
destroyPopupOnHide: PropTypes.bool,
prefixCls: PropTypes.string,
7 years ago
getContainer: PropTypes.func,
7 years ago
transitionName: PropTypes.string,
animation: PropTypes.any,
maskAnimation: PropTypes.string,
maskTransitionName: PropTypes.string,
mask: PropTypes.bool,
zIndex: PropTypes.number,
7 years ago
popupClassName: PropTypes.any,
7 years ago
popupStyle: PropTypes.object.def({}),
7 years ago
},
7 years ago
data () {
return {
destroyPopup: false,
}
},
7 years ago
beforeDestroy () {
this.$el.remove()
},
7 years ago
// beforeUpdate () {
// this.$nextTick(() => {
// const newContainer = this.getContainer()
// if (newContainer !== this._container) {
// this._container = newContainer
// this._container.appendChild(this.$el)
// this.$refs.alignInstance.forceAlign()
// }
// })
// },
7 years ago
watch: {
visible (val) {
if (val) {
this.destroyPopup = false
}
},
},
7 years ago
methods: {
onAlign (popupDomNode, align) {
const props = this.$props
const currentAlignClassName = props.getClassNameFromAlign(align)
7 years ago
popupDomNode.className = this.getClassName(currentAlignClassName)
7 years ago
this.$listeners.align && this.$listeners.align(popupDomNode, align)
7 years ago
},
getPopupDomNode () {
7 years ago
return this.$refs.popupInstance ? this.$refs.popupInstance.$el : null
7 years ago
},
getTarget () {
return this.$props.getRootDomNode()
},
getMaskTransitionName () {
const props = this.$props
let transitionName = props.maskTransitionName
const animation = props.maskAnimation
if (!transitionName && animation) {
transitionName = `${props.prefixCls}-${animation}`
}
return transitionName
},
getTransitionName () {
const props = this.$props
let transitionName = props.transitionName
7 years ago
const animation = props.animation
if (!transitionName) {
if (typeof animation === 'string') {
transitionName = `${animation}`
7 years ago
} else if (animation && animation.props && animation.props.name) {
7 years ago
transitionName = animation.props.name
}
7 years ago
}
7 years ago
return transitionName
7 years ago
},
getClassName (currentAlignClassName) {
7 years ago
return `${this.$props.prefixCls} ${this.$props.popupClassName} ${currentAlignClassName}`
7 years ago
},
getPopupElement () {
7 years ago
const { $props: props, $slots, $listeners, getTransitionName } = this
7 years ago
const { align, visible, prefixCls, animation, popupStyle } = props
7 years ago
const { mouseenter, mouseleave } = $listeners
7 years ago
const className = this.getClassName(props.getClassNameFromAlign(align))
7 years ago
// const hiddenClassName = `${prefixCls}-hidden`
7 years ago
const popupInnerProps = {
props: {
prefixCls,
visible,
7 years ago
// hiddenClassName,
7 years ago
},
7 years ago
class: className,
7 years ago
on: {
7 years ago
mouseenter: mouseenter || noop,
mouseleave: mouseleave || noop,
7 years ago
},
ref: 'popupInstance',
7 years ago
style: { ...this.getZIndexStyle(), ...popupStyle },
7 years ago
}
7 years ago
let transitionProps = {
7 years ago
props: Object.assign({
appear: true,
7 years ago
css: false,
}),
}
7 years ago
const transitionName = getTransitionName()
7 years ago
let useTransition = !!transitionName
7 years ago
const transitionEvent = {
beforeEnter: (el) => {
// el.style.display = el.__vOriginalDisplay
7 years ago
// this.$refs.alignInstance.forceAlign()
7 years ago
},
enter: (el, done) => {
7 years ago
// align updated后执行动画
this.$nextTick(() => {
this.$refs.alignInstance.$nextTick(() => {
animate(el, `${transitionName}-enter`, done)
})
7 years ago
})
7 years ago
},
leave: (el, done) => {
7 years ago
animate(el, `${transitionName}-leave`, done)
7 years ago
},
afterLeave: (el) => {
if (this.destroyPopupOnHide) {
this.destroyPopup = true
}
},
}
if (typeof animation === 'object') {
7 years ago
useTransition = true
7 years ago
const { on = {}, props = {}} = animation
transitionProps.props = { ...transitionProps.props, ...props }
transitionProps.on = { ...transitionEvent, ...on, afterLeave: (el) => {
7 years ago
transitionEvent.afterLeave(el)
on.afterLeave && on.afterLeave(el)
} }
} else {
transitionProps.on = transitionEvent
7 years ago
}
7 years ago
if (!useTransition) {
transitionProps = {}
}
7 years ago
return (<transition
7 years ago
{...transitionProps}
7 years ago
>
<Align
7 years ago
v-show={visible}
7 years ago
target={this.getTarget}
key='popup'
ref='alignInstance'
monitorWindowResize
align={align}
onAlign={this.onAlign}
7 years ago
visible={visible}
7 years ago
>
<PopupInner
{...popupInnerProps}
>
{$slots.default}
</PopupInner>
</Align>
7 years ago
</transition>)
7 years ago
},
getZIndexStyle () {
const style = {}
const props = this.$props
if (props.zIndex !== undefined) {
style.zIndex = props.zIndex
}
return style
},
getMaskElement () {
const props = this.$props
let maskElement = null
7 years ago
if (props.mask) {
7 years ago
const maskTransition = this.getMaskTransitionName()
7 years ago
maskElement = (
<LazyRenderBox
7 years ago
v-show={props.visible}
7 years ago
style={this.getZIndexStyle()}
key='mask'
class={`${props.prefixCls}-mask`}
visible={props.visible}
/>
)
if (maskTransition) {
maskElement = (
7 years ago
<transition
7 years ago
appear
7 years ago
name={maskTransition}
7 years ago
>
{maskElement}
7 years ago
</transition>
7 years ago
)
}
}
return maskElement
},
},
render () {
7 years ago
const { destroyPopup, getMaskElement, getPopupElement, visible } = this
7 years ago
return (
7 years ago
<div>
{getMaskElement()}
7 years ago
{(visible || !destroyPopup) ? getPopupElement() : null}
7 years ago
</div>
)
},
}