2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-10-20 14:53:36 +08:00
|
|
|
import classNames from 'classnames';
|
2024-04-08 14:04:08 +08:00
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
2023-10-20 14:53:36 +08:00
|
|
|
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { RenderFunction } from '../_util/getRenderPropValue';
|
|
|
|
import { getRenderPropValue } from '../_util/getRenderPropValue';
|
2021-02-08 17:09:13 +08:00
|
|
|
import { getTransitionName } from '../_util/motion';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2023-05-08 19:33:34 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-07-08 17:39:59 +08:00
|
|
|
import type { AbstractTooltipProps, TooltipRef } from '../tooltip';
|
2023-05-08 19:33:34 +08:00
|
|
|
import Tooltip from '../tooltip';
|
2022-10-11 20:29:55 +08:00
|
|
|
import PurePanel from './PurePanel';
|
2022-03-25 13:20:42 +08:00
|
|
|
// CSSINJS
|
|
|
|
import useStyle from './style';
|
|
|
|
|
2016-11-30 16:43:35 +08:00
|
|
|
export interface PopoverProps extends AbstractTooltipProps {
|
2020-03-10 13:57:02 +08:00
|
|
|
title?: React.ReactNode | RenderFunction;
|
|
|
|
content?: React.ReactNode | RenderFunction;
|
2024-03-22 10:33:56 +08:00
|
|
|
onOpenChange?: (
|
|
|
|
open: boolean,
|
|
|
|
e?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLDivElement>,
|
|
|
|
) => void;
|
2016-07-29 14:45:06 +08:00
|
|
|
}
|
|
|
|
|
2023-02-23 21:56:43 +08:00
|
|
|
interface OverlayProps {
|
2022-09-30 10:17:49 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
title?: PopoverProps['title'];
|
|
|
|
content?: PopoverProps['content'];
|
|
|
|
}
|
|
|
|
|
2023-05-08 19:33:34 +08:00
|
|
|
const Overlay: React.FC<OverlayProps> = ({ title, content, prefixCls }) => (
|
|
|
|
<>
|
|
|
|
{title && <div className={`${prefixCls}-title`}>{getRenderPropValue(title)}</div>}
|
|
|
|
<div className={`${prefixCls}-inner-content`}>{getRenderPropValue(content)}</div>
|
|
|
|
</>
|
|
|
|
);
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2024-04-09 16:49:47 +08:00
|
|
|
const InternalPopover = React.forwardRef<TooltipRef, PopoverProps>((props, ref) => {
|
2022-09-30 10:17:49 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
title,
|
|
|
|
content,
|
2022-10-11 20:29:55 +08:00
|
|
|
overlayClassName,
|
2022-09-30 10:17:49 +08:00
|
|
|
placement = 'top',
|
|
|
|
trigger = 'hover',
|
2024-03-22 10:33:56 +08:00
|
|
|
children,
|
2022-09-30 10:17:49 +08:00
|
|
|
mouseEnterDelay = 0.1,
|
|
|
|
mouseLeaveDelay = 0.1,
|
2024-03-22 10:33:56 +08:00
|
|
|
onOpenChange,
|
2022-09-30 10:17:49 +08:00
|
|
|
overlayStyle = {},
|
|
|
|
...otherProps
|
|
|
|
} = props;
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
2021-02-08 17:09:13 +08:00
|
|
|
|
2022-09-30 10:17:49 +08:00
|
|
|
const prefixCls = getPrefixCls('popover', customizePrefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2022-09-30 10:17:49 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2022-03-25 13:20:42 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const overlayCls = classNames(overlayClassName, hashId, cssVarCls);
|
2024-03-22 10:33:56 +08:00
|
|
|
const [open, setOpen] = useMergedState(false, {
|
|
|
|
value: props.open ?? props.visible,
|
2024-04-17 11:34:11 +08:00
|
|
|
defaultValue: props.defaultOpen ?? props.defaultVisible,
|
2024-03-22 10:33:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const settingOpen = (
|
|
|
|
value: boolean,
|
|
|
|
e?: React.MouseEvent<HTMLButtonElement> | React.KeyboardEvent<HTMLDivElement>,
|
|
|
|
) => {
|
|
|
|
setOpen(value, true);
|
|
|
|
onOpenChange?.(value, e);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
|
|
if (e.keyCode === KeyCode.ESC) {
|
|
|
|
settingOpen(false, e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onInternalOpenChange = (value: boolean) => {
|
|
|
|
settingOpen(value);
|
|
|
|
};
|
2022-10-11 20:29:55 +08:00
|
|
|
|
2023-11-08 14:56:15 +08:00
|
|
|
return wrapCSSVar(
|
2023-10-23 17:16:41 +08:00
|
|
|
<Tooltip
|
|
|
|
placement={placement}
|
|
|
|
trigger={trigger}
|
|
|
|
mouseEnterDelay={mouseEnterDelay}
|
|
|
|
mouseLeaveDelay={mouseLeaveDelay}
|
|
|
|
overlayStyle={overlayStyle}
|
|
|
|
{...otherProps}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
overlayClassName={overlayCls}
|
|
|
|
ref={ref}
|
2024-03-22 10:33:56 +08:00
|
|
|
open={open}
|
|
|
|
onOpenChange={onInternalOpenChange}
|
2023-10-23 17:16:41 +08:00
|
|
|
overlay={
|
|
|
|
title || content ? <Overlay prefixCls={prefixCls} title={title} content={content} /> : null
|
|
|
|
}
|
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'zoom-big', otherProps.transitionName)}
|
|
|
|
data-popover-inject
|
2024-03-22 10:33:56 +08:00
|
|
|
>
|
|
|
|
{cloneElement(children, {
|
2024-06-15 19:44:35 +08:00
|
|
|
onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>) => {
|
2024-03-22 10:33:56 +08:00
|
|
|
if (React.isValidElement(children)) {
|
|
|
|
children?.props.onKeyDown?.(e);
|
|
|
|
}
|
|
|
|
onKeyDown(e);
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
</Tooltip>,
|
2022-09-30 10:17:49 +08:00
|
|
|
);
|
2024-04-09 16:49:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
type CompoundedComponent = typeof InternalPopover & {
|
2022-07-14 12:06:18 +08:00
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2024-04-09 16:49:47 +08:00
|
|
|
const Popover = InternalPopover as CompoundedComponent;
|
|
|
|
|
|
|
|
Popover._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Popover.displayName = 'Popover';
|
|
|
|
}
|
2020-04-27 11:59:05 +08:00
|
|
|
|
|
|
|
export default Popover;
|