linkmore-design
Version:
π πlmη»δ»ΆεΊγπ
99 lines β’ 3.11 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import useState from 'rc-util/lib/hooks/useState';
import * as React from 'react';
import Button from "../button";
import { convertLegacyProps } from "../button/button";
function isThenable(thing) {
return !!(thing && !!thing.then);
}
var ActionButton = function ActionButton(props) {
var clickedRef = React.useRef(false);
var ref = React.useRef(null);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
loading = _useState2[0],
setLoading = _useState2[1];
var close = props.close;
var onInternalClose = function onInternalClose() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
close === null || close === void 0 ? void 0 : close.apply(void 0, args);
};
React.useEffect(function () {
var timeoutId = null;
if (props.autoFocus) {
timeoutId = setTimeout(function () {
var _ref$current;
(_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.focus();
});
}
return function () {
if (timeoutId) {
clearTimeout(timeoutId);
}
};
}, []);
var handlePromiseOnOk = function handlePromiseOnOk(returnValueOfOnOk) {
if (!isThenable(returnValueOfOnOk)) {
return;
}
setLoading(true);
returnValueOfOnOk.then(function () {
setLoading(false, true);
onInternalClose.apply(void 0, arguments);
clickedRef.current = false;
}, 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, true);
clickedRef.current = false;
});
};
var onClick = function onClick(e) {
var actionFn = props.actionFn;
if (clickedRef.current) {
return;
}
clickedRef.current = true;
if (!actionFn) {
onInternalClose();
return;
}
var returnValueOfOnOk;
if (props.emitEvent) {
returnValueOfOnOk = actionFn(e);
if (props.quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) {
clickedRef.current = false;
onInternalClose(e);
return;
}
} else if (actionFn.length) {
returnValueOfOnOk = actionFn(close);
// https://github.com/ant-design/ant-design/issues/23358
clickedRef.current = false;
} else {
returnValueOfOnOk = actionFn();
if (!returnValueOfOnOk) {
onInternalClose();
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;