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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/hooks/useDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { OffsetValues } from './useOffset';
const REMOVE_DIST = 130;

function getPosition(e: React.MouseEvent | React.TouchEvent | MouseEvent | TouchEvent) {
const obj = 'touches' in e ? e.touches[0] : e;
const obj = 'targetTouches' in e ? e.targetTouches[0] : e;

return { pageX: obj.pageX, pageY: obj.pageY };
}
Expand Down Expand Up @@ -40,6 +40,7 @@ function useDrag(

const mouseMoveEventRef = React.useRef<(event: MouseEvent) => void>(null);
const mouseUpEventRef = React.useRef<(event: MouseEvent) => void>(null);
const touchEventTargetRef = React.useRef<EventTarget>(null);

const { onDragStart, onDragChange } = React.useContext(UnstableContext);

Expand All @@ -54,8 +55,10 @@ function useDrag(
() => () => {
document.removeEventListener('mousemove', mouseMoveEventRef.current);
document.removeEventListener('mouseup', mouseUpEventRef.current);
document.removeEventListener('touchmove', mouseMoveEventRef.current);
document.removeEventListener('touchend', mouseUpEventRef.current);
if (touchEventTargetRef.current) {
touchEventTargetRef.current.removeEventListener('touchmove', mouseMoveEventRef.current);
touchEventTargetRef.current.removeEventListener('touchend', mouseUpEventRef.current);
}
},
[],
);
Expand Down Expand Up @@ -193,10 +196,11 @@ function useDrag(

document.removeEventListener('mouseup', onMouseUp);
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('touchend', onMouseUp);
document.removeEventListener('touchmove', onMouseMove);
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!

mouseMoveEventRef.current = null;
mouseUpEventRef.current = null;
touchEventTargetRef.current = null;

finishChange(deleteMark);

Expand All @@ -206,10 +210,11 @@ function useDrag(

document.addEventListener('mouseup', onMouseUp);
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('touchend', onMouseUp);
document.addEventListener('touchmove', onMouseMove);
e.currentTarget.addEventListener('touchend', onMouseUp);
e.currentTarget.addEventListener('touchmove', onMouseMove);
mouseMoveEventRef.current = onMouseMove;
mouseUpEventRef.current = onMouseUp;
touchEventTargetRef.current = e.currentTarget;
};

// Only return cache value when it mapping with rawValues
Expand Down
10 changes: 6 additions & 4 deletions tests/Range.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,18 @@ describe('Range', () => {
) {
const touchStart = createEvent.touchStart(container.getElementsByClassName(element)[0], {
touches: [{}],
targetTouches: [{}],
});
(touchStart as any).touches[0].pageX = start;
(touchStart as any).targetTouches[0].pageX = start;
fireEvent(container.getElementsByClassName(element)[0], touchStart);

// Drag
const touchMove = createEvent.touchMove(document, {
const touchMove = createEvent.touchMove(container.getElementsByClassName(element)[0], {
touches: [{}],
targetTouches: [{}],
});
(touchMove as any).touches[0].pageX = end;
fireEvent(document, touchMove);
(touchMove as any).targetTouches[0].pageX = end;
fireEvent(container.getElementsByClassName(element)[0], touchMove);
}

it('should render Range with correct DOM structure', () => {
Expand Down
Loading