2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2020-03-02 12:09:38 +08:00
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
2018-12-07 20:02:01 +08:00
|
|
|
import Tooltip, { AbstractTooltipProps } from '../tooltip';
|
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';
|
2019-07-24 10:34:55 +08:00
|
|
|
import defaultLocale from '../locale/default';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-03-10 13:57:02 +08:00
|
|
|
import { getRenderPropValue, RenderFunction } from '../_util/getRenderPropValue';
|
2015-11-24 11:21:37 +08:00
|
|
|
|
2016-11-30 16:43:35 +08:00
|
|
|
export interface PopconfirmProps extends AbstractTooltipProps {
|
2020-03-10 13:57:02 +08:00
|
|
|
title: React.ReactNode | RenderFunction;
|
2019-06-05 20:33:00 +08:00
|
|
|
disabled?: boolean;
|
2019-06-16 20:51:47 +08:00
|
|
|
onConfirm?: (e?: React.MouseEvent<HTMLElement>) => void;
|
|
|
|
onCancel?: (e?: React.MouseEvent<HTMLElement>) => 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;
|
2019-06-16 20:51:47 +08:00
|
|
|
onVisibleChange?: (visible: boolean, e?: React.MouseEvent<HTMLElement>) => 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',
|
2018-12-22 21:21:42 +08:00
|
|
|
placement: 'top' as PopconfirmProps['placement'],
|
|
|
|
trigger: 'click' as PopconfirmProps['trigger'],
|
|
|
|
okType: 'primary' as PopconfirmProps['okType'],
|
2019-08-13 14:07:17 +08:00
|
|
|
icon: <ExclamationCircleFilled />,
|
2019-06-05 20:33:00 +08:00
|
|
|
disabled: false,
|
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 };
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
if ('defaultVisible' in nextProps) {
|
2018-10-19 23:57:16 +08:00
|
|
|
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
|
|
|
}
|
2018-12-07 20:02:01 +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
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
onVisibleChange = (visible: boolean) => {
|
2019-06-05 20:33:00 +08:00
|
|
|
const { disabled } = this.props;
|
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-04 14:58:19 +08:00
|
|
|
this.setVisible(visible);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2018-09-07 14:41:31 +08:00
|
|
|
setVisible(visible: boolean, e?: React.MouseEvent<HTMLButtonElement>) {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { props } = this;
|
2016-11-30 15:46:50 +08:00
|
|
|
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-07 20:02:01 +08:00
|
|
|
};
|
2017-09-08 09:44:05 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderOverlay = (prefixCls: string, popconfirmLocale: PopconfirmLocale) => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const {
|
|
|
|
okButtonProps,
|
|
|
|
cancelButtonProps,
|
|
|
|
title,
|
|
|
|
cancelText,
|
|
|
|
okText,
|
|
|
|
okType,
|
|
|
|
icon,
|
|
|
|
} = this.props;
|
2017-10-20 14:54:38 +08:00
|
|
|
return (
|
2020-03-10 15:17:23 +08:00
|
|
|
<div className={`${prefixCls}-inner-content`}>
|
|
|
|
<div className={`${prefixCls}-message`}>
|
|
|
|
{icon}
|
|
|
|
<div className={`${prefixCls}-message-title`}>{getRenderPropValue(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>
|
2015-06-24 19:20:47 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2016-01-07 14:21:29 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +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 = (
|
2018-12-07 20:02:01 +08:00
|
|
|
<LocaleReceiver componentName="Popconfirm" defaultLocale={defaultLocale.Popconfirm}>
|
|
|
|
{(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-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderConfirm}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2018-08-10 17:04:40 +08:00
|
|
|
|
|
|
|
export default Popconfirm;
|