forked from ide/react-native-button
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Button.js
140 lines (127 loc) · 3.66 KB
/
Button.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
TouchableOpacity,
TouchableNativeFeedback,
View
} from 'react-native';
import coalesceNonElementChildren from './coalesceNonElementChildren';
const systemButtonOpacity = 0.2;
export default class Button extends Component {
static propTypes = {
...TouchableOpacity.propTypes,
accessibilityLabel: PropTypes.string,
disabled: PropTypes.bool,
androidBackground: PropTypes.object,
};
render() {
let touchableProps = {
activeOpacity: this._computeActiveOpacity(),
};
let containerStyle = [
this.props.containerStyle,
this.props.disabled ? this.props.disabledContainerStyle : null
];
if (!this.props.disabled) {
touchableProps.onPress = this.props.onPress;
touchableProps.onPressIn = this.props.onPressIn;
touchableProps.onPressOut = this.props.onPressOut;
touchableProps.onLongPress = this.props.onLongPress;
touchableProps.delayPressIn = this.props.delayPressIn;
touchableProps.delayPressOut = this.props.delayPressOut;
touchableProps.delayLongPress = this.props.delayLongPress;
}
if (Platform.OS === 'ios') {
return (
<TouchableOpacity
{...touchableProps}
testID={this.props.testID}
style={containerStyle}
accessibilityLabel={this.props.accessibilityLabel}
accessibilityRole="button">
{this._renderGroupedChildren()}
</TouchableOpacity>
);
} else {
const background = this.props.androidBackground
? this.props.androidBackground
: TouchableNativeFeedback.SelectableBackground();
let padding = 0;
if (containerStyle[0] && containerStyle[0].padding) {
padding = containerStyle[0].padding;
const fixedStyle = Object.assign({}, containerStyle[0], {padding: 0});
containerStyle[0] = fixedStyle;
}
return (
<View style={containerStyle}>
<TouchableNativeFeedback
{...touchableProps}
style={{flex: 1}}
testID={this.props.testID}
accessibilityLabel={this.props.accessibilityLabel}
accessibilityRole="button"
background={background}>
<View style={{padding: padding}}>
{this._renderGroupedChildren()}
</View>
</TouchableNativeFeedback>
</View>
);
}
}
_renderGroupedChildren() {
let { disabled } = this.props;
let style = [
styles.text,
disabled ? styles.disabledText : null,
this.props.style,
disabled ? this.props.styleDisabled : null,
];
let childGroupStyle = [
styles.group,
this.props.childGroupStyle,
];
let children = coalesceNonElementChildren(this.props.children, (children, index) => {
return (
<Text key={index} style={style} allowFontScaling={this.props.allowFontScaling}>
{children}
</Text>
);
});
switch (children.length) {
case 0:
return null;
case 1:
return children[0];
default:
return <View style={childGroupStyle}>{children}</View>;
}
}
_computeActiveOpacity() {
if (this.props.disabled) {
return 1;
}
return this.props.activeOpacity != null ?
this.props.activeOpacity :
systemButtonOpacity;
}
};
const styles = StyleSheet.create({
text: {
color: '#007aff',
fontSize: 17,
fontWeight: '500',
textAlign: 'center',
},
disabledText: {
color: '#dcdcdc',
},
group: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
});