Skip to content

Commit

Permalink
feat(popover): add document click case (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
berber1016 committed Nov 8, 2021
1 parent 7d9a270 commit 13a9c87
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/popover/Popover.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useMemo, useCallback, useEffect, useRef } from 'react';
import React, { useState, useMemo, useCallback, useEffect, useRef, useLayoutEffect } from 'react';
import classNames from 'classnames';
import { debounce, isUndefined } from 'lodash';
import { usePopper } from 'react-popper';
Expand Down Expand Up @@ -26,7 +26,6 @@ const Popover = (props: PopoverProps) => {
const prefixCls = usePrefixCls('popover-new', customPrefixCls);
const [visible, setVisible] = useState(defaultVisible);
const overContentRef = useRef<boolean>(false);

const referenceElement = useRef<HTMLDivElement | null>(null);
const popperElement = useRef<HTMLDivElement | null>(null);
const arrowElement = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -65,6 +64,28 @@ const Popover = (props: PopoverProps) => {
},
[onVisibleChange, enterVisible, enterable]
);
const onDocumentClick = useCallback(
(event: MouseEvent) => {
const { target } = event;
if (!referenceElement.current?.contains(target as Node) && !popperElement.current?.contains(target as Node)) {
updateVisible(false);
}
},
[updateVisible]
);

useEffect(() => {
if (!isUndefined(enterVisible)) {
setVisible(enterVisible);
}
}, [enterVisible]);

useLayoutEffect(() => {
document.addEventListener('mousedown', onDocumentClick);
return () => {
document.removeEventListener('mousedown', onDocumentClick);
};
}, [onDocumentClick]);

const isClickToShow = useMemo(() => trigger.indexOf('click') !== -1, [trigger]);

Expand Down Expand Up @@ -124,12 +145,6 @@ const Popover = (props: PopoverProps) => {
return roles;
}, [isClickToShow, isFocusToShow, isHoverToShow, onClick, onFocus, onBlur, onMouseEnter, onMouseLeave]);

useEffect(() => {
if (!isUndefined(enterVisible)) {
setVisible(enterVisible);
}
}, [enterVisible]);

return (
<>
<div className={`${prefixCls}__popcorn`} ref={referenceElement} {...divRoles}>
Expand Down
3 changes: 2 additions & 1 deletion src/popover/demos/Popover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const ControlTemplate: Story<PopoverProps> = (args) => {
const show = () => setVisible(true);
const hide = () => setVisible(false);
const onVisibleChange = (resetVisible: boolean) => {
setVisible(resetVisible);
console.log(resetVisible);
};

Expand Down Expand Up @@ -178,7 +179,7 @@ const ControlTemplate: Story<PopoverProps> = (args) => {
<Button onClick={hide2} style={{ marginRight: 50 }}>
Hide Popover
</Button>
<Popover {...args} visible={visible2} placement="top" onVisibleChange={onVisibleChange2}>
<Popover {...args} trigger="click" visible={visible2} placement="top" onVisibleChange={onVisibleChange2}>
<Input value="Show Popover Me!" />
</Popover>
</div>
Expand Down

1 comment on commit 13a9c87

@vercel
Copy link

@vercel vercel bot commented on 13a9c87 Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.