Skip to content

Commit 4d6f3b6

Browse files
refactor TouchableOpacity forwarding event on onPress and onLongPress (#2462)
* refactor TouchableOpacity forwarding event on onPress and onLongPress * improve typing * formatting + edit API --------- Co-authored-by: Lidor Dafna <[email protected]>
1 parent e0ac8bf commit 4d6f3b6

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/components/touchableOpacity/index.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import _ from 'lodash';
2-
import React, {BaseSyntheticEvent, PureComponent} from 'react';
3-
import {TouchableOpacity as RNTouchableOpacity, TouchableOpacityProps as RNTouchableOpacityProps} from 'react-native';
2+
import React, {PureComponent} from 'react';
3+
import {
4+
GestureResponderEvent,
5+
TouchableOpacity as RNTouchableOpacity,
6+
TouchableOpacityProps as RNTouchableOpacityProps
7+
} from 'react-native';
48
import {
59
asBaseComponent,
610
forwardRef,
@@ -43,10 +47,16 @@ export interface TouchableOpacityProps
4347
*/
4448
customValue?: any;
4549
style?: ViewProps['style'];
46-
onPress?: (props?: TouchableOpacityProps | any) => void;
47-
onPressIn?: (props?: TouchableOpacityProps | any) => void | RNTouchableOpacityProps['onPressIn'];
48-
onPressOut?: (props?: TouchableOpacityProps | any) => void | RNTouchableOpacityProps['onPressOut'];
49-
onLongPress?: (props?: TouchableOpacityProps | any) => void | RNTouchableOpacityProps['onLongPress'];
50+
onPress?: (props?: (TouchableOpacityProps & {event: GestureResponderEvent}) | any) => void;
51+
onPressIn?: (
52+
props?: TouchableOpacityProps | GestureResponderEvent | any
53+
) => void | RNTouchableOpacityProps['onPressIn'];
54+
onPressOut?: (
55+
props?: TouchableOpacityProps | GestureResponderEvent | any
56+
) => void | RNTouchableOpacityProps['onPressOut'];
57+
onLongPress?: (
58+
props?: (TouchableOpacityProps & {event: GestureResponderEvent}) | any
59+
) => void | RNTouchableOpacityProps['onLongPress'];
5060
}
5161

5262
type Props = BaseComponentInjectedProps & ForwardRefInjectedProps & TouchableOpacityProps;
@@ -155,12 +165,12 @@ class TouchableOpacity extends PureComponent<Props, {active: boolean}> {
155165
);
156166
}
157167

158-
onPress(event: BaseSyntheticEvent) {
159-
this.props.onPress?.({...this.props, ...event});
168+
onPress(event: GestureResponderEvent) {
169+
this.props.onPress?.({...this.props, event});
160170
}
161171

162-
onLongPress = (event: BaseSyntheticEvent) => {
163-
this.props.onLongPress?.({...this.props, ...event});
172+
onLongPress = (event: GestureResponderEvent) => {
173+
this.props.onLongPress?.({...this.props, event});
164174
};
165175
}
166176

src/components/touchableOpacity/touchableOpacity.api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"description": "Custom value of any type to pass on to TouchableOpacity and receive back in onPress callback"
2828
},
2929
{"name": "style", "type": "ViewStyle", "description": "Custom style"},
30-
{"name": "onPress", "type": "(props) => void", "description": "On press callback"}
30+
{"name": "onPress", "type": "(props?: TouchableOpacityProps & {event: GestureResponderEvent} | any) => void", "description": "On press callback"}
3131
],
3232
"snippet": [
3333
"<TouchableOpacity onPress={() => console.log('pressed')$1}/>"

0 commit comments

Comments
 (0)