forked from marcosrdz/react-native-tooltip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolTip.ios.js
More file actions
86 lines (71 loc) · 2.24 KB
/
ToolTip.ios.js
File metadata and controls
86 lines (71 loc) · 2.24 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
'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
};
toolTipText = React.createRef();
showMenu = () => {
ToolTipMenu.show(findNodeHandle(this.toolTipText.current), 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;
};
handleToolTipTextChange = (event) => {
const callback = this.getCallback(event.nativeEvent.text);
if (callback) {
callback(event);
}
};
handleBlurToolTip = () => {
this.hideMenu();
};
render() {
const { children, ...props } = this.props;
return (
<RCTToolTipText ref={this.toolTipText} onChange={this.handleToolTipTextChange} onBlur={this.handleBlurToolTip}>
<TouchableHighlight {...props}>
<View>
{children}
</View>
</TouchableHighlight>
</RCTToolTipText>
);
}
}