confirm.jsx 2.4 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

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

A
afc163 已提交
11
  let d;
Y
yiminghe 已提交
12
  props = props || {};
E
elrrrrrrr 已提交
13 14 15
  props.iconClassName = props.iconClassName || 'question-circle';
  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 26

  function close() {
    d.setState({
      visible: false
    });
Y
beta2  
yiminghe 已提交
27
    ReactDOM.unmountComponentAtNode(div);
Y
yiminghe 已提交
28 29 30
  }

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

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

A
afc163 已提交
70
  let body = <div className="ant-confirm-body">
E
elrrrrrrr 已提交
71
    <Icon type={iconClassType} />
Y
yiminghe 已提交
72 73
    <span className="ant-confirm-title">{props.title}</span>
    <div className="ant-confirm-content">{props.content}</div>
Y
yiminghe 已提交
74
  </div>;
A
afc163 已提交
75
  let footer = <div className="ant-confirm-btns">
A
afc163 已提交
76
    <Button type="ghost" size="large" onClick={onCancel}>取 消</Button>
A
afc163 已提交
77
    <Button type="primary" size="large" onClick={onOk}>确 定</Button>
Y
yiminghe 已提交
78 79
  </div>;

A
afc163 已提交
80 81
  if (props.okCancel) {
    footer = <div className="ant-confirm-btns">
A
afc163 已提交
82
      <Button type="ghost" size="large" onClick={onCancel}>取 消</Button>
A
afc163 已提交
83
      <Button type="primary" size="large" onClick={onOk}>确 定</Button>
A
afc163 已提交
84 85 86
    </div>;
  } else {
    footer = <div className="ant-confirm-btns">
A
afc163 已提交
87
      <Button type="primary" size="large" onClick={onOk}>知道了</Button>
A
afc163 已提交
88 89 90
    </div>;
  }

Y
beta2  
yiminghe 已提交
91
  ReactDOM.render(<Dialog
Y
yiminghe 已提交
92 93 94 95 96 97
    prefixCls="ant-modal"
    className="ant-confirm"
    visible={true}
    closable={false}
    title=""
    transitionName="zoom"
98
    footer=""
Y
yiminghe 已提交
99
    maskTransitionName="fade" width={width}>
Y
yiminghe 已提交
100
    <div style={{zoom: 1, overflow: 'hidden'}}>{body} {footer}</div>
Y
yiminghe 已提交
101 102 103
  </Dialog>, div, function () {
    d = this;
  });
U
ustccjw 已提交
104
}