-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
162 lines (151 loc) · 3.33 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React from "react";
import PropTypes from "prop-types";
import {
ColorPropType,
Platform,
StyleSheet,
Text,
TouchableNativeFeedback,
TouchableOpacity,
View
} from "react-native";
import { Link } from "react-router-native";
let defaultBlue = "#2196F3";
if (Platform.OS === "ios") {
defaultBlue = "#0C42FD";
}
const styles = StyleSheet.create({
button: Platform.select({
ios: {},
android: {
elevation: 4,
backgroundColor: defaultBlue,
borderRadius: 2
}
}),
text: Platform.select({
ios: {
color: defaultBlue,
textAlign: "center",
padding: 8,
fontSize: 18
},
android: {
textAlign: "center",
color: "white",
padding: 8,
fontWeight: "500"
}
}),
buttonDisabled: Platform.select({
ios: {},
android: {
elevation: 0,
backgroundColor: "#dfdfdf"
}
}),
textDisabled: Platform.select({
ios: {
color: "#cdcdcd"
},
android: {
color: "#a1a1a1"
}
})
});
function RouterButton({
accessibilityLabel,
color,
title,
disabled,
testID,
replace,
to
}) {
const buttonStyles = [styles.button];
const textStyles = [styles.text];
const Touchable =
Platform.OS === "android" ? TouchableNativeFeedback : TouchableOpacity;
if (color && Platform.OS === "ios") {
textStyles.push({
color
});
} else if (color) {
buttonStyles.push({
backgroundColor: color
});
}
if (disabled) {
buttonStyles.push(styles.buttonDisabled);
textStyles.push(styles.textDisabled);
}
const formattedTitle =
Platform.OS === "android" ? title.toUpperCase() : title;
const accessibilityTraits = ["button"];
if (disabled) {
accessibilityTraits.push("disabled");
}
function TouchableWithProps({ children, onPress }) {
return (
<Touchable
accessibilityComponentType="button"
accessibilityLabel={accessibilityLabel}
accessibilityTraits={accessibilityTraits}
testID={testID}
disabled={disabled}
onPress={onPress}
>
{children}
</Touchable>
);
}
TouchableWithProps.propTypes = {
onPress: PropTypes.func.isRequired,
children: PropTypes.node.isRequired
};
return (
<Link to={to} replace={replace} component={TouchableWithProps}>
<View style={buttonStyles}>
<Text style={textStyles}>{formattedTitle}</Text>
</View>
</Link>
);
}
RouterButton.propTypes = {
/**
* Text to display inside the button
*/
title: PropTypes.string.isRequired,
/**
* Text to display for blindness accessibility features
*/
accessibilityLabel: PropTypes.string,
/**
* Color of the text (iOS), or background color of the button (Android)
*/
color: ColorPropType,
/**
* If true, disable all interactions for this component.
*/
disabled: PropTypes.bool,
/**
* If true, replace the current point in history rather than push
*/
replace: PropTypes.bool,
/**
* Handler to be called when the user taps the button
*/
testID: PropTypes.string,
/**
* Used to determine which path to link to
*/
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
};
RouterButton.defaultProps = {
replace: false,
disabled: false,
testID: null,
color: null,
accessibilityLabel: null
};
export default RouterButton;