Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/components/MapView/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {forwardRef, memo, useCallback, useEffect, useImperativeHandle, useMemo,
import {View} from 'react-native';
import Button from '@components/Button';
import * as Expensicons from '@components/Icon/Expensicons';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import Text from '@components/Text';
import useOnyx from '@hooks/useOnyx';
import useTheme from '@hooks/useTheme';
Expand All @@ -24,6 +23,7 @@ import Direction from './Direction';
import type {MapViewHandle, MapViewProps} from './MapViewTypes';
import PendingMapView from './PendingMapView';
import responder from './responder';
import ToggleDistanceUnitButton from './ToggleDistanceUnitButton';
import utils from './utils';

const MapView = forwardRef<MapViewHandle, MapViewProps>(
Expand Down Expand Up @@ -317,15 +317,15 @@ const MapView = forwardRef<MapViewHandle, MapViewProps>(
key="distance-label"
>
<View style={{zIndex: 1}}>
<PressableWithoutFeedback
<ToggleDistanceUnitButton
accessibilityRole={CONST.ROLE.BUTTON}
accessibilityLabel="distance-label"
onPress={toggleDistanceUnit}
>
<View style={[styles.distanceLabelWrapper]}>
<Text style={styles.distanceLabelText}> {distanceLabelText}</Text>
</View>
</PressableWithoutFeedback>
</ToggleDistanceUnitButton>
</View>
</MarkerView>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, {useRef} from 'react';
import type {GestureResponderEvent} from 'react-native';
import {PressableWithoutFeedback} from '@components/Pressable';
import type PressableProps from '@components/Pressable/GenericPressable/types';

/**
* This component is a workaround to resolve an issue for an Android issue where onPress does not trigger on Mapbox.MapView due to a PanResponder that captures touch events.
* Related issue: https://github.com/Expensify/App/issues/56499
*/
function ToggleDistanceUnitButton({onPress, children, ...rest}: PressableProps) {
const touchStartLocation = useRef<{x: number; y: number} | null>(null);

const handleTouchStart = (event: GestureResponderEvent) => {
touchStartLocation.current = {
x: event.nativeEvent.pageX,
y: event.nativeEvent.pageY,
};
};

const handleTouchMove = (event: GestureResponderEvent) => {
if (!touchStartLocation.current) {
return;
}
// Determine if the touch movement exceeds a small threshold (1 pixel in any direction)
// If movement is detected, treat it as a drag and reset touch tracking
const dx = Math.abs(event.nativeEvent.pageX - touchStartLocation.current.x);
const dy = Math.abs(event.nativeEvent.pageY - touchStartLocation.current.y);
if (dx > 1 || dy > 1) {
touchStartLocation.current = null;
}
};

const handleTouchEnd = () => {
if (!touchStartLocation.current) {
return;
}
onPress?.();
touchStartLocation.current = null;
};

return (
<PressableWithoutFeedback
onTouchStart={handleTouchStart}
onTouchMove={handleTouchMove}
onTouchEnd={handleTouchEnd}
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
>
{children}
</PressableWithoutFeedback>
);
}

ToggleDistanceUnitButton.displayName = 'ToggleDistanceUnitButton';

export default ToggleDistanceUnitButton;
4 changes: 4 additions & 0 deletions src/components/MapView/ToggleDistanceUnitButton/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';

const ToggleDistanceUnitButton = PressableWithoutFeedback;
export default ToggleDistanceUnitButton;
73 changes: 73 additions & 0 deletions tests/ui/ToggleDistanceUnitButtonTests.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {fireEvent, render, screen} from '@testing-library/react-native';
import React from 'react';
import ToggleDistanceUnitButton from '@components/MapView/ToggleDistanceUnitButton/index.android';
import type PressableProps from '@components/Pressable/GenericPressable/types';
import Text from '@components/Text';
import CONST from '@src/CONST';

const onPressMock = jest.fn();
describe('ToggleDistanceUnitButton', () => {
const renderButton = (props: PressableProps) =>
render(
<ToggleDistanceUnitButton
testID="pressable"
accessibilityLabel="fake-button"
accessibilityRole={CONST.ROLE.BUTTON}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
>
<Text>Click me</Text>
</ToggleDistanceUnitButton>,
);
beforeEach(() => {
jest.clearAllMocks();
});

test('should trigger onPress when tapped quickly', () => {
// Given the component is rendered
renderButton({onPress: onPressMock, accessibilityLabel: 'ToggleDistanceUnitButton'});
const pressable = screen.getByTestId('pressable');

// When touch starts on the button
fireEvent(pressable, 'touchStart', {
nativeEvent: {
pageX: 100,
pageY: 100,
},
});

// When touch end on the button
fireEvent(pressable, 'touchEnd');

// Then onPress should be called once
expect(onPressMock).toHaveBeenCalledTimes(1);
});

test('should not trigger onPress when dragged', () => {
// Given the component is rendered
renderButton({onPress: onPressMock, accessibilityLabel: 'ToggleDistanceUnitButton'});
const pressable = screen.getByTestId('pressable');

// When touch start on the button
fireEvent(pressable, 'touchStart', {
nativeEvent: {
pageX: 100,
pageY: 100,
},
});

// When the touch moves beyond the threshold (dragging)
fireEvent(pressable, 'touchMove', {
nativeEvent: {
pageX: 110,
pageY: 110,
},
});

// When touch end on the button
fireEvent(pressable, 'touchEnd');

// Then onPress should not be called
expect(onPressMock).not.toHaveBeenCalled();
});
});