# All files / components/Modal Modal.js

0% Statements 0/71
0% Branches 0/14
0% Functions 0/9
0% Lines 0/24

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import ModalActionContent from './styles/ModalActionContent';
    import ModalBlocker from './styles/ModalBlocker';
    import ModalContent from './styles/ModalContent';
    import ModalTitle from './styles/ModalTitle';
    import ModalWindow from './styles/ModalWindow';
    import ModalCloseIcon from './styles/ModalCloseIcon';
    import ButtonContainer from './styles/ButtonContainer';

    class Modal extends React.Component {
      static propTypes = {
        /**
         * Content to render in the modal.
         */
        children: PropTypes.node.isRequired,
        /**
         * An optional title for the top of the modal.
         */
        title: PropTypes.string,
        /**
         * Handles click events on the backdrop behind the modal or on the X in the title bar.
         */
        onClose: PropTypes.func,
        /**
         * @deprecated
         * Handles click events on the backdrop, deprecated in favor of onClose
         */
        onBlockerClicked: PropTypes.func,
        /**
         * Adds a class name to the modal window element.
         */
        className: PropTypes.string,
        /**
         * Adds an id to the modal window element.
         */
        id: PropTypes.string,
        /**
         * Content to render in a fixed position at the bottom of the modal - meant for action buttons, like close.
         */
        actionContent: PropTypes.node,
        /**
         * A component to render the action content area
         */
        ActionContent: PropTypes.func,
        /**
         * A component to render the full-screen blocker behind the modal
         */
        Blocker: PropTypes.func,
        /**
         * A component to render the content inside the modal window
         */
        Content: PropTypes.func,
        /**
         * A component to render the title at the top of the modal
         */
        Title: PropTypes.func,
        /**
         * A component to render the window container of the modal
         */
        Window: PropTypes.func,
        /**
         * A component to render the close icon in the right-top corner of the modal
         */
        CloseIcon: PropTypes.func,
        /**
         * Align action buttons
         */
        alignButtons: PropTypes.oneOf(['right', 'left', 'center']),
        /**
         * Space between buttons
         */
        spaceBetweenButtons: PropTypes.oneOf([
          'xs',
          'extraSmall',
          'sm',
          'small',
          'md',
          'medium',
          'lg',
          'large',
          'xl',
          'extraLarge',
        ]),
      };

      static defaultProps = {
        title: null,
        onBlockerClicked: () => null,
        className: null,
        id: null,
        actionContent: null,
        ActionContent: ModalActionContent,
        Blocker: ModalBlocker,
        Content: ModalContent,
        Title: ModalTitle,
        Window: ModalWindow,
        CloseIcon: ModalCloseIcon,
        alignButtons: 'right',
        spaceBetweenButtons: 'sm',
      };

      handleModalClicked = event => {
        // prevents click event bubbling to blocker and triggering blockerclicked callback
        event.stopPropagation();
      };

      renderTitle = () => {
        const { title, Title } = this.props;
        if (!title) return null;
        return <Title>{title}</Title>;
      };

      renderCloseIcon = () => {
        const { CloseIcon, onClose } = this.props;
        if (!onClose) return null;
        return <CloseIcon onClick={onClose} />;
      };

      renderActions = () => {
        const {
          actionContent,
          ActionContent,
          alignButtons,
          spaceBetweenButtons,
        } = this.props;

        return (
          actionContent && (
            <ActionContent>
              <ButtonContainer align={alignButtons} spacing={spaceBetweenButtons}>
                {actionContent}
              </ButtonContainer>
            </ActionContent>
          )
        );
      };

      render() {
        const {
          id,
          className,
          onClose,
          onBlockerClicked,
          children,
          Blocker,
          Content,
          Window,
        } = this.props;

        return (
          <Blocker onClick={onClose || onBlockerClicked}>
            <Window onClick={this.handleModalClicked} id={id} className={className}>
              {this.renderTitle()}
              {this.renderCloseIcon()}
              <Content>{children}</Content>
              {this.renderActions()}
            </Window>
          </Blocker>
        );
      }
    }

    export default Modal;