Skip to content
Open
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
27 changes: 17 additions & 10 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
Platform,
Pressable,
} from 'react-native';
import type { LayoutChangeEvent, ViewStyle } from 'react-native';
import type {
GestureResponderEvent,
LayoutChangeEvent,
ViewStyle,
} from 'react-native';

import { getTooltipPosition } from './utils';
import type { Measurement, TooltipChildProps } from './utils';
Expand Down Expand Up @@ -146,15 +150,18 @@ const Tooltip = ({
hideTooltipTimer.current.push(id);
}, [leaveTouchDelay]);

const handlePress = React.useCallback(() => {
if (touched.current) {
return null;
}
if (!isValidChild) return null;
const props = children.props as TooltipChildProps;
if (props.disabled) return null;
return props.onPress?.();
}, [children.props, isValidChild]);
const handlePress = React.useCallback(
(e: GestureResponderEvent) => {
if (touched.current) {
return null;
}
if (!isValidChild) return null;
const props = children.props as TooltipChildProps;
if (props.disabled) return null;
return props.onPress?.(e);
},
[children.props, isValidChild]
);

const handleHoverIn = React.useCallback(() => {
handleTouchStart();
Expand Down
9 changes: 7 additions & 2 deletions src/components/Tooltip/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Dimensions, StyleSheet } from 'react-native';
import type { LayoutRectangle, StyleProp, ViewStyle } from 'react-native';
import type {
GestureResponderEvent,
LayoutRectangle,
StyleProp,
ViewStyle,
} from 'react-native';

type ChildrenMeasurement = {
width: number;
Expand All @@ -19,7 +24,7 @@ export type Measurement = {
export type TooltipChildProps = {
style: StyleProp<ViewStyle>;
disabled?: boolean;
onPress?: () => void;
onPress?: (e: GestureResponderEvent) => void;
onHoverIn?: () => void;
onHoverOut?: () => void;
};
Expand Down
28 changes: 27 additions & 1 deletion src/components/__tests__/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Dimensions, Text, View, Platform } from 'react-native';
import type { ViewProps } from 'react-native';
import type { GestureResponderEvent, ViewProps } from 'react-native';

import {
afterAll,
Expand Down Expand Up @@ -28,9 +28,12 @@ jest.mock('../../utils/addEventListener', () => ({

const DummyComponent = ({
ref,
// `onPress` is consumed by Tooltip via `children.props`, not by the inner View
onPress: _onPress,
...props
}: ViewProps & {
ref?: React.RefObject<View | null>;
onPress?: (e: GestureResponderEvent) => void;
}) => (
<View {...props} ref={ref}>
<Text>dummy component</Text>
Expand Down Expand Up @@ -150,6 +153,29 @@ describe('Tooltip', () => {
});
});

describe('onPress', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('forwards the press event object to the child onPress', () => {
const onPress = jest.fn();
const {
wrapper: { getByText },
} = setup({
children: <DummyComponent onPress={onPress} />,
});

const nativeEvent = { nativeEvent: { locationX: 1, locationY: 2 } };
fireEvent(getTrigger(getByText), 'press', nativeEvent);

expect(onPress).toHaveBeenCalledTimes(1);
expect(onPress).toHaveBeenCalledWith(
expect.objectContaining(nativeEvent)
);
});
});

describe('pressOut', () => {
// eslint-disable-next-line jest/valid-title
it('hides the tooltip when the user stop pressing the component', async () => {
Expand Down