confirm.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use client";
  2. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  3. import React, { useContext } from 'react';
  4. import warning from '../_util/warning';
  5. import ConfigProvider, { ConfigContext, globalConfig, warnContext } from '../config-provider';
  6. import { unstableSetRender } from '../config-provider/UnstableContext';
  7. import ConfirmDialog from './ConfirmDialog';
  8. import destroyFns from './destroyFns';
  9. import { getConfirmLocale } from './locale';
  10. let defaultRootPrefixCls = '';
  11. function getRootPrefixCls() {
  12. return defaultRootPrefixCls;
  13. }
  14. const ConfirmDialogWrapper = props => {
  15. var _a, _b;
  16. const {
  17. prefixCls: customizePrefixCls,
  18. getContainer,
  19. direction
  20. } = props;
  21. const runtimeLocale = getConfirmLocale();
  22. const config = useContext(ConfigContext);
  23. const rootPrefixCls = getRootPrefixCls() || config.getPrefixCls();
  24. // because Modal.config set rootPrefixCls, which is different from other components
  25. const prefixCls = customizePrefixCls || `${rootPrefixCls}-modal`;
  26. let mergedGetContainer = getContainer;
  27. if (mergedGetContainer === false) {
  28. mergedGetContainer = undefined;
  29. if (process.env.NODE_ENV !== 'production') {
  30. process.env.NODE_ENV !== "production" ? warning(false, 'Modal', 'Static method not support `getContainer` to be `false` since it do not have context env.') : void 0;
  31. }
  32. }
  33. return /*#__PURE__*/React.createElement(ConfirmDialog, Object.assign({}, props, {
  34. rootPrefixCls: rootPrefixCls,
  35. prefixCls: prefixCls,
  36. iconPrefixCls: config.iconPrefixCls,
  37. theme: config.theme,
  38. direction: direction !== null && direction !== void 0 ? direction : config.direction,
  39. locale: (_b = (_a = config.locale) === null || _a === void 0 ? void 0 : _a.Modal) !== null && _b !== void 0 ? _b : runtimeLocale,
  40. getContainer: mergedGetContainer
  41. }));
  42. };
  43. export default function confirm(config) {
  44. const global = globalConfig();
  45. if (process.env.NODE_ENV !== 'production' && !global.holderRender) {
  46. warnContext('Modal');
  47. }
  48. const container = document.createDocumentFragment();
  49. let currentConfig = Object.assign(Object.assign({}, config), {
  50. close,
  51. open: true
  52. });
  53. let timeoutId;
  54. let reactUnmount;
  55. function destroy(...args) {
  56. var _a;
  57. const triggerCancel = args.some(param => param === null || param === void 0 ? void 0 : param.triggerCancel);
  58. if (triggerCancel) {
  59. var _a2;
  60. (_a = config.onCancel) === null || _a === void 0 ? void 0 : (_a2 = _a).call.apply(_a2, [config, () => {}].concat(_toConsumableArray(args.slice(1))));
  61. }
  62. for (let i = 0; i < destroyFns.length; i++) {
  63. const fn = destroyFns[i];
  64. if (fn === close) {
  65. destroyFns.splice(i, 1);
  66. break;
  67. }
  68. }
  69. reactUnmount();
  70. }
  71. function render(props) {
  72. clearTimeout(timeoutId);
  73. /**
  74. * https://github.com/ant-design/ant-design/issues/23623
  75. *
  76. * Sync render blocks React event. Let's make this async.
  77. */
  78. timeoutId = setTimeout(() => {
  79. const rootPrefixCls = global.getPrefixCls(undefined, getRootPrefixCls());
  80. const iconPrefixCls = global.getIconPrefixCls();
  81. const theme = global.getTheme();
  82. const dom = /*#__PURE__*/React.createElement(ConfirmDialogWrapper, Object.assign({}, props));
  83. const reactRender = unstableSetRender();
  84. reactUnmount = reactRender(/*#__PURE__*/React.createElement(ConfigProvider, {
  85. prefixCls: rootPrefixCls,
  86. iconPrefixCls: iconPrefixCls,
  87. theme: theme
  88. }, global.holderRender ? global.holderRender(dom) : dom), container);
  89. });
  90. }
  91. function close(...args) {
  92. currentConfig = Object.assign(Object.assign({}, currentConfig), {
  93. open: false,
  94. afterClose: () => {
  95. if (typeof config.afterClose === 'function') {
  96. config.afterClose();
  97. }
  98. // @ts-ignore
  99. destroy.apply(this, args);
  100. }
  101. });
  102. // Legacy support
  103. if (currentConfig.visible) {
  104. delete currentConfig.visible;
  105. }
  106. render(currentConfig);
  107. }
  108. function update(configUpdate) {
  109. if (typeof configUpdate === 'function') {
  110. currentConfig = configUpdate(currentConfig);
  111. } else {
  112. currentConfig = Object.assign(Object.assign({}, currentConfig), configUpdate);
  113. }
  114. render(currentConfig);
  115. }
  116. render(currentConfig);
  117. destroyFns.push(close);
  118. return {
  119. destroy: close,
  120. update
  121. };
  122. }
  123. export function withWarn(props) {
  124. return Object.assign(Object.assign({}, props), {
  125. type: 'warning'
  126. });
  127. }
  128. export function withInfo(props) {
  129. return Object.assign(Object.assign({}, props), {
  130. type: 'info'
  131. });
  132. }
  133. export function withSuccess(props) {
  134. return Object.assign(Object.assign({}, props), {
  135. type: 'success'
  136. });
  137. }
  138. export function withError(props) {
  139. return Object.assign(Object.assign({}, props), {
  140. type: 'error'
  141. });
  142. }
  143. export function withConfirm(props) {
  144. return Object.assign(Object.assign({}, props), {
  145. type: 'confirm'
  146. });
  147. }
  148. export function modalGlobalConfig({
  149. rootPrefixCls
  150. }) {
  151. process.env.NODE_ENV !== "production" ? warning(false, 'Modal', 'Modal.config is deprecated. Please use ConfigProvider.config instead.') : void 0;
  152. defaultRootPrefixCls = rootPrefixCls;
  153. }