File size: 2,488 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// @flow

import * as React from "react";
import cn from "classnames";
import { Icon, Avatar, Button } from "../";
import AlertLink from "./AlertLink.react";

import type { MouseEvents, PointerEvents } from "../../";

type AlertType =
  | "primary"
  | "secondary"
  | "success"
  | "info"
  | "warning"
  | "danger";

type Props = {|
  ...MouseEvents,
  ...PointerEvents,
  +children?: React.Node,
  +className?: string,
  /**
   * The type of this Alert, changes it's color
   */
  +type: AlertType,
  /**
   * An Icon to be displayed on the right hand side of the Alert
   */
  +icon?: string,
  /**
   * Add extra space above and below the alert
   */
  +hasExtraSpace?: boolean,
  /**
   * Adds an 'X' to the left side of the Alert that dismisses the Alert
   */
  +isDismissible?: boolean,
  /**
   * Display an Avatar on the left hand side of this Alert
   */
  +avatar?: string,
  /**
   * Handle the dismissing of the Alert yourself
   */
  +onDismissClick?: () => void,
|};

type State = {
  isDismissed: boolean,
};

class Alert extends React.Component<Props, State> {
  state = {
    isDismissed: false,
  };

  _handleOnDismissClick = (): void => {
    if (this.props.onDismissClick) this.props.onDismissClick();
    this.setState({ isDismissed: true });
  };

  static Link = AlertLink;

  render(): React.Node {
    const { isDismissed } = this.state;
    const {
      className,
      children,
      type,
      icon,
      hasExtraSpace,
      isDismissible,
      avatar,
      onClick,
      onMouseEnter,
      onMouseLeave,
      onPointerEnter,
      onPointerLeave,
    }: Props = this.props;
    const classes = cn(
      "alert",
      `alert-${type}`,
      {
        "alert-icon": !!icon,
        "mt-5 mb-6": hasExtraSpace,
        "alert-dismissible": isDismissible,
        "alert-avatar": !!avatar,
      },
      className
    );

    const events = {
      onClick: onClick,
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave,
      onPointerEnter: onPointerEnter,
      onPointerLeave: onPointerLeave,
    };

    return (
      !isDismissed && (
        <div {...events} className={classes} role="alert">
          {isDismissible && (
            <Button className="close" onClick={this._handleOnDismissClick} />
          )}
          {avatar && <Avatar imageURL={avatar} />}
          {icon && <Icon name={icon} className="mr-2" isAriaHidden={true} />}
          {children}
        </div>
      )
    );
  }
}

export default Alert;