2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2020-07-20 13:39:28 +08:00
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
2020-05-25 17:43:59 +08:00
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
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';
|
2020-05-25 17:43:59 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2021-02-08 17:09:13 +08:00
|
|
|
import { getTransitionName } from '../_util/motion';
|
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;
|
2020-05-25 17:43:59 +08:00
|
|
|
onVisibleChange?: (
|
|
|
|
visible: boolean,
|
|
|
|
e?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLDivElement>,
|
|
|
|
) => 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-05-25 17:43:59 +08:00
|
|
|
const settingVisible = (
|
|
|
|
value: boolean,
|
|
|
|
e?: React.MouseEvent<HTMLButtonElement> | React.KeyboardEvent<HTMLDivElement>,
|
|
|
|
) => {
|
2020-04-26 22:24:13 +08:00
|
|
|
if (!('visible' in props)) {
|
|
|
|
setVisible(value);
|
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2021-02-19 18:26:53 +08:00
|
|
|
props.onVisibleChange?.(value, e);
|
2020-04-26 22:24:13 +08:00
|
|
|
};
|
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);
|
2021-02-19 18:26:53 +08:00
|
|
|
props.onConfirm?.call(this, e);
|
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);
|
2021-02-19 18:26:53 +08:00
|
|
|
props.onCancel?.call(this, e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2020-05-25 17:43:59 +08:00
|
|
|
const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
|
|
if (e.keyCode === KeyCode.ESC && visible) {
|
|
|
|
settingVisible(false, e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
2020-07-20 13:39:28 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
placement,
|
|
|
|
children,
|
|
|
|
overlayClassName,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
2020-04-26 22:24:13 +08:00
|
|
|
const prefixCls = getPrefixCls('popover', customizePrefixCls);
|
2020-07-20 13:39:28 +08:00
|
|
|
const prefixClsConfirm = getPrefixCls('popconfirm', customizePrefixCls);
|
|
|
|
const overlayClassNames = classNames(prefixClsConfirm, overlayClassName);
|
2020-04-26 22:24:13 +08:00
|
|
|
|
|
|
|
const overlay = (
|
|
|
|
<LocaleReceiver componentName="Popconfirm" defaultLocale={defaultLocale.Popconfirm}>
|
|
|
|
{(popconfirmLocale: PopconfirmLocale) => renderOverlay(prefixCls, popconfirmLocale)}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-04-26 22:24:13 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
{...restProps}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
placement={placement}
|
|
|
|
onVisibleChange={onVisibleChange}
|
|
|
|
visible={visible}
|
|
|
|
overlay={overlay}
|
2020-07-20 13:39:28 +08:00
|
|
|
overlayClassName={overlayClassNames}
|
2020-04-26 22:24:13 +08:00
|
|
|
ref={ref as any}
|
2021-02-08 17:09:13 +08:00
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'zoom-big', props.transitionName)}
|
2020-05-25 17:43:59 +08:00
|
|
|
>
|
|
|
|
{cloneElement(children, {
|
|
|
|
onKeyDown: (e: React.KeyboardEvent<any>) => {
|
2020-09-02 15:35:49 +08:00
|
|
|
if (React.isValidElement(children)) {
|
|
|
|
children?.props.onKeyDown?.(e);
|
|
|
|
}
|
2020-05-25 17:43:59 +08:00
|
|
|
onKeyDown(e);
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
</Tooltip>
|
2020-04-26 22:24:13 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Popconfirm.defaultProps = {
|
|
|
|
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;
|