[improvement] mixins typescript (#3690)

This commit is contained in:
neverland
2019-06-29 11:31:53 +08:00
committed by GitHub
parent 0a79536a05
commit abe3e59bed
17 changed files with 135 additions and 82 deletions
+55
View File
@@ -0,0 +1,55 @@
import Vue, { PropType } from 'vue';
import { GetContainer } from './popup/type';
type PortalMixinOptions = {
afterPortal?: () => void;
};
function getElement(selector: string | GetContainer): Element | null {
if (typeof selector === 'string') {
return document.querySelector(selector);
}
return selector();
}
export function PortalMixin({ afterPortal }: PortalMixinOptions) {
return Vue.extend({
props: {
getContainer: [String, Function] as (PropType<string | GetContainer>)
},
watch: {
getContainer() {
this.portal();
}
},
mounted() {
if (this.getContainer) {
this.portal();
}
},
methods: {
portal() {
const { getContainer } = this;
let container;
if (getContainer) {
container = getElement(getContainer);
} else if (this.$parent) {
container = this.$parent.$el;
}
if (container && container !== this.$el.parentNode) {
container.appendChild(this.$el);
}
if (afterPortal) {
afterPortal.call(this);
}
}
}
});
}