MessageBox: add uid

pull/2204/head
Leopoldthecoder 2017-01-06 13:53:06 +08:00
parent 35f102fa2c
commit 900d7af019
5 changed files with 22 additions and 8 deletions

View File

@ -274,7 +274,7 @@ The corresponding methods are: `MessageBox`, `MessageBox.alert`, `MessageBox.con
| message | content of the MessageBox | string | — | — | | message | content of the MessageBox | string | — | — |
| type | message type, used for icon display | string | success/info/warning/error | — | | type | message type, used for icon display | string | success/info/warning/error | — |
| customClass | custom class name for MessageBox | string | — | — | | customClass | custom class name for MessageBox | string | — | — |
| callback | MessageBox closing callback if you don't prefer Promise | function(action), where action can be 'confirm' or 'cancel' | — | — | | callback | MessageBox closing callback if you don't prefer Promise | function(action), where action can be 'confirm' or 'cancel', and `instance` is the MessageBox instance. You can access to that instance's attributes and methods | — | — |
| beforeClose | callback before MessageBox closes, and it will prevent MessageBox from closing | function(action, instance), where `action` can be 'confirm' or 'cancel', and `instance` is the MessageBox instance. You can access to that instance's attributes and methods | — | — | | beforeClose | callback before MessageBox closes, and it will prevent MessageBox from closing | function(action, instance), where `action` can be 'confirm' or 'cancel', and `instance` is the MessageBox instance. You can access to that instance's attributes and methods | — | — |
| lockScroll | whether to lock body scroll when MessageBox prompts | boolean | — | true | | lockScroll | whether to lock body scroll when MessageBox prompts | boolean | — | true |
| showCancelButton | whether to show a cancel button | boolean | — | false (true when called with confirm and prompt) | | showCancelButton | whether to show a cancel button | boolean | — | false (true when called with confirm and prompt) |

View File

@ -271,7 +271,7 @@ import { MessageBox } from 'element-ui';
| message | MessageBox 消息正文内容 | string | — | — | | message | MessageBox 消息正文内容 | string | — | — |
| type | 消息类型,用于显示图标 | string | success/info/warning/error | — | | type | 消息类型,用于显示图标 | string | success/info/warning/error | — |
| customClass | MessageBox 的自定义类名 | string | — | — | | customClass | MessageBox 的自定义类名 | string | — | — |
| callback | 若不使用 Promise可以使用此参数指定 MessageBox 关闭后的回调 | function(action)action 的值为'confirm'或'cancel' | — | — | | callback | 若不使用 Promise可以使用此参数指定 MessageBox 关闭后的回调 | function(action, instance)action 的值为'confirm'或'cancel', instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法 | — | — |
| beforeClose | MessageBox 关闭前的回调,会阻止实例的关闭 | function(action, instance)action 的值为'confirm'或'cancel', instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法 | — | — | | beforeClose | MessageBox 关闭前的回调,会阻止实例的关闭 | function(action, instance)action 的值为'confirm'或'cancel', instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法 | — | — |
| lockScroll | 是否在 MessageBox 出现时将 body 滚动锁定 | boolean | — | true | | lockScroll | 是否在 MessageBox 出现时将 body 滚动锁定 | boolean | — | true |
| showCancelButton | 是否显示取消按钮 | boolean | — | false以 confirm 和 prompt 方式调用时为 true | | showCancelButton | 是否显示取消按钮 | boolean | — | false以 confirm 和 prompt 方式调用时为 true |

View File

@ -20,7 +20,8 @@ const defaults = {
confirmButtonText: '', confirmButtonText: '',
cancelButtonText: '', cancelButtonText: '',
confirmButtonClass: '', confirmButtonClass: '',
cancelButtonClass: '' cancelButtonClass: '',
beforeClose: null
}; };
import Vue from 'vue'; import Vue from 'vue';

View File

@ -92,6 +92,14 @@
}, },
methods: { methods: {
safeClose() {
const currentId = this.uid;
return () => {
this.$nextTick(() => {
if (currentId === this.uid) this.doClose();
});
};
},
doClose() { doClose() {
if (!this.value) return; if (!this.value) return;
this.value = false; this.value = false;
@ -120,7 +128,7 @@
handleWrapperClick() { handleWrapperClick() {
if (this.closeOnClickModal) { if (this.closeOnClickModal) {
this.action = ''; this.action = '';
this.close(); this.doClose();
} }
}, },
@ -130,9 +138,10 @@
} }
this.action = action; this.action = action;
if (typeof this.beforeClose === 'function') { if (typeof this.beforeClose === 'function') {
this.close = this.safeClose();
this.beforeClose(action, this); this.beforeClose(action, this);
} else { } else {
this.close(); this.doClose();
} }
}, },
@ -172,6 +181,7 @@
}, },
value(val) { value(val) {
if (val) this.uid++;
if (this.$type === 'alert' || this.$type === 'confirm') { if (this.$type === 'alert' || this.$type === 'confirm') {
this.$nextTick(() => { this.$nextTick(() => {
this.$refs.confirm.$el.focus(); this.$refs.confirm.$el.focus();
@ -193,6 +203,7 @@
data() { data() {
return { return {
uid: 1,
title: undefined, title: undefined,
message: '', message: '',
type: '', type: '',

View File

@ -169,9 +169,11 @@ describe('MessageBox', () => {
}); });
setTimeout(() => { setTimeout(() => {
document.querySelector('.el-message-box__wrapper .el-button--primary').click(); document.querySelector('.el-message-box__wrapper .el-button--primary').click();
expect(msgAction).to.equal('confirm'); setTimeout(() => {
done(); expect(msgAction).to.equal('confirm');
}, 50); done();
}, 10);
}, 10);
}); });
describe('promise', () => { describe('promise', () => {