2022-05-23 15:41:59 +08:00
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
2020-07-20 13:39:28 +08:00
|
|
|
import classNames from 'classnames';
|
2020-05-25 17:43:59 +08:00
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
2023-05-11 15:44:55 +08:00
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2022-12-06 23:14:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-05-11 15:44:55 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import type { RenderFunction } from '../_util/getRenderPropValue';
|
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-05-23 15:41:59 +08:00
|
|
|
import type { ButtonProps, LegacyButtonType } from '../button/button';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import Popover from '../popover';
|
2023-07-08 19:47:54 +08:00
|
|
|
import type { AbstractTooltipProps, TooltipRef } from '../tooltip';
|
2022-07-15 15:28:09 +08:00
|
|
|
import PurePanel, { Overlay } from './PurePanel';
|
2022-05-23 20:34:20 +08:00
|
|
|
import usePopconfirmStyle from './style';
|
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;
|
2022-12-08 16:39:42 +08:00
|
|
|
description?: 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;
|
2021-03-31 09:42:13 +08:00
|
|
|
okButtonProps?: ButtonProps;
|
|
|
|
cancelButtonProps?: ButtonProps;
|
2021-10-26 19:12:08 +08:00
|
|
|
showCancel?: boolean;
|
2018-07-06 23:48:37 +08:00
|
|
|
icon?: React.ReactNode;
|
2022-08-24 21:34:17 +08:00
|
|
|
onOpenChange?: (
|
|
|
|
open: boolean,
|
2020-05-25 17:43:59 +08:00
|
|
|
e?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLDivElement>,
|
|
|
|
) => void;
|
2023-05-11 15:44:55 +08:00
|
|
|
onPopupClick?: (e: React.MouseEvent<HTMLElement>) => void;
|
2016-07-25 11:28:43 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
export interface PopconfirmState {
|
2022-08-24 21:34:17 +08:00
|
|
|
open?: boolean;
|
2017-11-21 15:37:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-08 19:47:54 +08:00
|
|
|
const Popconfirm = React.forwardRef<TooltipRef, PopconfirmProps>((props, ref) => {
|
2023-05-16 15:40:16 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
placement = 'top',
|
|
|
|
trigger = 'click',
|
|
|
|
okType = 'primary',
|
|
|
|
icon = <ExclamationCircleFilled />,
|
|
|
|
children,
|
|
|
|
overlayClassName,
|
|
|
|
onOpenChange,
|
|
|
|
onVisibleChange,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
2021-06-09 12:18:52 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
2022-08-24 21:34:17 +08:00
|
|
|
const [open, setOpen] = useMergedState(false, {
|
|
|
|
value: props.open,
|
|
|
|
defaultValue: props.defaultOpen,
|
2021-03-01 17:25:30 +08:00
|
|
|
});
|
2016-11-30 15:46:50 +08:00
|
|
|
|
2022-03-10 18:54:32 +08:00
|
|
|
// const isDestroyed = useDestroyed();
|
2021-12-27 18:06:46 +08:00
|
|
|
|
2022-08-24 21:34:17 +08:00
|
|
|
const settingOpen = (
|
2020-05-25 17:43:59 +08:00
|
|
|
value: boolean,
|
|
|
|
e?: React.MouseEvent<HTMLButtonElement> | React.KeyboardEvent<HTMLDivElement>,
|
|
|
|
) => {
|
2022-08-24 21:34:17 +08:00
|
|
|
setOpen(value, true);
|
2023-05-16 15:40:16 +08:00
|
|
|
onVisibleChange?.(value);
|
|
|
|
onOpenChange?.(value, e);
|
2020-04-26 22:24:13 +08:00
|
|
|
};
|
2017-07-07 20:26:08 +08:00
|
|
|
|
2021-06-09 12:18:52 +08:00
|
|
|
const close = (e: React.MouseEvent<HTMLButtonElement>) => {
|
2022-08-24 21:34:17 +08:00
|
|
|
settingOpen(false, e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2021-06-09 12:18:52 +08:00
|
|
|
const onConfirm = (e: React.MouseEvent<HTMLButtonElement>) => props.onConfirm?.call(this, e);
|
|
|
|
|
2020-04-26 22:24:13 +08:00
|
|
|
const onCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
|
2022-08-24 21:34:17 +08:00
|
|
|
settingOpen(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>) => {
|
2022-08-24 21:34:17 +08:00
|
|
|
if (e.keyCode === KeyCode.ESC && open) {
|
|
|
|
settingOpen(false, e);
|
2020-05-25 17:43:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-16 15:40:16 +08:00
|
|
|
const onInternalOpenChange = (value: boolean) => {
|
2022-10-01 21:58:46 +08:00
|
|
|
const { disabled = false } = props;
|
2019-06-05 20:33:00 +08:00
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-24 21:34:17 +08:00
|
|
|
settingOpen(value);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2022-05-23 20:34:20 +08:00
|
|
|
const prefixCls = getPrefixCls('popconfirm', customizePrefixCls);
|
|
|
|
const overlayClassNames = classNames(prefixCls, overlayClassName);
|
|
|
|
|
|
|
|
const [wrapSSR] = usePopconfirmStyle(prefixCls);
|
2020-04-26 22:24:13 +08:00
|
|
|
|
2022-05-23 20:34:20 +08:00
|
|
|
return wrapSSR(
|
2022-05-23 15:41:59 +08:00
|
|
|
<Popover
|
2022-12-06 23:14:30 +08:00
|
|
|
{...omit(restProps, ['title'])}
|
2022-10-01 21:58:46 +08:00
|
|
|
trigger={trigger}
|
2020-04-26 22:24:13 +08:00
|
|
|
placement={placement}
|
2023-05-16 15:40:16 +08:00
|
|
|
onOpenChange={onInternalOpenChange}
|
2022-08-24 21:34:17 +08:00
|
|
|
open={open}
|
2022-10-01 21:58:46 +08:00
|
|
|
ref={ref}
|
|
|
|
overlayClassName={overlayClassNames}
|
2022-12-06 23:14:30 +08:00
|
|
|
content={
|
2022-07-15 11:11:50 +08:00
|
|
|
<Overlay
|
2022-10-01 21:58:46 +08:00
|
|
|
okType={okType}
|
|
|
|
icon={icon}
|
2022-07-15 11:11:50 +08:00
|
|
|
{...props}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
close={close}
|
|
|
|
onConfirm={onConfirm}
|
|
|
|
onCancel={onCancel}
|
|
|
|
/>
|
|
|
|
}
|
2022-05-23 20:34:20 +08:00
|
|
|
data-popover-inject
|
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);
|
|
|
|
},
|
|
|
|
})}
|
2022-05-23 20:34:20 +08:00
|
|
|
</Popover>,
|
2020-04-26 22:24:13 +08:00
|
|
|
);
|
2022-07-13 23:06:28 +08:00
|
|
|
}) as React.ForwardRefExoticComponent<
|
|
|
|
React.PropsWithoutRef<PopconfirmProps> & React.RefAttributes<unknown>
|
|
|
|
> & {
|
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
|
|
|
};
|
2020-04-26 22:24:13 +08:00
|
|
|
|
2022-07-13 23:06:28 +08:00
|
|
|
// We don't care debug panel
|
2023-06-07 21:59:21 +08:00
|
|
|
/* istanbul ignore next */
|
2022-07-13 23:06:28 +08:00
|
|
|
Popconfirm._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Popconfirm.displayName = 'Popconfirm';
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:04:40 +08:00
|
|
|
export default Popconfirm;
|