Skip to content

Commit

Permalink
Merge branch 'feature' into feature
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan authored Mar 22, 2024
2 parents 5368a72 + dee3901 commit f9a75fe
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
24 changes: 6 additions & 18 deletions components/popconfirm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import * as React from 'react';
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 omit from 'rc-util/lib/omit';

import type { RenderFunction } from '../_util/getRenderPropValue';
import { cloneElement } from '../_util/reactNode';
import type { ButtonProps, LegacyButtonType } from '../button/button';
import { ConfigContext } from '../config-provider';
import Popover from '../popover';
Expand Down Expand Up @@ -78,18 +76,15 @@ const Popconfirm = React.forwardRef<TooltipRef, PopconfirmProps>((props, ref) =>
props.onCancel?.call(this, e);
};

const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.keyCode === KeyCode.ESC && open) {
settingOpen(false, e);
}
};

const onInternalOpenChange = (value: boolean) => {
const onInternalOpenChange = (
value: boolean,
e?: React.MouseEvent<HTMLButtonElement> | React.KeyboardEvent<HTMLDivElement>,
) => {
const { disabled = false } = props;
if (disabled) {
return;
}
settingOpen(value);
settingOpen(value, e);
};

const prefixCls = getPrefixCls('popconfirm', customizePrefixCls);
Expand Down Expand Up @@ -119,14 +114,7 @@ const Popconfirm = React.forwardRef<TooltipRef, PopconfirmProps>((props, ref) =>
}
data-popover-inject
>
{cloneElement(children, {
onKeyDown: (e: React.KeyboardEvent<any>) => {
if (React.isValidElement(children)) {
children?.props.onKeyDown?.(e);
}
onKeyDown(e);
},
})}
{children}
</Popover>,
);
}) as React.ForwardRefExoticComponent<
Expand Down
21 changes: 21 additions & 0 deletions components/popover/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const { _InternalPanelDoNotUseOrYouWillBeFired: InternalPanelDoNotUseOrYouWillBe
describe('Popover', () => {
mountTest(Popover);

const eventObject = expect.objectContaining({
target: expect.anything(),
preventDefault: expect.any(Function),
});

it('should show overlay when trigger is clicked', () => {
const ref = React.createRef<TooltipRef>();
const { container } = render(
Expand Down Expand Up @@ -94,4 +99,20 @@ describe('Popover', () => {
render(<InternalPanelDoNotUseOrYouWillBeFired content={null} title={null} trigger="click" />);
}).not.toThrow();
});

it('should be closed by pressing ESC', () => {
const onOpenChange = jest.fn((_, e) => {
e?.persist?.();
});
const wrapper = render(
<Popover title="Title" trigger="click" onOpenChange={onOpenChange}>
<span>Delete</span>
</Popover>,
);
const triggerNode = wrapper.container.querySelectorAll('span')[0];
fireEvent.click(triggerNode);
expect(onOpenChange).toHaveBeenLastCalledWith(true, undefined);
fireEvent.keyDown(triggerNode, { key: 'Escape', keyCode: 27 });
expect(onOpenChange).toHaveBeenLastCalledWith(false, eventObject);
});
});
44 changes: 43 additions & 1 deletion components/popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ import PurePanel from './PurePanel';
// CSSINJS
import useStyle from './style';

import KeyCode from 'rc-util/lib/KeyCode';
import { cloneElement } from '../_util/reactNode';
import useMergedState from 'rc-util/lib/hooks/useMergedState';

export interface PopoverProps extends AbstractTooltipProps {
title?: React.ReactNode | RenderFunction;
content?: React.ReactNode | RenderFunction;
onOpenChange?: (
open: boolean,
e?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLDivElement>,
) => void;
}

interface OverlayProps {
Expand All @@ -37,8 +45,10 @@ const Popover = React.forwardRef<TooltipRef, PopoverProps>((props, ref) => {
overlayClassName,
placement = 'top',
trigger = 'hover',
children,
mouseEnterDelay = 0.1,
mouseLeaveDelay = 0.1,
onOpenChange,
overlayStyle = {},
...otherProps
} = props;
Expand All @@ -49,6 +59,27 @@ const Popover = React.forwardRef<TooltipRef, PopoverProps>((props, ref) => {
const rootPrefixCls = getPrefixCls();

const overlayCls = classNames(overlayClassName, hashId, cssVarCls);
const [open, setOpen] = useMergedState(false, {
value: props.open ?? props.visible,
});

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);
};

return wrapCSSVar(
<Tooltip
Expand All @@ -61,12 +92,23 @@ const Popover = React.forwardRef<TooltipRef, PopoverProps>((props, ref) => {
prefixCls={prefixCls}
overlayClassName={overlayCls}
ref={ref}
open={open}
onOpenChange={onInternalOpenChange}
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>,
);
}) as React.ForwardRefExoticComponent<
React.PropsWithoutRef<PopoverProps> & React.RefAttributes<unknown>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
"size-limit": [
{
"path": "./dist/antd.min.js",
"limit": "335 KiB"
"limit": "336 KiB"
},
{
"path": "./dist/antd-with-locales.min.js",
Expand Down

0 comments on commit f9a75fe

Please sign in to comment.