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';
|
2020-05-05 12:03:16 +08:00
|
|
|
import { LegacyButtonType, NativeButtonProps, convertLegacyProps } 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';
|
2020-04-26 22:24:13 +08:00
|
|
|
import { ConfigContext } 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;
|
2020-05-05 12:03:16 +08:00
|
|
|
okType?: LegacyButtonType;
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const Popconfirm = React.forwardRef<unknown, PopconfirmProps>((props, ref) => {
|
|
|
|
const [visible, setVisible] = React.useState(props.visible);
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if ('visible' in props) {
|
|
|
|
setVisible(props.visible);
|
2018-08-10 17:04:40 +08:00
|
|
|
}
|
2020-04-26 22:24:13 +08:00
|
|
|
}, [props.visible]);
|
2017-07-07 20:26:08 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if ('defaultVisible' in props) {
|
|
|
|
setVisible(props.defaultVisible);
|
|
|
|
}
|
|
|
|
}, [props.defaultVisible]);
|
2016-11-30 15:46:50 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const settingVisible = (value: boolean, e?: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
if (!('visible' in props)) {
|
|
|
|
setVisible(value);
|
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
if (props.onVisibleChange) {
|
|
|
|
props.onVisibleChange(value, e);
|
|
|
|
}
|
|
|
|
};
|
2017-07-07 20:26:08 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const onConfirm = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
settingVisible(false, e);
|
2016-10-24 12:04:26 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
if (props.onConfirm) {
|
|
|
|
props.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
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const onCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
settingVisible(false, e);
|
2016-10-24 12:04:26 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
if (props.onCancel) {
|
|
|
|
props.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
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const onVisibleChange = (value: boolean) => {
|
|
|
|
const { disabled } = props;
|
2019-06-05 20:33:00 +08:00
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-26 22:24:13 +08:00
|
|
|
settingVisible(value);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const renderOverlay = (prefixCls: string, popconfirmLocale: PopconfirmLocale) => {
|
|
|
|
const { okButtonProps, cancelButtonProps, title, cancelText, okText, okType, icon } = 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`}>
|
2020-04-26 22:24:13 +08:00
|
|
|
<Button onClick={onCancel} size="small" {...cancelButtonProps}>
|
2020-03-10 15:17:23 +08:00
|
|
|
{cancelText || popconfirmLocale.cancelText}
|
|
|
|
</Button>
|
2020-05-05 12:03:16 +08:00
|
|
|
<Button
|
|
|
|
onClick={onConfirm}
|
|
|
|
{...convertLegacyProps(okType)}
|
|
|
|
size="small"
|
|
|
|
{...okButtonProps}
|
|
|
|
>
|
2020-03-10 15:17:23 +08:00
|
|
|
{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
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
const { prefixCls: customizePrefixCls, placement, ...restProps } = props;
|
|
|
|
const prefixCls = getPrefixCls('popover', customizePrefixCls);
|
|
|
|
|
|
|
|
const overlay = (
|
|
|
|
<LocaleReceiver componentName="Popconfirm" defaultLocale={defaultLocale.Popconfirm}>
|
|
|
|
{(popconfirmLocale: PopconfirmLocale) => renderOverlay(prefixCls, popconfirmLocale)}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
{...restProps}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
placement={placement}
|
|
|
|
onVisibleChange={onVisibleChange}
|
|
|
|
visible={visible}
|
|
|
|
overlay={overlay}
|
|
|
|
ref={ref as any}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Popconfirm.defaultProps = {
|
|
|
|
transitionName: 'zoom-big',
|
|
|
|
placement: 'top' as PopconfirmProps['placement'],
|
|
|
|
trigger: 'click' as PopconfirmProps['trigger'],
|
|
|
|
okType: 'primary' as PopconfirmProps['okType'],
|
|
|
|
icon: <ExclamationCircleFilled />,
|
|
|
|
disabled: false,
|
|
|
|
};
|
2018-08-10 17:04:40 +08:00
|
|
|
|
|
|
|
export default Popconfirm;
|