index.jsx 1.7 KB
Newer Older
Y
yiminghe 已提交
1
import React from 'react';
翰文 已提交
2
import Notification from 'rc-notification';
A
afc163 已提交
3
import Icon from '../icon';
翰文 已提交
4

A
afc163 已提交
5
let defaultDuration = 1.5;
A
afc163 已提交
6
let top;
A
afc163 已提交
7
let messageInstance;
A
afc163 已提交
8
let key = 1;
Y
yiminghe 已提交
9

A
afc163 已提交
10
function getMessageInstance() {
Y
yiminghe 已提交
11
  messageInstance = messageInstance || Notification.newInstance({
翰文 已提交
12 13
    prefixCls: 'ant-message',
    transitionName: 'move-up',
A
afc163 已提交
14 15 16
    style: {
      top: top
    }  // 覆盖原来的样式
翰文 已提交
17
  });
Y
yiminghe 已提交
18
  return messageInstance;
A
afc163 已提交
19 20
}

A
afc163 已提交
21
function notice(content, duration = defaultDuration, type, onClose) {
A
afc163 已提交
22
  let iconClass = ({
E
elrrrrrrr 已提交
23 24 25 26
    'info': 'ant-message-info',
    'success': 'ant-message-success',
    'error': 'ant-message-error',
    'loading': 'ant-message-loading'
A
afc163 已提交
27
  })[type];
E
elrrrrrrr 已提交
28 29 30 31 32 33 34 35

  let iconType = ({
    'info': 'info-circle',
    'success': 'check-circle',
    'error': 'exclamation-circle',
    'loading': 'loading'
  })[type];

A
afc163 已提交
36 37 38
  let instance = getMessageInstance();
  instance.notice({
    key: key,
A
afc163 已提交
39 40
    duration: duration,
    style: {},
A
afc163 已提交
41
    content: <div className={'ant-message-custom-content ' + iconClass}>
E
elrrrrrrr 已提交
42
      <Icon className={iconClass} type={iconType} />
A
afc163 已提交
43
      <span>{content}</span>
A
afc163 已提交
44 45
    </div>,
    onClose: onClose
A
afc163 已提交
46
  });
A
afc163 已提交
47 48 49 50 51 52
  return (function() {
    let target = key++;
    return function() {
      instance.removeNotice(target);
    };
  })();
A
afc163 已提交
53
}
翰文 已提交
54

A
afc163 已提交
55
export default {
A
afc163 已提交
56 57
  info(content, duration, onClose) {
    return notice(content, duration, 'info', onClose);
翰文 已提交
58
  },
A
afc163 已提交
59 60
  success(content, duration, onClose) {
    return notice(content, duration, 'success', onClose);
翰文 已提交
61
  },
A
afc163 已提交
62 63 64 65 66
  error(content, duration, onClose) {
    return notice(content, duration, 'error', onClose);
  },
  loading(content, duration, onClose) {
    return notice(content, duration, 'loading', onClose);
A
afc163 已提交
67 68 69 70 71
  },
  config(options) {
    if (options.top) {
      top = options.top;
    }
翰文 已提交
72 73
  }
};