ant-design/components/notification/index.jsx

135 lines
3.0 KiB
React
Raw Normal View History

2015-09-01 16:18:46 +08:00
import React from 'react';
2015-07-28 15:40:28 +08:00
import Notification from 'rc-notification';
2015-11-20 11:05:34 +08:00
import Icon from '../icon';
2015-07-28 15:40:28 +08:00
let defaultTop = 24;
2015-08-06 15:19:45 +08:00
let notificationInstance;
let defaultDuration = 4.5;
2015-08-03 18:00:22 +08:00
function getNotificationInstance() {
2015-08-18 00:57:02 +08:00
if (notificationInstance) {
return notificationInstance;
}
notificationInstance = Notification.newInstance({
2015-08-15 15:08:55 +08:00
prefixCls: 'ant-notification',
style: {
top: defaultTop,
right: 0,
2015-08-15 15:08:55 +08:00
}
});
2015-08-03 18:00:22 +08:00
return notificationInstance;
2015-07-31 18:13:32 +08:00
}
2015-07-28 15:40:28 +08:00
2015-07-31 18:13:32 +08:00
function notice(args) {
2015-08-06 15:32:18 +08:00
let duration;
if (args.duration === undefined) {
duration = defaultDuration;
2015-08-06 15:32:18 +08:00
} else {
duration = args.duration;
}
2015-07-28 15:40:28 +08:00
if (args.icon) {
2015-08-03 15:17:12 +08:00
let prefixCls = ' ant-notification-notice-content-icon-';
2015-10-02 15:48:56 +08:00
let iconType = '';
2015-08-03 15:17:12 +08:00
switch (args.icon) {
2016-01-21 22:45:21 +08:00
case 'success':
iconType = 'check-circle-o';
break;
case 'info':
iconType = 'info-circle-o';
break;
case 'error':
2016-03-28 16:09:13 +08:00
iconType = 'cross-circle-o';
2016-01-21 22:45:21 +08:00
break;
2016-03-28 14:38:25 +08:00
case 'warning':
2016-03-28 16:09:13 +08:00
iconType = 'exclamation-circle-o';
2016-01-21 22:45:21 +08:00
break;
default:
iconType = 'info-circle';
2015-08-03 15:17:12 +08:00
}
2015-08-03 18:00:22 +08:00
getNotificationInstance().notice({
2015-07-28 15:40:28 +08:00
content: <div>
<Icon className={`${prefixCls}icon-${args.icon}${prefixCls}icon`} type={iconType} />
2015-08-06 20:55:30 +08:00
<div className={`${prefixCls}message`}>{args.message}</div>
2015-08-06 20:55:30 +08:00
<div className={`${prefixCls}description`}>{args.description}</div>
2015-07-28 15:40:28 +08:00
</div>,
duration,
2015-07-28 15:40:28 +08:00
closable: true,
2015-08-03 19:23:25 +08:00
onClose: args.onClose,
key: args.key,
2015-07-28 15:40:28 +08:00
style: {}
});
} else {
2015-08-03 15:17:12 +08:00
let prefixCls = 'ant-notification-notice-content-';
2015-07-28 15:40:28 +08:00
if (!args.btn) {
2015-08-03 18:00:22 +08:00
getNotificationInstance().notice({
2015-07-28 15:40:28 +08:00
content: <div>
<div className={`${prefixCls}message`}>{args.message}</div>
2015-08-06 20:55:30 +08:00
<div className={`${prefixCls}description`}>{args.description}</div>
2015-08-03 15:17:12 +08:00
</div>,
duration,
2015-07-28 15:40:28 +08:00
closable: true,
2015-08-03 19:23:25 +08:00
onClose: args.onClose,
key: args.key,
2015-07-28 15:40:28 +08:00
style: {}
});
} else {
2015-08-03 18:00:22 +08:00
getNotificationInstance().notice({
2015-07-28 15:40:28 +08:00
content: <div>
<div className={`${prefixCls}message`}>{args.message}</div>
2015-08-06 20:55:30 +08:00
<div className={`${prefixCls}description`}>{args.description}</div>
<span className={`${prefixCls}btn`}>
2015-07-29 20:08:16 +08:00
{args.btn}
</span>
2015-07-28 15:40:28 +08:00
</div>,
duration,
2015-07-28 15:40:28 +08:00
closable: true,
2015-07-30 21:04:52 +08:00
onClose: args.onClose,
2015-08-18 00:57:02 +08:00
key: args.key,
2015-07-28 15:40:28 +08:00
style: {}
});
}
}
2015-07-31 18:13:32 +08:00
}
2015-07-28 15:40:28 +08:00
const api = {
open(args) {
2015-07-31 18:13:32 +08:00
notice(args);
},
close(key) {
2015-08-04 15:16:10 +08:00
if (notificationInstance) {
notificationInstance.removeNotice(key);
}
2015-08-03 18:00:22 +08:00
},
config(options) {
if ('top' in options) {
defaultTop = options.top;
}
if ('duration' in options) {
defaultDuration = options.duration;
}
},
destroy() {
if (notificationInstance) {
notificationInstance.destroy();
notificationInstance = null;
}
},
2015-07-31 18:13:32 +08:00
};
2015-08-04 15:16:10 +08:00
2016-03-28 14:38:25 +08:00
['success', 'info', 'warning', 'error'].forEach((type) => {
2015-08-04 15:16:10 +08:00
api[type] = (args) => {
2016-03-25 18:16:48 +08:00
let newArgs = {
...args,
2015-08-04 15:16:10 +08:00
icon: type
2016-03-25 18:16:48 +08:00
};
2015-08-04 15:16:10 +08:00
return api.open(newArgs);
};
});
export default api;