Skip to content
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

Merged
merged 5 commits into from
Sep 17, 2024

Conversation

sus-yoshikane-t
Copy link
Contributor

@sus-yoshikane-t sus-yoshikane-t commented Sep 17, 2024

Changes

  • Since TouchEvent.touches includes touches on areas other than the slider, I changed to use targetTouches.
  • Because if you start touching on the slider, touch events will be generated even if you drag outside the slider, I changed the target for registering the event listener to the slider (i.e., Event.currentTarget).

Summary by CodeRabbit

  • 新功能
    • 改进了触摸事件的处理,增强了拖放功能的准确性和可靠性,特别是在触摸设备上。
  • 测试
    • 更新了触摸交互的事件模拟,确保测试用例准确反映触摸事件的行为。

Copy link

vercel bot commented Sep 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
slider ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 17, 2024 5:20am

Copy link

coderabbitai bot commented Sep 17, 2024

Walkthrough

此次更改主要集中在改进触摸事件的处理,特别是在拖动功能中。代码更新了触摸事件的访问方式,从使用 e.touches[0] 改为 e.targetTouches[0],确保引用正确的触摸点。此外,事件监听器的清理方式也进行了优化,确保监听器从特定事件目标中移除,以防止潜在的内存泄漏和不当行为。这些更改增强了拖放功能在触摸设备上的可靠性。

Changes

文件 更改摘要
src/hooks/useDrag.ts 更新触摸事件处理,改用 e.targetTouches[0],优化事件监听器的清理,新增 touchEventTargetRef
tests/Range.test.tsx 调整触摸事件的模拟,使用 targetTouches,确保事件正确派发到特定元素。

Assessment against linked issues

Objective Addressed Explanation
当触摸滑块时,滑块应随第二个触摸移动 (#1036)

🐇 在滑动中跳跃,轻触即动,
触摸的舞蹈,滑块随风,
事件精准,听从指令,
快乐的代码,兔子欢腾!
🥕✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 02fe81f and 59f5bb8.

Files selected for processing (1)
  • src/hooks/useDrag.ts (5 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/hooks/useDrag.ts

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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai generate interesting stats about this repository and render them as a table.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.
Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI o1 for code reviews: OpenAI's new o1 model is being tested for generating code suggestions in code reviews.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

Copy link

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between 0b1f05b and 02fe81f.

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 添加触摸事件监听器

在触摸开始时,为目标元素添加 touchendtouchmove 事件监听器,确保在触摸移动和结束时正确地处理拖动逻辑。


217-217: 保存触摸事件目标到 touchEventTargetRef.current

e.currentTarget 保存到 touchEventTargetRef.current,以便在后续清理阶段正确地移除绑定的事件监听器。

tests/Range.test.tsx (2)

83-85: 正确使用targetTouches改进了触摸事件的测试模拟。

这确保了触摸事件仅针对滑块元素,提高了测试的准确性。


89-94: touchMove事件中正确使用targetTouches

这使得触摸移动事件更加精确地定位到滑块元素,符合预期行为。

Comment on lines 199 to 200
event.currentTarget.removeEventListener('touchend', onMouseUp);
event.currentTarget.removeEventListener('touchmove', onMouseMove);
Copy link

@coderabbitai coderabbitai bot Sep 17, 2024

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.

Suggested change
event.currentTarget.removeEventListener('touchend', onMouseUp);
event.currentTarget.removeEventListener('touchmove', onMouseMove);
if (touchEventTargetRef.current) {
touchEventTargetRef.current.removeEventListener('touchend', onMouseUp);
touchEventTargetRef.current.removeEventListener('touchmove', onMouseMove);
}

Copy link
Member

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?

Copy link

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 可能不可靠或指向不同的元素。建议采用此修改以提高代码的可靠性。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed it. 59f5bb8

Copy link

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!

Copy link

codecov bot commented Sep 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.39%. Comparing base (0b1f05b) to head (59f5bb8).
Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

@afc163 afc163 merged commit 622c6b6 into react-component:master Sep 17, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When touch the slider while touching another area, the slider will move with the first touch
2 participants