Skip to content

Commit 1fd8c21

Browse files
MessageBox: add distinguishCancelAndClose
1 parent b799d2e commit 1fd8c21

File tree

7 files changed

+236
-21
lines changed

7 files changed

+236
-21
lines changed

examples/docs/en-US/message-box.md

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@
100100
},
101101

102102
open6() {
103+
this.$confirm('You have unsaved changes, save and proceed?', 'Confirm', {
104+
distinguishCancelAndClose: true,
105+
confirmButtonText: 'Save',
106+
cancelButtonText: 'Discard Changes'
107+
})
108+
.then(() => {
109+
this.$message({
110+
type: 'info',
111+
message: 'Changes saved. Proceeding to a new route.'
112+
});
113+
})
114+
.catch(action => {
115+
this.$message({
116+
type: 'info',
117+
message: action === 'cancel'
118+
? 'Changes discarded. Proceeding to a new route.'
119+
: 'Stay in the current route'
120+
})
121+
});
122+
},
123+
124+
open7() {
103125
this.$confirm('This will permanently delete the file. Continue?', 'Warning', {
104126
confirmButtonText: 'OK',
105127
cancelButtonText: 'Cancel',
@@ -292,6 +314,7 @@ The content of MessageBox can be `VNode`, allowing us to pass custom components.
292314
:::
293315

294316
### Use HTML String
317+
295318
`message` supports HTML string.
296319

297320
:::demo Set `dangerouslyUseHTMLString` to true and `message` will be treated as an HTML string.
@@ -319,20 +342,61 @@ The content of MessageBox can be `VNode`, allowing us to pass custom components.
319342
Although `message` property supports HTML strings, dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to [XSS attacks](https://en.wikipedia.org/wiki/Cross-site_scripting). So when `dangerouslyUseHTMLString` is on, please make sure the content of `message` is trusted, and **never** assign `message` to user-provided content.
320343
:::
321344

345+
### Distinguishing cancel and close
346+
347+
In some cases, clicking the cancel button and close button may have different meanings.
348+
349+
:::demo By default, the parameters of Promise's reject callback and `callback` are 'cancel' when the user cancels (clicking the cancel button) and closes (clicking the close button or mask layer, pressing the ESC key) the MessageBox. If `distinguishCancelAndClose` is set to true, the parameters of the above two operations are 'cancel' and 'close' respectively.
350+
351+
```html
352+
<template>
353+
<el-button type="text" @click="open6">Click to open Message Box</el-button>
354+
</template>
355+
356+
<script>
357+
export default {
358+
methods: {
359+
open6() {
360+
this.$confirm('You have unsaved changes, save and proceed?', 'Confirm', {
361+
distinguishCancelAndClose: true,
362+
confirmButtonText: 'Save',
363+
cancelButtonText: 'Discard Changes'
364+
})
365+
.then(() => {
366+
this.$message({
367+
type: 'info',
368+
message: 'Changes saved. Proceeding to a new route.'
369+
});
370+
})
371+
.catch(action => {
372+
this.$message({
373+
type: 'info',
374+
message: action === 'cancel'
375+
? 'Changes discarded. Proceeding to a new route.'
376+
: 'Stay in the current route'
377+
})
378+
});
379+
}
380+
}
381+
}
382+
</script>
383+
```
384+
:::
385+
322386
### Centered content
323387
Content of MessageBox can be centered.
324388

325389
:::demo Setting `center` to `true` will center the content
326390

327391
```html
328392
<template>
329-
<el-button type="text" @click="open6">Click to open Message Box</el-button>
393+
<el-button type="text" @click="open7">Click to open Message Box</el-button>
330394
</template>
331395

332396
<script>
333397
export default {
334398
methods: {
335-
open6() {
399+
open7() {
336400
this.$confirm('This will permanently delete the file. Continue?', 'Warning', {
337401
confirmButtonText: 'OK',
338402
cancelButtonText: 'Cancel',
@@ -384,9 +448,10 @@ The corresponding methods are: `MessageBox`, `MessageBox.alert`, `MessageBox.con
384448
| type | message type, used for icon display | string | success / info / warning / error ||
385449
| iconClass | custom icon's class, overrides `type` | string |||
386450
| customClass | custom class name for MessageBox | string |||
387-
| 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 |||
451+
| callback | MessageBox closing callback if you don't prefer Promise | function(action), where action can be 'confirm', 'cancel' or 'close', and `instance` is the MessageBox instance. You can access to that instance's attributes and methods |||
388452
| showClose | whether to show close icon of MessageBox | boolean || true |
389-
| beforeClose | callback before MessageBox closes, and it will prevent MessageBox from closing | function(action, instance, done), where `action` can be 'confirm' or 'cancel'; `instance` is the MessageBox instance, and you can access to that instance's attributes and methods; `done` is for closing the instance |||
453+
| beforeClose | callback before MessageBox closes, and it will prevent MessageBox from closing | function(action, instance, done), where `action` can be 'confirm', 'cancel' or 'close'; `instance` is the MessageBox instance, and you can access to that instance's attributes and methods; `done` is for closing the instance |||
454+
| distinguishCancelAndClose | whether to distinguish canceling and closing the MessageBox | boolean || false |
390455
| lockScroll | whether to lock body scroll when MessageBox prompts | boolean || true |
391456
| showCancelButton | whether to show a cancel button | boolean || false (true when called with confirm and prompt) |
392457
| showConfirmButton | whether to show a confirm button | boolean || true |

examples/docs/es/message-box.md

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,30 @@
9898
dangerouslyUseHTMLString: true
9999
});
100100
},
101-
101+
102102
open6() {
103+
this.$confirm('You have unsaved changes, save and proceed?', 'Confirm', {
104+
distinguishCancelAndClose: true,
105+
confirmButtonText: 'Save',
106+
cancelButtonText: 'Discard Changes'
107+
})
108+
.then(() => {
109+
this.$message({
110+
type: 'info',
111+
message: 'Changes saved. Proceeding to a new route.'
112+
});
113+
})
114+
.catch(action => {
115+
this.$message({
116+
type: 'info',
117+
message: action === 'cancel'
118+
? 'Changes discarded. Proceeding to a new route.'
119+
: 'Stay in the current route'
120+
})
121+
});
122+
},
123+
124+
open7() {
103125
this.$confirm('This will permanently delete the file. Continue?', 'Warning', {
104126
confirmButtonText: 'OK',
105127
cancelButtonText: 'Cancel',
@@ -295,6 +317,7 @@ El contenido de MessageBox puede ser `VNode`, permitiéndonos pasar componentes
295317
:::
296318

297319
### Utiliza cadenas HTML
320+
298321
`message` soporta cadenas HTML.
299322

300323
:::demo Establezca el valor de `dangerouslyUseHTMLString` a true y `message` sera tratado como una cadena HTML.
@@ -322,20 +345,61 @@ El contenido de MessageBox puede ser `VNode`, permitiéndonos pasar componentes
322345
Aunque la propiedad `message` soporta cadenas HTML, realizar arbitrariamente render dinamico de HTML en nuestro sitio web puede ser muy peligroso ya que puede conducir facilmente a [XSS attacks](https://en.wikipedia.org/wiki/Cross-site_scripting). Entonces cuando `dangerouslyUseHTMLString` esta activada, asegurece que el contendio de `message` sea de confianza, y **nunca** asignar `message` a contenido generado por el usuario.
323346
:::
324347

348+
### Distinguishing cancel and close
349+
350+
In some cases, clicking the cancel button and close button may have different meanings.
351+
352+
:::demo By default, the parameters of Promise's reject callback and `callback` are 'cancel' when the user cancels (clicking the cancel button) and closes (clicking the close button or mask layer, pressing the ESC key) the MessageBox. If `distinguishCancelAndClose` is set to true, the parameters of the above two operations are 'cancel' and 'close' respectively.
353+
354+
```html
355+
<template>
356+
<el-button type="text" @click="open6">Click to open Message Box</el-button>
357+
</template>
358+
359+
<script>
360+
export default {
361+
methods: {
362+
open6() {
363+
this.$confirm('You have unsaved changes, save and proceed?', 'Confirm', {
364+
distinguishCancelAndClose: true,
365+
confirmButtonText: 'Save',
366+
cancelButtonText: 'Discard Changes'
367+
})
368+
.then(() => {
369+
this.$message({
370+
type: 'info',
371+
message: 'Changes saved. Proceeding to a new route.'
372+
});
373+
})
374+
.catch(action => {
375+
this.$message({
376+
type: 'info',
377+
message: action === 'cancel'
378+
? 'Changes discarded. Proceeding to a new route.'
379+
: 'Stay in the current route'
380+
})
381+
});
382+
}
383+
}
384+
}
385+
</script>
386+
```
387+
:::
388+
325389
### Centered content
326390
El contenido del componente MessageBox puede ser centrado.
327391

328392
:::demo Establecer `center` a `true` centrara el contenido
329393

330394
```html
331395
<template>
332-
<el-button type="text" @click="open6">Click to open Message Box</el-button>
396+
<el-button type="text" @click="open7">Click to open Message Box</el-button>
333397
</template>
334398

335399
<script>
336400
export default {
337401
methods: {
338-
open6() {
402+
open7() {
339403
this.$confirm('This will permanently delete the file. Continue?', 'Warning', {
340404
confirmButtonText: 'OK',
341405
cancelButtonText: 'Cancel',
@@ -387,8 +451,9 @@ Los metodos correspondientes: `MessageBox`, `MessageBox.alert`, `MessageBox.conf
387451
| type | tipo de mensaje , utilizado para mostrar el icono | string | success / info / warning / error ||
388452
| iconClass | clase personalizada para el icono, sobreescribe `type` | string |||
389453
| customClass | nombre de la clase personzalida para el componente MessageBox | string |||
390-
| callback | MessageBox callback al cerrar si no desea utilizar Promise | function(action), donde la accion puede ser 'confirm' o 'cancel', e `instance` es la instancia del componente MessageBox. Puedes acceder a los metodos y atributos de esa instancia |||
391-
| beforeClose | callback llamado antes de cerrar el componente MessageBox, y previene que el componente MessageBox se cierre | function(action, instance, done), donde `action` pueden ser 'confirm' o 'cancel'; `instance` es la instancia del componente MessageBox, Puedes acceder a los metodos y atributos de esa instancia; `done` es para cerrar la instancia |||
454+
| callback | MessageBox callback al cerrar si no desea utilizar Promise | function(action), donde la accion puede ser 'confirm', 'cancel' o 'close', e `instance` es la instancia del componente MessageBox. Puedes acceder a los metodos y atributos de esa instancia |||
455+
| beforeClose | callback llamado antes de cerrar el componente MessageBox, y previene que el componente MessageBox se cierre | function(action, instance, done), donde `action` pueden ser 'confirm', 'cancel' o 'close'; `instance` es la instancia del componente MessageBox, Puedes acceder a los metodos y atributos de esa instancia; `done` es para cerrar la instancia |||
456+
| distinguishCancelAndClose | whether to distinguish canceling and closing the MessageBox | boolean || false |
392457
| lockScroll | utilizado para bloquear el desplazamiento del contenido del MessageBox prompts | boolean || true |
393458
| showCancelButton | utlizado para mostrar un boton cancelar | boolean || false (true cuando es llamado con confirm y prompt) |
394459
| showConfirmButton | utlizado para mostrar un boton confirmar | boolean || true |

examples/docs/zh-CN/message-box.md

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@
101101
},
102102

103103
open6() {
104+
this.$confirm('检测到未保存的内容,是否在离开页面前保存修改?', '确认信息', {
105+
distinguishCancelAndClose: true,
106+
confirmButtonText: '保存',
107+
cancelButtonText: '放弃修改'
108+
})
109+
.then(() => {
110+
this.$message({
111+
type: 'info',
112+
message: '保存修改'
113+
});
114+
})
115+
.catch(action => {
116+
this.$message({
117+
type: 'info',
118+
message: action === 'cancel'
119+
? '放弃保存并离开页面'
120+
: '停留在当前页面'
121+
})
122+
});
123+
},
124+
125+
open7() {
104126
this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
105127
confirmButtonText: '确定',
106128
cancelButtonText: '取消',
@@ -290,7 +312,8 @@
290312
:::
291313

292314
### 使用 HTML 片段
293-
`message` 属性支持传入 HTML 片段
315+
316+
`message` 属性支持传入 HTML 片段。
294317

295318
:::demo 将`dangerouslyUseHTMLString`属性设置为 true,`message` 就会被当作 HTML 片段处理。
296319

@@ -317,20 +340,61 @@
317340
`message` 属性虽然支持传入 HTML 片段,但是在网站上动态渲染任意 HTML 是非常危险的,因为容易导致 [XSS 攻击](https://en.wikipedia.org/wiki/Cross-site_scripting)。因此在 `dangerouslyUseHTMLString` 打开的情况下,请确保 `message` 的内容是可信的,**永远不要**将用户提交的内容赋值给 `message` 属性。
318341
:::
319342

343+
### 区分取消与关闭
344+
345+
有些场景下,点击取消按钮与点击关闭按钮有着不同的含义。
346+
347+
:::demo 默认情况下,当用户触发取消(点击取消按钮)和触发关闭(点击关闭按钮或遮罩层、按下 ESC 键)时,Promise 的 reject 回调和`callback`回调的参数均为 'cancel'。如果将`distinguishCancelAndClose`属性设置为 true,则上述两种行为的参数分别为 'cancel' 和 'close'。
348+
349+
```html
350+
<template>
351+
<el-button type="text" @click="open6">点击打开 Message Box</el-button>
352+
</template>
353+
354+
<script>
355+
export default {
356+
methods: {
357+
open6() {
358+
this.$confirm('检测到未保存的内容,是否在离开页面前保存修改?', '确认信息', {
359+
distinguishCancelAndClose: true,
360+
confirmButtonText: '保存',
361+
cancelButtonText: '放弃修改'
362+
})
363+
.then(() => {
364+
this.$message({
365+
type: 'info',
366+
message: '保存修改'
367+
});
368+
})
369+
.catch(action => {
370+
this.$message({
371+
type: 'info',
372+
message: action === 'cancel'
373+
? '放弃保存并离开页面'
374+
: '停留在当前页面'
375+
})
376+
});
377+
}
378+
}
379+
}
380+
</script>
381+
```
382+
:::
383+
320384
### 居中布局
321385
内容支持居中布局
322386

323387
:::demo 将 `center` 设置为 `true` 即可开启居中布局
324388

325389
```html
326390
<template>
327-
<el-button type="text" @click="open6">点击打开 Message Box</el-button>
391+
<el-button type="text" @click="open7">点击打开 Message Box</el-button>
328392
</template>
329393

330394
<script>
331395
export default {
332396
methods: {
333-
open6() {
397+
open7() {
334398
this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
335399
confirmButtonText: '确定',
336400
cancelButtonText: '取消',
@@ -382,9 +446,10 @@ import { MessageBox } from 'element-ui';
382446
| type | 消息类型,用于显示图标 | string | success / info / warning / error ||
383447
| iconClass | 自定义图标的类名,会覆盖 `type` | string |||
384448
| customClass | MessageBox 的自定义类名 | string |||
385-
| callback | 若不使用 Promise,可以使用此参数指定 MessageBox 关闭后的回调 | function(action, instance),action 的值为'confirm''cancel', instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法 |||
449+
| callback | 若不使用 Promise,可以使用此参数指定 MessageBox 关闭后的回调 | function(action, instance),action 的值为'confirm', 'cancel'或'close', instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法 |||
386450
| showClose | MessageBox 是否显示右上角关闭按钮 | boolean || true |
387-
| beforeClose | MessageBox 关闭前的回调,会暂停实例的关闭 | function(action, instance, done),action 的值为'confirm'或'cancel';instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法;done 用于关闭 MessageBox 实例 |||
451+
| beforeClose | MessageBox 关闭前的回调,会暂停实例的关闭 | function(action, instance, done),action 的值为'confirm', 'cancel'或'close';instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法;done 用于关闭 MessageBox 实例 |||
452+
| distinguishCancelAndClose | 是否将取消(点击取消按钮)与关闭(点击关闭按钮或遮罩层、按下 ESC 键)进行区分 | boolean || false |
388453
| lockScroll | 是否在 MessageBox 出现时将 body 滚动锁定 | boolean || true |
389454
| showCancelButton | 是否显示取消按钮 | boolean || false(以 confirm 和 prompt 方式调用时为 true) |
390455
| showConfirmButton | 是否显示确定按钮 | boolean || true |

packages/message-box/src/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const defaults = {
2929
beforeClose: null,
3030
dangerouslyUseHTMLString: false,
3131
center: false,
32-
roundButton: false
32+
roundButton: false,
33+
distinguishCancelAndClose: false
3334
};
3435

3536
import Vue from 'vue';
@@ -59,7 +60,7 @@ const defaultCallback = action => {
5960
} else {
6061
currentMsg.resolve(action);
6162
}
62-
} else if (action === 'cancel' && currentMsg.reject) {
63+
} else if (currentMsg.reject && (action === 'cancel' || action === 'close')) {
6364
currentMsg.reject(action);
6465
}
6566
}

packages/message-box/src/main.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
class="el-message-box__headerbtn"
2323
aria-label="Close"
2424
v-if="showClose"
25-
@click="handleAction('cancel')"
26-
@keydown.enter="handleAction('cancel')">
25+
@click="handleAction(distinguishCancelAndClose ? 'close' : 'cancel')"
26+
@keydown.enter="handleAction(distinguishCancelAndClose ? 'close' : 'cancel')">
2727
<i class="el-message-box__close el-icon-close"></i>
2828
</button>
2929
</div>
@@ -173,7 +173,7 @@
173173
174174
handleWrapperClick() {
175175
if (this.closeOnClickModal) {
176-
this.handleAction('cancel');
176+
this.handleAction(this.distinguishCancelAndClose ? 'close' : 'cancel');
177177
}
178178
},
179179
@@ -319,7 +319,8 @@
319319
callback: null,
320320
dangerouslyUseHTMLString: false,
321321
focusAfterClosed: null,
322-
isOnComposition: false
322+
isOnComposition: false,
323+
distinguishCancelAndClose: false
323324
};
324325
}
325326
};

0 commit comments

Comments
 (0)