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

7 8 9 10 11 12 13 14 15 16
const defaultLocale = {
  okText: '确定',
  cancelText: '取消',
  justOkText: '知道了',
};

let runtimeLocale = { ...defaultLocale };

export function changeConfirmLocale(newLocale) {
  if (newLocale) {
B
Benjy Cui 已提交
17
    runtimeLocale = { ...runtimeLocale, ...newLocale };
18
  } else {
19
    runtimeLocale = { ...defaultLocale };
20 21 22 23
  }
}

export default function confirm(config) {
B
Benjy Cui 已提交
24
  const props = { ...config };
A
afc163 已提交
25 26 27
  let div = document.createElement('div');
  document.body.appendChild(div);

A
afc163 已提交
28
  let d;
E
elrrrrrrr 已提交
29
  props.iconClassName = props.iconClassName || 'question-circle';
30

E
elrrrrrrr 已提交
31 32
  let iconClassType = props.iconClassName;

A
afc163 已提交
33 34 35 36 37 38
  let width = props.width || 416;

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

40 41 42
  props.okText = props.okText ||
    (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
  props.cancelText = props.cancelText || runtimeLocale.cancelText;
43

Y
yiminghe 已提交
44 45
  function close() {
    d.setState({
46
      visible: false,
Y
yiminghe 已提交
47
    });
Y
beta2  
yiminghe 已提交
48
    ReactDOM.unmountComponentAtNode(div);
B
Benjy Cui 已提交
49
    div.parentNode.removeChild(div);
Y
yiminghe 已提交
50 51 52
  }

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

  function onOk() {
A
afc163 已提交
73
    let okFn = props.onOk;
Y
yiminghe 已提交
74
    if (okFn) {
A
afc163 已提交
75
      let ret;
Y
yiminghe 已提交
76
      if (okFn.length) {
Y
yiminghe 已提交
77
        ret = okFn(close);
Y
yiminghe 已提交
78
      } else {
Y
yiminghe 已提交
79 80 81 82 83 84 85
        ret = okFn();
        if (!ret) {
          close();
        }
      }
      if (ret && ret.then) {
        ret.then(close);
Y
yiminghe 已提交
86
      }
Y
yiminghe 已提交
87 88 89 90 91
    } else {
      close();
    }
  }

92 93 94 95 96 97 98
  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 已提交
99

100
  let footer = null;
A
afc163 已提交
101
  if (props.okCancel) {
102 103 104 105 106 107 108 109 110 111
    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 已提交
112
  } else {
113 114 115 116 117 118 119
    footer = (
      <div className="ant-confirm-btns">
        <Button type="primary" size="large" onClick={onOk}>
          {props.okText}
        </Button>
      </div>
    );
A
afc163 已提交
120 121
  }

Y
beta2  
yiminghe 已提交
122
  ReactDOM.render(<Dialog
Y
yiminghe 已提交
123 124
    prefixCls="ant-modal"
    className="ant-confirm"
A
afc163 已提交
125
    visible
Y
yiminghe 已提交
126 127 128
    closable={false}
    title=""
    transitionName="zoom"
129
    footer=""
Y
yiminghe 已提交
130
    maskTransitionName="fade" width={width}>
131
    <div style={{ zoom: 1, overflow: 'hidden' }}>{body} {footer}</div>
Y
yiminghe 已提交
132 133 134
  </Dialog>, div, function () {
    d = this;
  });
U
ustccjw 已提交
135
}