Inline createPanResponder (#1483)

zio/stable
dan 2023-09-20 03:46:59 +01:00 committed by GitHub
parent 1af8e83d53
commit 8b8fba7284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 53 deletions

View File

@ -1,4 +1,3 @@
/* eslint-disable react-hooks/exhaustive-deps */
/** /**
* Copyright (c) JOB TODAY S.A. and its affiliates. * Copyright (c) JOB TODAY S.A. and its affiliates.
* *
@ -7,19 +6,19 @@
* *
*/ */
import {useMemo, useEffect} from 'react' import {useEffect} from 'react'
import { import {
Animated, Animated,
Dimensions, Dimensions,
GestureResponderEvent, GestureResponderEvent,
GestureResponderHandlers, GestureResponderHandlers,
NativeTouchEvent, NativeTouchEvent,
PanResponder,
PanResponderGestureState, PanResponderGestureState,
} from 'react-native' } from 'react-native'
import {Position} from '../@types' import {Position} from '../@types'
import { import {
createPanResponder,
getDistanceBetweenTouches, getDistanceBetweenTouches,
getImageTranslate, getImageTranslate,
getImageDimensionsByTranslate, getImageDimensionsByTranslate,
@ -160,8 +159,12 @@ const usePanResponder = ({
longPressHandlerRef && clearTimeout(longPressHandlerRef) longPressHandlerRef && clearTimeout(longPressHandlerRef)
} }
const handlers = { const panResponder = PanResponder.create({
onGrant: ( onStartShouldSetPanResponder: () => true,
onStartShouldSetPanResponderCapture: () => true,
onMoveShouldSetPanResponder: () => true,
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderGrant: (
_: GestureResponderEvent, _: GestureResponderEvent,
gestureState: PanResponderGestureState, gestureState: PanResponderGestureState,
) => { ) => {
@ -173,7 +176,7 @@ const usePanResponder = ({
longPressHandlerRef = setTimeout(onLongPress, delayLongPress) longPressHandlerRef = setTimeout(onLongPress, delayLongPress)
}, },
onStart: ( onPanResponderStart: (
event: GestureResponderEvent, event: GestureResponderEvent,
gestureState: PanResponderGestureState, gestureState: PanResponderGestureState,
) => { ) => {
@ -234,7 +237,7 @@ const usePanResponder = ({
lastTapTS = Date.now() lastTapTS = Date.now()
} }
}, },
onMove: ( onPanResponderMove: (
event: GestureResponderEvent, event: GestureResponderEvent,
gestureState: PanResponderGestureState, gestureState: PanResponderGestureState,
) => { ) => {
@ -356,7 +359,7 @@ const usePanResponder = ({
tmpTranslate = {x: nextTranslateX, y: nextTranslateY} tmpTranslate = {x: nextTranslateX, y: nextTranslateY}
} }
}, },
onRelease: () => { onPanResponderRelease: () => {
cancelLongPressHandle() cancelLongPressHandle()
if (isDoubleTapPerformed) { if (isDoubleTapPerformed) {
@ -418,9 +421,9 @@ const usePanResponder = ({
tmpTranslate = null tmpTranslate = null
} }
}, },
} onPanResponderTerminationRequest: () => false,
onShouldBlockNativeResponder: () => false,
const panResponder = useMemo(() => createPanResponder(handlers), [handlers]) })
return [panResponder.panHandlers, scaleValue, translateValue] return [panResponder.panHandlers, scaleValue, translateValue]
} }

View File

@ -6,14 +6,7 @@
* *
*/ */
import { import {Animated, NativeTouchEvent} from 'react-native'
Animated,
GestureResponderEvent,
PanResponder,
PanResponderGestureState,
PanResponderInstance,
NativeTouchEvent,
} from 'react-native'
import {Dimensions, Position} from './@types' import {Dimensions, Position} from './@types'
type CacheStorageItem = {key: string; value: any} type CacheStorageItem = {key: string; value: any}
@ -131,40 +124,6 @@ export const getImageTranslateForScale = (
return getImageTranslate(targetImageDimensions, screen) return getImageTranslate(targetImageDimensions, screen)
} }
type HandlerType = (
event: GestureResponderEvent,
state: PanResponderGestureState,
) => void
type PanResponderProps = {
onGrant: HandlerType
onStart?: HandlerType
onMove: HandlerType
onRelease?: HandlerType
onTerminate?: HandlerType
}
export const createPanResponder = ({
onGrant,
onStart,
onMove,
onRelease,
onTerminate,
}: PanResponderProps): PanResponderInstance =>
PanResponder.create({
onStartShouldSetPanResponder: () => true,
onStartShouldSetPanResponderCapture: () => true,
onMoveShouldSetPanResponder: () => true,
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderGrant: onGrant,
onPanResponderStart: onStart,
onPanResponderMove: onMove,
onPanResponderRelease: onRelease,
onPanResponderTerminate: onTerminate,
onPanResponderTerminationRequest: () => false,
onShouldBlockNativeResponder: () => false,
})
export const getDistanceBetweenTouches = ( export const getDistanceBetweenTouches = (
touches: NativeTouchEvent[], touches: NativeTouchEvent[],
): number => { ): number => {