You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.0 KiB

10 years ago
import React from 'react';
import Notification from 'rc-notification';
9 years ago
import Icon from '../icon';
let defaultDuration = 1.5;
let defaultTop;
let messageInstance;
let key = 1;
9 years ago
function getMessageInstance() {
9 years ago
messageInstance = messageInstance || Notification.newInstance({
prefixCls: 'ant-message',
transitionName: 'move-up',
style: { defaultTop }, // 覆盖原来的样式
});
9 years ago
return messageInstance;
}
function notice(content, duration = defaultDuration, type, onClose) {
let iconClass = ({
info: 'ant-message-info',
success: 'ant-message-success',
error: 'ant-message-error',
warn: 'ant-message-warn',
loading: 'ant-message-loading'
})[type];
let iconType = ({
info: 'info-circle',
success: 'check-circle',
error: 'cross-circle',
warn: 'exclamation-circle',
loading: 'loading'
})[type];
let instance = getMessageInstance();
instance.notice({
key,
duration,
style: {},
content: <div className={`ant-message-custom-content ${iconClass}`}>
<Icon className={iconClass} type={iconType} />
<span>{content}</span>
</div>,
onClose
});
return (function () {
let target = key++;
return function () {
instance.removeNotice(target);
};
}());
}
export default {
info(content, duration, onClose) {
return notice(content, duration, 'info', onClose);
},
success(content, duration, onClose) {
return notice(content, duration, 'success', onClose);
},
error(content, duration, onClose) {
return notice(content, duration, 'error', onClose);
},
warn(content, duration, onClose) {
return notice(content, duration, 'warn', onClose);
},
loading(content, duration, onClose) {
return notice(content, duration, 'loading', onClose);
},
config(options) {
if ('top' in options) {
defaultTop = options.top;
}
if ('duration' in options) {
defaultDuration = options.duration;
}
},
destroy() {
if (messageInstance) {
messageInstance.destroy();
messageInstance = null;
}
},
};