123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- const React = require('react');
- const { Component } = require('react');
- const PropTypes = require('prop-types');
- const { Platform, ColorPropType, StyleSheet, TouchableNativeFeedback, TouchableOpacity, View, Text } = require('react-native');
-
- class Button extends Component {
-
- static propTypes = {
- title: PropTypes.string.isRequired,
- accessibilityLabel: PropTypes.string,
- color: ColorPropType,
- disabled: PropTypes.bool,
- hasTVPreferredFocus: PropTypes.bool,
- onPress: PropTypes.func,
- onPressIn: PropTypes.func,
- testID: PropTypes.string,
- };
-
- render() {
- const {
- accessibilityLabel,
- color,
- onPress,
- onPressIn,
- title,
- hasTVPreferredFocus,
- disabled,
- testID,
- } = this.props;
- const buttonStyles = [styles.button];
- const textStyles = [styles.text];
-
- if (color) {
- if (Platform.OS === 'ios') {
- textStyles.push({color: color});
- } else {
- buttonStyles.push({backgroundColor: color});
- }
- }
-
- const accessibilityTraits = ['button'];
- if (disabled) {
- buttonStyles.push(styles.buttonDisabled);
- textStyles.push(styles.textDisabled);
- accessibilityTraits.push('disabled');
- }
-
- const formattedTitle =
- Platform.OS === 'android' ? title.toUpperCase() : title;
- const Touchable =
- Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
- return (
- <Touchable
- accessibilityComponentType='button'
- accessibilityLabel={accessibilityLabel}
- accessibilityTraits={accessibilityTraits}
- hasTVPreferredFocus={hasTVPreferredFocus}
- testID={testID}
- disabled={disabled}
- onPress={onPress}
- onPressIn={onPressIn}>
- <View style={buttonStyles}>
- <Text style={textStyles} disabled={disabled}>
- {formattedTitle}
- </Text>
- </View>
- </Touchable>
- );
- }
- }
-
- const styles = StyleSheet.create({
- button: Platform.select({
- ios: {},
- android: {
- elevation: 4,
- backgroundColor: '#2196F3',
- borderRadius: 2,
- },
- }),
- text: Platform.select({
- ios: {
- color: '#007AFF',
- textAlign: 'center',
- padding: 8,
- fontSize: 18,
- },
- android: {
- color: 'white',
- textAlign: 'center',
- padding: 8,
- fontWeight: '500',
- },
- }),
- buttonDisabled: Platform.select({
- ios: {},
- android: {
- elevation: 0,
- backgroundColor: '#dfdfdf',
- },
- }),
- textDisabled: Platform.select({
- ios: {
- color: '#cdcdcd',
- },
- android: {
- color: '#a1a1a1',
- },
- }),
- });
-
- module.exports = Button;
|