-
Notifications
You must be signed in to change notification settings - Fork 71
/
ToolTip.ios.js
98 lines (81 loc) · 2.56 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
90
91
92
93
94
95
96
97
98
'use strict';
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {
requireNativeComponent,
TouchableHighlight,
View,
NativeModules,
findNodeHandle,
} from 'react-native';
const ToolTipMenu = NativeModules.ToolTipMenu;
const RCTToolTipText = requireNativeComponent('RCTToolTipText', null);
export default class ToolTip extends PureComponent {
static propTypes = {
actions: PropTypes.arrayOf(PropTypes.shape({
text: PropTypes.string.isRequired,
onPress: PropTypes.func
})),
arrowDirection: PropTypes.oneOf(['up', 'down', 'left', 'right', 'default']),
longPress: PropTypes.bool,
onHide: PropTypes.func,
onShow: PropTypes.func,
...TouchableHighlight.propTypes
};
static defaultProps = {
arrowDirection: 'default',
onHide: () => true,
onShow: () => true
};
showMenu = () => {
ToolTipMenu.show(findNodeHandle(this.refs.toolTipText), this.getOptionTexts(), this.props.arrowDirection);
this.props.onShow();
};
hideMenu = () => {
ToolTipMenu.hide();
this.props.onHide();
};
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 = () => {
const props = {};
Object.keys(TouchableHighlight.propTypes).forEach((key) => props[key] = this.props[key]);
if (this.props.longPress) {
props.onLongPress = this.showMenu;
} else {
props.onPress = this.showMenu;
}
return props;
};
handleToolTipTextChange = (event) => {
const callback = this.getCallback(event.nativeEvent.text);
if (callback) {
callback(event);
}
};
handleBlurToolTip = () => {
this.hideMenu();
};
render() {
return (
<RCTToolTipText ref='toolTipText' onChange={this.handleToolTipTextChange} onBlur={this.handleBlurToolTip}>
<TouchableHighlight
{...this.getTouchableHighlightProps()}
>
<View>
{this.props.children}
</View>
</TouchableHighlight>
</RCTToolTipText>
);
}
}