Skip to content

Commit 2dcc666

Browse files
mottox2facebook-github-bot
authored andcommitted
Flow TouchableNativeFeedback.android.js (facebook#22176)
Summary: Related to facebook#22100 Turn Flow strict mode on for Libraries/Components/Touchable/TouchableNativeFeedback.android.js. Pull Request resolved: facebook#22176 Reviewed By: TheSavior Differential Revision: D13033239 Pulled By: RSNara fbshipit-source-id: 57e89ce16040e6238e01e0437327db943a5f2581
1 parent 4c5083a commit 2dcc666

File tree

2 files changed

+90
-23
lines changed

2 files changed

+90
-23
lines changed

Libraries/Components/Touchable/Touchable.js

+83-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow
78
* @format
89
*/
910

@@ -23,6 +24,9 @@ const View = require('View');
2324
const keyMirror = require('fbjs/lib/keyMirror');
2425
const normalizeColor = require('normalizeColor');
2526

27+
import type {PressEvent} from 'CoreEventTypes';
28+
import type {EdgeInsetsProp} from 'EdgeInsetsPropType';
29+
2630
/**
2731
* `Touchable`: Taps done right.
2832
*
@@ -111,6 +115,7 @@ const normalizeColor = require('normalizeColor');
111115
/**
112116
* Touchable states.
113117
*/
118+
114119
const States = keyMirror({
115120
NOT_RESPONDER: null, // Not the responder
116121
RESPONDER_INACTIVE_PRESS_IN: null, // Responder, inactive, in the `PressRect`
@@ -122,10 +127,33 @@ const States = keyMirror({
122127
ERROR: null,
123128
});
124129

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+
/*
126141
* Quick lookup map for states that are considered to be "active"
127142
*/
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+
128155
const IsActive = {
156+
...baseStatesConditions,
129157
RESPONDER_ACTIVE_PRESS_OUT: true,
130158
RESPONDER_ACTIVE_PRESS_IN: true,
131159
};
@@ -135,12 +163,14 @@ const IsActive = {
135163
* therefore eligible to result in a "selection" if the press stops.
136164
*/
137165
const IsPressingIn = {
166+
...baseStatesConditions,
138167
RESPONDER_INACTIVE_PRESS_IN: true,
139168
RESPONDER_ACTIVE_PRESS_IN: true,
140169
RESPONDER_ACTIVE_LONG_PRESS_IN: true,
141170
};
142171

143172
const IsLongPressingIn = {
173+
...baseStatesConditions,
144174
RESPONDER_ACTIVE_LONG_PRESS_IN: true,
145175
};
146176

@@ -157,6 +187,15 @@ const Signals = keyMirror({
157187
LONG_PRESS_DETECTED: null,
158188
});
159189

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+
160199
/**
161200
* Mapping from States x Signals => States
162201
*/
@@ -391,7 +430,7 @@ const TouchableMixin = {
391430
* @param {SyntheticEvent} e Synthetic event from event system.
392431
*
393432
*/
394-
touchableHandleResponderGrant: function(e) {
433+
touchableHandleResponderGrant: function(e: PressEvent) {
395434
const dispatchID = e.currentTarget;
396435
// Since e is used in a callback invoked on another event loop
397436
// (as in setTimeout etc), we need to call e.persist() on the
@@ -432,21 +471,21 @@ const TouchableMixin = {
432471
/**
433472
* Place as callback for a DOM element's `onResponderRelease` event.
434473
*/
435-
touchableHandleResponderRelease: function(e) {
474+
touchableHandleResponderRelease: function(e: PressEvent) {
436475
this._receiveSignal(Signals.RESPONDER_RELEASE, e);
437476
},
438477

439478
/**
440479
* Place as callback for a DOM element's `onResponderTerminate` event.
441480
*/
442-
touchableHandleResponderTerminate: function(e) {
481+
touchableHandleResponderTerminate: function(e: PressEvent) {
443482
this._receiveSignal(Signals.RESPONDER_TERMINATED, e);
444483
},
445484

446485
/**
447486
* Place as callback for a DOM element's `onResponderMove` event.
448487
*/
449-
touchableHandleResponderMove: function(e) {
488+
touchableHandleResponderMove: function(e: PressEvent) {
450489
// Not enough time elapsed yet, wait for highlight -
451490
// this is just a perf optimization.
452491
if (
@@ -633,7 +672,14 @@ const TouchableMixin = {
633672
UIManager.measure(tag, this._handleQueryLayout);
634673
},
635674

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+
) {
637683
//don't do anything UIManager failed to measure node
638684
if (!l && !t && !w && !h && !globalX && !globalY) {
639685
return;
@@ -652,12 +698,12 @@ const TouchableMixin = {
652698
);
653699
},
654700

655-
_handleDelay: function(e) {
701+
_handleDelay: function(e: PressEvent) {
656702
this.touchableDelayTimeout = null;
657703
this._receiveSignal(Signals.DELAY, e);
658704
},
659705

660-
_handleLongDelay: function(e) {
706+
_handleLongDelay: function(e: PressEvent) {
661707
this.longPressDelayTimeout = null;
662708
const curState = this.state.touchable.touchState;
663709
if (
@@ -685,7 +731,7 @@ const TouchableMixin = {
685731
* @throws Error if invalid state transition or unrecognized signal.
686732
* @sideeffects
687733
*/
688-
_receiveSignal: function(signal, e) {
734+
_receiveSignal: function(signal: Signal, e: PressEvent) {
689735
const responderID = this.state.touchable.responderID;
690736
const curState = this.state.touchable.touchState;
691737
const nextState = Transitions[curState] && Transitions[curState][signal];
@@ -725,14 +771,14 @@ const TouchableMixin = {
725771
this.longPressDelayTimeout = null;
726772
},
727773

728-
_isHighlight: function(state) {
774+
_isHighlight: function(state: State) {
729775
return (
730776
state === States.RESPONDER_ACTIVE_PRESS_IN ||
731777
state === States.RESPONDER_ACTIVE_LONG_PRESS_IN
732778
);
733779
},
734780

735-
_savePressInLocation: function(e) {
781+
_savePressInLocation: function(e: PressEvent) {
736782
const touch = TouchEventUtils.extractSingleTouch(e.nativeEvent);
737783
const pageX = touch && touch.pageX;
738784
const pageY = touch && touch.pageY;
@@ -741,7 +787,12 @@ const TouchableMixin = {
741787
this.pressInLocation = {pageX, pageY, locationX, locationY};
742788
},
743789

744-
_getDistanceBetweenPoints: function(aX, aY, bX, bY) {
790+
_getDistanceBetweenPoints: function(
791+
aX: number,
792+
aY: number,
793+
bX: number,
794+
bY: number,
795+
) {
745796
const deltaX = aX - bX;
746797
const deltaY = aY - bY;
747798
return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
@@ -758,7 +809,12 @@ const TouchableMixin = {
758809
* @param {Event} e Native event.
759810
* @sideeffects
760811
*/
761-
_performSideEffectsForTransition: function(curState, nextState, signal, e) {
812+
_performSideEffectsForTransition: function(
813+
curState: State,
814+
nextState: State,
815+
signal: Signal,
816+
e: PressEvent,
817+
) {
762818
const curIsHighlight = this._isHighlight(curState);
763819
const newIsHighlight = this._isHighlight(nextState);
764820

@@ -813,12 +869,12 @@ const TouchableMixin = {
813869
UIManager.playTouchSound();
814870
},
815871

816-
_startHighlight: function(e) {
872+
_startHighlight: function(e: PressEvent) {
817873
this._savePressInLocation(e);
818874
this.touchableHandleActivePressIn && this.touchableHandleActivePressIn(e);
819875
},
820876

821-
_endHighlight: function(e) {
877+
_endHighlight: function(e: PressEvent) {
822878
if (this.touchableHandleActivePressOut) {
823879
if (
824880
this.touchableGetPressOutDelayMS &&
@@ -840,7 +896,13 @@ const Touchable = {
840896
/**
841897
* Renders a debugging overlay to visualize touch target with hitSlop (might not work on Android).
842898
*/
843-
renderDebugView: ({color, hitSlop}) => {
899+
renderDebugView: ({
900+
color,
901+
hitSlop,
902+
}: {
903+
color: string | number,
904+
hitSlop: EdgeInsetsProp,
905+
}) => {
844906
if (!Touchable.TOUCH_TARGET_DEBUG) {
845907
return null;
846908
}
@@ -854,8 +916,12 @@ const Touchable = {
854916
for (const key in hitSlop) {
855917
debugHitSlopStyle[key] = -hitSlop[key];
856918
}
919+
const normalizedColor = normalizeColor(color);
920+
if (typeof normalizedColor !== 'number') {
921+
return null;
922+
}
857923
const hexColor =
858-
'#' + ('00000000' + normalizeColor(color).toString(16)).substr(-8);
924+
'#' + ('00000000' + normalizedColor.toString(16)).substr(-8);
859925
return (
860926
<View
861927
pointerEvents="none"

Libraries/Components/Touchable/TouchableNativeFeedback.android.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -22,6 +23,8 @@ const createReactClass = require('create-react-class');
2223
const ensurePositiveDelayProps = require('ensurePositiveDelayProps');
2324
const processColor = require('processColor');
2425

26+
import type {PressEvent} from 'CoreEventTypes';
27+
2528
const rippleBackgroundPropType = PropTypes.shape({
2629
type: PropTypes.oneOf(['RippleAndroid']),
2730
color: PropTypes.number,
@@ -38,8 +41,6 @@ const backgroundPropType = PropTypes.oneOfType([
3841
themeAttributeBackgroundPropType,
3942
]);
4043

41-
type Event = Object;
42-
4344
const PRESS_RETENTION_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
4445

4546
/**
@@ -167,7 +168,7 @@ const TouchableNativeFeedback = createReactClass({
167168
* `Touchable.Mixin` self callbacks. The mixin will invoke these if they are
168169
* defined on your component.
169170
*/
170-
touchableHandleActivePressIn: function(e: Event) {
171+
touchableHandleActivePressIn: function(e: PressEvent) {
171172
this.props.onPressIn && this.props.onPressIn(e);
172173
this._dispatchPressedStateChange(true);
173174
if (this.pressInLocation) {
@@ -178,16 +179,16 @@ const TouchableNativeFeedback = createReactClass({
178179
}
179180
},
180181

181-
touchableHandleActivePressOut: function(e: Event) {
182+
touchableHandleActivePressOut: function(e: PressEvent) {
182183
this.props.onPressOut && this.props.onPressOut(e);
183184
this._dispatchPressedStateChange(false);
184185
},
185186

186-
touchableHandlePress: function(e: Event) {
187+
touchableHandlePress: function(e: PressEvent) {
187188
this.props.onPress && this.props.onPress(e);
188189
},
189190

190-
touchableHandleLongPress: function(e: Event) {
191+
touchableHandleLongPress: function(e: PressEvent) {
191192
this.props.onLongPress && this.props.onLongPress(e);
192193
},
193194

0 commit comments

Comments
 (0)