-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
fix(Popover): popover ref.current.show() invalid #6732
base: master
Are you sure you want to change the base?
Conversation
PR preview has been successfully built and deployed to https://antd-mobile-preview-pr-6732.surge.sh |
CI failed. Pls check |
@@ -196,6 +200,10 @@ export const Popover = forwardRef<PopoverRef, PopoverProps>((p, ref) => { | |||
useClickAway( | |||
() => { | |||
if (!props.trigger) return | |||
if (stateRef.current.isClickShow) { | |||
stateRef.current.isClickShow = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个实现方式有个问题,如果用户点击了一个阻止冒泡的元素且执行了ref.current.show()。会导致stateRef.current.isClickShow
为true的状态持续在Popover组件内,下次点击任意地方都不会隐藏。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我尝试在stateRef.current.isClickShow = true
后,加一个定时器及时修改为false,可以解决该问题。但我不太确定这样的解决方案是否有其他副作用。
useImperativeHandle(
ref,
() => ({
show: () => {
stateRef.current.isClickShow = true
setTimeout(() => {
stateRef.current.isClickShow = false
})
setVisible(true)
},
hide: () => setVisible(false),
visible,
}),
[visible]
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实会有这个问题 那不使用isClickShow了 直接改成
show: () => {
setTimeout(() => {
setVisible(true)
})
},
是不是更直接点
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实会有这个问题 那不使用isClickShow了 直接改成 show: () => { setTimeout(() => { setVisible(true) }) }, 是不是更直接点
直接使用定时器会造成break change。不满足之前的部分测试用例。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看之前好像没有写过关于通过ref打开popver的测试用例
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不好意思,我记错了..
Popover测试用例确实没有ref相关的,欢迎pr补充。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我想了下,在不改变组件现有代码结构的设计的前提下,可以通过以下几种方式:
show方法直接通过setTimeout延时执行setVisible
通过isClickShow来标识show方法调用并在setTimeout重置isClickShow的状态
show方法要求外部传递evnet参数来阻止冒泡
感觉都可以实现,但又感觉都不是最优解,大佬有什么好的建议吗
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
等确定方案了我把这块的测试用例补充一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个还有后续吗?
在ref.current.show()前使用e.stopPropagation(); 就不会执行useClickAway的回调。
…------------------ Original ------------------
From: wudajian ***@***.***>
Date: Fri,Aug 30,2024 9:23 AM
To: ant-design/ant-design-mobile ***@***.***>
Cc: Leisure ***@***.***>, Comment ***@***.***>
Subject: Re: [ant-design/ant-design-mobile] fix(Popover): popoverref.current.show() invalid (PR #6732)
@DaJianWu commented on this pull request.
In src/components/popover/popover.tsx:
> @@ -196,6 +200,10 @@ export const Popover = forwardRef<PopoverRef, PopoverProps>((p, ref) => { useClickAway( () => { if (!props.trigger) return + if (stateRef.current.isClickShow) { + stateRef.current.isClickShow = false
没太理解 会导致stateRef.current.isClickShow为true的状态持续在Popover组件内 我在useClickAway把isClickShow改为false了
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
#6731