Message: add closeOnHashChange, update docs

pull/6231/head
Leopoldthecoder 2017-07-24 19:09:36 +08:00 committed by 杨奕
parent 97183e3bc6
commit 798bb8e75d
7 changed files with 96 additions and 17 deletions

View File

@ -96,7 +96,8 @@ cooking.add('vueMarkdown', {
return '</div></demo-block>\n'; return '</div></demo-block>\n';
} }
}], }],
[require('markdown-it-container'), 'tip'] [require('markdown-it-container'), 'tip'],
[require('markdown-it-container'), 'warning']
], ],
preprocess: function(MarkdownIt, source) { preprocess: function(MarkdownIt, source) {
MarkdownIt.renderer.rules.table_open = function() { MarkdownIt.renderer.rules.table_open = function() {

View File

@ -114,6 +114,39 @@
color: #445368; color: #445368;
} }
} }
.warning {
border-bottom-right-radius: 4px;
border-left: 4px solid #f66;
border-top-right-radius: 4px;
padding: 12px 24px 12px 30px;
position: relative;
background-color: #f8f8f8;
margin-top: 20px;
&::before {
background-color: #f66;
border-radius: 100%;
color: #fff;
content: "!";
font-family: Dosis,Source Sans Pro,Helvetica Neue,Arial,sans-serif;
font-size: 14px;
font-weight: 700;
left: -12px;
line-height: 20px;
position: absolute;
width: 20px;
height: 20px;
text-align: center;
top: 50%;
transform: translateY(-50%);
}
code {
background-color: rgba(#fff, .7);
color: #445368;
}
}
} }
.demo { .demo {
margin: 20px 0; margin: 20px 0;

View File

@ -99,7 +99,10 @@
## MessageBox ## MessageBox
A set of modal boxes simulating system message box, mainly for message prompt, success tips, error messages and query information. A set of modal boxes simulating system message box, mainly for alerting information, confirm operations and prompting messages.
:::tip
By design MessageBox provides simulations of system's `alert`, `confirm` and `prompt`so it's content should be simple. For more complicated contents, please use Dialog.
:::
### Alert ### Alert
@ -262,17 +265,25 @@ Can be customized to show various content.
### Global method ### Global method
Element has added the following global methods for Vue.prototype: `$msgbox`, `$alert`, `$confirm` and `$prompt`. So in a vue instance you can call `MessageBox` like what we did in this page. If Element is fully imported, it will add the following global methods for Vue.prototype: `$msgbox`, `$alert`, `$confirm` and `$prompt`. So in a Vue instance you can call `MessageBox` like what we did in this page. The parameters are:
- `$msgbox(options)`
- `$alert(message, title, options)` or `$alert(message, options)`
- `$confirm(message, title, options)` or `$confirm(message, options)`
- `$prompt(message, title, options)` or `$prompt(message, options)`
### Local import ### Local import
Import `MessageBox`: If you prefer importing `MessageBox` on demand:
```javascript ```javascript
import { MessageBox } from 'element-ui'; import { MessageBox } from 'element-ui';
``` ```
The corresponding methods are: `MessageBox`, `MessageBox.alert`, `MessageBox.confirm` and `MessageBox.prompt`. The corresponding methods are: `MessageBox`, `MessageBox.alert`, `MessageBox.confirm` and `MessageBox.prompt`. The parameters are the same as above.
:::warning
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). Please make sure the content of `message` is trusted, and **never** assign `message` to user-provided content.
:::
### Options ### Options
@ -293,6 +304,7 @@ The corresponding methods are: `MessageBox`, `MessageBox.alert`, `MessageBox.con
| confirmButtonClass | custom class name of confirm button | string | — | — | | confirmButtonClass | custom class name of confirm button | string | — | — |
| closeOnClickModal | whether MessageBox can be closed by clicking the mask | boolean | — | true (false when called with alert) | | closeOnClickModal | whether MessageBox can be closed by clicking the mask | boolean | — | true (false when called with alert) |
| closeOnPressEscape | whether MessageBox can be closed by pressing the ESC | boolean | — | true (false when called with alert) | | closeOnPressEscape | whether MessageBox can be closed by pressing the ESC | boolean | — | true (false when called with alert) |
| closeOnHashChange | whether to close MessageBox when hash changes | boolean | — | true |
| showInput | whether to show an input | boolean | — | false (true when called with prompt) | | showInput | whether to show an input | boolean | — | false (true when called with prompt) |
| inputPlaceholder | placeholder of input | string | — | — | | inputPlaceholder | placeholder of input | string | — | — |
| inputValue | initial value of input | string | — | — | | inputValue | initial value of input | string | — | — |

View File

@ -98,7 +98,11 @@
}; };
</script> </script>
## MessageBox 弹框 ## MessageBox 弹框
模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示、成功提示、错误提示、询问信息。 模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示、确认消息和提交内容。
:::tip
从场景上说MessageBox 的作用是美化系统自带的 `alert`、`confirm` 和 `prompt`,因此适合展示较为简单的内容。如果需要弹出较为复杂的内容,请使用 Dialog。
:::
### 消息提示 ### 消息提示
@ -171,7 +175,7 @@
当用户进行操作时会被触发,中断用户操作,提示用户进行输入的对话框。 当用户进行操作时会被触发,中断用户操作,提示用户进行输入的对话框。
:::demo 调用`$prompt`方法即可打开消息提示,它模拟了系统的 `prompt`。可以用`inputPattern`字段自己规定匹配模式,或者用`inputValidator`规定校验函数,可以返回`Boolean`或`String``Boolean`为`false`或字符串时均表示校验未通过,`String`相当于定义了`inputErrorMessage`字段。此外,可以用`inputPlaceholder`字段来定义输入框的占位符。 :::demo 调用`$prompt`方法即可打开消息提示,它模拟了系统的 `prompt`。可以用`inputPattern`字段自己规定匹配模式,或者用`inputValidator`规定校验函数,可以返回`Boolean`或`String`返回`false`或字符串时均表示校验未通过,同时返回的字符串相当于定义了`inputErrorMessage`字段。此外,可以用`inputPlaceholder`字段来定义输入框的占位符。
```html ```html
<template> <template>
@ -259,17 +263,25 @@
### 全局方法 ### 全局方法
Element 为 Vue.prototype 添加了如下全局方法:$msgbox, $alert, $confirm 和 $prompt。因此在 vue instance 中可以采用本页面中的方式调用 `MessageBox` 如果你完整引入了 Element它会为 Vue.prototype 添加如下全局方法:$msgbox, $alert, $confirm 和 $prompt。因此在 Vue instance 中可以采用本页面中的方式调用 `MessageBox`。调用参数为:
- `$msgbox(options)`
- `$alert(message, title, options)``$alert(message, options)`
- `$confirm(message, title, options)``$confirm(message, options)`
- `$prompt(message, title, options)``$prompt(message, options)`
### 单独引用 ### 单独引用
单独引入 `MessageBox` 如果单独引入 `MessageBox`
```javascript ```javascript
import { MessageBox } from 'element-ui'; import { MessageBox } from 'element-ui';
``` ```
对应于上述四个全局方法的调用方法依次为MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt。 那么对应于上述四个全局方法的调用方法依次为MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt调用参数与全局方法相同。
:::warning
`message` 属性虽然支持传入 HTML 片段,但是在网站上动态渲染任意 HTML 是非常危险的,因为容易导致 [XSS 攻击](https://en.wikipedia.org/wiki/Cross-site_scripting)。请确保 `message` 的内容是可信的,**永远不要**将用户提交的内容赋值给 `message` 属性。
:::
### Options ### Options
@ -290,6 +302,7 @@ import { MessageBox } from 'element-ui';
| confirmButtonClass | 确定按钮的自定义类名 | string | — | — | | confirmButtonClass | 确定按钮的自定义类名 | string | — | — |
| closeOnClickModal | 是否可通过点击遮罩关闭 MessageBox | boolean | — | true以 alert 方式调用时为 false | | closeOnClickModal | 是否可通过点击遮罩关闭 MessageBox | boolean | — | true以 alert 方式调用时为 false |
| closeOnPressEscape | 是否可通过按下 ESC 键关闭 MessageBox | boolean | — | true以 alert 方式调用时为 false | | closeOnPressEscape | 是否可通过按下 ESC 键关闭 MessageBox | boolean | — | true以 alert 方式调用时为 false |
| closeOnHashChange | 是否在 hashchange 时关闭 MessageBox | boolean | — | true |
| showInput | 是否显示输入框 | boolean | — | false以 prompt 方式调用时为 true| | showInput | 是否显示输入框 | boolean | — | false以 prompt 方式调用时为 true|
| inputPlaceholder | 输入框的占位符 | string | — | — | | inputPlaceholder | 输入框的占位符 | string | — | — |
| inputValue | 输入框的初始文本 | string | — | — | | inputValue | 输入框的初始文本 | string | — | — |

View File

@ -36,6 +36,13 @@
padding-left: 10px; padding-left: 10px;
} }
} }
ul {
margin: 10px 0;
padding: 0 0 0 20px;
font-size: 14px;
color: #5e6d82;
line-height: 2em;
}
} }
} }
.page-component-up { .page-component-up {

View File

@ -8,6 +8,7 @@ const defaults = {
lockScroll: true, lockScroll: true,
closeOnClickModal: true, closeOnClickModal: true,
closeOnPressEscape: true, closeOnPressEscape: true,
closeOnHashChange: true,
inputValue: null, inputValue: null,
inputPlaceholder: '', inputPlaceholder: '',
inputPattern: null, inputPattern: null,
@ -99,7 +100,7 @@ const showNextMsg = () => {
} else { } else {
delete instance.$slots.default; delete instance.$slots.default;
} }
['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape'].forEach(prop => { ['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape', 'closeOnHashChange'].forEach(prop => {
if (instance[prop] === undefined) { if (instance[prop] === undefined) {
instance[prop] = true; instance[prop] = true;
} }
@ -115,16 +116,13 @@ const showNextMsg = () => {
const MessageBox = function(options, callback) { const MessageBox = function(options, callback) {
if (Vue.prototype.$isServer) return; if (Vue.prototype.$isServer) return;
if (typeof options === 'string') { if (typeof options === 'string' || isVNode(options)) {
options = { options = {
message: options message: options
}; };
if (arguments[1]) { if (typeof arguments[1] === 'string') {
options.title = arguments[1]; options.title = arguments[1];
} }
if (arguments[2]) {
options.type = arguments[2];
}
} else if (options.callback && !callback) { } else if (options.callback && !callback) {
callback = options.callback; callback = options.callback;
} }

View File

@ -12,7 +12,7 @@
<div class="el-message-box__content" v-if="message !== ''"> <div class="el-message-box__content" v-if="message !== ''">
<div class="el-message-box__status" :class="[ typeClass ]"></div> <div class="el-message-box__status" :class="[ typeClass ]"></div>
<div class="el-message-box__message" :style="{ 'margin-left': typeClass ? '50px' : '0' }"> <div class="el-message-box__message" :style="{ 'margin-left': typeClass ? '50px' : '0' }">
<slot><p>{{ message }}</p></slot> <slot><p v-html="message"></p></slot>
</div> </div>
<div class="el-message-box__input" v-show="showInput"> <div class="el-message-box__input" v-show="showInput">
<el-input v-model="inputValue" @keyup.enter.native="handleAction('confirm')" :placeholder="inputPlaceholder" ref="input"></el-input> <el-input v-model="inputValue" @keyup.enter.native="handleAction('confirm')" :placeholder="inputPlaceholder" ref="input"></el-input>
@ -75,6 +75,9 @@
}, },
closeOnPressEscape: { closeOnPressEscape: {
default: true default: true
},
closeOnHashChange: {
default: true
} }
}, },
@ -210,6 +213,18 @@
} }
}, },
mounted() {
if (this.closeOnHashChange) {
window.addEventListener('hashchange', this.close);
}
},
beforeDestroy() {
if (this.closeOnHashChange) {
window.removeEventListener('hashchange', this.close);
}
},
data() { data() {
return { return {
uid: 1, uid: 1,