ant-design-vue/components/_util/ContainerRender.jsx

95 lines
2.2 KiB
React
Raw Normal View History

2018-03-19 02:16:27 +00:00
2018-03-05 11:05:23 +00:00
import Vue from 'vue'
import PropTypes from './vue-types'
export default {
props: {
autoMount: PropTypes.bool.def(true),
autoDestroy: PropTypes.bool.def(true),
visible: PropTypes.bool,
forceRender: PropTypes.bool.def(false),
parent: PropTypes.any,
getComponent: PropTypes.func.isRequired,
getContainer: PropTypes.func.isRequired,
children: PropTypes.func.isRequired,
},
mounted () {
if (this.autoMount) {
this.renderComponent()
}
},
updated () {
if (this.autoMount) {
this.renderComponent()
}
},
beforeDestroy () {
if (this.autoDestroy) {
this.removeContainer()
}
},
methods: {
removeContainer () {
if (this.container) {
this._component && this._component.$destroy()
this.container.parentNode.removeChild(this.container)
this.container = null
}
},
2018-03-07 13:36:15 +00:00
renderComponent (props = {}, ready) {
2018-03-05 11:05:23 +00:00
const { visible, getComponent, forceRender, getContainer, parent } = this
const self = this
if (visible || parent.$refs._component || forceRender) {
2018-03-07 13:36:15 +00:00
let el = this.componentEl
2018-03-05 11:05:23 +00:00
if (!this.container) {
this.container = getContainer()
2018-03-07 13:36:15 +00:00
el = document.createElement('div')
this.componentEl = el
this.container.appendChild(el)
2018-03-05 11:05:23 +00:00
}
if (!this._component) {
this._component = new Vue({
2018-03-07 13:36:15 +00:00
data: {
comProps: props,
},
2018-03-05 11:05:23 +00:00
parent: self.parent,
2018-03-07 13:36:15 +00:00
el: el,
2018-03-05 11:05:23 +00:00
mounted () {
2018-03-05 14:50:25 +00:00
this.$nextTick(() => {
2018-03-05 11:05:23 +00:00
if (ready) {
ready.call(self)
}
})
},
2018-05-30 06:11:36 +00:00
updated () {
this.$nextTick(() => {
if (ready) {
ready.call(self)
}
})
},
2018-03-05 11:05:23 +00:00
render () {
2018-03-07 13:36:15 +00:00
return getComponent(this.comProps)
2018-03-05 11:05:23 +00:00
},
})
2018-03-07 13:36:15 +00:00
} else {
this._component.comProps = props
2018-03-05 11:05:23 +00:00
}
}
},
},
render () {
return this.children({
renderComponent: this.renderComponent,
removeContainer: this.removeContainer,
})
},
}