Add global duration config for message and notification, close #1143

This commit is contained in:
afc163 2016-03-09 21:55:46 +08:00
parent c79e2dcfe4
commit 2e823f3c5b
4 changed files with 26 additions and 16 deletions

View File

@ -3,7 +3,7 @@ import Notification from 'rc-notification';
import Icon from '../icon';
let defaultDuration = 1.5;
let top;
let defaultTop;
let messageInstance;
let key = 1;
@ -11,9 +11,7 @@ function getMessageInstance() {
messageInstance = messageInstance || Notification.newInstance({
prefixCls: 'ant-message',
transitionName: 'move-up',
style: {
top,
} //
style: { defaultTop }, //
});
return messageInstance;
}
@ -71,8 +69,11 @@ export default {
return notice(content, duration, 'loading', onClose);
},
config(options) {
if (options.top) {
top = options.top;
if ('top' in options) {
defaultTop = options.top;
}
if ('duration' in options) {
defaultDuration = options.duration;
}
},
destroy() {

View File

@ -26,7 +26,7 @@
| 参数 | 说明 | 类型 | 默认值 |
|------------|----------------|----------------------------|--------------|
| content | 提示内容 | React.Element or String | 无 |
| duration | 自动关闭的延时 | number | 1.5 |
| duration | 自动关闭的延时,单位秒 | number | 1.5 |
还提供了全局配置和全局销毁方法:
@ -36,10 +36,12 @@
```js
message.config({
top: 100
top: 100,
duration: 2,
});
```
| 参数 | 说明 | 类型 | 默认值 |
|------------|--------------------|----------------------------|--------------|
| top | 消息距离顶部的位置 | Number | 24px |
| duration | 默认自动关闭延时,单位秒 | Number | 1.5 |

View File

@ -3,8 +3,9 @@ import Notification from 'rc-notification';
import assign from 'object-assign';
import Icon from '../icon';
let top = 24;
let defaultTop = 24;
let notificationInstance;
let defaultDuration = 4.5;
function getNotificationInstance() {
if (notificationInstance) {
@ -13,8 +14,8 @@ function getNotificationInstance() {
notificationInstance = Notification.newInstance({
prefixCls: 'ant-notification',
style: {
top,
right: 0
top: defaultTop,
right: 0,
}
});
return notificationInstance;
@ -23,11 +24,10 @@ function getNotificationInstance() {
function notice(args) {
let duration;
if (args.duration === undefined) {
duration = 4.5;
duration = defaultDuration;
} else {
duration = args.duration;
}
if (args.icon) {
let prefixCls = ' ant-notification-notice-content-icon-';
let iconType = '';
@ -107,7 +107,12 @@ const api = {
}
},
config(options) {
top = isNaN(options.top) ? 24 : options.top;
if ('top' in options) {
defaultTop = options.top;
}
if ('duration' in options) {
defaultDuration = options.duration;
}
},
destroy() {
if (notificationInstance) {

View File

@ -37,16 +37,18 @@ config 参数如下:
| onClose | 点击默认关闭按钮时触发的回调函数 | Function | 无 |
| duration | 默认 4.5 秒后自动关闭,配置为 null 则不自动关闭 | Number | 4.5 |
还提供了一个全局配置方法,需要在调用前提前配置,一次有效。
还提供了一个全局配置方法,在调用前提前配置,全局一次生效。
- `notification.config(options)`
```js
notification.config({
top: 100
top: 100,
duration: 3,
});
```
| 参数 | 说明 | 类型 | 默认值 |
|------------|--------------------|----------------------------|--------------|
| top | 消息距离顶部的位置 | Number | 24px |
| duration | 默认自动关闭延时,单位秒 | Number | 4.5 |