From bd8c741c74a8d43edce6fd9b7cb94aaf54520c48 Mon Sep 17 00:00:00 2001 From: mottox2 Date: Tue, 6 Nov 2018 22:59:54 +0900 Subject: [PATCH 1/8] [flow] Flow TouchableNativeFeedback.android.js --- .../Touchable/TouchableNativeFeedback.android.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Libraries/Components/Touchable/TouchableNativeFeedback.android.js b/Libraries/Components/Touchable/TouchableNativeFeedback.android.js index 77e54347928b..551d2fe55058 100644 --- a/Libraries/Components/Touchable/TouchableNativeFeedback.android.js +++ b/Libraries/Components/Touchable/TouchableNativeFeedback.android.js @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * + * @flow * @format */ @@ -22,6 +23,8 @@ const createReactClass = require('create-react-class'); const ensurePositiveDelayProps = require('ensurePositiveDelayProps'); const processColor = require('processColor'); +import type {PressEvent} from 'CoreEventTypes'; + const rippleBackgroundPropType = PropTypes.shape({ type: PropTypes.oneOf(['RippleAndroid']), color: PropTypes.number, @@ -38,8 +41,6 @@ const backgroundPropType = PropTypes.oneOfType([ themeAttributeBackgroundPropType, ]); -type Event = Object; - const PRESS_RETENTION_OFFSET = {top: 20, left: 20, right: 20, bottom: 30}; /** @@ -167,7 +168,7 @@ const TouchableNativeFeedback = createReactClass({ * `Touchable.Mixin` self callbacks. The mixin will invoke these if they are * defined on your component. */ - touchableHandleActivePressIn: function(e: Event) { + touchableHandleActivePressIn: function(e: PressEvent) { this.props.onPressIn && this.props.onPressIn(e); this._dispatchPressedStateChange(true); if (this.pressInLocation) { @@ -178,16 +179,16 @@ const TouchableNativeFeedback = createReactClass({ } }, - touchableHandleActivePressOut: function(e: Event) { + touchableHandleActivePressOut: function(e: PressEvent) { this.props.onPressOut && this.props.onPressOut(e); this._dispatchPressedStateChange(false); }, - touchableHandlePress: function(e: Event) { + touchableHandlePress: function(e: PressEvent) { this.props.onPress && this.props.onPress(e); }, - touchableHandleLongPress: function(e: Event) { + touchableHandleLongPress: function(e: PressEvent) { this.props.onLongPress && this.props.onLongPress(e); }, From d5d44268bb6a6a796f83a81eb12bf95773ddb310 Mon Sep 17 00:00:00 2001 From: mottox2 Date: Wed, 7 Nov 2018 01:28:25 +0900 Subject: [PATCH 2/8] [flow-strict] Apply flow-strict --- .../Components/Touchable/TouchableNativeFeedback.android.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Libraries/Components/Touchable/TouchableNativeFeedback.android.js b/Libraries/Components/Touchable/TouchableNativeFeedback.android.js index 551d2fe55058..2a883cefe7cc 100644 --- a/Libraries/Components/Touchable/TouchableNativeFeedback.android.js +++ b/Libraries/Components/Touchable/TouchableNativeFeedback.android.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow + * @flow strict-local * @format */ @@ -14,6 +14,7 @@ const Platform = require('Platform'); const React = require('React'); const PropTypes = require('prop-types'); const ReactNative = require('ReactNative'); +// $FlowFixMe const Touchable = require('Touchable'); const TouchableWithoutFeedback = require('TouchableWithoutFeedback'); const UIManager = require('UIManager'); From 6d446370506da76a1fcda351a65b7ec9d6003c30 Mon Sep 17 00:00:00 2001 From: mottox2 Date: Wed, 7 Nov 2018 00:50:35 +0900 Subject: [PATCH 3/8] Apply flow to Libraries/Components/Touchable/Touchable.js --- Libraries/Components/Touchable/Touchable.js | 97 +++++++++++++++++---- 1 file changed, 80 insertions(+), 17 deletions(-) diff --git a/Libraries/Components/Touchable/Touchable.js b/Libraries/Components/Touchable/Touchable.js index 8fc27be8882e..f13e01bb84b7 100644 --- a/Libraries/Components/Touchable/Touchable.js +++ b/Libraries/Components/Touchable/Touchable.js @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * + * @flow * @format */ @@ -22,6 +23,9 @@ const View = require('View'); const keyMirror = require('fbjs/lib/keyMirror'); const normalizeColor = require('normalizeColor'); +import type {PressEvent, SyntheticEvent, LayoutEvent} from 'CoreEventTypes'; +import type {EdgeInsetsProp} from 'EdgeInsetsPropType'; + /** * `Touchable`: Taps done right. * @@ -110,6 +114,7 @@ const normalizeColor = require('normalizeColor'); /** * Touchable states. */ + const States = keyMirror({ NOT_RESPONDER: null, // Not the responder RESPONDER_INACTIVE_PRESS_IN: null, // Responder, inactive, in the `PressRect` @@ -121,10 +126,33 @@ const States = keyMirror({ ERROR: null, }); -/** +type State = + | typeof States.NOT_RESPONDER + | typeof States.RESPONDER_INACTIVE_PRESS_IN + | typeof States.RESPONDER_INACTIVE_PRESS_OUT + | typeof States.RESPONDER_ACTIVE_PRESS_IN + | typeof States.RESPONDER_ACTIVE_PRESS_OUT + | typeof States.RESPONDER_ACTIVE_LONG_PRESS_IN + | typeof States.RESPONDER_ACTIVE_LONG_PRESS_OUT + | typeof States.ERROR; + +/* * Quick lookup map for states that are considered to be "active" */ + +const baseStatsConditions = { + NOT_RESPONDER: false, + RESPONDER_INACTIVE_PRESS_IN: false, + RESPONDER_INACTIVE_PRESS_OUT: false, + RESPONDER_ACTIVE_PRESS_IN: false, + RESPONDER_ACTIVE_PRESS_OUT: false, + RESPONDER_ACTIVE_LONG_PRESS_IN: false, + RESPONDER_ACTIVE_LONG_PRESS_OUT: false, + ERROR: false, +}; + const IsActive = { + ...baseStatsConditions, RESPONDER_ACTIVE_PRESS_OUT: true, RESPONDER_ACTIVE_PRESS_IN: true, }; @@ -134,12 +162,14 @@ const IsActive = { * therefore eligible to result in a "selection" if the press stops. */ const IsPressingIn = { + ...baseStatsConditions, RESPONDER_INACTIVE_PRESS_IN: true, RESPONDER_ACTIVE_PRESS_IN: true, RESPONDER_ACTIVE_LONG_PRESS_IN: true, }; const IsLongPressingIn = { + ...baseStatsConditions, RESPONDER_ACTIVE_LONG_PRESS_IN: true, }; @@ -156,6 +186,15 @@ const Signals = keyMirror({ LONG_PRESS_DETECTED: null, }); +type Signal = + | typeof Signals.DELAY + | typeof Signals.RESPONDER_GRANT + | typeof Signals.RESPONDER_RELEASE + | typeof Signals.RESPONDER_TERMINATED + | typeof Signals.ENTER_PRESS_RECT + | typeof Signals.LEAVE_PRESS_RECT + | typeof Signals.LONG_PRESS_DETECTED; + /** * Mapping from States x Signals => States */ @@ -390,7 +429,7 @@ const TouchableMixin = { * @param {SyntheticEvent} e Synthetic event from event system. * */ - touchableHandleResponderGrant: function(e) { + touchableHandleResponderGrant: function(e: SyntheticEvent) { const dispatchID = e.currentTarget; // Since e is used in a callback invoked on another event loop // (as in setTimeout etc), we need to call e.persist() on the @@ -431,21 +470,21 @@ const TouchableMixin = { /** * Place as callback for a DOM element's `onResponderRelease` event. */ - touchableHandleResponderRelease: function(e) { + touchableHandleResponderRelease: function(e: PressEvent) { this._receiveSignal(Signals.RESPONDER_RELEASE, e); }, /** * Place as callback for a DOM element's `onResponderTerminate` event. */ - touchableHandleResponderTerminate: function(e) { + touchableHandleResponderTerminate: function(e: PressEvent) { this._receiveSignal(Signals.RESPONDER_TERMINATED, e); }, /** * Place as callback for a DOM element's `onResponderMove` event. */ - touchableHandleResponderMove: function(e) { + touchableHandleResponderMove: function(e: PressEvent) { // Not enough time elapsed yet, wait for highlight - // this is just a perf optimization. if ( @@ -632,7 +671,14 @@ const TouchableMixin = { UIManager.measure(tag, this._handleQueryLayout); }, - _handleQueryLayout: function(l, t, w, h, globalX, globalY) { + _handleQueryLayout: function( + l: number, + t: number, + w: number, + h: number, + globalX: number, + globalY: number, + ) { //don't do anything UIManager failed to measure node if (!l && !t && !w && !h && !globalX && !globalY) { return; @@ -651,12 +697,12 @@ const TouchableMixin = { ); }, - _handleDelay: function(e) { + _handleDelay: function(e: LayoutEvent) { this.touchableDelayTimeout = null; this._receiveSignal(Signals.DELAY, e); }, - _handleLongDelay: function(e) { + _handleLongDelay: function(e: LayoutEvent) { this.longPressDelayTimeout = null; const curState = this.state.touchable.touchState; if ( @@ -684,7 +730,7 @@ const TouchableMixin = { * @throws Error if invalid state transition or unrecognized signal. * @sideeffects */ - _receiveSignal: function(signal, e) { + _receiveSignal: function(signal: Signal, e: LayoutEvent) { const responderID = this.state.touchable.responderID; const curState = this.state.touchable.touchState; const nextState = Transitions[curState] && Transitions[curState][signal]; @@ -724,14 +770,14 @@ const TouchableMixin = { this.longPressDelayTimeout = null; }, - _isHighlight: function(state) { + _isHighlight: function(state: State) { return ( state === States.RESPONDER_ACTIVE_PRESS_IN || state === States.RESPONDER_ACTIVE_LONG_PRESS_IN ); }, - _savePressInLocation: function(e) { + _savePressInLocation: function(e: LayoutEvent) { const touch = TouchEventUtils.extractSingleTouch(e.nativeEvent); const pageX = touch && touch.pageX; const pageY = touch && touch.pageY; @@ -740,7 +786,12 @@ const TouchableMixin = { this.pressInLocation = {pageX, pageY, locationX, locationY}; }, - _getDistanceBetweenPoints: function(aX, aY, bX, bY) { + _getDistanceBetweenPoints: function( + aX: number, + aY: number, + bX: number, + bY: number, + ) { const deltaX = aX - bX; const deltaY = aY - bY; return Math.sqrt(deltaX * deltaX + deltaY * deltaY); @@ -757,7 +808,12 @@ const TouchableMixin = { * @param {Event} e Native event. * @sideeffects */ - _performSideEffectsForTransition: function(curState, nextState, signal, e) { + _performSideEffectsForTransition: function( + curState: State, + nextState: State, + signal: Signal, + e: LayoutEvent, + ) { const curIsHighlight = this._isHighlight(curState); const newIsHighlight = this._isHighlight(nextState); @@ -812,12 +868,12 @@ const TouchableMixin = { UIManager.playTouchSound(); }, - _startHighlight: function(e) { + _startHighlight: function(e: LayoutEvent) { this._savePressInLocation(e); this.touchableHandleActivePressIn && this.touchableHandleActivePressIn(e); }, - _endHighlight: function(e) { + _endHighlight: function(e: LayoutEvent) { if (this.touchableHandleActivePressOut) { if ( this.touchableGetPressOutDelayMS && @@ -839,7 +895,13 @@ const Touchable = { /** * Renders a debugging overlay to visualize touch target with hitSlop (might not work on Android). */ - renderDebugView: ({color, hitSlop}) => { + renderDebugView: ({ + color, + hitSlop, + }: { + color: string | number, + hitSlop: EdgeInsetsProp, + }) => { if (!Touchable.TOUCH_TARGET_DEBUG) { return null; } @@ -854,7 +916,8 @@ const Touchable = { debugHitSlopStyle[key] = -hitSlop[key]; } const hexColor = - '#' + ('00000000' + normalizeColor(color).toString(16)).substr(-8); + '#' + + ('00000000' + (normalizeColor(color): number).toString(16)).substr(-8); return ( Date: Wed, 7 Nov 2018 01:11:38 +0900 Subject: [PATCH 4/8] Fix --- Libraries/Components/Touchable/Touchable.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Libraries/Components/Touchable/Touchable.js b/Libraries/Components/Touchable/Touchable.js index f13e01bb84b7..2d4bbc5b4e24 100644 --- a/Libraries/Components/Touchable/Touchable.js +++ b/Libraries/Components/Touchable/Touchable.js @@ -915,9 +915,12 @@ const Touchable = { for (const key in hitSlop) { debugHitSlopStyle[key] = -hitSlop[key]; } + const normalizedColor = normalizeColor(color); const hexColor = '#' + - ('00000000' + (normalizeColor(color): number).toString(16)).substr(-8); + (normalizedColor + ? '00000000' + normalizedColor.toString(16).substr(-8) + : ''); return ( Date: Wed, 7 Nov 2018 01:34:29 +0900 Subject: [PATCH 5/8] Fix typo --- Libraries/Components/Touchable/Touchable.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Libraries/Components/Touchable/Touchable.js b/Libraries/Components/Touchable/Touchable.js index 2d4bbc5b4e24..6865569331f6 100644 --- a/Libraries/Components/Touchable/Touchable.js +++ b/Libraries/Components/Touchable/Touchable.js @@ -140,7 +140,7 @@ type State = * Quick lookup map for states that are considered to be "active" */ -const baseStatsConditions = { +const baseStatesConditions = { NOT_RESPONDER: false, RESPONDER_INACTIVE_PRESS_IN: false, RESPONDER_INACTIVE_PRESS_OUT: false, @@ -152,7 +152,7 @@ const baseStatsConditions = { }; const IsActive = { - ...baseStatsConditions, + ...baseStatesConditions, RESPONDER_ACTIVE_PRESS_OUT: true, RESPONDER_ACTIVE_PRESS_IN: true, }; @@ -162,14 +162,14 @@ const IsActive = { * therefore eligible to result in a "selection" if the press stops. */ const IsPressingIn = { - ...baseStatsConditions, + ...baseStatesConditions, RESPONDER_INACTIVE_PRESS_IN: true, RESPONDER_ACTIVE_PRESS_IN: true, RESPONDER_ACTIVE_LONG_PRESS_IN: true, }; const IsLongPressingIn = { - ...baseStatsConditions, + ...baseStatesConditions, RESPONDER_ACTIVE_LONG_PRESS_IN: true, }; From 3fb851babb54ebcc30545b11c61043a171651000 Mon Sep 17 00:00:00 2001 From: mottox2 Date: Wed, 7 Nov 2018 10:24:43 +0900 Subject: [PATCH 6/8] Fix styles --- Libraries/Components/Touchable/Touchable.js | 22 +++++++++------------ 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/Libraries/Components/Touchable/Touchable.js b/Libraries/Components/Touchable/Touchable.js index 6865569331f6..2c576fcb06c1 100644 --- a/Libraries/Components/Touchable/Touchable.js +++ b/Libraries/Components/Touchable/Touchable.js @@ -921,19 +921,15 @@ const Touchable = { (normalizedColor ? '00000000' + normalizedColor.toString(16).substr(-8) : ''); - return ( - - ); + const styles = { + position: 'absolute', + borderColor: hexColor.slice(0, -2) + '55', // More opaque + borderWidth: 1, + borderStyle: 'dashed', + backgroundColor: hexColor.slice(0, -2) + '0F', // Less opaque + ...debugHitSlopStyle, + }; + return ; }, }; From 4623d517acb56cbad7e378ab1fa3a2715b363ce6 Mon Sep 17 00:00:00 2001 From: mottox2 Date: Wed, 7 Nov 2018 10:24:57 +0900 Subject: [PATCH 7/8] Remove $FlowFixMe --- .../Components/Touchable/TouchableNativeFeedback.android.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Libraries/Components/Touchable/TouchableNativeFeedback.android.js b/Libraries/Components/Touchable/TouchableNativeFeedback.android.js index 2a883cefe7cc..82031a4198aa 100644 --- a/Libraries/Components/Touchable/TouchableNativeFeedback.android.js +++ b/Libraries/Components/Touchable/TouchableNativeFeedback.android.js @@ -14,7 +14,6 @@ const Platform = require('Platform'); const React = require('React'); const PropTypes = require('prop-types'); const ReactNative = require('ReactNative'); -// $FlowFixMe const Touchable = require('Touchable'); const TouchableWithoutFeedback = require('TouchableWithoutFeedback'); const UIManager = require('UIManager'); From 68f335fefa1b79a615fd2569c760c2a4fd49f535 Mon Sep 17 00:00:00 2001 From: mottox2 Date: Wed, 7 Nov 2018 11:03:41 +0900 Subject: [PATCH 8/8] Check color --- Libraries/Components/Touchable/Touchable.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Libraries/Components/Touchable/Touchable.js b/Libraries/Components/Touchable/Touchable.js index 121719559eb7..16bbd6835e9f 100644 --- a/Libraries/Components/Touchable/Touchable.js +++ b/Libraries/Components/Touchable/Touchable.js @@ -916,8 +916,12 @@ const Touchable = { for (const key in hitSlop) { debugHitSlopStyle[key] = -hitSlop[key]; } + const normalizedColor = normalizeColor(color); + if (typeof normalizedColor !== 'number') { + return null; + } const hexColor = - '#' + ('00000000' + normalizeColor(color).toString(16)).substr(-8); + '#' + ('00000000' + normalizedColor.toString(16)).substr(-8); return (