2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +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-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* @providesModule TouchableOpacity
|
2015-12-02 03:09:01 +00:00
|
|
|
* @noflow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
// Note (avik): add @flow when Flow supports spread properties in propTypes
|
|
|
|
|
2015-07-20 23:29:40 +00:00
|
|
|
var Animated = require('Animated');
|
2016-12-13 19:21:48 +00:00
|
|
|
var Easing = require('Easing');
|
2016-11-04 12:40:26 +00:00
|
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
2015-02-20 04:10:52 +00:00
|
|
|
var React = require('React');
|
2017-04-12 23:09:48 +00:00
|
|
|
var PropTypes = require('prop-types');
|
[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 TimerMixin = require('react-timer-mixin');
|
2015-02-20 04:10:52 +00:00
|
|
|
var Touchable = require('Touchable');
|
2015-03-17 23:16:57 +00:00
|
|
|
var TouchableWithoutFeedback = require('TouchableWithoutFeedback');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2017-07-07 21:24:25 +00:00
|
|
|
var createReactClass = require('create-react-class');
|
[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');
|
2015-05-07 00:02:03 +00:00
|
|
|
var flattenStyle = require('flattenStyle');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
type Event = Object;
|
|
|
|
|
2015-11-16 21:51:13 +00:00
|
|
|
var PRESS_RETENTION_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-09 16:28:51 +00:00
|
|
|
* A wrapper for making views respond properly to touches.
|
2015-02-20 04:10:52 +00:00
|
|
|
* On press down, the opacity of the wrapped view is decreased, dimming it.
|
2017-02-27 09:49:36 +00:00
|
|
|
*
|
|
|
|
* Opacity is controlled by wrapping the children in an Animated.View, which is
|
|
|
|
* added to the view hiearchy. Be aware that this can affect layout.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-09 16:28:51 +00:00
|
|
|
* Example:
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-09 16:28:51 +00:00
|
|
|
* ```
|
|
|
|
* renderButton: function() {
|
|
|
|
* return (
|
|
|
|
* <TouchableOpacity onPress={this._onPressButton}>
|
|
|
|
* <Image
|
|
|
|
* style={styles.button}
|
2016-07-14 06:11:28 +00:00
|
|
|
* source={require('./myButton.png')}
|
2015-03-09 16:28:51 +00:00
|
|
|
* />
|
2015-03-28 22:26:15 +00:00
|
|
|
* </TouchableOpacity>
|
2015-03-09 16:28:51 +00:00
|
|
|
* );
|
|
|
|
* },
|
|
|
|
* ```
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2017-07-07 21:24:25 +00:00
|
|
|
var TouchableOpacity = createReactClass({
|
|
|
|
displayName: 'TouchableOpacity',
|
2015-07-20 23:29:40 +00:00
|
|
|
mixins: [TimerMixin, Touchable.Mixin, NativeMethodsMixin],
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
propTypes: {
|
2015-03-17 23:16:57 +00:00
|
|
|
...TouchableWithoutFeedback.propTypes,
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* Determines what the opacity of the wrapped view should be when touch is
|
2016-04-29 10:44:16 +00:00
|
|
|
* active. Defaults to 0.2.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
activeOpacity: PropTypes.number,
|
2016-12-19 14:26:07 +00:00
|
|
|
/**
|
|
|
|
* Apple TV parallax effects
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
tvParallaxProperties: PropTypes.object,
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2015-04-01 04:25:51 +00:00
|
|
|
activeOpacity: 0.2,
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
2015-07-20 23:29:40 +00:00
|
|
|
return {
|
|
|
|
...this.touchableGetInitialState(),
|
2017-03-02 05:28:53 +00:00
|
|
|
anim: new Animated.Value(this._getChildStyleOpacityWithDefault()),
|
2015-07-20 23:29:40 +00:00
|
|
|
};
|
2015-02-20 04:10:52 +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-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
[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
|
|
|
componentWillReceiveProps: function(nextProps) {
|
|
|
|
ensurePositiveDelayProps(nextProps);
|
|
|
|
},
|
|
|
|
|
2016-04-09 18:12:46 +00:00
|
|
|
/**
|
|
|
|
* Animate the touchable to a new opacity.
|
|
|
|
*/
|
2016-12-13 19:21:48 +00:00
|
|
|
setOpacityTo: function(value: number, duration: number) {
|
2015-07-20 23:29:40 +00:00
|
|
|
Animated.timing(
|
|
|
|
this.state.anim,
|
2016-12-13 19:21:48 +00:00
|
|
|
{
|
|
|
|
toValue: value,
|
|
|
|
duration: duration,
|
|
|
|
easing: Easing.inOut(Easing.quad),
|
|
|
|
useNativeDriver: true,
|
|
|
|
}
|
2015-07-20 23:29:40 +00:00
|
|
|
).start();
|
2015-02-20 04:10:52 +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) {
|
2016-11-14 17:29:18 +00:00
|
|
|
if (e.dispatchConfig.registrationName === 'onResponderGrant') {
|
|
|
|
this._opacityActive(0);
|
|
|
|
} else {
|
|
|
|
this._opacityActive(150);
|
|
|
|
}
|
2015-08-21 08:52:13 +00:00
|
|
|
this.props.onPressIn && this.props.onPressIn(e);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandleActivePressOut: function(e: Event) {
|
2016-12-13 19:21:48 +00:00
|
|
|
this._opacityInactive(250);
|
2015-08-21 08:52:13 +00:00
|
|
|
this.props.onPressOut && this.props.onPressOut(e);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandlePress: function(e: Event) {
|
|
|
|
this.props.onPress && this.props.onPress(e);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2015-08-21 08:52:13 +00:00
|
|
|
touchableHandleLongPress: function(e: Event) {
|
|
|
|
this.props.onLongPress && this.props.onLongPress(e);
|
2015-02-26 18:03:22 +00:00
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
touchableGetPressRectOffset: function() {
|
2015-11-16 21:51:13 +00:00
|
|
|
return this.props.pressRetentionOffset || PRESS_RETENTION_OFFSET;
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2016-02-17 00:50:35 +00:00
|
|
|
touchableGetHitSlop: function() {
|
|
|
|
return this.props.hitSlop;
|
|
|
|
},
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
touchableGetHighlightDelayMS: 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
|
|
|
return this.props.delayPressIn || 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
touchableGetLongPressDelayMS: function() {
|
|
|
|
return this.props.delayLongPress === 0 ? 0 :
|
|
|
|
this.props.delayLongPress || 500;
|
|
|
|
},
|
|
|
|
|
|
|
|
touchableGetPressOutDelayMS: function() {
|
|
|
|
return this.props.delayPressOut;
|
|
|
|
},
|
|
|
|
|
2016-11-14 17:29:18 +00:00
|
|
|
_opacityActive: function(duration: number) {
|
|
|
|
this.setOpacityTo(this.props.activeOpacity, duration);
|
[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
|
|
|
},
|
|
|
|
|
2016-12-13 19:21:48 +00:00
|
|
|
_opacityInactive: function(duration: number) {
|
[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.setOpacityTo(
|
2017-03-02 05:28:53 +00:00
|
|
|
this._getChildStyleOpacityWithDefault(),
|
2016-12-13 19:21:48 +00:00
|
|
|
duration
|
[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
|
|
|
);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
|
2017-03-02 05:28:53 +00:00
|
|
|
_getChildStyleOpacityWithDefault: function() {
|
|
|
|
var childStyle = flattenStyle(this.props.style) || {};
|
|
|
|
return childStyle.opacity == undefined ? 1 : childStyle.opacity;
|
|
|
|
},
|
2016-12-19 14:26:07 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
render: function() {
|
2015-07-20 23:29:40 +00:00
|
|
|
return (
|
|
|
|
<Animated.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-07-20 23:29:40 +00:00
|
|
|
style={[this.props.style, {opacity: this.state.anim}]}
|
2017-04-07 18:47:35 +00:00
|
|
|
nativeID={this.props.nativeID}
|
2015-07-20 23:29:40 +00:00
|
|
|
testID={this.props.testID}
|
2015-09-01 17:31:20 +00:00
|
|
|
onLayout={this.props.onLayout}
|
2016-12-19 14:26:07 +00:00
|
|
|
isTVSelectable={true}
|
|
|
|
tvParallaxProperties={this.props.tvParallaxProperties}
|
2016-02-17 00:50:35 +00:00
|
|
|
hitSlop={this.props.hitSlop}
|
2015-07-20 23:29:40 +00:00
|
|
|
onStartShouldSetResponder={this.touchableHandleStartShouldSetResponder}
|
|
|
|
onResponderTerminationRequest={this.touchableHandleResponderTerminationRequest}
|
|
|
|
onResponderGrant={this.touchableHandleResponderGrant}
|
|
|
|
onResponderMove={this.touchableHandleResponderMove}
|
|
|
|
onResponderRelease={this.touchableHandleResponderRelease}
|
|
|
|
onResponderTerminate={this.touchableHandleResponderTerminate}>
|
|
|
|
{this.props.children}
|
2016-04-14 21:27:35 +00:00
|
|
|
{Touchable.renderDebugView({color: 'cyan', hitSlop: this.props.hitSlop})}
|
2015-07-20 23:29:40 +00:00
|
|
|
</Animated.View>
|
|
|
|
);
|
2015-02-20 04:10:52 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TouchableOpacity;
|