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 * as React from 'react'; import Button from '../button'; import type { ButtonProps, LegacyButtonType } from '../button/button'; import { convertLegacyProps } from '../button/button'; import { ConfigContext } from '../config-provider'; import LocaleReceiver from '../locale-provider/LocaleReceiver'; import defaultLocale from '../locale/default'; import Popover from '../popover'; import type { AbstractTooltipProps } from '../tooltip'; import ActionButton from '../_util/ActionButton'; import type { RenderFunction } from '../_util/getRenderPropValue'; import { getRenderPropValue } from '../_util/getRenderPropValue'; import { cloneElement } from '../_util/reactNode'; export interface PopconfirmProps extends AbstractTooltipProps { title: 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; onVisibleChange?: ( visible: boolean, e?: React.MouseEvent | React.KeyboardEvent, ) => void; } export interface PopconfirmState { visible?: boolean; } export interface PopconfirmLocale { okText: string; cancelText: string; } const Popconfirm = React.forwardRef((props, ref) => { const { getPrefixCls } = React.useContext(ConfigContext); const [visible, setVisible] = useMergedState(false, { value: props.visible, defaultValue: props.defaultVisible, }); // const isDestroyed = useDestroyed(); const settingVisible = ( value: boolean, e?: React.MouseEvent | React.KeyboardEvent, ) => { setVisible(value, true); props.onVisibleChange?.(value, e); }; const close = (e: React.MouseEvent) => { settingVisible(false, e); }; const onConfirm = (e: React.MouseEvent) => props.onConfirm?.call(this, e); const onCancel = (e: React.MouseEvent) => { settingVisible(false, e); props.onCancel?.call(this, e); }; const onKeyDown = (e: React.KeyboardEvent) => { if (e.keyCode === KeyCode.ESC && visible) { settingVisible(false, e); } }; const onVisibleChange = (value: boolean) => { const { disabled } = props; if (disabled) { return; } settingVisible(value); }; const renderOverlay = (prefixCls: string, popconfirmLocale: PopconfirmLocale) => { const { okButtonProps, cancelButtonProps, title, cancelText, okText, okType, icon, showCancel = true, } = props; return (
{icon}
{getRenderPropValue(title)}
{showCancel && ( )} {okText || popconfirmLocale.okText}
); }; const { prefixCls: customizePrefixCls, placement, children, overlayClassName, ...restProps } = props; const prefixCls = getPrefixCls('popover', customizePrefixCls); const prefixClsConfirm = getPrefixCls('popconfirm', customizePrefixCls); const overlayClassNames = classNames(prefixClsConfirm, overlayClassName); const overlay = ( {(popconfirmLocale: PopconfirmLocale) => renderOverlay(prefixCls, popconfirmLocale)} ); return ( {cloneElement(children, { onKeyDown: (e: React.KeyboardEvent) => { if (React.isValidElement(children)) { children?.props.onKeyDown?.(e); } onKeyDown(e); }, })} ); }); Popconfirm.defaultProps = { placement: 'top' as PopconfirmProps['placement'], trigger: 'click' as PopconfirmProps['trigger'], okType: 'primary' as PopconfirmProps['okType'], icon: , disabled: false, }; export default Popconfirm;