You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.3 KiB

import * as React from 'react';
import { polyfill } from 'react-lifecycles-compat';
import Tooltip, { AbstractTooltipProps } from '../tooltip';
9 years ago
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<any>) => void;
onCancel?: (e?: React.MouseEvent<any>) => void;
okText?: React.ReactNode;
okType?: ButtonType;
cancelText?: React.ReactNode;
okButtonProps?: NativeButtonProps;
cancelButtonProps?: NativeButtonProps;
icon?: React.ReactNode;
onVisibleChange?: (visible?: boolean, e?: React.MouseEvent<any>) => void;
}
export interface PopconfirmState {
visible?: boolean;
}
export interface PopconfirmLocale {
okText: string;
cancelText: string;
}
class Popconfirm extends React.Component<PopconfirmProps, PopconfirmState> {
static defaultProps = {
transitionName: 'zoom-big',
placement: 'top',
trigger: 'click',
okType: 'primary',
icon: <Icon type="exclamation-circle" theme="filled" />,
};
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;
7 years ago
constructor(props: PopconfirmProps) {
super(props);
this.state = {
visible: props.visible,
};
}
7 years ago
getPopupDomNode() {
return this.tooltip.getPopupDomNode();
7 years ago
}
onConfirm = (e: React.MouseEvent<HTMLButtonElement>) => {
this.setVisible(false, e);
const { onConfirm } = this.props;
if (onConfirm) {
onConfirm.call(this, e);
}
}
onCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
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<HTMLButtonElement>) {
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 (
<div>
<div className={`${prefixCls}-inner-content`}>
<div className={`${prefixCls}-message`}>
{icon}
<div className={`${prefixCls}-message-title`}>{title}</div>
</div>
<div className={`${prefixCls}-buttons`}>
<Button onClick={this.onCancel} size="small" {...cancelButtonProps}>
{cancelText || popconfirmLocale.cancelText}
</Button>
<Button onClick={this.onConfirm} type={okType} size="small" {...okButtonProps}>
{okText || popconfirmLocale.okText}
</Button>
</div>
</div>
</div>
);
}
renderConfirm = ({ getPrefixCls }: ConfigConsumerProps) => {
const { prefixCls: customizePrefixCls, placement, ...restProps } = this.props;
const prefixCls = getPrefixCls('popover', customizePrefixCls);
const overlay = (
<LocaleReceiver
componentName="Popconfirm"
defaultLocale={defaultLocale.Popconfirm}
>
{(popconfirmLocale: PopconfirmLocale) => (
this.renderOverlay(prefixCls, popconfirmLocale)
)}
</LocaleReceiver>
);
return (
<Tooltip
{...restProps}
prefixCls={prefixCls}
placement={placement}
onVisibleChange={this.onVisibleChange}
visible={this.state.visible}
overlay={overlay}
ref={this.saveTooltip}
/>
);
}
render() {
return (
<ConfigConsumer>
{this.renderConfirm}
</ConfigConsumer>
);
}
}
polyfill(Popconfirm);
export default Popconfirm;