forked from GeekyAnts/NativeBase
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButton.js
More file actions
156 lines (145 loc) · 4.51 KB
/
Button.js
File metadata and controls
156 lines (145 loc) · 4.51 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
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
import React, { Component } from "react";
import PropTypes from "prop-types";
import {
TouchableOpacity,
Platform,
View,
TouchableNativeFeedback,
StyleSheet
} from "react-native";
import { connectStyle } from "native-base-shoutem-theme";
import variable from "./../theme/variables/platform";
import { Text } from "./Text";
import computeProps from "../utils/computeProps";
import mapPropsToStyleNames from "../utils/mapPropsToStyleNames";
class Button extends Component {
static contextTypes = {
theme: PropTypes.object
};
getInitialStyle() {
return {
borderedBtn: {
borderWidth: this.props.bordered ? 1 : undefined,
borderRadius:
this.props.rounded && this.props.bordered
? variable.borderRadiusLarge
: 2
}
};
}
_root: React$Element<TouchableOpacity | TouchableNativeFeedback>;
prepareRootProps() {
const defaultProps = {
style: this.getInitialStyle().borderedBtn
};
if(Array.isArray(this.props.style)){
const flattenedStyle = this.props.style.reduce(( accumulator, currentValue ) => accumulator.concat(currentValue), []);
return computeProps({...this.props, style: flattenedStyle}, defaultProps);
}
return computeProps(this.props, defaultProps);
}
render() {
const variables = this.context.theme
? this.context.theme["@@shoutem.theme/themeStyle"].variables
: variable;
const children =
Platform.OS === "ios"
? this.props.children
: React.Children.map(
this.props.children,
child =>
child && child.type === Text
? React.cloneElement(child, {
uppercase: variables.btnUppercaseAndroidText,
...child.props
})
: child
);
if (
Platform.OS === "ios" ||
Platform.OS === "web" ||
variables.androidRipple === false ||
Platform["Version"] < 21
) {
return (
<TouchableOpacity
{...this.prepareRootProps()}
ref={c => (this._root = c)}
activeOpacity={
this.props.activeOpacity > 0 ? this.props.activeOpacity : 0.5
}
>
{children}
</TouchableOpacity>
);
} else {
if (this.props.rounded) {
let buttonStyle = { ...this.prepareRootProps().style };
let buttonFlex = (this.props.full || this.props.block) ? 1 : buttonStyle.flex;
return (
<View style={[{ maxHeight: buttonStyle.height }, buttonStyle, { paddingTop: undefined, paddingBottom: undefined }]} >
<TouchableNativeFeedback
ref={c => (this._root = c)}
background={
this.props.androidRippleColor
? TouchableNativeFeedback.Ripple(this.props.androidRippleColor, true)
: TouchableNativeFeedback.Ripple(variables.androidRippleColor, true)
}
{...this.prepareRootProps()}
>
<View style={[styles.childContainer, { paddingTop: buttonStyle.paddingTop, paddingBottom: buttonStyle.paddingBottom, height: buttonStyle.height, flexGrow: buttonFlex }]}>
{children}
</View>
</TouchableNativeFeedback>
</View >
);
} else {
return (
<TouchableNativeFeedback
ref={c => (this._root = c)}
onPress={this.props.onPress}
background={this.props.transparent ? TouchableNativeFeedback.Ripple('transparent') : TouchableNativeFeedback.Ripple(variables.androidRippleColor, false)}
{...this.prepareRootProps()}
>
<View {...this.prepareRootProps()}>{children}</View>
</TouchableNativeFeedback>
);
}
}
}
}
Button.propTypes = {
...TouchableOpacity.propTypes,
style: PropTypes.oneOfType([
PropTypes.object,
PropTypes.number,
PropTypes.array
]),
block: PropTypes.bool,
primary: PropTypes.bool,
transparent: PropTypes.bool,
success: PropTypes.bool,
danger: PropTypes.bool,
warning: PropTypes.bool,
info: PropTypes.bool,
bordered: PropTypes.bool,
disabled: PropTypes.bool,
rounded: PropTypes.bool,
large: PropTypes.bool,
small: PropTypes.bool,
active: PropTypes.bool
};
const styles = StyleSheet.create({
childContainer: {
flexShrink: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
},
});
const StyledButton = connectStyle(
"NativeBase.Button",
{},
mapPropsToStyleNames
)(Button);
export { StyledButton as Button };