You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design/components/modal/confirm.tsx

94 lines
2.5 KiB
TypeScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import React from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import assign from 'object-assign';
import Icon from '../icon';
import Dialog from './Modal';
import ActionButton from './ActionButton';
import { getConfirmLocale } from './locale';
export default function confirm(config) {
const props = assign({ iconType: 'question-circle' }, config);
const prefixCls = props.prefixCls || 'ant-confirm';
let div = document.createElement('div');
document.body.appendChild(div);
let width = props.width || 416;
let style = props.style || {};
// 默认为 true保持向下兼容
if (!('okCancel' in props)) {
props.okCancel = true;
}
const runtimeLocale = getConfirmLocale();
props.okText = props.okText ||
(props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
props.cancelText = props.cancelText || runtimeLocale.cancelText;
function close() {
const unmountResult = ReactDOM.unmountComponentAtNode(div);
if (unmountResult && div.parentNode) {
div.parentNode.removeChild(div);
}
}
let body = (
<div className={`${prefixCls}-body`}>
<Icon type={props.iconType} />
<span className={`${prefixCls}-title`}>{props.title}</span>
<div className={`${prefixCls}-content`}>{props.content}</div>
</div>
);
let footer: React.ReactElement<any> | null = null;
if (props.okCancel) {
footer = (
<div className={`${prefixCls}-btns`}>
<ActionButton type="ghost" actionFn={props.onCancel} closeModal={close}>
{props.cancelText}
</ActionButton>
<ActionButton type="primary" actionFn={props.onOk} closeModal={close} autoFocus>
{props.okText}
</ActionButton>
</div>
);
} else {
footer = (
<div className={`${prefixCls}-btns`}>
<ActionButton type="primary" actionFn={props.onOk} closeModal={close} autoFocus>
{props.okText}
</ActionButton>
</div>
);
}
const classString = classNames(prefixCls, {
[`${prefixCls}-${props.type}`]: true,
}, props.className);
ReactDOM.render(
<Dialog
className={classString}
onCancel={close}
visible
title=""
transitionName="zoom"
footer=""
maskTransitionName="fade"
maskClosable={false}
style={style}
width={width}
>
<div className={`${prefixCls}-body-wrapper`}>
{body} {footer}
</div>
</Dialog>
, div);
return {
destroy: close,
};
}