forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouchableHighlight.js
More file actions
106 lines (96 loc) · 2.42 KB
/
TouchableHighlight.js
File metadata and controls
106 lines (96 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, { Component } from 'react';
import GenericTouchable, { TOUCHABLE_STATE } from './GenericTouchable';
import { StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
/**
* TouchableHighlight follows RN's implementation
*/
export default class TouchableHighlight extends Component {
static defaultProps = {
...GenericTouchable.defaultProps,
activeOpacity: 0.85,
delayPressOut: 100,
underlayColor: 'black',
};
static propTypes = {
...GenericTouchable.publicPropTypes,
activeOpacity: PropTypes.number,
underlayColor: PropTypes.string,
style: PropTypes.object,
onShowUnderlay: PropTypes.func,
onHideUnderlay: PropTypes.func,
};
constructor(props) {
super(props);
this.state = {
extraChildStyle: null,
extraUnderlayStyle: {
backgroundColor: props.underlayColor,
},
};
}
// Copied from RN
showUnderlay = () => {
if (!this.hasPressHandler()) {
return;
}
this.setState({
extraChildStyle: {
opacity: this.props.activeOpacity,
},
extraUnderlayStyle: {
backgroundColor: this.props.underlayColor,
},
});
this.props.onShowUnderlay && this.props.onShowUnderlay();
};
hasPressHandler = () =>
this.props.onPress ||
this.props.onPressIn ||
this.props.onPressOut ||
this.props.onLongPress;
hideUnderlay = () => {
this.setState({
extraChildStyle: null,
extraUnderlayStyle: null,
});
this.props.onHideUnderlay && this.props.onHideUnderlay();
};
renderChildren() {
if (!this.props.children) {
return <View />;
}
const child = React.Children.only(this.props.children);
return React.cloneElement(child, {
style: StyleSheet.compose(
child.props.style,
this.state.extraChildStyle
),
});
}
onStateChange = (from, to) => {
if (to === TOUCHABLE_STATE.BEGAN) {
this.showUnderlay();
} else if (
to === TOUCHABLE_STATE.UNDETERMINED ||
to === TOUCHABLE_STATE.MOVED_OUTSIDE
) {
this.hideUnderlay();
}
};
render() {
const { style = {}, ...rest } = this.props;
const { extraUnderlayStyle } = this.state;
return (
<GenericTouchable
{...rest}
style={{
...style,
...extraUnderlayStyle,
}}
onStateChange={this.onStateChange}>
{this.renderChildren()}
</GenericTouchable>
);
}
}