1
This commit is contained in:
+108
@@ -0,0 +1,108 @@
|
||||
"use strict";
|
||||
"use client";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
||||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
var _useState = _interopRequireDefault(require("@rc-component/util/lib/hooks/useState"));
|
||||
var _Button = _interopRequireDefault(require("../button/Button"));
|
||||
var _buttonHelpers = require("../button/buttonHelpers");
|
||||
var _is = require("./is");
|
||||
const ActionButton = props => {
|
||||
const {
|
||||
type,
|
||||
children,
|
||||
prefixCls,
|
||||
buttonProps,
|
||||
close,
|
||||
autoFocus,
|
||||
emitEvent,
|
||||
isSilent,
|
||||
quitOnNullishReturnValue,
|
||||
actionFn
|
||||
} = props;
|
||||
const clickedRef = React.useRef(false);
|
||||
const buttonRef = React.useRef(null);
|
||||
const [loading, setLoading] = (0, _useState.default)(false);
|
||||
const onInternalClose = (...args) => {
|
||||
close?.(...args);
|
||||
};
|
||||
React.useEffect(() => {
|
||||
let timeoutId = null;
|
||||
if (autoFocus) {
|
||||
timeoutId = setTimeout(() => {
|
||||
buttonRef.current?.focus({
|
||||
preventScroll: true
|
||||
});
|
||||
});
|
||||
}
|
||||
return () => {
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
};
|
||||
}, [autoFocus]);
|
||||
const handlePromiseOnOk = returnValueOfOnOk => {
|
||||
if (!(0, _is.isThenable)(returnValueOfOnOk)) {
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
returnValueOfOnOk.then((...args) => {
|
||||
setLoading(false, true);
|
||||
onInternalClose.apply(void 0, args);
|
||||
clickedRef.current = false;
|
||||
}, e => {
|
||||
// See: https://github.com/ant-design/ant-design/issues/6183
|
||||
setLoading(false, true);
|
||||
clickedRef.current = false;
|
||||
// Do not throw if is `await` mode
|
||||
if (isSilent?.()) {
|
||||
return;
|
||||
}
|
||||
return Promise.reject(e);
|
||||
});
|
||||
};
|
||||
const onClick = e => {
|
||||
if (clickedRef.current) {
|
||||
return;
|
||||
}
|
||||
clickedRef.current = true;
|
||||
if (!actionFn) {
|
||||
onInternalClose();
|
||||
return;
|
||||
}
|
||||
let returnValueOfOnOk;
|
||||
if (emitEvent) {
|
||||
returnValueOfOnOk = actionFn(e);
|
||||
if (quitOnNullishReturnValue && !(0, _is.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 (!(0, _is.isThenable)(returnValueOfOnOk)) {
|
||||
onInternalClose();
|
||||
return;
|
||||
}
|
||||
}
|
||||
handlePromiseOnOk(returnValueOfOnOk);
|
||||
};
|
||||
return /*#__PURE__*/React.createElement(_Button.default, {
|
||||
...(0, _buttonHelpers.convertLegacyProps)(type),
|
||||
onClick: onClick,
|
||||
loading: loading,
|
||||
prefixCls: prefixCls,
|
||||
...buttonProps,
|
||||
ref: buttonRef
|
||||
}, children);
|
||||
};
|
||||
var _default = exports.default = ActionButton;
|
||||
Reference in New Issue
Block a user