2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-10 17:04:40 +08:00
|
|
|
import { polyfill } from 'react-lifecycles-compat';
|
2017-03-17 15:23:25 +08:00
|
|
|
import Tooltip, { AbstractTooltipProps } from '../tooltip';
|
2015-11-20 11:05:34 +08:00
|
|
|
import Icon from '../icon';
|
2017-07-17 09:40:55 +08:00
|
|
|
import Button from '../button';
|
2018-10-19 11:13:29 +08:00
|
|
|
import { ButtonType, NativeButtonProps } from '../button/button';
|
2017-10-20 14:54:38 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2017-10-26 10:50:18 +08:00
|
|
|
import defaultLocale from '../locale-provider/default';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2015-11-24 11:21:37 +08:00
|
|
|
|
2016-11-30 16:43:35 +08:00
|
|
|
export interface PopconfirmProps extends AbstractTooltipProps {
|
|
|
|
title: React.ReactNode;
|
2018-09-07 14:41:31 +08:00
|
|
|
onConfirm?: (e?: React.MouseEvent<any>) => void;
|
|
|
|
onCancel?: (e?: React.MouseEvent<any>) => void;
|
2016-07-25 11:28:43 +08:00
|
|
|
okText?: React.ReactNode;
|
2017-07-16 16:52:06 +08:00
|
|
|
okType?: ButtonType;
|
2016-07-25 11:28:43 +08:00
|
|
|
cancelText?: React.ReactNode;
|
2018-10-19 11:13:29 +08:00
|
|
|
okButtonProps?: NativeButtonProps;
|
|
|
|
cancelButtonProps?: NativeButtonProps;
|
2018-07-06 23:48:37 +08:00
|
|
|
icon?: React.ReactNode;
|
2018-09-07 14:41:31 +08:00
|
|
|
onVisibleChange?: (visible?: boolean, e?: React.MouseEvent<any>) => void;
|
2016-07-25 11:28:43 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
export interface PopconfirmState {
|
|
|
|
visible?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface PopconfirmLocale {
|
|
|
|
okText: string;
|
|
|
|
cancelText: string;
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:04:40 +08:00
|
|
|
class Popconfirm extends React.Component<PopconfirmProps, PopconfirmState> {
|
2016-03-29 17:33:37 +08:00
|
|
|
static defaultProps = {
|
2016-04-07 15:02:22 +08:00
|
|
|
transitionName: 'zoom-big',
|
2016-03-29 17:33:37 +08:00
|
|
|
placement: 'top',
|
|
|
|
trigger: 'click',
|
2017-07-17 09:50:24 +08:00
|
|
|
okType: 'primary',
|
2018-09-08 15:46:26 +08:00
|
|
|
icon: <Icon type="exclamation-circle" theme="filled" />,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2018-08-10 17:04:40 +08:00
|
|
|
static getDerivedStateFromProps(nextProps: PopconfirmProps) {
|
|
|
|
if ('visible' in nextProps) {
|
|
|
|
return { visible: nextProps.visible };
|
2018-10-19 23:57:16 +08:00
|
|
|
} else if ('defaultVisible' in nextProps) {
|
|
|
|
return { visible: nextProps.defaultVisible };
|
2018-08-10 17:04:40 +08:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
private tooltip: any;
|
2017-07-07 20:26:08 +08:00
|
|
|
|
2016-11-30 15:46:50 +08:00
|
|
|
constructor(props: PopconfirmProps) {
|
2016-03-29 17:33:37 +08:00
|
|
|
super(props);
|
2016-11-30 15:46:50 +08:00
|
|
|
|
2016-03-29 17:33:37 +08:00
|
|
|
this.state = {
|
2016-11-30 15:46:50 +08:00
|
|
|
visible: props.visible,
|
2015-06-24 19:20:47 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 20:26:08 +08:00
|
|
|
getPopupDomNode() {
|
2017-09-08 09:44:05 +08:00
|
|
|
return this.tooltip.getPopupDomNode();
|
2017-07-07 20:26:08 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
onConfirm = (e: React.MouseEvent<HTMLButtonElement>) => {
|
2018-09-07 14:41:31 +08:00
|
|
|
this.setVisible(false, e);
|
2016-10-24 12:04:26 +08:00
|
|
|
|
2017-03-17 15:23:25 +08:00
|
|
|
const { onConfirm } = this.props;
|
2016-10-24 12:04:26 +08:00
|
|
|
if (onConfirm) {
|
2017-02-24 16:07:39 +08:00
|
|
|
onConfirm.call(this, e);
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
onCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
|
2018-09-07 14:41:31 +08:00
|
|
|
this.setVisible(false, e);
|
2016-10-24 12:04:26 +08:00
|
|
|
|
2017-03-17 15:23:25 +08:00
|
|
|
const { onCancel } = this.props;
|
2016-10-24 12:04:26 +08:00
|
|
|
if (onCancel) {
|
2017-02-24 16:07:39 +08:00
|
|
|
onCancel.call(this, e);
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
onVisibleChange = (visible: boolean) => {
|
2015-12-04 14:58:19 +08:00
|
|
|
this.setVisible(visible);
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
|
|
|
|
2018-09-07 14:41:31 +08:00
|
|
|
setVisible(visible: boolean, e?: React.MouseEvent<HTMLButtonElement>) {
|
2016-11-30 15:46:50 +08:00
|
|
|
const props = this.props;
|
|
|
|
if (!('visible' in props)) {
|
2015-12-04 14:58:19 +08:00
|
|
|
this.setState({ visible });
|
|
|
|
}
|
2016-10-24 12:04:26 +08:00
|
|
|
|
2017-03-17 15:23:25 +08:00
|
|
|
const { onVisibleChange } = props;
|
2016-10-24 12:04:26 +08:00
|
|
|
if (onVisibleChange) {
|
2018-09-07 14:41:31 +08:00
|
|
|
onVisibleChange(visible, e);
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
saveTooltip = (node: any) => {
|
2017-09-08 09:44:05 +08:00
|
|
|
this.tooltip = node;
|
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderOverlay = (prefixCls: string, popconfirmLocale: PopconfirmLocale) => {
|
|
|
|
const { okButtonProps, cancelButtonProps, title, cancelText, okText, okType, icon } = this.props;
|
2017-10-20 14:54:38 +08:00
|
|
|
return (
|
2016-01-07 14:21:29 +08:00
|
|
|
<div>
|
2016-03-17 15:10:49 +08:00
|
|
|
<div className={`${prefixCls}-inner-content`}>
|
2016-03-10 16:22:52 +08:00
|
|
|
<div className={`${prefixCls}-message`}>
|
2018-07-06 23:48:37 +08:00
|
|
|
{icon}
|
2016-03-10 16:22:52 +08:00
|
|
|
<div className={`${prefixCls}-message-title`}>{title}</div>
|
|
|
|
</div>
|
2016-02-17 18:04:42 +08:00
|
|
|
<div className={`${prefixCls}-buttons`}>
|
2018-10-19 11:13:29 +08:00
|
|
|
<Button onClick={this.onCancel} size="small" {...cancelButtonProps}>
|
2017-03-17 15:23:25 +08:00
|
|
|
{cancelText || popconfirmLocale.cancelText}
|
|
|
|
</Button>
|
2018-10-19 11:13:29 +08:00
|
|
|
<Button onClick={this.onConfirm} type={okType} size="small" {...okButtonProps}>
|
2017-03-17 15:23:25 +08:00
|
|
|
{okText || popconfirmLocale.okText}
|
|
|
|
</Button>
|
2016-01-07 14:21:29 +08:00
|
|
|
</div>
|
2015-06-24 19:20:47 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2016-01-07 14:21:29 +08:00
|
|
|
);
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderConfirm = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const { prefixCls: customizePrefixCls, placement, ...restProps } = this.props;
|
|
|
|
const prefixCls = getPrefixCls('popover', customizePrefixCls);
|
2017-10-20 14:54:38 +08:00
|
|
|
|
|
|
|
const overlay = (
|
|
|
|
<LocaleReceiver
|
|
|
|
componentName="Popconfirm"
|
2017-10-26 10:50:18 +08:00
|
|
|
defaultLocale={defaultLocale.Popconfirm}
|
2017-10-20 14:54:38 +08:00
|
|
|
>
|
2018-12-05 19:12:18 +08:00
|
|
|
{(popconfirmLocale: PopconfirmLocale) => (
|
|
|
|
this.renderOverlay(prefixCls, popconfirmLocale)
|
|
|
|
)}
|
2017-10-20 14:54:38 +08:00
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
2015-06-24 19:20:47 +08:00
|
|
|
|
|
|
|
return (
|
2016-08-31 13:50:50 +08:00
|
|
|
<Tooltip
|
|
|
|
{...restProps}
|
2016-01-07 17:46:46 +08:00
|
|
|
prefixCls={prefixCls}
|
2016-08-31 13:50:50 +08:00
|
|
|
placement={placement}
|
2016-01-07 17:46:46 +08:00
|
|
|
onVisibleChange={this.onVisibleChange}
|
|
|
|
visible={this.state.visible}
|
2016-06-06 13:54:10 +08:00
|
|
|
overlay={overlay}
|
2017-09-08 09:44:05 +08:00
|
|
|
ref={this.saveTooltip}
|
2016-08-31 13:50:50 +08:00
|
|
|
/>
|
2015-06-24 19:20:47 +08:00
|
|
|
);
|
|
|
|
}
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ConfigConsumer>
|
|
|
|
{this.renderConfirm}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2018-08-10 17:04:40 +08:00
|
|
|
|
|
|
|
polyfill(Popconfirm);
|
|
|
|
|
|
|
|
export default Popconfirm;
|