import React from 'react'; import ReactDOM from 'react-dom'; import Dialog from './Modal'; import Icon from '../icon'; import Button from '../button'; import classNames from 'classnames'; import { getConfirmLocale } from './locale'; import assign from 'object-assign'; export interface ActionButtonProps { type: 'primary' | 'ghost' | 'dashed'; actionFn: Function; closeModal: Function; autoFocus?: Boolean; } class ActionButton extends React.Component { timeoutId: number; constructor(props) { super(props); this.state = { loading: false, }; } componentDidMount () { if (this.props.autoFocus) { const $this = ReactDOM.findDOMNode(this) as HTMLInputElement; this.timeoutId = setTimeout(() => $this.focus()); } } componentWillUnmount () { clearTimeout(this.timeoutId); } onClick = () => { const { actionFn, closeModal } = this.props; if (actionFn) { let ret; if (actionFn.length) { ret = actionFn(closeModal); } else { ret = actionFn(); if (!ret) { closeModal(); } } if (ret && ret.then) { this.setState({ loading: true }); ret.then((...args) => { // It's unnecessary to set loading=false, for the Modal will be unmounted after close. // this.setState({ loading: false }); closeModal(...args); }); } } else { closeModal(); } } render() { const { type, children } = this.props; const loading = this.state.loading; return ( ); } } 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.removeChild(div); } } let body = (
{props.title}
{props.content}
); let footer: React.ReactElement | null = null; if (props.okCancel) { footer = (
{props.cancelText} {props.okText}
); } else { footer = (
{props.okText}
); } const classString = classNames({ [prefixCls]: true, [`${prefixCls}-${props.type}`]: true, [props.className]: !!props.className, }); ReactDOM.render(
{body} {footer}
, div); return { destroy: close, }; }