ActionButton.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. "use client";
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  5. Object.defineProperty(exports, "__esModule", {
  6. value: true
  7. });
  8. exports.default = void 0;
  9. var React = _interopRequireWildcard(require("react"));
  10. var _useState = _interopRequireDefault(require("rc-util/lib/hooks/useState"));
  11. var _button = _interopRequireDefault(require("../button"));
  12. var _buttonHelpers = require("../button/buttonHelpers");
  13. const isThenable = thing => {
  14. return typeof (thing === null || thing === void 0 ? void 0 : thing.then) === 'function';
  15. };
  16. const ActionButton = props => {
  17. const {
  18. type,
  19. children,
  20. prefixCls,
  21. buttonProps,
  22. close,
  23. autoFocus,
  24. emitEvent,
  25. isSilent,
  26. quitOnNullishReturnValue,
  27. actionFn
  28. } = props;
  29. const clickedRef = React.useRef(false);
  30. const buttonRef = React.useRef(null);
  31. const [loading, setLoading] = (0, _useState.default)(false);
  32. const onInternalClose = (...args) => {
  33. close === null || close === void 0 ? void 0 : close.apply(void 0, args);
  34. };
  35. React.useEffect(() => {
  36. let timeoutId = null;
  37. if (autoFocus) {
  38. timeoutId = setTimeout(() => {
  39. var _a;
  40. (_a = buttonRef.current) === null || _a === void 0 ? void 0 : _a.focus({
  41. preventScroll: true
  42. });
  43. });
  44. }
  45. return () => {
  46. if (timeoutId) {
  47. clearTimeout(timeoutId);
  48. }
  49. };
  50. }, []);
  51. const handlePromiseOnOk = returnValueOfOnOk => {
  52. if (!isThenable(returnValueOfOnOk)) {
  53. return;
  54. }
  55. setLoading(true);
  56. returnValueOfOnOk.then((...args) => {
  57. setLoading(false, true);
  58. onInternalClose.apply(void 0, args);
  59. clickedRef.current = false;
  60. }, e => {
  61. // See: https://github.com/ant-design/ant-design/issues/6183
  62. setLoading(false, true);
  63. clickedRef.current = false;
  64. // Do not throw if is `await` mode
  65. if (isSilent === null || isSilent === void 0 ? void 0 : isSilent()) {
  66. return;
  67. }
  68. return Promise.reject(e);
  69. });
  70. };
  71. const onClick = e => {
  72. if (clickedRef.current) {
  73. return;
  74. }
  75. clickedRef.current = true;
  76. if (!actionFn) {
  77. onInternalClose();
  78. return;
  79. }
  80. let returnValueOfOnOk;
  81. if (emitEvent) {
  82. returnValueOfOnOk = actionFn(e);
  83. if (quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) {
  84. clickedRef.current = false;
  85. onInternalClose(e);
  86. return;
  87. }
  88. } else if (actionFn.length) {
  89. returnValueOfOnOk = actionFn(close);
  90. // https://github.com/ant-design/ant-design/issues/23358
  91. clickedRef.current = false;
  92. } else {
  93. returnValueOfOnOk = actionFn();
  94. if (!isThenable(returnValueOfOnOk)) {
  95. onInternalClose();
  96. return;
  97. }
  98. }
  99. handlePromiseOnOk(returnValueOfOnOk);
  100. };
  101. return /*#__PURE__*/React.createElement(_button.default, Object.assign({}, (0, _buttonHelpers.convertLegacyProps)(type), {
  102. onClick: onClick,
  103. loading: loading,
  104. prefixCls: prefixCls
  105. }, buttonProps, {
  106. ref: buttonRef
  107. }), children);
  108. };
  109. var _default = exports.default = ActionButton;