confirm.jsx 1.9 KB
Newer Older
Y
yiminghe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var React = require('react');
var Dialog = require('rc-dialog');
var div;

module.exports = function (props) {
  var d;
  props = props || {};
  props.iconClassName = props.iconClassName || 'anticon-exclamation-circle';
  var width = props.width || 375;

  function close() {
    d.setState({
      visible: false
    });
  }

  function onCancel() {
Y
yiminghe 已提交
18 19
    var cancelFn = props.onCancel;
    if (cancelFn) {
Y
yiminghe 已提交
20
      var ret;
Y
yiminghe 已提交
21
      if (cancelFn.length) {
Y
yiminghe 已提交
22
        ret = cancelFn(close);
Y
yiminghe 已提交
23
      } else {
Y
yiminghe 已提交
24 25 26 27 28 29 30
        ret = cancelFn();
        if (!ret) {
          close();
        }
      }
      if (ret && ret.then) {
        ret.then(close);
Y
yiminghe 已提交
31
      }
Y
yiminghe 已提交
32 33 34 35 36 37
    } else {
      close();
    }
  }

  function onOk() {
Y
yiminghe 已提交
38 39
    var okFn = props.onOk;
    if (okFn) {
Y
yiminghe 已提交
40
      var ret;
Y
yiminghe 已提交
41
      if (okFn.length) {
Y
yiminghe 已提交
42
        ret = okFn(close);
Y
yiminghe 已提交
43
      } else {
Y
yiminghe 已提交
44 45 46 47 48 49 50
        ret = okFn();
        if (!ret) {
          close();
        }
      }
      if (ret && ret.then) {
        ret.then(close);
Y
yiminghe 已提交
51
      }
Y
yiminghe 已提交
52 53 54 55 56 57
    } else {
      close();
    }
  }

  var body = <div className="ant-confirm-body">
Y
yiminghe 已提交
58
    <i className={'anticon ' + props.iconClassName}></i>
Y
yiminghe 已提交
59 60
    <span className="ant-confirm-title">{props.title}</span>
    <div className="ant-confirm-content">{props.content}</div>
Y
yiminghe 已提交
61
  </div>;
Y
yiminghe 已提交
62 63 64 65 66 67 68 69 70 71
  var footer = <div className="ant-confirm-btns">
    <button type="button" className="ant-btn-default ant-btn ant-btn-lg" onClick={onCancel}>取 消</button>
    <button type="button" className="ant-btn-primary ant-btn ant-btn-lg" onClick={onOk}>确 定</button>
  </div>;

  if (!div) {
    div = document.createElement('div');
    document.body.appendChild(div);
  }

Y
yiminghe 已提交
72 73 74 75 76 77 78 79 80
  React.render(<Dialog
    prefixCls="ant-modal"
    className="ant-confirm"
    renderToBody={false}
    visible={true}
    closable={false}
    title=""
    transitionName="zoom"
    maskTransitionName="fade" width={width}>
Y
yiminghe 已提交
81
    <div style={{zoom: 1, overflow: 'hidden'}}>{body} {footer}</div>
Y
yiminghe 已提交
82 83 84 85
  </Dialog>, div, function () {
    d = this;
  });
};