confirm.js
4.62 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import _extends from "@babel/runtime/helpers/extends";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
import CheckCircleOutlined from '@ant-design/icons/CheckCircleOutlined';
import CloseCircleOutlined from '@ant-design/icons/CloseCircleOutlined';
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined';
import { getConfirmLocale } from './locale';
import { destroyFns } from './Modal';
import ConfirmDialog from './ConfirmDialog';
var defaultRootPrefixCls = 'ant';
function getRootPrefixCls() {
return defaultRootPrefixCls;
}
export default function confirm(config) {
var div = document.createElement('div');
document.body.appendChild(div); // eslint-disable-next-line @typescript-eslint/no-use-before-define
var currentConfig = _extends(_extends({}, config), {
close: close,
visible: true
});
function destroy() {
var unmountResult = ReactDOM.unmountComponentAtNode(div);
if (unmountResult && div.parentNode) {
div.parentNode.removeChild(div);
}
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var triggerCancel = args.some(function (param) {
return param && param.triggerCancel;
});
if (config.onCancel && triggerCancel) {
config.onCancel.apply(config, args);
}
for (var i = 0; i < destroyFns.length; i++) {
var fn = destroyFns[i]; // eslint-disable-next-line @typescript-eslint/no-use-before-define
if (fn === close) {
destroyFns.splice(i, 1);
break;
}
}
}
function render(_a) {
var okText = _a.okText,
cancelText = _a.cancelText,
prefixCls = _a.prefixCls,
props = __rest(_a, ["okText", "cancelText", "prefixCls"]);
/**
* https://github.com/ant-design/ant-design/issues/23623
* Sync render blocks React event. Let's make this async.
*/
setTimeout(function () {
var runtimeLocale = getConfirmLocale();
ReactDOM.render( /*#__PURE__*/React.createElement(ConfirmDialog, _extends({}, props, {
prefixCls: prefixCls || "".concat(getRootPrefixCls(), "-modal"),
rootPrefixCls: getRootPrefixCls(),
okText: okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText),
cancelText: cancelText || runtimeLocale.cancelText
})), div);
});
}
function close() {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
currentConfig = _extends(_extends({}, currentConfig), {
visible: false,
afterClose: destroy.bind.apply(destroy, [this].concat(args))
});
render(currentConfig);
}
function update(configUpdate) {
if (typeof configUpdate === 'function') {
currentConfig = configUpdate(currentConfig);
} else {
currentConfig = _extends(_extends({}, currentConfig), configUpdate);
}
render(currentConfig);
}
render(currentConfig);
destroyFns.push(close);
return {
destroy: close,
update: update
};
}
export function withWarn(props) {
return _extends({
type: 'warning',
icon: /*#__PURE__*/React.createElement(ExclamationCircleOutlined, null),
okCancel: false
}, props);
}
export function withInfo(props) {
return _extends({
type: 'info',
icon: /*#__PURE__*/React.createElement(InfoCircleOutlined, null),
okCancel: false
}, props);
}
export function withSuccess(props) {
return _extends({
type: 'success',
icon: /*#__PURE__*/React.createElement(CheckCircleOutlined, null),
okCancel: false
}, props);
}
export function withError(props) {
return _extends({
type: 'error',
icon: /*#__PURE__*/React.createElement(CloseCircleOutlined, null),
okCancel: false
}, props);
}
export function withConfirm(props) {
return _extends({
type: 'confirm',
icon: /*#__PURE__*/React.createElement(ExclamationCircleOutlined, null),
okCancel: true
}, props);
}
export function globalConfig(_ref) {
var rootPrefixCls = _ref.rootPrefixCls;
if (rootPrefixCls) {
defaultRootPrefixCls = rootPrefixCls;
}
}