Skip to content

Commit 078a3e4

Browse files
committed
feat: meaage add key for update content
1 parent b85bc0e commit 078a3e4

File tree

12 files changed

+96
-39
lines changed

12 files changed

+96
-39
lines changed

build/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
dev: {
3-
componentName: 'menu', // dev components
3+
componentName: 'message', // dev components
44
},
55
};

components/message/__tests__/__snapshots__/demo.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ exports[`renders ./components/message/demo/loading.md correctly 1`] = `<button t
99
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>`;
1010

1111
exports[`renders ./components/message/demo/thenable.md correctly 1`] = `<button type="button" class="ant-btn"><span>Display a sequence of message</span></button>`;
12+
13+
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>`;

components/message/__tests__/index.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ describe('message', () => {
9797
});
9898
});
9999

100-
it('should be called like promise', () => {
100+
it('should be called like promise', done => {
101101
const defaultDuration = 3;
102102
const now = Date.now();
103103
message.info('whatever').then(() => {
104104
// calculate the approximately duration value
105105
const aboutDuration = parseInt((Date.now() - now) / 1000, 10);
106106
expect(aboutDuration).toBe(defaultDuration);
107+
done();
107108
});
108109
});
109110

components/message/demo/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Info from './info';
44
import Loading from './loading';
55
import Other from './other';
66
import Thenable from './thenable';
7+
import Update from './update';
78
import CN from '../index.zh-CN.md';
89
import US from '../index.en-US.md';
910
const md = {
@@ -36,6 +37,7 @@ export default {
3637
<Loading />
3738
<Other />
3839
<Thenable />
40+
<Update />
3941
<api>
4042
<CN slot="cn" />
4143
<US />

components/message/demo/info.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<us>
77
#### Normal prompt
8-
Normal messages as feedbacks.
8+
Normal message for information.
99
</us>
1010

1111
```tpl

components/message/demo/loading.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</cn>
55

66
<us>
7-
#### Message of loading
7+
#### Message with loading indicator
88
Display a global loading indicator, which is dismissed by itself asynchronously.
99
</us>
1010

components/message/demo/other.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ Messages of success, error and warning types.
2020
export default {
2121
methods: {
2222
success() {
23-
this.$message.success('This is a message of success');
23+
this.$message.success('This is a success message');
2424
},
2525
error() {
26-
this.$message.error('This is a message of error');
26+
this.$message.error('This is an error message');
2727
},
2828
warning() {
29-
this.$message.warning('This is message of warning');
29+
this.$message.warning('This is a warning message');
3030
},
3131
},
3232
};

components/message/demo/update.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<cn>
2+
#### 更新消息内容
3+
可以通过唯一的 `key` 来更新内容。
4+
</cn>
5+
6+
<us>
7+
#### Update Message Content
8+
Update message content with unique `key`.
9+
</us>
10+
11+
```tpl
12+
<template>
13+
<a-button type="primary" @click="openMessage">Open the message box</a-button>
14+
</template>
15+
<script>
16+
const key = 'updatable';
17+
export default {
18+
methods: {
19+
openMessage() {
20+
this.$message.loading({ content: 'Loading...', key });
21+
setTimeout(() => {
22+
this.$message.success({ content: 'Loaded!', key, duration: 2 });
23+
}, 1000);
24+
},
25+
},
26+
};
27+
</script>
28+
```

components/message/index.en-US.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,22 @@ This components provides some static methods, with usage and arguments as follow
2323
where `level` refers one static methods of `message`. The result of `then` method will be a Promise.
2424

2525
- `message.open(config)`
26+
- `message.success(config)`
27+
- `message.error(config)`
28+
- `message.info(config)`
29+
- `message.warning(config)`
30+
- `message.warn(config)` // alias of warning
31+
- `message.loading(config)`
2632

2733
The properties of config are as follows:
2834

29-
| Property | Description | Type | Default |
30-
| --- | --- | --- | --- |
31-
| content | content of the message | string\| VNode \|(h) => VNode | - |
32-
| duration | time(seconds) before auto-dismiss, don't dismiss if set to 0 | number | 3 |
33-
| onClose | Specify a function that will be called when the message is closed | function | - |
34-
| icon | Customized Icon | string\| VNode \|(h) => VNode | - |
35+
| Property | Description | Type | Default | Version |
36+
| --- | --- | --- | --- | --- |
37+
| content | content of the message | string\| VNode \|(h) => VNode | - | |
38+
| duration | time(seconds) before auto-dismiss, don't dismiss if set to 0 | number | 3 | |
39+
| onClose | Specify a function that will be called when the message is closed | function | - | |
40+
| icon | Customized Icon | string\| VNode \|(h) => VNode | - | |
41+
| key | The unique identifier of the Message | string\|number | - | 1.5.0 |
3542

3643
### Global static methods
3744

components/message/index.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function notice(args) {
4646
loading: 'loading',
4747
}[args.type];
4848

49-
const target = key++;
49+
const target = args.key || key++;
5050
const closePromise = new Promise(resolve => {
5151
const callback = () => {
5252
if (typeof args.onClose === 'function') {
@@ -59,24 +59,24 @@ function notice(args) {
5959
key: target,
6060
duration,
6161
style: {},
62-
content: h => (
63-
<div
64-
class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`}
65-
>
66-
{args.icon ? (
67-
typeof args.icon === 'function' ? (
68-
args.icon(h)
69-
) : (
70-
args.icon
71-
)
72-
) : iconType ? (
73-
<Icon type={iconType} theme={iconType === 'loading' ? 'outlined' : 'filled'} />
74-
) : (
75-
''
76-
)}
77-
<span>{typeof args.content === 'function' ? args.content(h) : args.content}</span>
78-
</div>
79-
),
62+
content: h => {
63+
const iconNode = (
64+
<Icon type={iconType} theme={iconType === 'loading' ? 'outlined' : 'filled'} />
65+
);
66+
const switchIconNode = iconType ? iconNode : '';
67+
return (
68+
<div
69+
class={`${prefixCls}-custom-content${args.type ? ` ${prefixCls}-${args.type}` : ''}`}
70+
>
71+
{args.icon
72+
? typeof args.icon === 'function'
73+
? args.icon(h)
74+
: args.icon
75+
: switchIconNode}
76+
<span>{typeof args.content === 'function' ? args.content(h) : args.content}</span>
77+
</div>
78+
);
79+
},
8080
onClose: callback,
8181
});
8282
});
@@ -95,6 +95,10 @@ function notice(args) {
9595
// type ConfigDuration = number | (() => void);
9696
// export type ConfigOnClose = () => void;
9797

98+
function isArgsProps(content) {
99+
return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
100+
}
101+
98102
// export interface ConfigOptions {
99103
// top?: number;
100104
// duration?: number;
@@ -138,11 +142,14 @@ const api = {
138142

139143
['success', 'info', 'warning', 'error', 'loading'].forEach(type => {
140144
api[type] = (content, duration, onClose) => {
145+
if (isArgsProps(content)) {
146+
return api.open({ ...content, type });
147+
}
141148
if (typeof duration === 'function') {
142149
onClose = duration;
143150
duration = undefined;
144151
}
145-
return api.open({ content, duration: duration, type, onClose });
152+
return api.open({ content, duration, type, onClose });
146153
};
147154
});
148155

0 commit comments

Comments
 (0)