forked from prscX/react-native-tooltips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNTooltips.js
170 lines (154 loc) · 4.23 KB
/
RNTooltips.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { PureComponent } from "react";
import {
findNodeHandle,
ViewPropTypes,
NativeModules,
Platform
} from "react-native";
import PropTypes from "prop-types";
let { RNTooltips } = NativeModules;
class Tooltips extends PureComponent {
static POSITION: {
LEFT: 1,
RIGHT: 2,
TOP: 3,
BOTTOM: 4
};
static ALIGN: {
START: 1,
CENTER: 2,
END: 3
};
static GRAVITY: {
START: 1,
CENTER: 2,
END: 3
};
static propTypes = {
...ViewPropTypes,
text: PropTypes.string,
position: PropTypes.number,
align: PropTypes.number,
autoHide: PropTypes.bool,
duration: PropTypes.number,
clickToHide: PropTypes.bool,
corner: PropTypes.number,
tintColor: PropTypes.string,
textColor: PropTypes.string,
textSize: PropTypes.number,
gravity: PropTypes.number,
shadow: PropTypes.bool,
arrow: PropTypes.bool,
visible: PropTypes.bool,
target: PropTypes.object,
parent: PropTypes.object,
onHide: PropTypes.func
};
static defaultProps = {
text: "",
position: 4,
align: 2,
autoHide: true,
duration: Platform.OS === "android" ? 4000 : 4,
clickToHide: false,
corner: Platform.OS === "android" ? 30 : 0,
tintColor: "#1F7C82",
textColor: "#FFFFFF",
textSize: 12,
gravity: 2,
arrow: true,
shadow: true
};
static Show(target, parent, props) {
if (typeof target !== "number") {
target = findNodeHandle(target);
}
if (typeof parent !== "number") {
parent = findNodeHandle(parent);
}
if (props.text === undefined) {
props.text = Tooltips.defaultProps.text;
}
if (props.position === undefined) {
props.position = Tooltips.defaultProps.position;
}
if (props.align === undefined) {
props.align = Tooltips.defaultProps.align;
}
if (props.autoHide === undefined) {
props.autoHide = Tooltips.defaultProps.autoHide;
}
if (props.duration === undefined) {
props.duration = Tooltips.defaultProps.duration;
}
if (props.clickToHide === undefined) {
props.clickToHide = Tooltips.defaultProps.clickToHide;
}
if (props.corner === undefined) {
props.corner = Tooltips.defaultProps.corner;
}
if (props.tintColor === undefined) {
props.tintColor = Tooltips.defaultProps.tintColor;
}
if (props.textColor === undefined) {
props.textColor = Tooltips.defaultProps.textColor;
}
if (props.textSize === undefined) {
props.textSize = Tooltips.defaultProps.textSize;
}
if (props.gravity === undefined) {
props.gravity = Tooltips.defaultProps.gravity;
}
if (props.shadow === undefined) {
props.shadow = Tooltips.defaultProps.shadow;
}
if (props.arrow === undefined) {
props.arrow = Tooltips.defaultProps.arrow;
}
RNTooltips.Show(target, parent, props, () => {
props.onHide && props.onHide();
});
}
static Dismiss(target) {
if (typeof target !== "number") {
target = findNodeHandle(target);
}
RNTooltips.Dismiss(target);
}
componentDidUpdate() {
if (this.props.visible === true && this.props.target && this.props.parent) {
Tooltips.Show(
findNodeHandle(this.props.target),
findNodeHandle(this.props.parent),
{
text: this.props.text,
position: this.props.position,
align: this.props.align,
autoHide: this.props.autoHide,
duration: this.props.duration,
clickToHide: this.props.clickToHide,
corner: this.props.corner,
tintColor: this.props.tintColor,
textColor: this.props.textColor,
textSize: this.props.textSize,
arrow: this.props.arrow,
gravity: this.props.gravity,
shadow: this.props.shadow,
onHide: this.props.onHide
}
);
} else if (this.props.visible === false && this.props.target) {
Tooltips.Dismiss(findNodeHandle(this.props.target));
}
}
componentWillUnmount() {
if (Platform.OS === "android" && this.props.target) {
// this isn't required for iOS, but android tooltips have issues to disappear
Tooltips.Dismiss(findNodeHandle(this.props.target));
}
}
render() {
return null;
}
}
export default Tooltips;