-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (53 loc) · 1.53 KB
/
index.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
/**
* @flow
*/
import { Button } from 'native-base';
import React from 'react';
import { TouchableOpacity } from 'react-native';
export interface Props extends TouchableOpacity {
onDoublePress?: Function;
onPress?: Function;
useNativeBase?: boolean;
delay?: Number;
}
class ButtonWrapper extends React.Component<Props> {
pressedOnce = false;
lastClickedAt = 0;
timeout: any;
onPressHandler = () => {
const { onDoublePress, onPress,delay = 500 } = this.props;
const supportsDoublePress = onDoublePress && typeof onDoublePress === 'function';
const supportsSinglePress = onPress && typeof onPress === 'function';
if (supportsDoublePress) {
if (this.pressedOnce) {
clearTimeout(this.timeout);
this.pressedOnce = false;
onDoublePress();
} else {
this.timeout = setTimeout(() => {
this.pressedOnce = false;
if (supportsSinglePress) {
onPress();
}
}, 200);
this.pressedOnce = true;
}
} else {
const newTime = new Date().getTime();
if (newTime - this.lastClickedAt > delay) {
if (supportsSinglePress) {
onPress();
}
}
this.lastClickedAt = newTime;
}
};
render() {
const { useNativeBase = false, ...remainingProps } = this.props;
if (useNativeBase) {
return <Button {...remainingProps} onPress={this.onPressHandler} />;
}
return <TouchableOpacity {...remainingProps} onPress={this.onPressHandler} />;
}
}
export default ButtonWrapper;