forked from chirag04/react-native-tooltip
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ToolTip.ios.js
89 lines (69 loc) · 2.01 KB
/
ToolTip.ios.js
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
'use strict';
import React from 'react-native';
const {
requireNativeComponent,
TouchableHighlight,
View,
} = React;
const ToolTipMenu = React.NativeModules.ToolTipMenu;
const RCTToolTipText = requireNativeComponent('RCTToolTipText', null);
const propTypes = {
actions: React.PropTypes.arrayOf(React.PropTypes.shape({
text: React.PropTypes.string.isRequired,
onPress: React.PropTypes.func,
})),
longPress: React.PropTypes.bool,
...TouchableHighlight.propTypes,
};
class ToolTipText extends React.Component {
constructor(props) {
super(props);
this.handleToolTipTextChange = this.handleToolTipTextChange.bind(this);
this.handleTextPress = this.handleTextPress.bind(this);
}
getOptionTexts() {
return this.props.actions.map((option) => option.text);
}
// Assuming there is no actions with the same text
getCallback(optionText) {
const selectedOption = this.props.actions.find((option) => option.text === optionText);
if (selectedOption) {
return selectedOption.onPress;
}
return null;
}
getTouchableHighlightProps() {
let props = {};
Object.keys(TouchableHighlight.propTypes).forEach((key) => props[key] = this.props[key]);
if (this.props.longPress) {
props.onLongPress = this.handleTextPress;
} else {
props.onPress = this.handleTextPress;
}
return props;
}
handleTextPress() {
ToolTipMenu.show(React.findNodeHandle(this.refs.toolTipText), this.getOptionTexts());
}
handleToolTipTextChange(event) {
const callback = this.getCallback(event.nativeEvent.text);
if (callback) {
callback(event);
}
}
render() {
return (
<RCTToolTipText ref='toolTipText' onChange={this.handleToolTipTextChange}>
<TouchableHighlight
{...this.getTouchableHighlightProps()}
>
<View>
{this.props.children}
</View>
</TouchableHighlight>
</RCTToolTipText>
);
}
}
ToolTipText.propTypes = propTypes;
export default ToolTipText;