@@ -38,8 +38,8 @@ type PressState = {
38
38
39
39
function dispatchPressEvent (
40
40
context : EventResponderContext ,
41
- name : string ,
42
41
state : PressState ,
42
+ name : string ,
43
43
listener : ( e : Object ) = > void ,
44
44
) : void {
45
45
context. dispatchEvent ( name , listener , state . pressTarget , true ) ;
@@ -50,44 +50,50 @@ function dispatchPressInEvents(
50
50
props : Object ,
51
51
state : PressState ,
52
52
) : void {
53
+ function dispatchPressChangeEvent ( bool ) {
54
+ const pressChangeEventListener = ( ) => {
55
+ props . onPressChange ( bool ) ;
56
+ } ;
57
+ dispatchPressEvent ( context , state , 'presschange' , pressChangeEventListener ) ;
58
+ }
59
+
53
60
if ( props . onPressIn ) {
54
- context . dispatchEvent ( 'pressin' , props . onPressIn , state . pressTarget , true ) ;
61
+ dispatchPressEvent ( context , state , 'pressin' , props . onPressIn ) ;
55
62
}
56
63
if ( props . onPressChange ) {
57
- const pressChangeEventListener = ( ) => {
58
- props . onPressChange ( true ) ;
59
- } ;
60
- context . dispatchEvent (
61
- 'presschange' ,
62
- pressChangeEventListener ,
63
- state . pressTarget ,
64
- true ,
65
- ) ;
64
+ dispatchPressChangeEvent ( true ) ;
66
65
}
67
- if ( ! state . isLongPressed && ( props . onLongPress || props . onLongPressChange ) ) {
66
+ if ( ( props . onLongPress || props . onLongPressChange ) && ! state . isLongPressed ) {
68
67
const longPressDelay = props . longPressDelay || 1000 ;
68
+
69
69
state . longPressTimeout = setTimeout ( ( ) => {
70
70
state . isLongPressed = true ;
71
71
state . longPressTimeout = null ;
72
- if ( props . onLongPressChange ) {
73
- const longPressChangeEventListener = ( ) => {
74
- props . onLongPressChange ( true ) ;
75
- } ;
76
- context . dispatchEvent (
77
- 'longpresschange' ,
78
- longPressChangeEventListener ,
79
- state . pressTarget ,
80
- true ,
81
- ) ;
72
+
73
+ if ( props . onPressChange && props . longPressCancelsPress ) {
74
+ dispatchPressChangeEvent ( false ) ;
82
75
}
83
- if ( props . onLongPress && ! props . longPressCancelsPress ) {
76
+
77
+ if ( props . onLongPress ) {
84
78
const longPressEventListener = e => {
85
79
props . onLongPress ( e ) ;
86
80
if ( e . nativeEvent . defaultPrevented ) {
87
81
state . defaultPrevented = true ;
88
82
}
89
83
} ;
90
- dispatchPressEvent ( context , 'longpress' , state , longPressEventListener ) ;
84
+ dispatchPressEvent ( context , state , 'longpress' , longPressEventListener ) ;
85
+ }
86
+
87
+ if ( props . onLongPressChange ) {
88
+ const longPressChangeEventListener = ( ) => {
89
+ props . onLongPressChange ( true ) ;
90
+ } ;
91
+ dispatchPressEvent (
92
+ context ,
93
+ state ,
94
+ 'longpresschange' ,
95
+ longPressChangeEventListener ,
96
+ ) ;
91
97
}
92
98
} , longPressDelay ) ;
93
99
}
@@ -103,33 +109,23 @@ function dispatchPressOutEvents(
103
109
state . longPressTimeout = null ;
104
110
}
105
111
if ( props . onPressOut ) {
106
- context . dispatchEvent (
107
- 'pressout' ,
108
- props . onPressOut ,
109
- state . pressTarget ,
110
- true ,
111
- ) ;
112
+ dispatchPressEvent ( context , state , 'pressout' , props . onPressOut ) ;
112
113
}
113
- if ( props . onPressChange && ! props . longPressCancelsPress ) {
114
+ if ( props . onPressChange ) {
114
115
const pressChangeEventListener = ( ) = > {
115
116
props . onPressChange ( false ) ;
116
117
} ;
117
- context . dispatchEvent (
118
- 'presschange' ,
119
- pressChangeEventListener ,
120
- state . pressTarget ,
121
- true ,
122
- ) ;
118
+ dispatchPressEvent ( context , state , 'presschange' , pressChangeEventListener ) ;
123
119
}
124
- if ( state . isLongPressed && props . onLongPressChange ) {
120
+ if ( props . onLongPressChange && state . isLongPressed ) {
125
121
const longPressChangeEventListener = ( ) = > {
126
122
props . onLongPressChange ( false ) ;
127
123
} ;
128
- context . dispatchEvent (
124
+ dispatchPressEvent (
125
+ context ,
126
+ state ,
129
127
'longpresschange' ,
130
128
longPressChangeEventListener ,
131
- state . pressTarget ,
132
- true ,
133
129
) ;
134
130
}
135
131
}
@@ -188,7 +184,7 @@ const PressResponder = {
188
184
}
189
185
} ;
190
186
}
191
- dispatchPressEvent ( context , 'press' , state , keyPressEventListener ) ;
187
+ dispatchPressEvent ( context , state , 'press' , keyPressEventListener ) ;
192
188
break ;
193
189
}
194
190
case 'touchstart' :
@@ -233,7 +229,7 @@ const PressResponder = {
233
229
props . onPress &&
234
230
! ( state . isLongPressed && props . longPressCancelsPress )
235
231
) {
236
- dispatchPressEvent ( context , 'press' , state , props . onPress ) ;
232
+ dispatchPressEvent ( context , state , 'press' , props . onPress ) ;
237
233
}
238
234
}
239
235
}
@@ -296,7 +292,7 @@ const PressResponder = {
296
292
state . defaultPrevented = true ;
297
293
}
298
294
} ;
299
- dispatchPressEvent ( context , 'press' , state , pressEventListener ) ;
295
+ dispatchPressEvent ( context , state , 'press' , pressEventListener ) ;
300
296
}
301
297
}
302
298
}
0 commit comments