UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

96 lines 2.94 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import useState from "rc-util/es/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() { close === null || close === void 0 ? void 0 : close.apply(void 0, arguments); }; React.useEffect(function () { var timeoutId = null; if (props.autoFocus) { timeoutId = setTimeout(function () { var _a; (_a = ref.current) === null || _a === void 0 ? void 0 : _a.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;