import * as React from 'react'; import { polyfill } from 'react-lifecycles-compat'; import Tooltip, { AbstractTooltipProps } from '../tooltip'; import Icon from '../icon'; import Button from '../button'; import { ButtonType, NativeButtonProps } from '../button/button'; import LocaleReceiver from '../locale-provider/LocaleReceiver'; import defaultLocale from '../locale-provider/default'; import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; export interface PopconfirmProps extends AbstractTooltipProps { title: React.ReactNode; onConfirm?: (e?: React.MouseEvent) => void; onCancel?: (e?: React.MouseEvent) => void; okText?: React.ReactNode; okType?: ButtonType; cancelText?: React.ReactNode; okButtonProps?: NativeButtonProps; cancelButtonProps?: NativeButtonProps; icon?: React.ReactNode; onVisibleChange?: (visible: boolean, e?: React.MouseEvent) => void; } export interface PopconfirmState { visible?: boolean; } export interface PopconfirmLocale { okText: string; cancelText: string; } class Popconfirm extends React.Component { static defaultProps = { transitionName: 'zoom-big', placement: 'top' as PopconfirmProps['placement'], trigger: 'click' as PopconfirmProps['trigger'], okType: 'primary' as PopconfirmProps['okType'], icon: , }; static getDerivedStateFromProps(nextProps: PopconfirmProps) { if ('visible' in nextProps) { return { visible: nextProps.visible }; } else if ('defaultVisible' in nextProps) { return { visible: nextProps.defaultVisible }; } return null; } private tooltip: any; constructor(props: PopconfirmProps) { super(props); this.state = { visible: props.visible, }; } getPopupDomNode() { return this.tooltip.getPopupDomNode(); } onConfirm = (e: React.MouseEvent) => { this.setVisible(false, e); const { onConfirm } = this.props; if (onConfirm) { onConfirm.call(this, e); } }; onCancel = (e: React.MouseEvent) => { this.setVisible(false, e); const { onCancel } = this.props; if (onCancel) { onCancel.call(this, e); } }; onVisibleChange = (visible: boolean) => { this.setVisible(visible); }; setVisible(visible: boolean, e?: React.MouseEvent) { const props = this.props; if (!('visible' in props)) { this.setState({ visible }); } const { onVisibleChange } = props; if (onVisibleChange) { onVisibleChange(visible, e); } } saveTooltip = (node: any) => { this.tooltip = node; }; renderOverlay = (prefixCls: string, popconfirmLocale: PopconfirmLocale) => { const { okButtonProps, cancelButtonProps, title, cancelText, okText, okType, icon, } = this.props; return (
{icon}
{title}
); }; renderConfirm = ({ getPrefixCls }: ConfigConsumerProps) => { const { prefixCls: customizePrefixCls, placement, ...restProps } = this.props; const prefixCls = getPrefixCls('popover', customizePrefixCls); const overlay = ( {(popconfirmLocale: PopconfirmLocale) => this.renderOverlay(prefixCls, popconfirmLocale)} ); return ( ); }; render() { return {this.renderConfirm}; } } polyfill(Popconfirm); export default Popconfirm;