forked from ide/react-native-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.js
111 lines (100 loc) · 2.81 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
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import {
StyleSheet,
Text,
TouchableOpacity,
View,
ViewPropTypes,
} from 'react-native';
import coalesceNonElementChildren from './coalesceNonElementChildren';
const systemButtonOpacity = 0.2;
export default class Button extends Component {
static propTypes = {
...TouchableOpacity.propTypes,
accessibilityLabel: PropTypes.string,
allowFontScaling: Text.propTypes.allowFontScaling,
containerStyle: ViewPropTypes.style,
disabledContainerStyle: ViewPropTypes.style,
disabled: PropTypes.bool,
style: Text.propTypes.style,
styleDisabled: Text.propTypes.style,
};
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;
}
return (
<TouchableOpacity
{...touchableProps}
testID={this.props.testID}
style={containerStyle}
accessibilityLabel={this.props.accessibilityLabel}
accessibilityTraits="button"
accessibilityComponentType="button">
{this._renderGroupedChildren()}
</TouchableOpacity>
);
}
_renderGroupedChildren() {
let { disabled } = this.props;
let style = [
styles.text,
disabled ? styles.disabledText : null,
this.props.style,
disabled ? this.props.styleDisabled : null,
];
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={styles.group}>{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',
},
});