forked from MatthieuPeyrot/react-native-swipeout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativeButton.js
123 lines (110 loc) · 3.07 KB
/
NativeButton.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
import React, { Component } from "react";
import PropTypes from "prop-types";
import createReactClass from "create-react-class";
import {
TouchableWithoutFeedback,
TouchableNativeFeedback,
TouchableHighlight,
Text,
StyleSheet,
Platform,
View
} from "react-native";
const styles = StyleSheet.create({
button: {
flexDirection: "row",
alignSelf: "stretch",
justifyContent: "center",
borderRadius: 5,
marginLeft: 5
},
textButton: {
fontSize: 14,
alignSelf: "center"
},
opacity: {
opacity: 0.8
}
});
const NativeButton = createReactClass({
propTypes: {
// Extract parent props
...TouchableWithoutFeedback.propTypes,
textStyle: Text.propTypes.style,
disabledStyle: Text.propTypes.style,
children: PropTypes.node.isRequired,
underlayColor: PropTypes.string,
background: TouchableNativeFeedback.propTypes
? TouchableNativeFeedback.propTypes.background
: PropTypes.any
},
statics: {
isAndroid: Platform.OS === "android"
},
getDefaultProps: function() {
return {
textStyle: null,
disabledStyle: null,
underlayColor: null
};
},
_renderText: function() {
// If children is not a string don't wrapp it in a Text component
if (typeof this.props.children !== "string") {
return this.props.children;
}
return (
<Text numberOfLines={1} style={[styles.textButton, this.props.textStyle]}>
{this.props.children}
</Text>
);
},
render: function() {
const disabledStyle = this.props.disabled
? this.props.disabledStyle || styles.opacity
: {};
// Extract Button props
let buttonProps = {
accessibilityComponentType: this.props.accessibilityComponentType,
accessibilityTraits: this.props.accessibilityTraits,
accessible: this.props.accessible,
delayLongPress: this.props.delayLongPress,
delayPressIn: this.props.delayPressIn,
delayPressOut: this.props.delayPressOut,
disabled: this.props.disabled,
hitSlop: this.props.hitSlop,
onLayout: this.props.onLayout,
onPress: this.props.onPress,
onPressIn: this.props.onPressIn,
onPressOut: this.props.onPressOut,
onLongPress: this.props.onLongPress,
pressRetentionOffset: this.props.pressRetentionOffset
};
// Render Native Android Button
if (NativeButton.isAndroid) {
buttonProps = Object.assign(buttonProps, {
background:
this.props.background ||
TouchableNativeFeedback.SelectableBackground()
});
return (
<TouchableNativeFeedback {...buttonProps}>
<View style={[styles.button, this.props.style, disabledStyle]}>
{this._renderText()}
</View>
</TouchableNativeFeedback>
);
}
// Render default button
return (
<TouchableHighlight
{...buttonProps}
style={[styles.button, this.props.style, disabledStyle]}
underlayColor={this.props.underlayColor}
>
{this._renderText()}
</TouchableHighlight>
);
}
});
export default NativeButton;