UNPKG

antd

Version:

An enterprise-class UI design language and React-based implementation

97 lines (95 loc) 3.05 kB
import React from 'react'; import Notification from 'rc-notification'; import Icon from '../icon'; var defaultDuration = 3; var defaultTop = void 0; var messageInstance = void 0; var key = 1; var prefixCls = 'ant-message'; var getContainer = void 0; function getMessageInstance() { messageInstance = messageInstance || Notification.newInstance({ prefixCls: prefixCls, transitionName: 'move-up', style: { top: defaultTop }, getContainer: getContainer }); return messageInstance; } function notice(content) { var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultDuration; var type = arguments[2]; var onClose = arguments[3]; var iconType = { info: 'info-circle', success: 'check-circle', error: 'cross-circle', warning: 'exclamation-circle', loading: 'loading' }[type]; var instance = getMessageInstance(); instance.notice({ key: key, duration: duration, style: {}, content: React.createElement( 'div', { className: prefixCls + '-custom-content ' + prefixCls + '-' + type }, React.createElement(Icon, { type: iconType }), React.createElement( 'span', null, content ) ), onClose: onClose }); return function () { var target = key++; return function () { instance.removeNotice(target); }; }(); } export default { info: function info(content, duration, onClose) { return notice(content, duration, 'info', onClose); }, success: function success(content, duration, onClose) { return notice(content, duration, 'success', onClose); }, error: function error(content, duration, onClose) { return notice(content, duration, 'error', onClose); }, // Departed usage, please use warning() warn: function warn(content, duration, onClose) { return notice(content, duration, 'warning', onClose); }, warning: function warning(content, duration, onClose) { return notice(content, duration, 'warning', onClose); }, loading: function loading(content, duration, onClose) { return notice(content, duration, 'loading', onClose); }, config: function config(options) { if (options.top !== undefined) { defaultTop = options.top; messageInstance = null; // delete messageInstance for new defaultTop } if (options.duration !== undefined) { defaultDuration = options.duration; } if (options.prefixCls !== undefined) { prefixCls = options.prefixCls; } if (options.getContainer !== undefined) { getContainer = options.getContainer; } }, destroy: function destroy() { if (messageInstance) { messageInstance.destroy(); messageInstance = null; } } };