[change] Move 'onResponderRelease' cancel to 'Touchable'

Moves a fix for double-firing Touchables into 'Touchable'
This commit is contained in:
Nicolas Gallagher
2017-01-02 13:12:13 -08:00
parent a74be91b7c
commit a535c558d8
2 changed files with 10 additions and 13 deletions
+7
View File
@@ -404,6 +404,13 @@ var TouchableMixin = {
*/ */
touchableHandleResponderRelease: function(e) { touchableHandleResponderRelease: function(e) {
this._receiveSignal(Signals.RESPONDER_RELEASE, e); this._receiveSignal(Signals.RESPONDER_RELEASE, e);
// Browsers fire mouse events after touch events. This causes the
// 'onResponderRelease' handler to be called twice for Touchables.
// Auto-fix this issue by calling 'preventDefault' to cancel the mouse
// events.
if (e.cancelable && !e.isDefaultPrevented()) {
e.preventDefault();
}
}, },
/** /**
+3 -13
View File
@@ -112,7 +112,7 @@ class View extends Component {
eventHandlerNames.reduce((props, handlerName) => { eventHandlerNames.reduce((props, handlerName) => {
const handler = this.props[handlerName]; const handler = this.props[handlerName];
if (typeof handler === 'function') { if (typeof handler === 'function') {
props[handlerName] = this._normalizeEventForHandler(handler, handlerName); props[handlerName] = this._normalizeEventForHandler(handler);
} }
return props; return props;
}, otherProps); }, otherProps);
@@ -127,20 +127,10 @@ class View extends Component {
return createDOMElement(component, otherProps); return createDOMElement(component, otherProps);
} }
_normalizeEventForHandler(handler, handlerName) { _normalizeEventForHandler(handler) {
// Browsers fire mouse events after touch events. This causes the
// 'onResponderRelease' handler to be called twice for Touchables.
// Auto-fix this issue by calling 'preventDefault' to cancel the mouse
// events.
const shouldCancelEvent = handlerName === 'onResponderRelease';
return (e) => { return (e) => {
e.nativeEvent = normalizeNativeEvent(e.nativeEvent); e.nativeEvent = normalizeNativeEvent(e.nativeEvent);
const returnValue = handler(e); return handler(e);
if (shouldCancelEvent && e.cancelable) {
e.preventDefault();
}
return returnValue;
}; };
} }
} }