ant-design/components/message/index.jsx

74 lines
1.7 KiB
React
Raw Normal View History

2015-07-13 21:40:04 +08:00
import React from 'react';
import Notification from 'rc-notification';
import Icon from '../iconfont';
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({
prefixCls: 'ant-message',
transitionName: 'move-up',
2015-07-30 11:47:46 +08:00
style: {
top: top
} // 覆盖原来的样式
});
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 = ({
'info': 'ant-message-info',
'success': 'ant-message-success',
'error': 'ant-message-error',
'loading': 'ant-message-loading'
2015-07-23 11:59:56 +08:00
})[type];
let iconType = ({
'info': 'info-circle',
'success': 'check-circle',
'error': 'exclamation-circle',
'loading': 'loading'
})[type];
2015-09-07 12:27:09 +08:00
let instance = getMessageInstance();
instance.notice({
key: key,
2015-07-23 11:59:56 +08:00
duration: duration,
style: {},
content: <div className="ant-message-custom-content">
<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>,
onClose: onClose
2015-07-23 11:59:56 +08:00
});
2015-09-07 12:27:09 +08:00
return (function() {
let target = key++;
return function() {
instance.removeNotice(target);
};
})();
2015-07-23 11:59:56 +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-09-07 12:27:09 +08:00
success(content, duration, onClose) {
return notice(content, duration, 'success', onClose);
},
2015-09-07 12:27:09 +08:00
error(content, duration, onClose) {
return notice(content, duration, 'error', onClose);
},
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;
}
}
};