2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
|
|
|
* @providesModule TouchableHighlight
|
2015-12-02 03:09:01 +00:00
|
|
|
* @noflow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-25 22:36:50 +00:00
|
|
|
// Note (avik): add @flow when Flow supports spread properties in propTypes
|
|
|
|
|
2015-12-23 03:29:01 +00:00
|
|
|
var ColorPropType = require('ColorPropType');
|
2016-07-05 13:34:00 +00:00
|
|
|
var NativeMethodsMixin = require('react/lib/NativeMethodsMixin');
|
2015-01-30 01:10:49 +00:00
|
|
|
var React = require('React');
|
2015-05-08 16:45:43 +00:00
|
|
|
var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
|
2015-01-30 01:10:49 +00:00
|
|
|
var StyleSheet = require('StyleSheet');
|
2015-03-25 02:34:12 +00:00
|
|
|
var TimerMixin = require('react-timer-mixin');
|
2015-01-30 01:10:49 +00:00
|
|
|
var Touchable = require('Touchable');
|
2015-03-18 22:57:49 +00:00
|
|
|
var TouchableWithoutFeedback = require('TouchableWithoutFeedback');
|
2015-01-30 01:10:49 +00:00
|
|
|
var View = require('View');
|
|
|
|
|
|
|
|
var ensureComponentIsNative = require('ensureComponentIsNative');
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
var ensurePositiveDelayProps = require('ensurePositiveDelayProps');
|
2016-03-02 12:27:13 +00:00
|
|
|
var keyOf = require('fbjs/lib/keyOf');
|
2015-01-30 01:10:49 +00:00
|
|
|
var merge = require('merge');
|
2016-07-05 13:34:00 +00:00
|
|
|
var onlyChild = require('react/lib/onlyChild');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
type Event = Object;
|
|
|
|
|
2015-03-09 23:18:15 +00:00
|
|
|
var DEFAULT_PROPS = {
|
|
|
|
activeOpacity: 0.8,
|
|
|
|
underlayColor: 'black',
|
|
|
|
};
|
|
|
|
|
2015-11-16 21:51:13 +00:00
|
|
|
var PRESS_RETENTION_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-09 23:18:15 +00:00
|
|
|
* A wrapper for making views respond properly to touches.
|
2015-01-30 01:10:49 +00:00
|
|
|
* On press down, the opacity of the wrapped view is decreased, which allows
|
|
|
|
* the underlay color to show through, darkening or tinting the view. The
|
|
|
|
* underlay comes from adding a view to the view hierarchy, which can sometimes
|
|
|
|
* cause unwanted visual artifacts if not used correctly, for example if the
|
|
|
|
* backgroundColor of the wrapped view isn't explicitly set to an opaque color.
|
|
|
|
*
|
2015-03-09 23:18:15 +00:00
|
|
|
* Example:
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2015-03-09 23:18:15 +00:00
|
|
|
* ```
|
|
|
|
* renderButton: function() {
|
|
|
|
* return (
|
|
|
|
* <TouchableHighlight onPress={this._onPressButton}>
|
|
|
|
* <Image
|
|
|
|
* style={styles.button}
|
2015-03-21 17:07:45 +00:00
|
|
|
* source={require('image!myButton')}
|
2015-03-09 23:18:15 +00:00
|
|
|
* />
|
2015-03-31 03:12:32 +00:00
|
|
|
* </TouchableHighlight>
|
2015-03-09 23:18:15 +00:00
|
|
|
* );
|
|
|
|
* },
|
|
|
|
* ```
|
2015-09-23 18:22:22 +00:00
|
|
|
* > **NOTE**: TouchableHighlight supports only one child
|
|
|
|
* >
|
2015-10-12 04:10:23 +00:00
|
|
|
* > If you wish to have several child components, wrap them in a View.
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var TouchableHighlight = React.createClass({
|
|
|
|
propTypes: {
|
2015-03-18 22:57:49 +00:00
|
|
|
...TouchableWithoutFeedback.propTypes,
|
2015-01-30 01:10:49 +00:00
|
|
|
/**
|
|
|
|
* Determines what the opacity of the wrapped view should be when touch is
|
|
|
|
* active.
|
|
|
|
*/
|
|
|
|
activeOpacity: React.PropTypes.number,
|
|
|
|
/**
|
|
|
|
* The color of the underlay that will show through when the touch is
|
|
|
|
* active.
|
|
|
|
*/
|
2015-12-23 03:29:01 +00:00
|
|
|
underlayColor: ColorPropType,
|
2015-03-09 23:18:15 +00:00
|
|
|
style: View.propTypes.style,
|
2015-05-26 22:16:42 +00:00
|
|
|
/**
|
|
|
|
* Called immediately after the underlay is shown
|
|
|
|
*/
|
|
|
|
onShowUnderlay: React.PropTypes.func,
|
|
|
|
/**
|
|
|
|
* Called immediately after the underlay is hidden
|
|
|
|
*/
|
|
|
|
onHideUnderlay: React.PropTypes.func,
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
mixins: [NativeMethodsMixin, TimerMixin, Touchable.Mixin],
|
|
|
|
|
|
|
|
getDefaultProps: () => DEFAULT_PROPS,
|
|
|
|
|
|
|
|
// Performance optimization to avoid constantly re-generating these objects.
|
2016-04-09 18:12:46 +00:00
|
|
|
_computeSyntheticState: function(props) {
|
2015-01-30 01:10:49 +00:00
|
|
|
return {
|
|
|
|
activeProps: {
|
|
|
|
style: {
|
|
|
|
opacity: props.activeOpacity,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
activeUnderlayProps: {
|
|
|
|
style: {
|
|
|
|
backgroundColor: props.underlayColor,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
underlayStyle: [
|
|
|
|
INACTIVE_UNDERLAY_PROPS.style,
|
|
|
|
props.style,
|
|
|
|
]
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return merge(
|
2016-04-09 18:12:46 +00:00
|
|
|
this.touchableGetInitialState(), this._computeSyntheticState(this.props)
|
2015-01-30 01:10:49 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
ensurePositiveDelayProps(this.props);
|
2015-01-30 01:10:49 +00:00
|
|
|
ensureComponentIsNative(this.refs[CHILD_REF]);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidUpdate: function() {
|
|
|
|
ensureComponentIsNative(this.refs[CHILD_REF]);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps: function(nextProps) {
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
ensurePositiveDelayProps(nextProps);
|
2015-01-30 01:10:49 +00:00
|
|
|
if (nextProps.activeOpacity !== this.props.activeOpacity ||
|
|
|
|
nextProps.underlayColor !== this.props.underlayColor ||
|
|
|
|
nextProps.style !== this.props.style) {
|
2016-04-09 18:12:46 +00:00
|
|
|
this.setState(this._computeSyntheticState(nextProps));
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
viewConfig: {
|
|
|
|
uiViewClassName: 'RCTView',
|
2015-05-08 16:45:43 +00:00
|
|
|
validAttributes: ReactNativeViewAttributes.RCTView
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `Touchable.Mixin` self callbacks. The mixin will invoke these if they are
|
|
|
|
* defined on your component.
|
|
|
|
*/
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandleActivePressIn: function(e: Event) {
|
2015-01-30 01:10:49 +00:00
|
|
|
this.clearTimeout(this._hideTimeout);
|
|
|
|
this._hideTimeout = null;
|
|
|
|
this._showUnderlay();
|
2015-08-21 08:52:13 +00:00
|
|
|
this.props.onPressIn && this.props.onPressIn(e);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandleActivePressOut: function(e: Event) {
|
2015-01-30 01:10:49 +00:00
|
|
|
if (!this._hideTimeout) {
|
|
|
|
this._hideUnderlay();
|
|
|
|
}
|
2015-08-21 08:52:13 +00:00
|
|
|
this.props.onPressOut && this.props.onPressOut(e);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandlePress: function(e: Event) {
|
2015-01-30 01:10:49 +00:00
|
|
|
this.clearTimeout(this._hideTimeout);
|
|
|
|
this._showUnderlay();
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
this._hideTimeout = this.setTimeout(this._hideUnderlay,
|
|
|
|
this.props.delayPressOut || 100);
|
2015-08-21 08:52:13 +00:00
|
|
|
this.props.onPress && this.props.onPress(e);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandleLongPress: function(e: Event) {
|
|
|
|
this.props.onLongPress && this.props.onLongPress(e);
|
2015-03-02 18:45:03 +00:00
|
|
|
},
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
touchableGetPressRectOffset: function() {
|
2015-11-16 21:51:13 +00:00
|
|
|
return this.props.pressRetentionOffset || PRESS_RETENTION_OFFSET;
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-02-17 00:50:35 +00:00
|
|
|
touchableGetHitSlop: function() {
|
|
|
|
return this.props.hitSlop;
|
|
|
|
},
|
|
|
|
|
[Touchable] Add custom delay props to Touchable components
Summary:
@public
This PR adds quite a bit of functionality to the Touchable components, allowing the ms delays of each of the handlers (`onPressIn, onPressOut, onPress, onLongPress`) to be configured.
It adds the following props to `TouchableWithoutFeedback, TouchableOpacity, and TouchableHighlight`:
```javascript
/**
* Delay in ms, from the release of the touch, before onPress is called.
*/
delayOnPress: React.PropTypes.number,
/**
* Delay in ms, from the start of the touch, before onPressIn is called.
*/
delayOnPressIn: React.PropTypes.number,
/**
* Delay in ms, from the release of the touch, before onPressOut is called.
*/
delayOnPressOut: React.PropTypes.number,
/**
* Delay in ms, from onPressIn, before onLongPress is called.
*/
delayOnLongPress: React.PropTypes.number,
```
`TouchableHighlight` also gets an additional set of props:
```javascript
/**
* Delay in ms, from the start of the touch, before the highlight is shown.
*/
delayHighlightShow: React.PropTypes.number,
/**
* Del
...
```
Closes https://github.com/facebook/react-native/pull/1255
Github Author: jmstout <git@jmstout.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-03 19:56:32 +00:00
|
|
|
touchableGetHighlightDelayMS: function() {
|
|
|
|
return this.props.delayPressIn;
|
|
|
|
},
|
|
|
|
|
|
|
|
touchableGetLongPressDelayMS: function() {
|
|
|
|
return this.props.delayLongPress;
|
|
|
|
},
|
|
|
|
|
|
|
|
touchableGetPressOutDelayMS: function() {
|
|
|
|
return this.props.delayPressOut;
|
|
|
|
},
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
_showUnderlay: function() {
|
2016-02-12 19:32:06 +00:00
|
|
|
if (!this.isMounted() || !this._hasPressHandler()) {
|
2015-07-17 23:47:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
this.refs[UNDERLAY_REF].setNativeProps(this.state.activeUnderlayProps);
|
|
|
|
this.refs[CHILD_REF].setNativeProps(this.state.activeProps);
|
2015-05-26 22:16:42 +00:00
|
|
|
this.props.onShowUnderlay && this.props.onShowUnderlay();
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_hideUnderlay: function() {
|
|
|
|
this.clearTimeout(this._hideTimeout);
|
|
|
|
this._hideTimeout = null;
|
2016-02-12 19:32:06 +00:00
|
|
|
if (this._hasPressHandler() && this.refs[UNDERLAY_REF]) {
|
2015-01-30 01:10:49 +00:00
|
|
|
this.refs[CHILD_REF].setNativeProps(INACTIVE_CHILD_PROPS);
|
2015-03-18 22:57:49 +00:00
|
|
|
this.refs[UNDERLAY_REF].setNativeProps({
|
|
|
|
...INACTIVE_UNDERLAY_PROPS,
|
|
|
|
style: this.state.underlayStyle,
|
|
|
|
});
|
2015-05-26 22:16:42 +00:00
|
|
|
this.props.onHideUnderlay && this.props.onHideUnderlay();
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-12 19:32:06 +00:00
|
|
|
_hasPressHandler: function() {
|
|
|
|
return !!(
|
2016-02-15 23:13:42 +00:00
|
|
|
this.props.onPress ||
|
|
|
|
this.props.onPressIn ||
|
|
|
|
this.props.onPressOut ||
|
2016-02-12 19:32:06 +00:00
|
|
|
this.props.onLongPress
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View
|
2016-06-21 15:24:07 +00:00
|
|
|
accessible={this.props.accessible !== false}
|
2016-02-04 13:12:36 +00:00
|
|
|
accessibilityLabel={this.props.accessibilityLabel}
|
2015-09-03 19:19:15 +00:00
|
|
|
accessibilityComponentType={this.props.accessibilityComponentType}
|
|
|
|
accessibilityTraits={this.props.accessibilityTraits}
|
2015-01-30 01:10:49 +00:00
|
|
|
ref={UNDERLAY_REF}
|
|
|
|
style={this.state.underlayStyle}
|
2015-09-01 17:31:20 +00:00
|
|
|
onLayout={this.props.onLayout}
|
2016-02-17 00:50:35 +00:00
|
|
|
hitSlop={this.props.hitSlop}
|
2015-01-30 01:10:49 +00:00
|
|
|
onStartShouldSetResponder={this.touchableHandleStartShouldSetResponder}
|
|
|
|
onResponderTerminationRequest={this.touchableHandleResponderTerminationRequest}
|
|
|
|
onResponderGrant={this.touchableHandleResponderGrant}
|
|
|
|
onResponderMove={this.touchableHandleResponderMove}
|
|
|
|
onResponderRelease={this.touchableHandleResponderRelease}
|
2015-08-11 17:41:15 +00:00
|
|
|
onResponderTerminate={this.touchableHandleResponderTerminate}
|
|
|
|
testID={this.props.testID}>
|
2015-11-09 14:31:37 +00:00
|
|
|
{React.cloneElement(
|
2015-01-30 01:10:49 +00:00
|
|
|
onlyChild(this.props.children),
|
|
|
|
{
|
|
|
|
ref: CHILD_REF,
|
|
|
|
}
|
|
|
|
)}
|
2016-04-14 21:27:35 +00:00
|
|
|
{Touchable.renderDebugView({color: 'green', hitSlop: this.props.hitSlop})}
|
2015-01-30 01:10:49 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var CHILD_REF = keyOf({childRef: null});
|
|
|
|
var UNDERLAY_REF = keyOf({underlayRef: null});
|
|
|
|
var INACTIVE_CHILD_PROPS = {
|
|
|
|
style: StyleSheet.create({x: {opacity: 1.0}}).x,
|
|
|
|
};
|
|
|
|
var INACTIVE_UNDERLAY_PROPS = {
|
|
|
|
style: StyleSheet.create({x: {backgroundColor: 'transparent'}}).x,
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = TouchableHighlight;
|