nicecode-v2/packages/meta/lib/_util/ActionButton.js

139 lines
4.1 KiB
JavaScript

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/_util/ActionButton.tsx
var ActionButton_exports = {};
__export(ActionButton_exports, {
default: () => ActionButton_default
});
module.exports = __toCommonJS(ActionButton_exports);
var import_useState = __toESM(require("rc-util/lib/hooks/useState"));
var React = __toESM(require("react"));
var import_button = __toESM(require("../button"));
var import_buttonHelpers = require("../button/buttonHelpers");
function isThenable(thing) {
return !!(thing && thing.then);
}
var 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, import_useState.default)(false);
const onInternalClose = (...args) => {
close == null ? void 0 : close(...args);
};
React.useEffect(() => {
let timeoutId = null;
if (autoFocus) {
timeoutId = setTimeout(() => {
var _a;
(_a = buttonRef.current) == null ? void 0 : _a.focus();
});
}
return () => {
if (timeoutId) {
clearTimeout(timeoutId);
}
};
}, []);
const handlePromiseOnOk = (returnValueOfOnOk) => {
if (!isThenable(returnValueOfOnOk)) {
return;
}
setLoading(true);
returnValueOfOnOk.then(
(...args) => {
setLoading(false, true);
onInternalClose(...args);
clickedRef.current = false;
},
(e) => {
setLoading(false, true);
clickedRef.current = false;
if (isSilent == null ? 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);
clickedRef.current = false;
} else {
returnValueOfOnOk = actionFn();
if (!returnValueOfOnOk) {
onInternalClose();
return;
}
}
handlePromiseOnOk(returnValueOfOnOk);
};
return /* @__PURE__ */ React.createElement(
import_button.default,
{
...(0, import_buttonHelpers.convertLegacyProps)(type),
onClick,
loading,
prefixCls,
...buttonProps,
ref: buttonRef
},
children
);
};
var ActionButton_default = ActionButton;