ActionButton.js
2.51 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
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import * as React from 'react';
import Button from '../button';
import { convertLegacyProps } from '../button/button';
var ActionButton = function ActionButton(props) {
var clickedRef = React.useRef(false);
var ref = React.useRef();
var _React$useState = React.useState(false),
_React$useState2 = _slicedToArray(_React$useState, 2),
loading = _React$useState2[0],
setLoading = _React$useState2[1];
React.useEffect(function () {
var timeoutId;
if (props.autoFocus) {
var $this = ref.current;
timeoutId = setTimeout(function () {
return $this.focus();
});
}
return function () {
if (timeoutId) {
clearTimeout(timeoutId);
}
};
}, []);
var handlePromiseOnOk = function handlePromiseOnOk(returnValueOfOnOk) {
var closeModal = props.closeModal;
if (!returnValueOfOnOk || !returnValueOfOnOk.then) {
return;
}
setLoading(true);
returnValueOfOnOk.then(function () {
// It's unnecessary to set loading=false, for the Modal will be unmounted after close.
// setState({ loading: false });
closeModal.apply(void 0, arguments);
}, function (e) {
// Emit error when catch promise reject
// eslint-disable-next-line no-console
console.error(e); // See: https://github.com/ant-design/ant-design/issues/6183
setLoading(false);
clickedRef.current = false;
});
};
var onClick = function onClick() {
var actionFn = props.actionFn,
closeModal = props.closeModal;
if (clickedRef.current) {
return;
}
clickedRef.current = true;
if (!actionFn) {
closeModal();
return;
}
var returnValueOfOnOk;
if (actionFn.length) {
returnValueOfOnOk = actionFn(closeModal); // https://github.com/ant-design/ant-design/issues/23358
clickedRef.current = false;
} else {
returnValueOfOnOk = actionFn();
if (!returnValueOfOnOk) {
closeModal();
return;
}
}
handlePromiseOnOk(returnValueOfOnOk);
};
var type = props.type,
children = props.children,
prefixCls = props.prefixCls,
buttonProps = props.buttonProps;
return /*#__PURE__*/React.createElement(Button, _extends({}, convertLegacyProps(type), {
onClick: onClick,
loading: loading,
prefixCls: prefixCls
}, buttonProps, {
ref: ref
}), children);
};
export default ActionButton;