UNPKG

8.53 kBJavaScriptView Raw
1"use client";
2
3import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4var __rest = this && this.__rest || function (s, e) {
5 var t = {};
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
7 if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
9 }
10 return t;
11};
12import * as React from 'react';
13import CheckCircleFilled from "@ant-design/icons/es/icons/CheckCircleFilled";
14import CloseCircleFilled from "@ant-design/icons/es/icons/CloseCircleFilled";
15import ExclamationCircleFilled from "@ant-design/icons/es/icons/ExclamationCircleFilled";
16import InfoCircleFilled from "@ant-design/icons/es/icons/InfoCircleFilled";
17import classNames from 'classnames';
18import { CONTAINER_MAX_OFFSET } from '../_util/hooks/useZIndex';
19import { getTransitionName } from '../_util/motion';
20import { devUseWarning } from '../_util/warning';
21import ConfigProvider from '../config-provider';
22import { useLocale } from '../locale';
23import useToken from '../theme/useToken';
24import CancelBtn from './components/ConfirmCancelBtn';
25import OkBtn from './components/ConfirmOkBtn';
26import { ModalContextProvider } from './context';
27import Modal from './Modal';
28import Confirm from './style/confirm';
29export function ConfirmContent(props) {
30 const {
31 prefixCls,
32 icon,
33 okText,
34 cancelText,
35 confirmPrefixCls,
36 type,
37 okCancel,
38 footer,
39 // Legacy for static function usage
40 locale: staticLocale
41 } = props,
42 resetProps = __rest(props, ["prefixCls", "icon", "okText", "cancelText", "confirmPrefixCls", "type", "okCancel", "footer", "locale"]);
43 if (process.env.NODE_ENV !== 'production') {
44 const warning = devUseWarning('Modal');
45 process.env.NODE_ENV !== "production" ? warning(!(typeof icon === 'string' && icon.length > 2), 'breaking', `\`icon\` is using ReactNode instead of string naming in v4. Please check \`${icon}\` at https://ant.design/components/icon`) : void 0;
46 }
47 // Icon
48 let mergedIcon = icon;
49 // 支持传入{ icon: null }来隐藏`Modal.confirm`默认的Icon
50 if (!icon && icon !== null) {
51 switch (type) {
52 case 'info':
53 mergedIcon = /*#__PURE__*/React.createElement(InfoCircleFilled, null);
54 break;
55 case 'success':
56 mergedIcon = /*#__PURE__*/React.createElement(CheckCircleFilled, null);
57 break;
58 case 'error':
59 mergedIcon = /*#__PURE__*/React.createElement(CloseCircleFilled, null);
60 break;
61 default:
62 mergedIcon = /*#__PURE__*/React.createElement(ExclamationCircleFilled, null);
63 }
64 }
65 // 默认为 true,保持向下兼容
66 const mergedOkCancel = okCancel !== null && okCancel !== void 0 ? okCancel : type === 'confirm';
67 const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok';
68 const [locale] = useLocale('Modal');
69 const mergedLocale = staticLocale || locale;
70 // ================== Locale Text ==================
71 const okTextLocale = okText || (mergedOkCancel ? mergedLocale === null || mergedLocale === void 0 ? void 0 : mergedLocale.okText : mergedLocale === null || mergedLocale === void 0 ? void 0 : mergedLocale.justOkText);
72 const cancelTextLocale = cancelText || (mergedLocale === null || mergedLocale === void 0 ? void 0 : mergedLocale.cancelText);
73 // ================= Context Value =================
74 const btnCtxValue = Object.assign({
75 autoFocusButton,
76 cancelTextLocale,
77 okTextLocale,
78 mergedOkCancel
79 }, resetProps);
80 const btnCtxValueMemo = React.useMemo(() => btnCtxValue, _toConsumableArray(Object.values(btnCtxValue)));
81 // ====================== Footer Origin Node ======================
82 const footerOriginNode = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CancelBtn, null), /*#__PURE__*/React.createElement(OkBtn, null));
83 const hasTitle = props.title !== undefined && props.title !== null;
84 const bodyCls = `${confirmPrefixCls}-body`;
85 return /*#__PURE__*/React.createElement("div", {
86 className: `${confirmPrefixCls}-body-wrapper`
87 }, /*#__PURE__*/React.createElement("div", {
88 className: classNames(bodyCls, {
89 [`${bodyCls}-has-title`]: hasTitle
90 })
91 }, mergedIcon, /*#__PURE__*/React.createElement("div", {
92 className: `${confirmPrefixCls}-paragraph`
93 }, hasTitle && /*#__PURE__*/React.createElement("span", {
94 className: `${confirmPrefixCls}-title`
95 }, props.title), /*#__PURE__*/React.createElement("div", {
96 className: `${confirmPrefixCls}-content`
97 }, props.content))), footer === undefined || typeof footer === 'function' ? (/*#__PURE__*/React.createElement(ModalContextProvider, {
98 value: btnCtxValueMemo
99 }, /*#__PURE__*/React.createElement("div", {
100 className: `${confirmPrefixCls}-btns`
101 }, typeof footer === 'function' ? footer(footerOriginNode, {
102 OkBtn,
103 CancelBtn
104 }) : footerOriginNode))) : footer, /*#__PURE__*/React.createElement(Confirm, {
105 prefixCls: prefixCls
106 }));
107}
108const ConfirmDialog = props => {
109 const {
110 close,
111 zIndex,
112 afterClose,
113 open,
114 keyboard,
115 centered,
116 getContainer,
117 maskStyle,
118 direction,
119 prefixCls,
120 wrapClassName,
121 rootPrefixCls,
122 bodyStyle,
123 closable = false,
124 closeIcon,
125 modalRender,
126 focusTriggerAfterClose,
127 onConfirm,
128 styles
129 } = props;
130 if (process.env.NODE_ENV !== 'production') {
131 const warning = devUseWarning('Modal');
132 [['visible', 'open'], ['bodyStyle', 'styles.body'], ['maskStyle', 'styles.mask']].forEach(_ref => {
133 let [deprecatedName, newName] = _ref;
134 warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
135 });
136 }
137 const confirmPrefixCls = `${prefixCls}-confirm`;
138 const width = props.width || 416;
139 const style = props.style || {};
140 const mask = props.mask === undefined ? true : props.mask;
141 // 默认为 false,保持旧版默认行为
142 const maskClosable = props.maskClosable === undefined ? false : props.maskClosable;
143 const classString = classNames(confirmPrefixCls, `${confirmPrefixCls}-${props.type}`, {
144 [`${confirmPrefixCls}-rtl`]: direction === 'rtl'
145 }, props.className);
146 // ========================= zIndex =========================
147 const [, token] = useToken();
148 const mergedZIndex = React.useMemo(() => {
149 if (zIndex !== undefined) {
150 return zIndex;
151 }
152 // Static always use max zIndex
153 return token.zIndexPopupBase + CONTAINER_MAX_OFFSET;
154 }, [zIndex, token]);
155 // ========================= Render =========================
156 return /*#__PURE__*/React.createElement(Modal, {
157 prefixCls: prefixCls,
158 className: classString,
159 wrapClassName: classNames({
160 [`${confirmPrefixCls}-centered`]: !!props.centered
161 }, wrapClassName),
162 onCancel: () => {
163 close === null || close === void 0 ? void 0 : close({
164 triggerCancel: true
165 });
166 onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm(false);
167 },
168 open: open,
169 title: "",
170 footer: null,
171 transitionName: getTransitionName(rootPrefixCls || '', 'zoom', props.transitionName),
172 maskTransitionName: getTransitionName(rootPrefixCls || '', 'fade', props.maskTransitionName),
173 mask: mask,
174 maskClosable: maskClosable,
175 style: style,
176 styles: Object.assign({
177 body: bodyStyle,
178 mask: maskStyle
179 }, styles),
180 width: width,
181 zIndex: mergedZIndex,
182 afterClose: afterClose,
183 keyboard: keyboard,
184 centered: centered,
185 getContainer: getContainer,
186 closable: closable,
187 closeIcon: closeIcon,
188 modalRender: modalRender,
189 focusTriggerAfterClose: focusTriggerAfterClose
190 }, /*#__PURE__*/React.createElement(ConfirmContent, Object.assign({}, props, {
191 confirmPrefixCls: confirmPrefixCls
192 })));
193};
194const ConfirmDialogWrapper = props => {
195 const {
196 rootPrefixCls,
197 iconPrefixCls,
198 direction,
199 theme
200 } = props;
201 return /*#__PURE__*/React.createElement(ConfigProvider, {
202 prefixCls: rootPrefixCls,
203 iconPrefixCls: iconPrefixCls,
204 direction: direction,
205 theme: theme
206 }, /*#__PURE__*/React.createElement(ConfirmDialog, Object.assign({}, props)));
207};
208if (process.env.NODE_ENV !== 'production') {
209 ConfirmDialog.displayName = 'ConfirmDialog';
210 ConfirmDialogWrapper.displayName = 'ConfirmDialogWrapper';
211}
212export default ConfirmDialogWrapper;
\No newline at end of file