-
-
Notifications
You must be signed in to change notification settings - Fork 50.1k
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
5.x Popover 的 onOpenChange 触发的时机存在问题 #42924
Comments
@linxianxi 考虑使用 受控 + useClickOutside 解决呢 import { MutableRefObject, useCallback, useEffect, useRef } from 'react';
const defaultEvent = 'click';
type RefType = Element | (() => Element | null) | null | undefined;
const useClickOutside = <T extends Element>(
dom: RefType | RefType[],
callback: (event: Event) => void,
eventName: string = defaultEvent,
listenerOptions?: boolean | AddEventListenerOptions
) => {
const callbackRef = useRef(callback);
const targetRefs = useRef<RefType[]>([]);
const element = useRef<T>();
useEffect(() => {
callbackRef.current = callback;
}, [callback]);
useEffect(() => {
targetRefs.current = Array.isArray(dom) ? dom : [dom];
}, [dom]);
const eventHandler = useCallback(
(e: Event) => {
const isOutside = targetRefs.current.every((item) => {
const el = typeof item === 'function' ? item() : item || element.current;
return !el?.contains(e.target as Node);
});
if (isOutside) {
callbackRef.current(e);
}
},
[element.current, targetRefs.current]
);
useEffect(() => {
document.addEventListener(eventName, eventHandler, listenerOptions);
return () => {
document.removeEventListener(eventName, eventHandler, listenerOptions);
};
}, [eventName, listenerOptions, eventHandler]);
return element as MutableRefObject<T>;
};
export default useClickOutside; |
怎么实现不谈,这个 issue 谈的是 bug。 |
@BoyYangzai 你可以在 codesandbox 尝试用 useClickAway 实现一下 |
@linxianxi 晚点下班看看 |
@linxianxi @vaynevayne 简单实现了一下 拿不到PopOver和Modal的真实dom的ref有点难顶 |
不一定要 ref,document.querySelector 就能获取。还是重点关注 5.x 和 4.x 行为不同的问题吧 |
内部处理onBlur的代码在哪里啊?能贴个连接吗 分析一波 |
v4 的 Popover 底层监听的是 |
v5 现在实现这种需求最简单的方式应该是这样吧,但是感觉也不是很优雅。 |
popover 换成 select 试试 |
一样的 https://codesandbox.io/s/kuo-zhan-cai-dan-antd-5-6-0-forked-jrhw9d |
@zombieJ 也没比较好的办法了吧,要不要在文档里加个 demo? |
https://stackblitz.com/edit/react-djm2de?file=demo.tsx 弹窗打开时, 点一下这个例子里的modal , popover 就就关闭了, 你那个例子里的阻止关闭, 关键代码是什么? |
Popover 组件和 Select 组件没有关系。Select 底层没用 Popover,Popover 底层也没用 Select。 |
知道了, 没关系, 后面我不是找到原因还在 rc-select 里提了 pr 嘛, 怎么还看这个啊 |
也是这个思路,effect 延迟掉 visible。 |
因 rc-trigger 内部 useWinClick 改成捕获了,所以现在不需要 delay 来处理了。 |
Reproduction link
4.x 是可以的
Steps to reproduce
目的是为了在 Popover 内打开 Modal 时,Popover 不关闭
1、打开 Popover
2、打开 Modal
3、关闭 Modal
What is expected?
Popover 是打开的。且在 Modal 打开的时候点击任意位置不应触发 onOpenChange。
What is actually happening?
Popover 关闭了。且在 Modal 打开的时候点击任意位置都会触发 onOpenChange。
discussion #42863 (reply in thread)
The text was updated successfully, but these errors were encountered: