index.tsx 3.5 KB
Newer Older
1 2
import * as React from 'react';
import * as 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
  /** Whether to show icon */
  showIcon?: boolean;
26
  iconType?: string;
A
afc163 已提交
27 28
  style?: React.CSSProperties;
  prefixCls?: string;
A
afc163 已提交
29
  className?: string;
D
ddcat1115 已提交
30
  banner?: boolean;
A
afc163 已提交
31 32 33
}

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

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

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

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

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

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

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

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

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