Skip to content

Commit d1cbdc9

Browse files
authored
fix: onPress not working after disabling drag (#307)
## Description Fixes #306 ## Example recordings | Before | After | |-|-| | <video src="https://github.com/user-attachments/assets/2957ce00-a85d-497a-9ea9-dee859a302b0" /> | <video src="https://github.com/user-attachments/assets/bbdd5de9-6fb9-4a7a-aa9b-0cda3a688255" /> | <details> <summary>Code snippet</summary> ```tsx import { useMemo, useState } from 'react'; import { Button } from 'react-native'; import Sortable from 'react-native-sortables'; export default function BadGrid() { const [isSelecting, setIsSelecting] = useState(false); const data = useMemo(() => new Array(20).fill(0).map((_, i) => i), []); console.log({ isSelecting }); const renderItem = () => ( <Sortable.TouchableOpacity style={{ backgroundColor: 'red', height: 64, width: 64 }} onPress={() => console.log('hello')} /> ); return ( <> <Sortable.Grid columns={3} data={data} renderItem={renderItem} sortEnabled={!isSelecting} /> <Button title='Press' onPress={() => setIsSelecting(!isSelecting)} /> </> ); } ``` </details>
1 parent ecfe289 commit d1cbdc9

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed

packages/react-native-sortables/src/components/shared/createSortableTouchable.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { type ComponentType, useCallback } from 'react';
2-
import { useAnimatedReaction, useSharedValue } from 'react-native-reanimated';
32

43
import { useCommonValuesContext } from '../../providers';
54
import type { AnyFunction, Maybe } from '../../types';
@@ -46,32 +45,15 @@ export default function createSortableTouchable<P extends AnyPressHandlers>(
4645
): ComponentType<P> {
4746
function Wrapper({ onPress, ...rest }: P) {
4847
const { activationState } = useCommonValuesContext();
49-
const isCancelled = useSharedValue(false);
50-
51-
useAnimatedReaction(
52-
() => ({
53-
dragState: activationState.value
54-
}),
55-
({ dragState }) => {
56-
// Cancels when the item is active
57-
if (dragState === DragActivationState.ACTIVE) {
58-
isCancelled.value = true;
59-
}
60-
// Resets state when the item is touched again
61-
else if (dragState === DragActivationState.TOUCHED) {
62-
isCancelled.value = false;
63-
}
64-
}
65-
);
6648

6749
const handlePress = useCallback(
6850
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6951
(...args: Array<any>) => {
70-
if (isCancelled.value) return;
52+
if (activationState.value !== DragActivationState.INACTIVE) return;
7153
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
7254
onPress?.(...args);
7355
},
74-
[isCancelled, onPress]
56+
[activationState, onPress]
7557
);
7658

7759
return <Component {...(rest as P)} onPress={handlePress} />;

0 commit comments

Comments
 (0)