Repository navigation
Expand file tree
/
Copy pathButton.js
More file actions
155 lines (125 loc) · 6.28 KB
/
Copy pathButton.js
File metadata and controls
155 lines (125 loc) · 6.28 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
/* @flow */
'use strict';
import React from 'react';
import { Text, TouchableOpacity } from 'react-native';
import NativeBaseComponent from '../Base/NativeBaseComponent';
import computeProps from '../../Utils/computeProps';
import Icon from './Icon';
import _ from 'lodash';
export default class Button extends NativeBaseComponent {
getInitialStyle() {
return {
button: {
padding: 15,
justifyContent: 'space-around',
flexDirection: 'row',
alignSelf: 'center',
alignItems: 'center',
backgroundColor: this.getTheme().btnPrimaryBg,
elevation: (this.props.transparent) ? 0 : 2,
shadowColor: '#000',
shadowOffset: {width: 0, height: 2},
shadowOpacity: 0.1,
shadowRadius: 1.5
}
}
}
prepareRootProps() {
var type = {
backgroundColor: (this.props.primary) ? this.getTheme().btnPrimaryBg :
((this.props.transparent) || (this.props.bordered)) ? 'rgba(0,0,0,0)' :
(this.props.success) ? this.getTheme().btnSuccessBg :
(this.props.danger) ? this.getTheme().btnDangerBg :
(this.props.warning) ? this.getTheme().btnWarningBg :
(this.props.info) ? this.getTheme().btnInfoBg :
(this.props.backgroundColor) ? this.props.backgroundColor :
this.getInitialStyle().button.backgroundColor,
borderRadius: (this.props.rounded) ? this.getTheme().borderRadiusLarge : this.getTheme().borderRadiusBase,
borderWidth: (this.props.bordered) ? 1 : 0,
borderColor: (this.props.primary) ? this.getTheme().primary :
(this.props.success) ? this.getTheme().btnSuccessBg :
(this.props.danger) ? this.getTheme().btnDangerBg :
(this.props.warning) ? this.getTheme().btnWarningBg :
(this.props.info) ? this.getTheme().btnInfoBg :
this.getInitialStyle().button.backgroundColor,
height: (this.props.large) ? 60 : (this.props.small) ? 35 : 45,
alignSelf: (this.props.block) ? 'stretch' : 'flex-start'
}
var addedProps = _.merge(this.getInitialStyle().button,type);
var defaultProps = {
style: addedProps
}
return computeProps(this.props, defaultProps);
}
getTextStyle() {
var mergedStyle = {};
var btnType = {
paddingRight : 5,
paddingLeft : 3,
color:
((this.props.bordered) && (this.props.primary)) ? this.getTheme().btnPrimaryBg :
((this.props.bordered) && (this.props.success)) ? this.getTheme().btnSuccessBg :
((this.props.bordered) && (this.props.danger)) ? this.getTheme().btnDangerBg :
((this.props.bordered) && (this.props.warning)) ? this.getTheme().btnWarningBg :
((this.props.bordered) && (this.props.info)) ? this.getTheme().btnInfoBg :
((this.props.bordered)) ? this.getTheme().btnPrimaryBg :
(this.props.transparent) ? this.getContextForegroundColor() :
this.getTheme().inverseTextColor,
fontSize: (this.props.large) ? this.getTheme().btnTextSizeLarge : (this.props.small) ? this.getTheme().btnTextSizeSmall : this.getTheme().btnTextSize,
lineHeight: (this.props.large) ? 29 : (this.props.small) ? 13 : 20
}
return _.merge(mergedStyle, btnType, this.props.textStyle);
}
getIconProps(icon) {
var defaultStyle = {
color:
((this.props.bordered) && (this.props.primary)) ? this.getTheme().btnPrimaryBg :
((this.props.bordered) && (this.props.success)) ? this.getTheme().btnSuccessBg :
((this.props.bordered) && (this.props.danger)) ? this.getTheme().btnDangerBg :
((this.props.bordered) && (this.props.warning)) ? this.getTheme().btnWarningBg :
((this.props.bordered) && (this.props.info)) ? this.getTheme().btnInfoBg :
(this.props.bordered) ? this.getTheme().btnPrimaryBg :
(this.props.transparent) ? this.getContextForegroundColor() :
this.getTheme().inverseTextColor,
fontSize: (this.props.large) ? this.getTheme().iconSizeLarge : (this.props.small) ? this.getTheme().iconSizeSmall : this.getTheme().iconFontSize,
lineHeight: (this.props.large) ? 52: (this.props.small) ? 22 : 39
}
var defaultProps = {
style: defaultStyle
}
return computeProps(icon.props, defaultProps);
}
renderChildren() {
if(typeof this.props.children == "string") {
return <Text style={this.getTextStyle()}>{this.props.children}</Text>
}
else if(Array.isArray(this.props.children)) {
var newChildren = [];
var childrenArray = React.Children.toArray(this.props.children);
var iconElement = [];
iconElement = _.remove(childrenArray, function(item) {
if(item.type == Icon) {
return true;
}
});
if(this.props.iconRight) {
newChildren.push(<Text style={this.getTextStyle()}>{childrenArray[0]}</Text>);
newChildren.push(<Text>{React.cloneElement(iconElement[0], this.getIconProps(iconElement[0]))}</Text>);
}
else if(this.props.iconLeft || iconElement) {
newChildren.push(<Text>{React.cloneElement(iconElement[0], this.getIconProps(iconElement[0]))}</Text>);
newChildren.push(<Text style={this.getTextStyle()}>{childrenArray[0]}</Text>);
}
return newChildren;
}
else
return React.cloneElement(this.props.children, this.getIconProps(this.props.children));
}
render() {
return(
<TouchableOpacity {...this.prepareRootProps()} >
{this.renderChildren()}
</TouchableOpacity>
);
}
}