confirm.jsx 2.5 KB
Newer Older
U
ustccjw 已提交
1
import React from 'react';
Y
beta2  
yiminghe 已提交
2
import ReactDOM from 'react-dom';
3
import Dialog from './index';
A
afc163 已提交
4
import Icon from '../icon';
Y
yiminghe 已提交
5
import Button from '../button';
Y
yiminghe 已提交
6

7
export default function (props = {}) {
A
afc163 已提交
8 9 10
  let div = document.createElement('div');
  document.body.appendChild(div);

A
afc163 已提交
11
  let d;
E
elrrrrrrr 已提交
12
  props.iconClassName = props.iconClassName || 'question-circle';
13

E
elrrrrrrr 已提交
14 15
  let iconClassType = props.iconClassName;

A
afc163 已提交
16 17 18 19 20 21
  let width = props.width || 416;

  // 默认为 true,保持向下兼容
  if (!('okCancel' in props)) {
    props.okCancel = true;
  }
Y
yiminghe 已提交
22

23 24 25
  props.okText = props.okText || (props.okCancel ? '确定' : '知道了');
  props.cancelText = props.cancelText || '取消';

Y
yiminghe 已提交
26 27 28 29
  function close() {
    d.setState({
      visible: false
    });
Y
beta2  
yiminghe 已提交
30
    ReactDOM.unmountComponentAtNode(div);
B
Benjy Cui 已提交
31
    div.parentNode.removeChild(div);
Y
yiminghe 已提交
32 33 34
  }

  function onCancel() {
A
afc163 已提交
35
    let cancelFn = props.onCancel;
Y
yiminghe 已提交
36
    if (cancelFn) {
A
afc163 已提交
37
      let ret;
Y
yiminghe 已提交
38
      if (cancelFn.length) {
Y
yiminghe 已提交
39
        ret = cancelFn(close);
Y
yiminghe 已提交
40
      } else {
Y
yiminghe 已提交
41 42 43 44 45 46 47
        ret = cancelFn();
        if (!ret) {
          close();
        }
      }
      if (ret && ret.then) {
        ret.then(close);
Y
yiminghe 已提交
48
      }
Y
yiminghe 已提交
49 50 51 52 53 54
    } else {
      close();
    }
  }

  function onOk() {
A
afc163 已提交
55
    let okFn = props.onOk;
Y
yiminghe 已提交
56
    if (okFn) {
A
afc163 已提交
57
      let ret;
Y
yiminghe 已提交
58
      if (okFn.length) {
Y
yiminghe 已提交
59
        ret = okFn(close);
Y
yiminghe 已提交
60
      } else {
Y
yiminghe 已提交
61 62 63 64 65 66 67
        ret = okFn();
        if (!ret) {
          close();
        }
      }
      if (ret && ret.then) {
        ret.then(close);
Y
yiminghe 已提交
68
      }
Y
yiminghe 已提交
69 70 71 72 73
    } else {
      close();
    }
  }

74 75 76 77 78 79 80
  let body = (
    <div className="ant-confirm-body">
      <Icon type={iconClassType} />
      <span className="ant-confirm-title">{props.title}</span>
      <div className="ant-confirm-content">{props.content}</div>
    </div>
  );
Y
yiminghe 已提交
81

82
  let footer = null;
A
afc163 已提交
83
  if (props.okCancel) {
84 85 86 87 88 89 90 91 92 93
    footer = (
      <div className="ant-confirm-btns">
        <Button type="ghost" size="large" onClick={onCancel}>
          {props.cancelText}
        </Button>
        <Button type="primary" size="large" onClick={onOk}>
          {props.okText}
        </Button>
      </div>
    );
A
afc163 已提交
94
  } else {
95 96 97 98 99 100 101
    footer = (
      <div className="ant-confirm-btns">
        <Button type="primary" size="large" onClick={onOk}>
          {props.okText}
        </Button>
      </div>
    );
A
afc163 已提交
102 103
  }

Y
beta2  
yiminghe 已提交
104
  ReactDOM.render(<Dialog
Y
yiminghe 已提交
105 106
    prefixCls="ant-modal"
    className="ant-confirm"
A
afc163 已提交
107
    visible
Y
yiminghe 已提交
108 109 110
    closable={false}
    title=""
    transitionName="zoom"
111
    footer=""
Y
yiminghe 已提交
112
    maskTransitionName="fade" width={width}>
Y
yiminghe 已提交
113
    <div style={{zoom: 1, overflow: 'hidden'}}>{body} {footer}</div>
Y
yiminghe 已提交
114 115 116
  </Dialog>, div, function () {
    d = this;
  });
U
ustccjw 已提交
117
}