2015-07-13 21:40:04 +08:00
|
|
|
import React from 'react';
|
2015-07-11 16:40:59 +08:00
|
|
|
import Notification from 'rc-notification';
|
2015-11-20 11:05:34 +08:00
|
|
|
import Icon from '../icon';
|
2015-07-11 16:40:59 +08:00
|
|
|
|
2015-07-23 11:59:56 +08:00
|
|
|
let defaultDuration = 1.5;
|
2015-07-30 11:47:46 +08:00
|
|
|
let top;
|
2015-09-01 16:18:46 +08:00
|
|
|
let messageInstance;
|
2015-09-07 12:27:09 +08:00
|
|
|
let key = 1;
|
2015-07-30 21:32:12 +08:00
|
|
|
|
2015-07-23 11:59:56 +08:00
|
|
|
function getMessageInstance() {
|
2015-07-30 21:32:12 +08:00
|
|
|
messageInstance = messageInstance || Notification.newInstance({
|
2015-07-11 16:40:59 +08:00
|
|
|
prefixCls: 'ant-message',
|
|
|
|
transitionName: 'move-up',
|
2015-07-30 11:47:46 +08:00
|
|
|
style: {
|
2016-01-07 17:46:46 +08:00
|
|
|
top,
|
2015-07-30 11:47:46 +08:00
|
|
|
} // 覆盖原来的样式
|
2015-07-11 16:40:59 +08:00
|
|
|
});
|
2015-07-30 21:32:12 +08:00
|
|
|
return messageInstance;
|
2015-07-23 11:59:56 +08:00
|
|
|
}
|
|
|
|
|
2015-09-07 12:27:09 +08:00
|
|
|
function notice(content, duration = defaultDuration, type, onClose) {
|
2015-07-23 11:59:56 +08:00
|
|
|
let iconClass = ({
|
2016-01-07 17:46:46 +08:00
|
|
|
info: 'ant-message-info',
|
|
|
|
success: 'ant-message-success',
|
|
|
|
error: 'ant-message-error',
|
|
|
|
warn: 'ant-message-warn',
|
|
|
|
loading: 'ant-message-loading'
|
2015-07-23 11:59:56 +08:00
|
|
|
})[type];
|
2015-10-02 15:32:16 +08:00
|
|
|
|
|
|
|
let iconType = ({
|
2016-01-07 17:46:46 +08:00
|
|
|
info: 'info-circle',
|
|
|
|
success: 'check-circle',
|
|
|
|
error: 'exclamation-circle',
|
|
|
|
warn: 'exclamation-circle',
|
|
|
|
loading: 'loading'
|
2015-10-02 15:32:16 +08:00
|
|
|
})[type];
|
|
|
|
|
2015-09-07 12:27:09 +08:00
|
|
|
let instance = getMessageInstance();
|
|
|
|
instance.notice({
|
2016-01-07 17:46:46 +08:00
|
|
|
key,
|
|
|
|
duration,
|
2015-07-23 11:59:56 +08:00
|
|
|
style: {},
|
2016-02-17 18:04:42 +08:00
|
|
|
content: <div className={`ant-message-custom-content ${iconClass}`}>
|
2015-10-02 15:32:16 +08:00
|
|
|
<Icon className={iconClass} type={iconType} />
|
2015-07-23 11:59:56 +08:00
|
|
|
<span>{content}</span>
|
2015-09-07 12:27:09 +08:00
|
|
|
</div>,
|
2016-01-07 17:46:46 +08:00
|
|
|
onClose
|
2015-07-23 11:59:56 +08:00
|
|
|
});
|
2016-01-14 16:46:38 +08:00
|
|
|
return (function () {
|
2015-09-07 12:27:09 +08:00
|
|
|
let target = key++;
|
2016-01-14 16:46:38 +08:00
|
|
|
return function () {
|
2015-09-07 12:27:09 +08:00
|
|
|
instance.removeNotice(target);
|
|
|
|
};
|
2016-01-23 15:22:16 +08:00
|
|
|
}());
|
2015-07-23 11:59:56 +08:00
|
|
|
}
|
2015-07-11 16:40:59 +08:00
|
|
|
|
2015-07-11 18:00:58 +08:00
|
|
|
export default {
|
2015-09-07 12:27:09 +08:00
|
|
|
info(content, duration, onClose) {
|
|
|
|
return notice(content, duration, 'info', onClose);
|
2015-07-11 16:40:59 +08:00
|
|
|
},
|
2015-09-07 12:27:09 +08:00
|
|
|
success(content, duration, onClose) {
|
|
|
|
return notice(content, duration, 'success', onClose);
|
2015-07-11 16:40:59 +08:00
|
|
|
},
|
2015-09-07 12:27:09 +08:00
|
|
|
error(content, duration, onClose) {
|
|
|
|
return notice(content, duration, 'error', onClose);
|
|
|
|
},
|
2015-12-10 16:41:41 +08:00
|
|
|
warn(content, duration, onClose) {
|
|
|
|
return notice(content, duration, 'warn', onClose);
|
|
|
|
},
|
2015-09-07 12:27:09 +08:00
|
|
|
loading(content, duration, onClose) {
|
|
|
|
return notice(content, duration, 'loading', onClose);
|
2015-07-30 11:47:46 +08:00
|
|
|
},
|
|
|
|
config(options) {
|
|
|
|
if (options.top) {
|
|
|
|
top = options.top;
|
|
|
|
}
|
2016-01-21 14:13:28 +08:00
|
|
|
},
|
|
|
|
destroy() {
|
|
|
|
if (messageInstance) {
|
|
|
|
messageInstance.destroy();
|
|
|
|
messageInstance = null;
|
|
|
|
}
|
|
|
|
},
|
2015-07-11 16:40:59 +08:00
|
|
|
};
|