123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- "use client";
- import * as React from 'react';
- import useState from "rc-util/es/hooks/useState";
- import Button from '../button';
- import { convertLegacyProps } from '../button/buttonHelpers';
- const isThenable = thing => {
- return typeof (thing === null || thing === void 0 ? void 0 : thing.then) === 'function';
- };
- 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] = useState(false);
- const onInternalClose = (...args) => {
- close === null || close === void 0 ? void 0 : close.apply(void 0, args);
- };
- React.useEffect(() => {
- let timeoutId = null;
- if (autoFocus) {
- timeoutId = setTimeout(() => {
- var _a;
- (_a = buttonRef.current) === null || _a === void 0 ? void 0 : _a.focus({
- preventScroll: true
- });
- });
- }
- return () => {
- if (timeoutId) {
- clearTimeout(timeoutId);
- }
- };
- }, []);
- const handlePromiseOnOk = returnValueOfOnOk => {
- if (!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 === null || isSilent === void 0 ? void 0 : 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 && !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 (!isThenable(returnValueOfOnOk)) {
- onInternalClose();
- return;
- }
- }
- handlePromiseOnOk(returnValueOfOnOk);
- };
- return /*#__PURE__*/React.createElement(Button, Object.assign({}, convertLegacyProps(type), {
- onClick: onClick,
- loading: loading,
- prefixCls: prefixCls
- }, buttonProps, {
- ref: buttonRef
- }), children);
- };
- export default ActionButton;
|