This repository was archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathPlatformTouchable.js
85 lines (76 loc) · 2.48 KB
/
PlatformTouchable.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
import React from 'react';
import {
Platform,
TouchableNativeFeedback,
TouchableOpacity,
TouchableWithoutFeedback,
View,
} from 'react-native';
let TouchableComponent;
if (Platform.OS === 'android') {
TouchableComponent =
Platform.Version <= 20 ? TouchableOpacity : TouchableNativeFeedback;
} else {
TouchableComponent = TouchableOpacity;
}
if (TouchableComponent !== TouchableNativeFeedback) {
TouchableComponent.SelectableBackground = () => ({});
TouchableComponent.SelectableBackgroundBorderless = () => ({});
TouchableComponent.Ripple = () => ({});
TouchableComponent.canUseNativeForeground = () => false;
}
export default class PlatformTouchable extends React.Component {
static SelectableBackground = TouchableComponent.SelectableBackground;
static SelectableBackgroundBorderless = TouchableComponent.SelectableBackgroundBorderless;
static Ripple = TouchableComponent.Ripple;
static canUseNativeForeground = TouchableComponent.canUseNativeForeground;
render() {
let {
children,
style,
foreground,
background,
useForeground,
...props
} = this.props;
// Even though it works for TouchableWithoutFeedback and
// TouchableNativeFeedback with this component, we want
// the API to be the same for all components so we require
// exactly one direct child for every touchable type.
children = React.Children.only(children);
if (TouchableComponent === TouchableNativeFeedback) {
useForeground =
foreground && TouchableNativeFeedback.canUseNativeForeground();
if (foreground && background) {
console.warn(
'Specified foreground and background for Touchable, only one can be used at a time. Defaulted to foreground.'
);
}
return (
<TouchableComponent
{...props}
useForeground={useForeground}
background={(useForeground && foreground) || background}>
<View style={style}>
{children}
</View>
</TouchableComponent>
);
} else if (TouchableComponent === TouchableWithoutFeedback) {
return (
<TouchableWithoutFeedback {...props}>
<View style={style}>
{children}
</View>
</TouchableWithoutFeedback>
);
} else {
let TouchableFallback = this.props.fallback || TouchableComponent;
return (
<TouchableFallback {...props} style={style}>
{children}
</TouchableFallback>
);
}
}
}