-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[提案] useClickAway 的 target 传函数时,支持返回多个 dom target #1988
Comments
|
突然意识到,这样不就好了 const list = document.querySelectorAll('.list-item')
useClickAway(() => {
doSomething()
}, Array.from(list)) |
我觉得你说的对=.= 但我也不确定我是不是理解了Xekin97提案想表达的意思😂 |
那这个 issue 我先关了,有回复的话可以继续讨论 ~ |
这个是不是执行过程都不太一样了? 如果我没有 rerender 这个 hook 的外层组件,那 list 返回的结果就不会变化了 |
效果是一样的,你可以自己试一下 |
我测试了一下,完全不行 |
你是对的 |
https://codesandbox.io/s/zhi-chi-duo-ge-dom-dui-xiang-forked-fwirso?file=/App.tsx 前排蹲个答案😂 |
看了大半天代码,这里头的逻辑牵涉的地方太多,居然有点无从下手 :( |
这个需求看起来是一个悖论。 对于源码 hooks/packages/hooks/src/useClickAway/index.ts Lines 14 to 29 in 6db48a7
如果去支持动态target,你不得不在 handler 中获取动态的target + const realTarget = isFunction(target) ? target() : target;
const targets = Array.isArray(realTarget) ? realTarget : [realTarget]; 但是事件代理的节点的判定却依赖了target // 为此必须添加下面这一行,但是这样会让事件代理的根节点变得不可靠
const realTarget = isFunction(target) ? target() : target;
const documentOrShadow = getDocumentOrShadow(target); 例如,第一次的render,动态计算得到的target为ShadowRoot的子节点。但是第二次计算得到的target不再是ShadowRoot的子节点,此时事件依然绑定在ShadowRoot上(因为useClickAway所在的组件可能没有更新,进而useEffectWithTarget回调不会执行) 不过,动态target可能会很有用,如果可以去掉ShadowRoot的判断的话:p |
涉及的hook
useClickAway()
提案内容
当前行为: 传入
useClickAway()
的 targets 为函数时,只能返回一个 dom target修改为: 传入
useClickAway()
的 targets 为函数时,支持返回多个 dom target背景
需求场景,在使用 Antd Menu 组件做右键菜单时,由于子级的 SubMenu 和主 Menu 不在同一个 wrapper dom 中,导致在点击 SubMenuItem 前,先触发了 clickAway 回调执行的 hide 方法。无法点击命中 SubMenuItem
问题在于
所以希望条件支持返回多个 dom,例如:
涉及的源码
hooks/packages/hooks/src/useClickAway/index.ts
Lines 14 to 27 in e78af70
hooks/packages/hooks/src/utils/domTarget.ts
Lines 25 to 27 in e78af70
The text was updated successfully, but these errors were encountered: