ant-design/components/popconfirm/index.tsx

136 lines
3.7 KiB
TypeScript
Raw Normal View History

import classNames from 'classnames';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import KeyCode from 'rc-util/lib/KeyCode';
2022-05-23 15:41:59 +08:00
import * as React from 'react';
import type { ButtonProps, LegacyButtonType } from '../button/button';
import { ConfigContext } from '../config-provider';
import Popover from '../popover';
import type { AbstractTooltipProps } from '../tooltip';
import type { RenderFunction } from '../_util/getRenderPropValue';
2021-12-28 17:21:00 +08:00
import { cloneElement } from '../_util/reactNode';
import PurePanel, { Overlay } from './PurePanel';
2015-11-24 11:21:37 +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 {
title: 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;
}
export interface PopconfirmState {
open?: boolean;
}
const Popconfirm = React.forwardRef<unknown, PopconfirmProps>((props, ref) => {
const { getPrefixCls } = React.useContext(ConfigContext);
const [open, setOpen] = useMergedState(false, {
value: props.open,
defaultValue: props.defaultOpen,
});
2022-03-10 18:54:32 +08:00
// const isDestroyed = useDestroyed();
const settingOpen = (
value: boolean,
e?: React.MouseEvent<HTMLButtonElement> | React.KeyboardEvent<HTMLDivElement>,
) => {
setOpen(value, true);
props.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 onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.keyCode === KeyCode.ESC && open) {
settingOpen(false, e);
}
};
const onOpenChange = (value: boolean) => {
const { disabled } = props;
if (disabled) {
return;
}
settingOpen(value);
2018-12-07 20:02:01 +08:00
};
const {
prefixCls: customizePrefixCls,
placement,
children,
overlayClassName,
...restProps
} = props;
const prefixCls = getPrefixCls('popconfirm', customizePrefixCls);
const overlayClassNames = classNames(prefixCls, overlayClassName);
const [wrapSSR] = usePopconfirmStyle(prefixCls);
return wrapSSR(
2022-05-23 15:41:59 +08:00
<Popover
{...restProps}
placement={placement}
onOpenChange={onOpenChange}
open={open}
_overlay={
<Overlay
{...props}
prefixCls={prefixCls}
close={close}
onConfirm={onConfirm}
onCancel={onCancel}
/>
}
overlayClassName={overlayClassNames}
ref={ref as any}
data-popover-inject
>
{cloneElement(children, {
onKeyDown: (e: React.KeyboardEvent<any>) => {
if (React.isValidElement(children)) {
children?.props.onKeyDown?.(e);
}
onKeyDown(e);
},
})}
</Popover>,
);
}) as React.ForwardRefExoticComponent<
React.PropsWithoutRef<PopconfirmProps> & React.RefAttributes<unknown>
> & {
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
};
Popconfirm.defaultProps = {
trigger: 'click' as PopconfirmProps['trigger'],
disabled: false,
};
// We don't care debug panel
/* istanbul ignore next */
Popconfirm._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
export default Popconfirm;