index.tsx 3.4 KB
Newer Older
B
Benjy Cui 已提交
1 2
import React from 'react';
import ReactDOM from 'react-dom';
J
jljsj 已提交
3
import Animate from 'rc-animate';
A
afc163 已提交
4
import Icon from '../icon';
A
afc163 已提交
5
import classNames from 'classnames';
Z
zhujun24 已提交
6

7
function noop() { }
B
Benjy Cui 已提交
8

B
Benjy Cui 已提交
9
export interface AlertProps {
A
afc163 已提交
10 11 12
  /**
   * Type of Alert styles, options:`success`, `info`, `warning`, `error`
   */
D
ddcat1115 已提交
13
  type?: 'success' | 'info' | 'warning' | 'error';
A
afc163 已提交
14 15 16 17 18 19 20 21 22
  /** Whether Alert can be closed */
  closable?: boolean;
  /** Close text to show */
  closeText?: React.ReactNode;
  /** Content of Alert */
  message: React.ReactNode;
  /** Additional content of Alert */
  description?: React.ReactNode;
  /** Callback when close Alert */
23
  onClose?: React.MouseEventHandler<HTMLAnchorElement>;
A
afc163 已提交
24 25 26 27
  /** Whether to show icon */
  showIcon?: boolean;
  style?: React.CSSProperties;
  prefixCls?: string;
A
afc163 已提交
28
  className?: string;
D
ddcat1115 已提交
29
  banner?: boolean;
A
afc163 已提交
30 31 32
}

export default class Alert extends React.Component<AlertProps, any> {
33
  constructor(props: AlertProps) {
34 35
    super(props);
    this.state = {
J
jljsj 已提交
36
      closing: true,
B
Benjy Cui 已提交
37
      closed: false,
A
afc163 已提交
38
    };
39
  }
40
  handleClose = (e: React.MouseEvent<HTMLAnchorElement>) => {
41
    e.preventDefault();
A
afc163 已提交
42
    let dom = ReactDOM.findDOMNode(this) as HTMLElement;
43
    dom.style.height = `${dom.offsetHeight}px`;
J
jljsj 已提交
44
    // Magic code
A
afc163 已提交
45
    // 重复一次后才能正确设置 height
46
    dom.style.height = `${dom.offsetHeight}px`;
J
jljsj 已提交
47 48

    this.setState({
B
Benjy Cui 已提交
49
      closing: false,
J
jljsj 已提交
50
    });
B
Benjy Cui 已提交
51
    (this.props.onClose || noop)(e);
52 53
  }
  animationEnd = () => {
Z
zhujun24 已提交
54
    this.setState({
J
jljsj 已提交
55
      closed: true,
B
Benjy Cui 已提交
56
      closing: true,
Z
zhujun24 已提交
57
    });
58
  }
J
jljsj 已提交
59
  render() {
A
afc163 已提交
60
    let {
B
Benjy Cui 已提交
61
      closable, description, type, prefixCls = 'ant-alert', message, closeText, showIcon, banner,
A
afc163 已提交
62
      className = '', style,
A
afc163 已提交
63 64
    } = this.props;

D
ddcat1115 已提交
65
    // banner模式默认有 Icon
66
    showIcon = banner && showIcon === undefined ? true : showIcon;
D
ddcat1115 已提交
67
    // banner模式默认为警告
68
    type = banner && type === undefined ? 'warning' : type || 'info';
D
ddcat1115 已提交
69

E
elrrrrrrr 已提交
70
    let iconType = '';
A
afc163 已提交
71
    switch (type) {
A
afc163 已提交
72 73 74 75 76 77 78
      case 'success':
        iconType = 'check-circle';
        break;
      case 'info':
        iconType = 'info-circle';
        break;
      case 'error':
A
afc163 已提交
79
        iconType = 'cross-circle';
A
afc163 已提交
80
        break;
A
afc163 已提交
81
      case 'warning':
A
afc163 已提交
82 83 84 85
        iconType = 'exclamation-circle';
        break;
      default:
        iconType = 'default';
Z
zhujun24 已提交
86
    }
A
afc163 已提交
87

88 89 90 91 92
    // use outline icon in alert with description
    if (!!description) {
      iconType += '-o';
    }

93
    let alertCls = classNames(prefixCls, {
94 95 96 97
      [`${prefixCls}-${type}`]: true,
      [`${prefixCls}-close`]: !this.state.closing,
      [`${prefixCls}-with-description`]: !!description,
      [`${prefixCls}-no-icon`]: !showIcon,
D
ddcat1115 已提交
98
      [`${prefixCls}-banner`]: !!banner,
99
    }, className);
A
afc163 已提交
100

101
    // closeable when closeText is assigned
A
afc163 已提交
102 103
    if (closeText) {
      closable = true;
Z
zhujun24 已提交
104
    }
A
afc163 已提交
105

W
Wei Zhu 已提交
106 107 108 109 110 111
    const closeIcon = closable ? (
      <a onClick={this.handleClose} className={`${prefixCls}-close-icon`}>
        {closeText || <Icon type="cross" />}
      </a>
    ) : null;

A
afc163 已提交
112
    return this.state.closed ? null : (
A
afc163 已提交
113 114
      <Animate
        component=""
A
afc163 已提交
115
        showProp="data-show"
J
jiang 已提交
116
        transitionName={`${prefixCls}-slide-up`}
117 118
        onEnd={this.animationEnd}
      >
A
afc163 已提交
119
        <div data-show={this.state.closing} className={alertCls} style={style}>
120
          {showIcon ? <Icon className={`${prefixCls}-icon`} type={iconType} /> : null}
121 122
          <span className={`${prefixCls}-message`}>{message}</span>
          <span className={`${prefixCls}-description`}>{description}</span>
W
Wei Zhu 已提交
123
          {closeIcon}
A
afc163 已提交
124 125 126
        </div>
      </Animate>
    );
Z
zhujun24 已提交
127
  }
128
}