Last active
January 9, 2020 09:28
-
-
Save NateScarlet/1c7471fec66c45bb72419f8b613d230e to your computer and use it in GitHub Desktop.
element-ui modal show
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue, { VNode, VNodeData, VueConstructor } from 'vue'; | |
import { app } from '@/main'; | |
/** | |
* Show a modal vue component | |
* @param constructor Component constructor | |
* @param data Component data | |
*/ | |
export function show<V extends Vue>( | |
constructor: VueConstructor<V>, | |
data?: VNodeData | |
): V { | |
const wrapper = new Vue({ | |
name: 'ModalWrapper', | |
parent: app, | |
data: { | |
visible: false, | |
}, | |
watch: { | |
visible(v): void { | |
if (v === false) { | |
setTimeout((): void => { | |
this.$destroy(); | |
this.$el.remove(); | |
}, 5e3); | |
} | |
}, | |
}, | |
mounted(): void { | |
this.visible = true; | |
}, | |
render(h): VNode { | |
const d: VNodeData = { | |
...data, | |
props: { | |
...(data && data.props), | |
visible: this.$data.visible, | |
}, | |
on: { | |
...(data && data.on), | |
'update:visible': (v: boolean): void => { | |
this.$data.visible = v; | |
}, | |
}, | |
}; | |
return h(constructor, d); | |
}, | |
}); | |
wrapper.$mount(document.createElement('div')); | |
wrapper.$parent.$el.append(wrapper.$el); | |
const ret = wrapper.$children[0] as V; | |
return ret; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Prop, Vue } from 'vue-property-decorator'; | |
/** | |
* Mixin for support `dialog.show` | |
*/ | |
@Component | |
export class ModalMixin extends Vue { | |
@Prop({ default: false, type: Boolean }) | |
public visible!: boolean; | |
public get $_visible(): boolean { | |
return this.visible; | |
} | |
public set $_visible(v: boolean) { | |
this.$emit('update:visible', v); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
Add
:visible.sync='$_visible'
to element drawer or dialog.