import * as React from 'react'; import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled'; import classNames from 'classnames'; import useMergedState from 'rc-util/lib/hooks/useMergedState'; import KeyCode from 'rc-util/lib/KeyCode'; import omit from 'rc-util/lib/omit'; import type { RenderFunction } from '../_util/getRenderPropValue'; import { cloneElement } from '../_util/reactNode'; import type { ButtonProps, LegacyButtonType } from '../button/button'; import { ConfigContext } from '../config-provider'; import Popover from '../popover'; import type { AbstractTooltipProps, TooltipRef } from '../tooltip'; import PurePanel, { Overlay } from './PurePanel'; import usePopconfirmStyle from './style'; import useCSSVar from './style/cssVar'; export interface PopconfirmProps extends AbstractTooltipProps { title: React.ReactNode | RenderFunction; description?: React.ReactNode | RenderFunction; disabled?: boolean; onConfirm?: (e?: React.MouseEvent) => void; onCancel?: (e?: React.MouseEvent) => void; okText?: React.ReactNode; okType?: LegacyButtonType; cancelText?: React.ReactNode; okButtonProps?: ButtonProps; cancelButtonProps?: ButtonProps; showCancel?: boolean; icon?: React.ReactNode; onOpenChange?: ( open: boolean, e?: React.MouseEvent | React.KeyboardEvent, ) => void; onPopupClick?: (e: React.MouseEvent) => void; } export interface PopconfirmState { open?: boolean; } const Popconfirm = React.forwardRef((props, ref) => { const { prefixCls: customizePrefixCls, placement = 'top', trigger = 'click', okType = 'primary', icon = , children, overlayClassName, onOpenChange, onVisibleChange, ...restProps } = props; const { getPrefixCls } = React.useContext(ConfigContext); const [open, setOpen] = useMergedState(false, { value: props.open ?? props.visible, defaultValue: props.defaultOpen ?? props.defaultVisible, }); const settingOpen = ( value: boolean, e?: React.MouseEvent | React.KeyboardEvent, ) => { setOpen(value, true); onVisibleChange?.(value); onOpenChange?.(value, e); }; const close = (e: React.MouseEvent) => { settingOpen(false, e); }; const onConfirm = (e: React.MouseEvent) => props.onConfirm?.call(this, e); const onCancel = (e: React.MouseEvent) => { settingOpen(false, e); props.onCancel?.call(this, e); }; const onKeyDown = (e: React.KeyboardEvent) => { if (e.keyCode === KeyCode.ESC && open) { settingOpen(false, e); } }; const onInternalOpenChange = (value: boolean) => { const { disabled = false } = props; if (disabled) { return; } settingOpen(value); }; const prefixCls = getPrefixCls('popconfirm', customizePrefixCls); const overlayClassNames = classNames(prefixCls, overlayClassName); usePopconfirmStyle(prefixCls); const wrapCSSVar = useCSSVar(prefixCls); return wrapCSSVar( } data-popover-inject > {cloneElement(children, { onKeyDown: (e: React.KeyboardEvent) => { if (React.isValidElement(children)) { children?.props.onKeyDown?.(e); } onKeyDown(e); }, })} , ); }) as React.ForwardRefExoticComponent< React.PropsWithoutRef & React.RefAttributes > & { _InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel; }; // We don't care debug panel /* istanbul ignore next */ Popconfirm._InternalPanelDoNotUseOrYouWillBeFired = PurePanel; if (process.env.NODE_ENV !== 'production') { Popconfirm.displayName = 'Popconfirm'; } export default Popconfirm;