Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Popover can be closed by ESC when trigger is focus or click #47928

Merged
merged 19 commits into from Mar 22, 2024
24 changes: 23 additions & 1 deletion components/popover/index.tsx
Expand Up @@ -10,6 +10,8 @@
import PurePanel from './PurePanel';
// CSSINJS
import useStyle from './style';
import KeyCode from 'rc-util/lib/KeyCode';
import { cloneElement } from '../_util/reactNode';

export interface PopoverProps extends AbstractTooltipProps {
title?: React.ReactNode | RenderFunction;
Expand Down Expand Up @@ -37,6 +39,7 @@
overlayClassName,
placement = 'top',
trigger = 'hover',
children,
mouseEnterDelay = 0.1,
mouseLeaveDelay = 0.1,
overlayStyle = {},
Expand All @@ -49,6 +52,15 @@
const rootPrefixCls = getPrefixCls();

const overlayCls = classNames(overlayClassName, hashId, cssVarCls);
const [open, setOpen] = React.useState(props.open ?? props.visible);

const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.keyCode === KeyCode.ESC) {
console.log('esc');

Check warning on line 59 in components/popover/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

setOpen(false);
}
};

return wrapCSSVar(
<Tooltip
Expand All @@ -61,12 +73,22 @@
prefixCls={prefixCls}
overlayClassName={overlayCls}
ref={ref}
open={open}
overlay={
title || content ? <Overlay prefixCls={prefixCls} title={title} content={content} /> : null
}
transitionName={getTransitionName(rootPrefixCls, 'zoom-big', otherProps.transitionName)}
data-popover-inject
/>,
>
{cloneElement(children, {
onKeyDown: (e: React.KeyboardEvent<any>) => {
if (React.isValidElement(children)) {
children?.props.onKeyDown?.(e);
}
onKeyDown(e);
},
})}
</Tooltip>,
CooperHash marked this conversation as resolved.
Show resolved Hide resolved
);
}) as React.ForwardRefExoticComponent<
React.PropsWithoutRef<PopoverProps> & React.RefAttributes<unknown>
Expand Down