-
-
Notifications
You must be signed in to change notification settings - Fork 778
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: When touch the slider while touching another area, the slider will move with the first touch #1037
fix: When touch the slider while touching another area, the slider will move with the first touch #1037
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Walkthrough此次更改主要集中在改进触摸事件的处理,特别是在拖动功能中。代码更新了触摸事件的访问方式,从使用 Changes
Assessment against linked issues
Recent review detailsConfiguration used: CodeRabbit UI Files selected for processing (1)
Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- src/hooks/useDrag.ts (5 hunks)
- tests/Range.test.tsx (1 hunks)
Additional comments not posted (8)
src/hooks/useDrag.ts (6)
11-11
: 使用e.targetTouches[0]
获取正确的触摸点此修改确保只处理滑块本身的触摸事件,解决了在同时触摸其他区域时滑块移动不正确的问题。
43-43
: 引入touchEventTargetRef
以跟踪触摸事件目标添加了
touchEventTargetRef
来跟踪触摸事件的目标元素,确保事件监听器被正确地添加和移除,避免潜在的内存泄漏或错误行为。
58-61
: 在清理阶段从touchEventTargetRef.current
移除触摸事件监听器在组件卸载时,确保正确地移除绑定在触摸目标元素上的事件监听器,防止内存泄漏和意外行为。
203-203
: 在触摸结束后重置touchEventTargetRef.current
在
onMouseUp
中将touchEventTargetRef.current
重置为null
,有助于避免保留不必要的引用,防止潜在的内存泄漏。
213-214
: 为e.currentTarget
添加触摸事件监听器在触摸开始时,为目标元素添加
touchend
和touchmove
事件监听器,确保在触摸移动和结束时正确地处理拖动逻辑。
217-217
: 保存触摸事件目标到touchEventTargetRef.current
将
e.currentTarget
保存到touchEventTargetRef.current
,以便在后续清理阶段正确地移除绑定的事件监听器。tests/Range.test.tsx (2)
83-85
: 正确使用targetTouches
改进了触摸事件的测试模拟。这确保了触摸事件仅针对滑块元素,提高了测试的准确性。
89-94
: 在touchMove
事件中正确使用targetTouches
。这使得触摸移动事件更加精确地定位到滑块元素,符合预期行为。
src/hooks/useDrag.ts
Outdated
event.currentTarget.removeEventListener('touchend', onMouseUp); | ||
event.currentTarget.removeEventListener('touchmove', onMouseMove); |
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.
建议使用 touchEventTargetRef.current
来移除事件监听器
在 onMouseUp
函数中,使用 event.currentTarget
来移除事件监听器可能不可靠,因为 event.currentTarget
可能在不同情况下指向不同的元素。为了确保从正确的元素移除监听器,建议使用之前保存的 touchEventTargetRef.current
。
建议修改如下:
- event.currentTarget.removeEventListener('touchend', onMouseUp);
- event.currentTarget.removeEventListener('touchmove', onMouseMove);
+ if (touchEventTargetRef.current) {
+ touchEventTargetRef.current.removeEventListener('touchend', onMouseUp);
+ touchEventTargetRef.current.removeEventListener('touchmove', onMouseMove);
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
event.currentTarget.removeEventListener('touchend', onMouseUp); | |
event.currentTarget.removeEventListener('touchmove', onMouseMove); | |
if (touchEventTargetRef.current) { | |
touchEventTargetRef.current.removeEventListener('touchend', onMouseUp); | |
touchEventTargetRef.current.removeEventListener('touchmove', onMouseMove); | |
} |
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.
Could you check this suggestion?
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.
@afc163,我已经检查了这个建议。使用 touchEventTargetRef.current
来移除事件监听器可以确保从正确的元素移除监听器,因为在 onMouseUp
中,event.currentTarget
可能不可靠或指向不同的元素。建议采用此修改以提高代码的可靠性。
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.
I fixed it. 59f5bb8
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.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1037 +/- ##
=======================================
Coverage 99.39% 99.39%
=======================================
Files 14 14
Lines 656 661 +5
Branches 197 199 +2
=======================================
+ Hits 652 657 +5
Misses 4 4 ☔ View full report in Codecov by Sentry. |
Changes
Summary by CodeRabbit