2
2
import act from './act' ;
3
3
import { ErrorWithStack } from './helpers/errors' ;
4
4
5
- const isTextInputComponent = ( element : ReactTestInstance ) => {
5
+ const isHostElement = ( element ? : ReactTestInstance ) => {
6
+ return typeof element ?. type === 'string' ;
7
+ } ;
8
+
9
+ const isTextInput = ( element ? : ReactTestInstance ) => {
6
10
// eslint-disable-next-line import/no-extraneous-dependencies
7
11
const { TextInput } = require ( 'react-native' ) ;
8
- return element . type === TextInput ;
12
+ return element ?. type === TextInput ;
13
+ } ;
14
+
15
+ const isTouchResponder = ( element ? : ReactTestInstance ) => {
16
+ if ( ! isHostElement ( element ) ) return false ;
17
+
18
+ return ! ! element ?. props . onStartShouldSetResponder || isTextInput ( element ) ;
19
+ } ;
20
+
21
+ const isEventEnabled = (
22
+ element ? : ReactTestInstance ,
23
+ touchResponder ? : ReactTestInstance
24
+ ) => {
25
+ if ( isTextInput ( element ) ) return element ?. props . editable !== false ;
26
+
27
+ return touchResponder ?. props . onStartShouldSetResponder ?. ( ) !== false ;
9
28
} ;
10
29
11
30
const findEventHandler = (
12
31
element : ReactTestInstance ,
13
32
eventName : string ,
14
33
callsite ? : any ,
15
- nearestHostDescendent ? : ReactTestInstance ,
34
+ nearestTouchResponder ? : ReactTestInstance ,
16
35
hasDescendandHandler ? : boolean
17
36
) => {
18
- const handler = getEventHandler ( element , eventName ) ;
19
- const hasHandler = handler != null || hasDescendandHandler ;
20
-
21
- const isHostComponent = typeof element . type === 'string' ;
22
- const hostElement = isHostComponent ? element : nearestHostDescendent ;
37
+ const touchResponder = isTouchResponder ( element )
38
+ ? element
39
+ : nearestTouchResponder ;
23
40
24
- const isEventEnabled = isTextInputComponent ( element )
25
- ? element . props . editable !== false
26
- : hostElement ?. props . onStartShouldSetResponder ?. ( ) !== false ;
27
-
28
- if ( handler && isEventEnabled ) return handler ;
41
+ const handler = getEventHandler ( element , eventName ) ;
42
+ if ( handler && isEventEnabled ( element , touchResponder ) ) return handler ;
29
43
30
44
// Do not bubble event to the root element
45
+ const hasHandler = handler != null || hasDescendandHandler ;
31
46
if ( element . parent === null || element . parent . parent === null ) {
32
47
if ( hasHandler ) {
33
48
return null ;
@@ -43,7 +58,7 @@ const findEventHandler = (
43
58
element . parent ,
44
59
eventName ,
45
60
callsite ,
46
- hostElement ,
61
+ touchResponder ,
47
62
hasHandler
48
63
) ;
49
64
} ;
0 commit comments