Skip to content

fix(tooltip): forward press event to wrapped child onPress#5010

Open
patrickwehbe wants to merge 1 commit into
callstack:mainfrom
patrickwehbe:fix/tooltip-onpress-event-5003
Open

fix(tooltip): forward press event to wrapped child onPress#5010
patrickwehbe wants to merge 1 commit into
callstack:mainfrom
patrickwehbe:fix/tooltip-onpress-event-5003

Conversation

@patrickwehbe

Copy link
Copy Markdown

Motivation

On native, the press event object is not propagated to the component wrapped by Tooltip. A child such as Button or IconButton with onPress={(e) => ...} receives undefined instead of the GestureResponderEvent.

The cause is in Tooltip. The component injects its own handlePress as the child's onPress through cloneElement. handlePress took no argument and called props.onPress?.() with nothing, so the event coming from Pressable was dropped before it reached the child's handler.

This change makes handlePress accept the GestureResponderEvent from Pressable and forward it to the child's onPress. The onPress type in TooltipChildProps (in Tooltip/utils.ts) was too narrow (() => void) and is widened to (e: GestureResponderEvent) => void to match the onPress signatures of Button and IconButton.

Note: PR #4994 also rewrites Tooltip/Tooltip.tsx (for the Material Design 3 update) and overlaps with this file. That PR targets a different issue (#4980) and does not address the event-forwarding problem, so there may be a small conflict to resolve depending on merge order.

Related issue

Closes #5003

A reproduction is provided in the issue (Expo Snack).

Test plan

  • yarn jest Tooltip

A test was added to the existing mobile suite in src/components/__tests__/Tooltip.test.tsx. It renders a tooltip-wrapped component with an onPress spy, fires a press with a nativeEvent, and asserts the spy is called with that event. The test fails on the previous code (the spy is called with no arguments) and passes with this change.

Locally verified: yarn typescript and yarn jest Tooltip both pass.

The Tooltip injects its own handlePress as the wrapped child's onPress
via cloneElement. handlePress took no argument and called
props.onPress?.() with nothing, so a child such as Button or IconButton
with onPress={(e) => ...} received undefined for the press event on
native.

handlePress now accepts the GestureResponderEvent from Pressable and
forwards it to the child's onPress. The onPress type in TooltipChildProps
is widened to (e: GestureResponderEvent) => void to match the Button and
IconButton signatures.

Closes callstack#5003
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tooltip: onPress event object is not propagated to parent component on native

1 participant