# All files / lib/modal index.tsx

100% Statements 24/24
100% Branches 8/8
100% Functions 3/3
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    891x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    8x
    8x
    8x
    8x
    8x
    8x

    8x

    2x

    8x
    1x

    8x
    1x

    8x

    8x

    1x

    1x

     import * as React from "react";
    import * as ReactDOM from 'react-dom';
    import {Fragment} from "react";
    import sc from 'utils/classname'
    import Message from "../button";
    import './index.scss';
    import Icon from "../icon/icon";

    interface IProps {
      visible: boolean,
      title?: string,
      width?: string,
      className?: string,
      onOk?: React.MouseEventHandler,
      onCancel?: React.MouseEventHandler,
    }

    export const Modal: React.FunctionComponent<IProps> = (props) => {
      const {
        visible,
        className,
        title,
        children,
        onCancel,
        onOk
      } = props;

      let width = '560px';  // default modal width
      if (props.width) {
        width = Number.isInteger(+props.width) ? props.width + 'px' : props.width;
      }

      const onClickOk: React.MouseEventHandler = (e) => {
        onOk && onOk(e);
      };

      const onClickCancel: React.MouseEventHandler = (e) => {
        onCancel && onCancel(e);
      };

      const renderModal = visible ?
        <Fragment>
          <div className="wen-modal-mask"/>
          <div className={sc('wen-modal', className)}
               style={{width: width}}>
            <Icon
              className="wen-modal-close-icon"
              name="close"
              onClick={onCancel}
            />
            <div className="wen-modal-head">
              {title}
            </div>
            <div className="wen-modal-body">
              {children}
            </div>
            <div className="wen-modal-footer">
              <Message
                className="wen-modal-ok-button"
                type="primary"
                onClick={onClickOk}
              >
                确认
              </Message>
              <Message
                className="wen-modal-cancel-button"
                type="danger"
                onClick={onClickCancel}
              >
                取消
              </Message>
            </div>
          </div>
        </Fragment> :
        null;

      return ReactDOM.createPortal(renderModal, document.body);
    };

    Modal.defaultProps = {
      title: 'Modal title',
      visible: false
    };

    export default Modal;