ant-design/components/popconfirm/index.tsx

132 lines
3.7 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2022-05-23 15:41:59 +08:00
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
import classNames from 'classnames';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import omit from 'rc-util/lib/omit';
import type { RenderFunction } from '../_util/getRenderPropValue';
2022-05-23 15:41:59 +08:00
import type { ButtonProps, LegacyButtonType } from '../button/button';
import { ConfigContext } from '../config-provider';
import type { PopoverProps } from '../popover';
2022-05-23 15:41:59 +08:00
import Popover from '../popover';
import type { AbstractTooltipProps, TooltipRef } from '../tooltip';
import PurePanel, { Overlay } from './PurePanel';
import useStyle from './style';
2015-11-24 11:21:37 +08:00
2016-11-30 16:43:35 +08:00
export interface PopconfirmProps extends AbstractTooltipProps {
title: React.ReactNode | RenderFunction;
description?: React.ReactNode | RenderFunction;
disabled?: boolean;
onConfirm?: (e?: React.MouseEvent<HTMLElement>) => void;
onCancel?: (e?: React.MouseEvent<HTMLElement>) => void;
okText?: React.ReactNode;
okType?: LegacyButtonType;
cancelText?: React.ReactNode;
okButtonProps?: ButtonProps;
cancelButtonProps?: ButtonProps;
showCancel?: boolean;
2018-07-06 23:48:37 +08:00
icon?: React.ReactNode;
onOpenChange?: (
open: boolean,
e?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLDivElement>,
) => void;
onPopupClick?: (e: React.MouseEvent<HTMLElement>) => void;
}
export interface PopconfirmState {
open?: boolean;
}
const InternalPopconfirm = React.forwardRef<TooltipRef, PopconfirmProps>((props, ref) => {
const {
prefixCls: customizePrefixCls,
placement = 'top',
trigger = 'click',
okType = 'primary',
icon = <ExclamationCircleFilled />,
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: PopoverProps['onOpenChange'] = (value, e) => {
setOpen(value, true);
onVisibleChange?.(value);
onOpenChange?.(value, e);
};
2017-07-07 20:26:08 +08:00
const close = (e: React.MouseEvent<HTMLButtonElement>) => {
settingOpen(false, e);
2018-12-07 20:02:01 +08:00
};
const onConfirm = (e: React.MouseEvent<HTMLButtonElement>) => props.onConfirm?.call(this, e);
const onCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
settingOpen(false, e);
props.onCancel?.call(this, e);
2018-12-07 20:02:01 +08:00
};
const onInternalOpenChange: PopoverProps['onOpenChange'] = (value, e) => {
2022-10-01 21:58:46 +08:00
const { disabled = false } = props;
if (disabled) {
return;
}
settingOpen(value, e);
2018-12-07 20:02:01 +08:00
};
const prefixCls = getPrefixCls('popconfirm', customizePrefixCls);
const overlayClassNames = classNames(prefixCls, overlayClassName);
const [wrapCSSVar] = useStyle(prefixCls);
return wrapCSSVar(
<Popover
{...omit(restProps, ['title'])}
trigger={trigger}
placement={placement}
onOpenChange={onInternalOpenChange}
open={open}
ref={ref}
overlayClassName={overlayClassNames}
content={
<Overlay
okType={okType}
icon={icon}
{...props}
prefixCls={prefixCls}
close={close}
onConfirm={onConfirm}
onCancel={onCancel}
/>
}
data-popover-inject
>
{children}
</Popover>,
);
});
type CompoundedComponent = typeof InternalPopconfirm & {
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
};
const Popconfirm = InternalPopconfirm as CompoundedComponent;
// We don't care debug panel
/* istanbul ignore next */
Popconfirm._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
if (process.env.NODE_ENV !== 'production') {
Popconfirm.displayName = 'Popconfirm';
}
export default Popconfirm;