feat: meaage add key for update content

pull/1845/head
tangjinzhou 2020-02-22 10:50:43 +08:00
parent b85bc0e738
commit 078a3e4fff
12 changed files with 97 additions and 40 deletions

View File

@ -1,5 +1,5 @@
module.exports = { module.exports = {
dev: { dev: {
componentName: 'menu', // dev components componentName: 'message', // dev components
}, },
}; };

View File

@ -9,3 +9,5 @@ exports[`renders ./components/message/demo/loading.md correctly 1`] = `<button t
exports[`renders ./components/message/demo/other.md correctly 1`] = `<div><button type="button" class="ant-btn"><span>Success</span></button> <button type="button" class="ant-btn"><span>Error</span></button> <button type="button" class="ant-btn"><span>Warning</span></button></div>`; exports[`renders ./components/message/demo/other.md correctly 1`] = `<div><button type="button" class="ant-btn"><span>Success</span></button> <button type="button" class="ant-btn"><span>Error</span></button> <button type="button" class="ant-btn"><span>Warning</span></button></div>`;
exports[`renders ./components/message/demo/thenable.md correctly 1`] = `<button type="button" class="ant-btn"><span>Display a sequence of message</span></button>`; exports[`renders ./components/message/demo/thenable.md correctly 1`] = `<button type="button" class="ant-btn"><span>Display a sequence of message</span></button>`;
exports[`renders ./components/message/demo/update.md correctly 1`] = `<button type="button" class="ant-btn ant-btn-primary"><span>Open the message box</span></button>`;

View File

@ -97,13 +97,14 @@ describe('message', () => {
}); });
}); });
it('should be called like promise', () => { it('should be called like promise', done => {
const defaultDuration = 3; const defaultDuration = 3;
const now = Date.now(); const now = Date.now();
message.info('whatever').then(() => { message.info('whatever').then(() => {
// calculate the approximately duration value // calculate the approximately duration value
const aboutDuration = parseInt((Date.now() - now) / 1000, 10); const aboutDuration = parseInt((Date.now() - now) / 1000, 10);
expect(aboutDuration).toBe(defaultDuration); expect(aboutDuration).toBe(defaultDuration);
done();
}); });
}); });

View File

@ -4,6 +4,7 @@ import Info from './info';
import Loading from './loading'; import Loading from './loading';
import Other from './other'; import Other from './other';
import Thenable from './thenable'; import Thenable from './thenable';
import Update from './update';
import CN from '../index.zh-CN.md'; import CN from '../index.zh-CN.md';
import US from '../index.en-US.md'; import US from '../index.en-US.md';
const md = { const md = {
@ -36,6 +37,7 @@ export default {
<Loading /> <Loading />
<Other /> <Other />
<Thenable /> <Thenable />
<Update />
<api> <api>
<CN slot="cn" /> <CN slot="cn" />
<US /> <US />

View File

@ -5,7 +5,7 @@
<us> <us>
#### Normal prompt #### Normal prompt
Normal messages as feedbacks. Normal message for information.
</us> </us>
```tpl ```tpl

View File

@ -4,7 +4,7 @@
</cn> </cn>
<us> <us>
#### Message of loading #### Message with loading indicator
Display a global loading indicator, which is dismissed by itself asynchronously. Display a global loading indicator, which is dismissed by itself asynchronously.
</us> </us>

View File

@ -20,13 +20,13 @@ Messages of success, error and warning types.
export default { export default {
methods: { methods: {
success() { success() {
this.$message.success('This is a message of success'); this.$message.success('This is a success message');
}, },
error() { error() {
this.$message.error('This is a message of error'); this.$message.error('This is an error message');
}, },
warning() { warning() {
this.$message.warning('This is message of warning'); this.$message.warning('This is a warning message');
}, },
}, },
}; };

View File

@ -0,0 +1,28 @@
<cn>
#### 更新消息内容
可以通过唯一的 `key` 来更新内容。
</cn>
<us>
#### Update Message Content
Update message content with unique `key`.
</us>
```tpl
<template>
<a-button type="primary" @click="openMessage">Open the message box</a-button>
</template>
<script>
const key = 'updatable';
export default {
methods: {
openMessage() {
this.$message.loading({ content: 'Loading...', key });
setTimeout(() => {
this.$message.success({ content: 'Loaded!', key, duration: 2 });
}, 1000);
},
},
};
</script>
```

View File

@ -23,15 +23,22 @@ This components provides some static methods, with usage and arguments as follow
where `level` refers one static methods of `message`. The result of `then` method will be a Promise. where `level` refers one static methods of `message`. The result of `then` method will be a Promise.
- `message.open(config)` - `message.open(config)`
- `message.success(config)`
- `message.error(config)`
- `message.info(config)`
- `message.warning(config)`
- `message.warn(config)` // alias of warning
- `message.loading(config)`
The properties of config are as follows: The properties of config are as follows:
| Property | Description | Type | Default | | Property | Description | Type | Default | Version |
| --- | --- | --- | --- | | --- | --- | --- | --- | --- |
| content | content of the message | string\| VNode \|(h) => VNode | - | | content | content of the message | string\| VNode \|(h) => VNode | - | |
| duration | time(seconds) before auto-dismiss, don't dismiss if set to 0 | number | 3 | | duration | time(seconds) before auto-dismiss, don't dismiss if set to 0 | number | 3 | |
| onClose | Specify a function that will be called when the message is closed | function | - | | onClose | Specify a function that will be called when the message is closed | function | - | |
| icon | Customized Icon | string\| VNode \|(h) => VNode | - | | icon | Customized Icon | string\| VNode \|(h) => VNode | - | |
| key | The unique identifier of the Message | string\|number | - | 1.5.0 |
### Global static methods ### Global static methods

View File

@ -46,7 +46,7 @@ function notice(args) {
loading: 'loading', loading: 'loading',
}[args.type]; }[args.type];
const target = key++; const target = args.key || key++;
const closePromise = new Promise(resolve => { const closePromise = new Promise(resolve => {
const callback = () => { const callback = () => {
if (typeof args.onClose === 'function') { if (typeof args.onClose === 'function') {
@ -59,24 +59,24 @@ function notice(args) {
key: target, key: target,
duration, duration,
style: {}, style: {},
content: h => ( content: h => {
const iconNode = (
<Icon type={iconType} theme={iconType === 'loading' ? 'outlined' : 'filled'} />
);
const switchIconNode = iconType ? iconNode : '';
return (
<div <div
class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`} class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`}
> >
{args.icon ? ( {args.icon
typeof args.icon === 'function' ? ( ? typeof args.icon === 'function'
args.icon(h) ? args.icon(h)
) : ( : args.icon
args.icon : switchIconNode}
)
) : iconType ? (
<Icon type={iconType} theme={iconType === 'loading' ? 'outlined' : 'filled'} />
) : (
''
)}
<span>{typeof args.content === 'function' ? args.content(h) : args.content}</span> <span>{typeof args.content === 'function' ? args.content(h) : args.content}</span>
</div> </div>
), );
},
onClose: callback, onClose: callback,
}); });
}); });
@ -95,6 +95,10 @@ function notice(args) {
// type ConfigDuration = number | (() => void); // type ConfigDuration = number | (() => void);
// export type ConfigOnClose = () => void; // export type ConfigOnClose = () => void;
function isArgsProps(content) {
return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
}
// export interface ConfigOptions { // export interface ConfigOptions {
// top?: number; // top?: number;
// duration?: number; // duration?: number;
@ -138,11 +142,14 @@ const api = {
['success', 'info', 'warning', 'error', 'loading'].forEach(type => { ['success', 'info', 'warning', 'error', 'loading'].forEach(type => {
api[type] = (content, duration, onClose) => { api[type] = (content, duration, onClose) => {
if (isArgsProps(content)) {
return api.open({ ...content, type });
}
if (typeof duration === 'function') { if (typeof duration === 'function') {
onClose = duration; onClose = duration;
duration = undefined; duration = undefined;
} }
return api.open({ content, duration: duration, type, onClose }); return api.open({ content, duration, type, onClose });
}; };
}); });

View File

@ -22,14 +22,23 @@
其中`message[level]` 是组件已经提供的静态方法。`then` 接口返回值是 Promise 。 其中`message[level]` 是组件已经提供的静态方法。`then` 接口返回值是 Promise 。
- `message.open(config)` 也可以对象的形式传递参数:
| 参数 | 说明 | 类型 | 默认值 | - `message.open(config)`
| --- | --- | --- | --- | - `message.success(config)`
| content | 提示内容 | string\| VNode \|(h) => VNode | - | - `message.error(config)`
| duration | 自动关闭的延时,单位秒。设为 0 时不自动关闭。 | number | 3 | - `message.info(config)`
| onClose | 关闭时触发的回调函数 | Function | - | - `message.warning(config)`
| icon | 自定义图标 | string\| VNode \|(h) => VNode | - | - `message.warn(config)` // alias of warning
- `message.loading(config)`
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| content | 提示内容 | string\| VNode \|(h) => VNode | - | |
| duration | 自动关闭的延时,单位秒。设为 0 时不自动关闭。 | number | 3 | |
| onClose | 关闭时触发的回调函数 | Function | - | |
| icon | 自定义图标 | string\| VNode \|(h) => VNode | - | |
| key | 当前提示的唯一标志 | string\|number | - | 1.5.0 |
### 全局方法 ### 全局方法

1
types/message.d.ts vendored
View File

@ -33,6 +33,7 @@ export interface MessageOptions {
* @type Function * @type Function
*/ */
onClose?: () => void; onClose?: () => void;
key?: string | number;
} }
export interface MessageConfigOptions { export interface MessageConfigOptions {