mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-29 13:47:02 +08:00
d02c486052
* init hooks * portal it in * children with 2 context demo * Remove config since not more second demo * Quit with same Component for logic reuse * add rest functions * use localeReceiver * use localeReceiver * update test case * fix lint * update docs * update demo title
136 lines
3.1 KiB
TypeScript
136 lines
3.1 KiB
TypeScript
import * as React from 'react';
|
|
import * as ReactDOM from 'react-dom';
|
|
import {
|
|
InfoCircleOutlined,
|
|
CheckCircleOutlined,
|
|
CloseCircleOutlined,
|
|
ExclamationCircleOutlined,
|
|
} from '@ant-design/icons';
|
|
import { getConfirmLocale } from './locale';
|
|
import { ModalFuncProps, destroyFns } from './Modal';
|
|
import ConfirmDialog from './ConfirmDialog';
|
|
|
|
export type ModalFunc = (
|
|
props: ModalFuncProps,
|
|
) => {
|
|
destroy: () => void;
|
|
update: (newConfig: ModalFuncProps) => void;
|
|
};
|
|
|
|
export interface ModalStaticFunctions {
|
|
info: ModalFunc;
|
|
success: ModalFunc;
|
|
error: ModalFunc;
|
|
warn: ModalFunc;
|
|
warning: ModalFunc;
|
|
confirm: ModalFunc;
|
|
}
|
|
|
|
export default function confirm(config: ModalFuncProps) {
|
|
const div = document.createElement('div');
|
|
document.body.appendChild(div);
|
|
// eslint-disable-next-line no-use-before-define
|
|
let currentConfig = { ...config, close, visible: true } as any;
|
|
|
|
function destroy(...args: any[]) {
|
|
const unmountResult = ReactDOM.unmountComponentAtNode(div);
|
|
if (unmountResult && div.parentNode) {
|
|
div.parentNode.removeChild(div);
|
|
}
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
|
if (config.onCancel && triggerCancel) {
|
|
config.onCancel(...args);
|
|
}
|
|
for (let i = 0; i < destroyFns.length; i++) {
|
|
const fn = destroyFns[i];
|
|
// eslint-disable-next-line no-use-before-define
|
|
if (fn === close) {
|
|
destroyFns.splice(i, 1);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
function render({ okText, cancelText, ...props }: any) {
|
|
const runtimeLocale = getConfirmLocale();
|
|
ReactDOM.render(
|
|
<ConfirmDialog
|
|
{...props}
|
|
okText={okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText)}
|
|
cancelText={cancelText || runtimeLocale.cancelText}
|
|
/>,
|
|
div,
|
|
);
|
|
}
|
|
|
|
function close(...args: any[]) {
|
|
currentConfig = {
|
|
...currentConfig,
|
|
visible: false,
|
|
afterClose: destroy.bind(this, ...args),
|
|
};
|
|
render(currentConfig);
|
|
}
|
|
|
|
function update(newConfig: ModalFuncProps) {
|
|
currentConfig = {
|
|
...currentConfig,
|
|
...newConfig,
|
|
};
|
|
render(currentConfig);
|
|
}
|
|
|
|
render(currentConfig);
|
|
|
|
destroyFns.push(close);
|
|
|
|
return {
|
|
destroy: close,
|
|
update,
|
|
};
|
|
}
|
|
|
|
export function withWarn(props: ModalFuncProps): ModalFuncProps {
|
|
return {
|
|
type: 'warning',
|
|
icon: <ExclamationCircleOutlined />,
|
|
okCancel: false,
|
|
...props,
|
|
};
|
|
}
|
|
|
|
export function withInfo(props: ModalFuncProps): ModalFuncProps {
|
|
return {
|
|
type: 'info',
|
|
icon: <InfoCircleOutlined />,
|
|
okCancel: false,
|
|
...props,
|
|
};
|
|
}
|
|
|
|
export function withSuccess(props: ModalFuncProps): ModalFuncProps {
|
|
return {
|
|
type: 'success',
|
|
icon: <CheckCircleOutlined />,
|
|
okCancel: false,
|
|
...props,
|
|
};
|
|
}
|
|
|
|
export function withError(props: ModalFuncProps): ModalFuncProps {
|
|
return {
|
|
type: 'error',
|
|
icon: <CloseCircleOutlined />,
|
|
okCancel: false,
|
|
...props,
|
|
};
|
|
}
|
|
|
|
export function withConfirm(props: ModalFuncProps): ModalFuncProps {
|
|
return {
|
|
type: 'confirm',
|
|
okCancel: true,
|
|
...props,
|
|
};
|
|
}
|