Skip to content

refactor TouchableOpacity forwarding event on onPress and onLongPress #2462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 12, 2023
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
30 changes: 20 additions & 10 deletions src/components/touchableOpacity/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import _ from 'lodash';
import React, {BaseSyntheticEvent, PureComponent} from 'react';
import {TouchableOpacity as RNTouchableOpacity, TouchableOpacityProps as RNTouchableOpacityProps} from 'react-native';
import React, {PureComponent} from 'react';
import {
GestureResponderEvent,
TouchableOpacity as RNTouchableOpacity,
TouchableOpacityProps as RNTouchableOpacityProps
} from 'react-native';
import {
asBaseComponent,
forwardRef,
Expand Down Expand Up @@ -43,10 +47,16 @@ export interface TouchableOpacityProps
*/
customValue?: any;
style?: ViewProps['style'];
onPress?: (props?: TouchableOpacityProps | any) => void;
onPressIn?: (props?: TouchableOpacityProps | any) => void | RNTouchableOpacityProps['onPressIn'];
onPressOut?: (props?: TouchableOpacityProps | any) => void | RNTouchableOpacityProps['onPressOut'];
onLongPress?: (props?: TouchableOpacityProps | any) => void | RNTouchableOpacityProps['onLongPress'];
onPress?: (props?: (TouchableOpacityProps & {event: GestureResponderEvent}) | any) => void;
onPressIn?: (
props?: TouchableOpacityProps | GestureResponderEvent | any
) => void | RNTouchableOpacityProps['onPressIn'];
onPressOut?: (
props?: TouchableOpacityProps | GestureResponderEvent | any
) => void | RNTouchableOpacityProps['onPressOut'];
onLongPress?: (
props?: (TouchableOpacityProps & {event: GestureResponderEvent}) | any
) => void | RNTouchableOpacityProps['onLongPress'];
}

type Props = BaseComponentInjectedProps & ForwardRefInjectedProps & TouchableOpacityProps;
Expand Down Expand Up @@ -155,12 +165,12 @@ class TouchableOpacity extends PureComponent<Props, {active: boolean}> {
);
}

onPress(event: BaseSyntheticEvent) {
this.props.onPress?.({...this.props, ...event});
onPress(event: GestureResponderEvent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you changed it from BaseSyntheticEvent to GestureResponderEvent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the correct typing in React Native:

    onLongPress?: ((event: GestureResponderEvent) => void) | undefined;

    /**
     * Called when the touch is released,
     * but not if cancelled (e.g. by a scroll that steals the responder lock).
     */
    onPress?: ((event: GestureResponderEvent) => void) | undefined;

    onPressIn?: ((event: GestureResponderEvent) => void) | undefined;

    onPressOut?: ((event: GestureResponderEvent) => void) | undefined;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, please just verify it's the same type for web events (you can run the web demo with this change).
I thought you picked the BaseSyntheticEvent last time for that reason

this.props.onPress?.({...this.props, event});
}

onLongPress = (event: BaseSyntheticEvent) => {
this.props.onLongPress?.({...this.props, ...event});
onLongPress = (event: GestureResponderEvent) => {
this.props.onLongPress?.({...this.props, event});
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/touchableOpacity/touchableOpacity.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"description": "Custom value of any type to pass on to TouchableOpacity and receive back in onPress callback"
},
{"name": "style", "type": "ViewStyle", "description": "Custom style"},
{"name": "onPress", "type": "(props) => void", "description": "On press callback"}
{"name": "onPress", "type": "(props?: TouchableOpacityProps & {event: GestureResponderEvent} | any) => void", "description": "On press callback"}
],
"snippet": [
"<TouchableOpacity onPress={() => console.log('pressed')$1}/>"
Expand Down