优化 trigger

pull/9/head
tjz 2018-01-20 20:24:42 +08:00
parent 90fd428942
commit f3c23a2bc1
3 changed files with 165 additions and 153 deletions

View File

@ -26,13 +26,14 @@ export default {
data () { data () {
return { return {
destroyPopup: false, destroyPopup: false,
initAlign: false, // mountedalign,this.$el initAlign: false, // mountedalign,this.$el,
} }
}, },
mounted () { mounted () {
this.rootNode = this.getPopupDomNode() this.rootNode = this.getPopupDomNode()
this._container = this.getContainer() this._container = this.getContainer()
this._container.appendChild(this.$el) this._container.appendChild(this.$el)
this.$nextTick(() => { this.$nextTick(() => {
this.initAlign = true this.initAlign = true
}) })
@ -46,14 +47,14 @@ export default {
this.destroyPopup = false this.destroyPopup = false
} }
}, },
initAlign (val) { // initAlign (val) {
if (val) { // if (val) {
this.$nextTick(() => { // this.$nextTick(() => {
// console.log(this.$refs.alignInstance.$el) // // console.log(this.$refs.alignInstance.$el)
// this.$refs.alignInstance.forceAlign() // // this.$refs.alignInstance.forceAlign()
}) // })
} // }
}, // },
}, },
methods: { methods: {
onAlign (popupDomNode, align) { onAlign (popupDomNode, align) {

View File

@ -1,4 +1,5 @@
<script> <script>
import Vue from 'vue'
import PropTypes from '../_util/vue-types' import PropTypes from '../_util/vue-types'
import contains from '../_util/Dom/contains' import contains from '../_util/Dom/contains'
import { hasProp, getComponentFromProp } from '../_util/props-util' import { hasProp, getComponentFromProp } from '../_util/props-util'
@ -78,7 +79,6 @@ export default {
beforeCreate () { beforeCreate () {
ALL_HANDLERS.forEach((h) => { ALL_HANDLERS.forEach((h) => {
this[`fire${h}`] = (e) => { this[`fire${h}`] = (e) => {
// const ev = `on${h[0].toUpperCase() + h.slice(1)}`
this.fireEvents(h, e) this.fireEvents(h, e)
} }
}) })
@ -107,6 +107,10 @@ export default {
beforeDestroy () { beforeDestroy () {
this.clearDelayTimer() this.clearDelayTimer()
this.clearOutsideHandler() this.clearOutsideHandler()
if (this._component) {
this._component.$destroy()
this._component = null
}
}, },
methods: { methods: {
updatedCal () { updatedCal () {
@ -163,9 +167,9 @@ export default {
onPopupMouseleave (e) { onPopupMouseleave (e) {
if (e.relatedTarget && !e.relatedTarget.setTimeout && if (e.relatedTarget && !e.relatedTarget.setTimeout &&
this.$refs.popup && this._component.$refs.popup &&
this.$refs.popup.getPopupDomNode && this._component.$refs.popup.getPopupDomNode &&
contains(this.$refs.popup.getPopupDomNode(), e.relatedTarget)) { contains(this._component.$refs.popup.getPopupDomNode(), e.relatedTarget)) {
return return
} }
this.delaySetPopupVisible(false, this.$props.mouseLeaveDelay) this.delaySetPopupVisible(false, this.$props.mouseLeaveDelay)
@ -252,9 +256,8 @@ export default {
} }
}, },
getPopupDomNode () { getPopupDomNode () {
// for test if (this._component.$refs.popup && this._component.$refs.popup.getPopupDomNode) {
if (this.$refs.popup && this.$refs.popup.getPopupDomNode) { return this._component.$refs.popup.getPopupDomNode()
return this.$refs.popup.getPopupDomNode()
} }
return null return null
}, },
@ -285,21 +288,21 @@ export default {
} }
return popupAlign return popupAlign
}, },
getComponent (h) { renderComponent () {
const self = this
const mouseProps = {} const mouseProps = {}
if (this.isMouseEnterToShow()) { if (this.isMouseEnterToShow()) {
mouseProps.mouseenter = this.onPopupMouseenter mouseProps.mouseenter = self.onPopupMouseenter
} }
if (this.isMouseLeaveToHide()) { if (this.isMouseLeaveToHide()) {
mouseProps.mouseleave = this.onPopupMouseleave mouseProps.mouseleave = self.onPopupMouseleave
} }
const { prefixCls, destroyPopupOnHide, sPopupVisible, const { prefixCls, destroyPopupOnHide, sPopupVisible,
popupStyle, popupClassName, action, popupStyle, popupClassName, action,
popupAnimation, handleGetPopupClassFromAlign, getRootDomNode, popupAnimation, handleGetPopupClassFromAlign, getRootDomNode,
mask, zIndex, popupTransitionName, getPopupAlign, mask, zIndex, popupTransitionName, getPopupAlign,
maskAnimation, maskTransitionName, getContainer } = this maskAnimation, maskTransitionName, getContainer } = self
const popupProps = { const popupProps = {
props: {
prefixCls, prefixCls,
destroyPopupOnHide, destroyPopupOnHide,
visible: sPopupVisible, visible: sPopupVisible,
@ -315,22 +318,41 @@ export default {
maskTransitionName, maskTransitionName,
getContainer, getContainer,
popupClassName, popupClassName,
}, popupStyle,
on: { popupEvents: {
align: this.$listeners.popupAlign || noop, align: self.$listeners.popupAlign || noop,
...mouseProps, ...mouseProps,
}, },
}
if (!this._component) {
const div = document.createElement('div')
this._component = new Vue({
props: {
},
data () {
return {}
},
el: div,
render () {
const { $props } = this
const { popupStyle, popupEvents, ...otherProps } = $props
const p = {
props: otherProps,
on: popupEvents,
ref: 'popup', ref: 'popup',
style: popupStyle, style: popupStyle,
key: '_ANT_PORTAL',
} }
return ( return ($props.getContainer ? <Popup
<Popup {...p}
{...popupProps}
> >
{getComponentFromProp(this, 'popup')} {getComponentFromProp(self, 'popup')}
</Popup> </Popup> : null)
) },
})
}
Object.assign(this._component.$props, popupProps)
this._component.$forceUpdate()
}, },
getContainer () { getContainer () {
@ -441,8 +463,8 @@ export default {
return action.indexOf('focus') !== -1 || hideAction.indexOf('blur') !== -1 return action.indexOf('focus') !== -1 || hideAction.indexOf('blur') !== -1
}, },
forcePopupAlign () { forcePopupAlign () {
if (this.$data.sPopupVisible && this.$refs.popup && this.$refs.popup.$refs.alignInstance) { if (this.$data.sPopupVisible && this._component.$refs.popup && this._component.$refs.popup.$refs.alignInstance) {
this.$refs.popup.$refs.alignInstance.forceAlign() this._component.$refs.popup.$refs.alignInstance.forceAlign()
} }
}, },
fireEvents (type, e) { fireEvents (type, e) {
@ -508,18 +530,7 @@ export default {
} }
const { sPopupVisible, forceRender } = this const { sPopupVisible, forceRender } = this
if (sPopupVisible || forceRender || this._component) { if (sPopupVisible || forceRender || this._component) {
this._component = this.getComponent(h) this.renderComponent(h)
} else {
this._component = null
}
if (child.componentOptions) {
child.componentOptions.children = child.componentOptions.children || []
child.componentOptions.children = child.componentOptions.children.filter(c => c.key !== '_ANT_PORTAL')
this._component && child.componentOptions.children.push(this._component)
} else {
child.children = child.children || []
child.children = child.children.filter(c => c.key !== '_ANT_PORTAL')
this._component && child.children.push(this._component)
} }
const trigger = cloneElement(child, newChildProps) const trigger = cloneElement(child, newChildProps)
return trigger return trigger

View File

@ -4,7 +4,7 @@
"description": "vue component", "description": "vue component",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "NODE_ENV=development webpack-dev-server --open --hot", "start": "NODE_ENV=development ./node_modules/.bin/webpack-dev-server --open --hot",
"test": "karma start test/karma.conf.js --single-run", "test": "karma start test/karma.conf.js --single-run",
"lint": "eslint -c ./.eslintrc --fix --ext .js ./src/", "lint": "eslint -c ./.eslintrc --fix --ext .js ./src/",
"lint:style": "stylelint \"./src/**/*.less\" --syntax less" "lint:style": "stylelint \"./src/**/*.less\" --syntax less"