Skip to content

Commit

Permalink
fix stopPropagation in click event handle not working
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jul 11, 2022
1 parent a5cba25 commit a2af02d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions packages/nextjs/components/src/_util/hooks/useTaroBaseEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface UseTaroBaseEventsReturn extends TaroBaseAttributes {

onMouseLeave: React.MouseEventHandler
onTouchCancel: React.TouchEventHandler

onClick: React.MouseEventHandler
}

function useTaroBaseEvents({
Expand All @@ -47,7 +49,7 @@ function useTaroBaseEvents({

const touchHasMoved = useCallback((reactEvent: React.TouchEvent | React.MouseEvent) => {
const start = 'targetTouches' in reactEvent ? reactEvent.targetTouches[0] : reactEvent
const end = touchStartInfo.current
const end = touchStartInfo.current!

return Math.sqrt(Math.pow(end.screenX - start.screenX, 2) + Math.pow(end.screenY - start.screenY, 2))
}, [])
Expand Down Expand Up @@ -120,14 +122,7 @@ function useTaroBaseEvents({
const taroEvent = createTaroTouchEvent('touchend', reactEvent)
onTouchEnd(taroEvent)
}
const endTime = Date.now()
if (trackingTap.current && onClick && endTime - startTime.current < 350) {
const taroEvent = createTaroMouseEvent('tap', reactEvent)
setTimeout(() => {
onClick(taroEvent)
})
}
}, [onTouchEnd, onClick])
}, [onTouchEnd])

const handleCancel = useCallback((reactEvent: React.TouchEvent | React.MouseEvent): void => {
const eventType = EVENT_TYPE_MAPPINGS[reactEvent.type]
Expand All @@ -144,6 +139,14 @@ function useTaroBaseEvents({
}
}, [onTouchCancel])

const handleClick = useCallback((reactEvent: React.TouchEvent | React.MouseEvent): void => {
const endTime = Date.now()
if (trackingTap.current && onClick && endTime - startTime.current < 350) {
const taroEvent = createTaroMouseEvent('tap', reactEvent)
onClick(taroEvent)
}
}, [])

return {
onMouseDown: handleStart,
onTouchStart: handleStart,
Expand All @@ -157,6 +160,8 @@ function useTaroBaseEvents({
onMouseLeave: handleCancel,
onTouchCancel: handleCancel,

onClick: handleClick,

...rest
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/components/src/_util/taroEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function createTaroBlurEvent<T extends HTMLTextAreaElement | HTMLInputEle
currentTarget,
target,
detail: {
cursor: el.selectionEnd,
cursor: el.selectionEnd || 0,
value: el.value
},
timeStamp,
Expand Down

0 comments on commit a2af02d

Please sign in to comment.