4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
+ * @flow
7
8
* @format
8
9
*/
9
10
@@ -23,6 +24,9 @@ const View = require('View');
23
24
const keyMirror = require ( 'fbjs/lib/keyMirror' ) ;
24
25
const normalizeColor = require ( 'normalizeColor' ) ;
25
26
27
+ import type { PressEvent } from 'CoreEventTypes' ;
28
+ import type { EdgeInsetsProp } from 'EdgeInsetsPropType' ;
29
+
26
30
/**
27
31
* `Touchable`: Taps done right.
28
32
*
@@ -111,6 +115,7 @@ const normalizeColor = require('normalizeColor');
111
115
/**
112
116
* Touchable states.
113
117
*/
118
+
114
119
const States = keyMirror ( {
115
120
NOT_RESPONDER : null , // Not the responder
116
121
RESPONDER_INACTIVE_PRESS_IN : null , // Responder, inactive, in the `PressRect`
@@ -122,10 +127,33 @@ const States = keyMirror({
122
127
ERROR : null ,
123
128
} ) ;
124
129
125
- /**
130
+ type State =
131
+ | typeof States . NOT_RESPONDER
132
+ | typeof States . RESPONDER_INACTIVE_PRESS_IN
133
+ | typeof States . RESPONDER_INACTIVE_PRESS_OUT
134
+ | typeof States . RESPONDER_ACTIVE_PRESS_IN
135
+ | typeof States . RESPONDER_ACTIVE_PRESS_OUT
136
+ | typeof States . RESPONDER_ACTIVE_LONG_PRESS_IN
137
+ | typeof States . RESPONDER_ACTIVE_LONG_PRESS_OUT
138
+ | typeof States . ERROR ;
139
+
140
+ /*
126
141
* Quick lookup map for states that are considered to be "active"
127
142
*/
143
+
144
+ const baseStatesConditions = {
145
+ NOT_RESPONDER : false ,
146
+ RESPONDER_INACTIVE_PRESS_IN : false ,
147
+ RESPONDER_INACTIVE_PRESS_OUT : false ,
148
+ RESPONDER_ACTIVE_PRESS_IN : false ,
149
+ RESPONDER_ACTIVE_PRESS_OUT : false ,
150
+ RESPONDER_ACTIVE_LONG_PRESS_IN : false ,
151
+ RESPONDER_ACTIVE_LONG_PRESS_OUT : false ,
152
+ ERROR : false ,
153
+ } ;
154
+
128
155
const IsActive = {
156
+ ...baseStatesConditions ,
129
157
RESPONDER_ACTIVE_PRESS_OUT : true ,
130
158
RESPONDER_ACTIVE_PRESS_IN : true ,
131
159
} ;
@@ -135,12 +163,14 @@ const IsActive = {
135
163
* therefore eligible to result in a "selection" if the press stops.
136
164
*/
137
165
const IsPressingIn = {
166
+ ...baseStatesConditions ,
138
167
RESPONDER_INACTIVE_PRESS_IN : true ,
139
168
RESPONDER_ACTIVE_PRESS_IN : true ,
140
169
RESPONDER_ACTIVE_LONG_PRESS_IN : true ,
141
170
} ;
142
171
143
172
const IsLongPressingIn = {
173
+ ...baseStatesConditions ,
144
174
RESPONDER_ACTIVE_LONG_PRESS_IN : true ,
145
175
} ;
146
176
@@ -157,6 +187,15 @@ const Signals = keyMirror({
157
187
LONG_PRESS_DETECTED : null ,
158
188
} ) ;
159
189
190
+ type Signal =
191
+ | typeof Signals . DELAY
192
+ | typeof Signals . RESPONDER_GRANT
193
+ | typeof Signals . RESPONDER_RELEASE
194
+ | typeof Signals . RESPONDER_TERMINATED
195
+ | typeof Signals . ENTER_PRESS_RECT
196
+ | typeof Signals . LEAVE_PRESS_RECT
197
+ | typeof Signals . LONG_PRESS_DETECTED ;
198
+
160
199
/**
161
200
* Mapping from States x Signals => States
162
201
*/
@@ -391,7 +430,7 @@ const TouchableMixin = {
391
430
* @param {SyntheticEvent } e Synthetic event from event system.
392
431
*
393
432
*/
394
- touchableHandleResponderGrant : function ( e ) {
433
+ touchableHandleResponderGrant : function ( e : PressEvent ) {
395
434
const dispatchID = e . currentTarget ;
396
435
// Since e is used in a callback invoked on another event loop
397
436
// (as in setTimeout etc), we need to call e.persist() on the
@@ -432,21 +471,21 @@ const TouchableMixin = {
432
471
/**
433
472
* Place as callback for a DOM element's `onResponderRelease` event.
434
473
*/
435
- touchableHandleResponderRelease : function ( e ) {
474
+ touchableHandleResponderRelease : function ( e : PressEvent ) {
436
475
this . _receiveSignal ( Signals . RESPONDER_RELEASE , e ) ;
437
476
} ,
438
477
439
478
/**
440
479
* Place as callback for a DOM element's `onResponderTerminate` event.
441
480
*/
442
- touchableHandleResponderTerminate : function ( e ) {
481
+ touchableHandleResponderTerminate : function ( e : PressEvent ) {
443
482
this . _receiveSignal ( Signals . RESPONDER_TERMINATED , e ) ;
444
483
} ,
445
484
446
485
/**
447
486
* Place as callback for a DOM element's `onResponderMove` event.
448
487
*/
449
- touchableHandleResponderMove : function ( e ) {
488
+ touchableHandleResponderMove : function ( e : PressEvent ) {
450
489
// Not enough time elapsed yet, wait for highlight -
451
490
// this is just a perf optimization.
452
491
if (
@@ -633,7 +672,14 @@ const TouchableMixin = {
633
672
UIManager . measure ( tag , this . _handleQueryLayout ) ;
634
673
} ,
635
674
636
- _handleQueryLayout : function ( l , t , w , h , globalX , globalY ) {
675
+ _handleQueryLayout : function (
676
+ l : number ,
677
+ t : number ,
678
+ w : number ,
679
+ h : number ,
680
+ globalX : number ,
681
+ globalY : number ,
682
+ ) {
637
683
//don't do anything UIManager failed to measure node
638
684
if ( ! l && ! t && ! w && ! h && ! globalX && ! globalY) {
639
685
return ;
@@ -652,12 +698,12 @@ const TouchableMixin = {
652
698
) ;
653
699
} ,
654
700
655
- _handleDelay : function ( e ) {
701
+ _handleDelay: function ( e : PressEvent ) {
656
702
this . touchableDelayTimeout = null ;
657
703
this . _receiveSignal ( Signals . DELAY , e ) ;
658
704
} ,
659
705
660
- _handleLongDelay : function ( e ) {
706
+ _handleLongDelay : function ( e : PressEvent ) {
661
707
this . longPressDelayTimeout = null ;
662
708
const curState = this . state . touchable . touchState ;
663
709
if (
@@ -685,7 +731,7 @@ const TouchableMixin = {
685
731
* @throws Error if invalid state transition or unrecognized signal.
686
732
* @sideeffects
687
733
*/
688
- _receiveSignal : function ( signal , e ) {
734
+ _receiveSignal : function ( signal : Signal , e : PressEvent ) {
689
735
const responderID = this . state . touchable . responderID ;
690
736
const curState = this . state . touchable . touchState ;
691
737
const nextState = Transitions [ curState ] && Transitions [ curState ] [ signal ] ;
@@ -725,14 +771,14 @@ const TouchableMixin = {
725
771
this . longPressDelayTimeout = null ;
726
772
} ,
727
773
728
- _isHighlight : function ( state ) {
774
+ _isHighlight : function ( state : State ) {
729
775
return (
730
776
state === States . RESPONDER_ACTIVE_PRESS_IN ||
731
777
state === States . RESPONDER_ACTIVE_LONG_PRESS_IN
732
778
) ;
733
779
} ,
734
780
735
- _savePressInLocation : function ( e ) {
781
+ _savePressInLocation: function ( e : PressEvent ) {
736
782
const touch = TouchEventUtils . extractSingleTouch ( e . nativeEvent ) ;
737
783
const pageX = touch && touch . pageX ;
738
784
const pageY = touch && touch . pageY ;
@@ -741,7 +787,12 @@ const TouchableMixin = {
741
787
this . pressInLocation = { pageX, pageY, locationX, locationY} ;
742
788
} ,
743
789
744
- _getDistanceBetweenPoints : function ( aX , aY , bX , bY ) {
790
+ _getDistanceBetweenPoints : function (
791
+ aX : number ,
792
+ aY : number ,
793
+ bX : number ,
794
+ bY : number ,
795
+ ) {
745
796
const deltaX = aX - bX ;
746
797
const deltaY = aY - bY ;
747
798
return Math . sqrt ( deltaX * deltaX + deltaY * deltaY ) ;
@@ -758,7 +809,12 @@ const TouchableMixin = {
758
809
* @param {Event } e Native event.
759
810
* @sideeffects
760
811
*/
761
- _performSideEffectsForTransition : function ( curState , nextState , signal , e ) {
812
+ _performSideEffectsForTransition : function (
813
+ curState : State ,
814
+ nextState : State ,
815
+ signal : Signal ,
816
+ e : PressEvent ,
817
+ ) {
762
818
const curIsHighlight = this . _isHighlight ( curState ) ;
763
819
const newIsHighlight = this . _isHighlight ( nextState ) ;
764
820
@@ -813,12 +869,12 @@ const TouchableMixin = {
813
869
UIManager . playTouchSound ( ) ;
814
870
} ,
815
871
816
- _startHighlight : function ( e ) {
872
+ _startHighlight : function ( e : PressEvent ) {
817
873
this . _savePressInLocation ( e ) ;
818
874
this . touchableHandleActivePressIn && this . touchableHandleActivePressIn ( e ) ;
819
875
} ,
820
876
821
- _endHighlight : function ( e ) {
877
+ _endHighlight: function ( e : PressEvent ) {
822
878
if ( this . touchableHandleActivePressOut ) {
823
879
if (
824
880
this . touchableGetPressOutDelayMS &&
@@ -840,7 +896,13 @@ const Touchable = {
840
896
/**
841
897
* Renders a debugging overlay to visualize touch target with hitSlop (might not work on Android).
842
898
*/
843
- renderDebugView : ( { color, hitSlop} ) => {
899
+ renderDebugView : ( {
900
+ color,
901
+ hitSlop,
902
+ } : {
903
+ color : string | number ,
904
+ hitSlop : EdgeInsetsProp ,
905
+ } ) => {
844
906
if ( ! Touchable . TOUCH_TARGET_DEBUG ) {
845
907
return null ;
846
908
}
@@ -854,8 +916,12 @@ const Touchable = {
854
916
for ( const key in hitSlop ) {
855
917
debugHitSlopStyle [ key ] = - hitSlop [ key ] ;
856
918
}
919
+ const normalizedColor = normalizeColor ( color ) ;
920
+ if ( typeof normalizedColor !== 'number' ) {
921
+ return null ;
922
+ }
857
923
const hexColor =
858
- '#' + ( '00000000' + normalizeColor ( color ) . toString ( 16 ) ) . substr ( - 8 ) ;
924
+ '#' + ( '00000000' + normalizedColor . toString ( 16 ) ) . substr ( - 8 ) ;
859
925
return (
860
926
< View
861
927
pointerEvents = "none"
0 commit comments