# All files / components/Alert Alert.js

0% Statements 0/84
0% Branches 0/26
0% Functions 0/10
0% Lines 0/32

    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
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179

     import React from 'react';
    import PropTypes from 'prop-types';
    import { withProps } from 'recompose';
    import AlertGroup from './styles/AlertGroup';
    import { UnmountClosed } from 'react-collapse';

    import DefaultCloseButton from './styles/AlertCloseButton';
    import AlertIcon from './styles/AlertIcon';
    import AlertBorder from './styles/AlertBorder';
    import AlertText from './styles/AlertText';
    import AlertContent from './styles/AlertContent';
    import icons from 'components/Icon/icons';

    const getIcon = type => {
      switch (type) {
        case 'error':
          return '!';
        case 'success':
          return icons('checkmark');
        default:
          return 'i';
      }
    };

    /**
     * Alerts are used to present messages to the user. Every alert is an interruption, so
     * you should use alerts only when they provide real value!
     */
    export default class Alert extends React.PureComponent {
      static propTypes = {
        /**
         * An alert type
         */
        type: PropTypes.oneOf(['info', 'success', 'error']),
        /**
         * Specify a CSS value or an object { top, right, bottom, left } or { vertical, horizontal } to
         * control the spacing around the heading. Defaults to a large space below the element.
         */
        spacing: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        /**
         * Whether to render only text (true) or an icon alongside text (false).
         */
        textOnly: PropTypes.bool,
        /**
         * A callback for when this alert is closed (dismissed). Providing this prop will
         * automatically create a close button for the user.
         */
        onClose: PropTypes.func,
        /**
         * A timeout for closing the alert (ms). Only works if `onClose` is also provided. Will
         * call `onClose` after time specified.
         */
        closeTimeout: PropTypes.number,
        /**
         * Additional class name to pass to the alert.
         */
        className: PropTypes.string,
        /**
         * Additional id to pass to the alert.
         */
        id: PropTypes.string,
        /**
         * A component to render the border container
         */
        Border: PropTypes.func,
        /**
         * A component to render the text inside
         */
        Text: PropTypes.func,
        /**
         * A component to render the icon
         */
        Icon: PropTypes.func,
        /**
         * A containing element around the alert, used for expanding style calc.
         */
        Content: PropTypes.func,
        /**
         * The anchor component to use when rendering a close button
         */
        CloseButton: PropTypes.func,
      };

      static defaultProps = {
        type: 'info',
        textOnly: false,
        className: null,
        id: null,
        spacing: 0,
        Border: AlertBorder,
        Content: AlertContent,
        Text: AlertText,
        Icon: AlertIcon,
        CloseButton: DefaultCloseButton,
      };

      static Small = withProps({
        Border: AlertBorder.Small,
        Icon: AlertIcon.Small,
        Content: AlertContent.Small,
      })(Alert);

      static Group = AlertGroup;

      closeTimeoutRef = null;
      closingTimeoutRef = null;

      state = {
        closing: false,
      };

      componentDidMount() {
        const { closeTimeout, onClose } = this.props;
        if (closeTimeout && onClose) {
          this.closeTimeoutRef = setTimeout(this.startClose, closeTimeout);
        }
      }

      componentWillUnmount() {
        if (this.closeTimeoutRef) {
          clearTimeout(this.closeTimeoutRef);
        }
      }

      getCloseButtonType = () => {
        const { CloseButton, type } = this.props;
        switch (type) {
          case 'error':
            return CloseButton.Negative || CloseButton;
          case 'success':
            return CloseButton.Positive || CloseButton;
          default:
            return CloseButton;
        }
      };

      startClose = () => {
        this.setState({ closing: true });
        this.closingTimeoutRef = setTimeout(this.completeClose, 200);
      };

      completeClose = () => {
        this.props.onClose();
      };

      render() {
        const {
          Border,
          Text,
          Icon,
          Content,
          type,
          textOnly,
          children,
          onClose,
          ...rest,
        } = this.props;
        const { closing } = this.state;

        const CloseButton = this.getCloseButtonType();

        return (
          <Border type={type} closing={closing} {...rest}>
            <UnmountClosed
              isOpened={!closing}
              forceInitialAnimation
              springConfig={{ stiffness: 300, damping: 20 }}
            >
              <Content>
                {!textOnly && <Icon type={type} />}
                <Text>{children}</Text>
                {onClose && <CloseButton icon="delete" onClick={this.startClose} />}
              </Content>
            </UnmountClosed>
          </Border>
        );
      }
    }