update Modal for ESC key, and add prop z-index

This commit is contained in:
梁灏 2018-08-29 17:03:45 +08:00
parent 753720d9bd
commit 707a3d825d
3 changed files with 54 additions and 39 deletions

View file

@ -1,43 +1,21 @@
<template> <template>
<div> <div>
<Button @click="instance('info')">Info</Button> <Button @click="modal12 = true">Open the first modal</Button>
<Button @click="instance('success')">Success</Button> <Button @click="modal13 = true">Open the second modal</Button>
<Button @click="instance('warning')">Warning</Button> <Modal v-model="modal12" draggable scrollable title="Modal 1">
<Button @click="instance('error')">Error</Button> <div>This is the first modal</div>
</Modal>
<Modal v-model="modal13" draggable scrollable title="Modal 2">
<div>This is the second modal</div>
</Modal>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
methods: { data () {
instance (type) { return {
const title = 'Title'; modal12: false,
const content = '<p>Content of dialog</p><p>Content of dialog</p>'; modal13: false
switch (type) {
case 'info':
this.$Modal.info({
title: title,
content: content
});
break;
case 'success':
this.$Modal.success({
title: title,
content: content
});
break;
case 'warning':
this.$Modal.warning({
title: title,
content: content
});
break;
case 'error':
this.$Modal.error({
title: title,
content: content
});
break;
}
} }
} }
} }

View file

@ -3,10 +3,10 @@
<transition :name="transitionNames[1]"> <transition :name="transitionNames[1]">
<div :class="maskClasses" v-show="visible" v-if="showMask" @click="handleMask"></div> <div :class="maskClasses" v-show="visible" v-if="showMask" @click="handleMask"></div>
</transition> </transition>
<div :class="wrapClasses" @click="handleWrapClick"> <div :class="wrapClasses" :style="wrapStyles" @click="handleWrapClick">
<transition :name="transitionNames[0]" @after-leave="animationFinish"> <transition :name="transitionNames[0]" @after-leave="animationFinish">
<div :class="classes" :style="mainStyles" v-show="visible"> <div :class="classes" :style="mainStyles" v-show="visible">
<div :class="contentClasses" ref="content" :style="contentStyles"> <div :class="contentClasses" ref="content" :style="contentStyles" @click="handleClickModal">
<a :class="[prefixCls + '-close']" v-if="closable" @click="close"> <a :class="[prefixCls + '-close']" v-if="closable" @click="close">
<slot name="close"> <slot name="close">
<Icon type="ios-close"></Icon> <Icon type="ios-close"></Icon>
@ -38,6 +38,9 @@
import ScrollbarMixins from './mixins-scrollbar'; import ScrollbarMixins from './mixins-scrollbar';
import { on, off } from '../../utils/dom'; import { on, off } from '../../utils/dom';
import { findComponentsDownward } from '../../utils/assist';
import { modalIndex, modalIncrease } from './q';
const prefixCls = 'ivu-modal'; const prefixCls = 'ivu-modal';
@ -114,7 +117,11 @@
draggable: { draggable: {
type: Boolean, type: Boolean,
default: false default: false
} },
zIndex: {
type: Number,
default: 1000
},
}, },
data () { data () {
return { return {
@ -129,7 +136,8 @@
dragX: null, dragX: null,
dragY: null, dragY: null,
dragging: false dragging: false
} },
modalIndex: this.handleGetModalIndex(), // for Esc close the top modal
}; };
}, },
computed: { computed: {
@ -143,6 +151,11 @@
} }
]; ];
}, },
wrapStyles () {
return {
zIndex: this.modalIndex + this.zIndex
};
},
maskClasses () { maskClasses () {
return `${prefixCls}-mask`; return `${prefixCls}-mask`;
}, },
@ -247,7 +260,15 @@
EscClose (e) { EscClose (e) {
if (this.visible && this.closable) { if (this.visible && this.closable) {
if (e.keyCode === 27) { if (e.keyCode === 27) {
this.close(); const $Modals = findComponentsDownward(this.$root, 'Modal').filter(item => item.$data.visible && item.$props.closable);
const $TopModal = $Modals.sort((a, b) => {
return a.$data.modalIndex < b.$data.modalIndex ? 1 : -1;
})[0];
setTimeout(() => {
$TopModal.close();
}, 0);
} }
} }
}, },
@ -298,6 +319,13 @@
this.dragData.dragging = false; this.dragData.dragging = false;
off(window, 'mousemove', this.handleMoveMove); off(window, 'mousemove', this.handleMoveMove);
off(window, 'mouseup', this.handleMoveEnd); off(window, 'mouseup', this.handleMoveEnd);
},
handleGetModalIndex () {
modalIncrease();
return modalIndex;
},
handleClickModal () {
this.modalIndex = this.handleGetModalIndex();
} }
}, },
mounted () { mounted () {
@ -332,6 +360,8 @@
this.removeScrollEffect(); this.removeScrollEffect();
}, 300); }, 300);
} else { } else {
this.modalIndex = this.handleGetModalIndex();
if (this.timer) clearTimeout(this.timer); if (this.timer) clearTimeout(this.timer);
this.wrapShow = true; this.wrapShow = true;
if (!this.scrollable) { if (!this.scrollable) {

View file

@ -0,0 +1,7 @@
let modalIndex = 0;
function modalIncrease() {
modalIndex++;
}
export {modalIndex, modalIncrease};