Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import {Platform, StyleSheet, Text, TouchableNativeFeedback, TouchableOpacity, View} from 'react-native';
  3.  
  4. class Button extends React.Component{
  5.   render() {
  6.     const {
  7.             accessibilityLabel,
  8.             color,
  9.             backgroundColor,
  10.       onPress,
  11.       touchSoundDisabled,
  12.       title,
  13.       hasTVPreferredFocus,
  14.       nextFocusDown,
  15.       nextFocusForward,
  16.       nextFocusLeft,
  17.       nextFocusRight,
  18.       nextFocusUp,
  19.       disabled,
  20.       testID,
  21.     } = this.props;
  22.     const buttonStyles = [styles.button];
  23.     const textStyles = [styles.text];
  24.     if (color) textStyles.push({color: color});
  25.     const accessibilityState = {};
  26.     if (disabled) {
  27.       buttonStyles.push(styles.buttonDisabled);
  28.       textStyles.push(styles.textDisabled);
  29.       accessibilityState.disabled = true;
  30.         }
  31.         if(backgroundColor) buttonStyles.push({backgroundColor: backgroundColor})
  32.     const formattedTitle =
  33.       Platform.OS === 'android' ? title.toUpperCase() : title;
  34.     const Touchable =
  35.       Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
  36.     return (
  37.       <Touchable
  38.         accessibilityLabel={accessibilityLabel}
  39.         accessibilityRole="button"
  40.         accessibilityState={accessibilityState}
  41.         hasTVPreferredFocus={hasTVPreferredFocus}
  42.         nextFocusDown={nextFocusDown}
  43.         nextFocusForward={nextFocusForward}
  44.         nextFocusLeft={nextFocusLeft}
  45.         nextFocusRight={nextFocusRight}
  46.         nextFocusUp={nextFocusUp}
  47.         testID={testID}
  48.         disabled={disabled}
  49.         onPress={onPress}
  50.         touchSoundDisabled={touchSoundDisabled}>
  51.         <View style={buttonStyles}>
  52.           <Text style={textStyles} disabled={disabled}>
  53.             {formattedTitle}
  54.           </Text>
  55.         </View>
  56.       </Touchable>
  57.     );
  58.   }
  59. }
  60.  
  61. const styles = StyleSheet.create({
  62.   button: Platform.select({
  63.     ios: {},
  64.     android: {
  65.       elevation: 4,
  66.       backgroundColor: '#2196F3',
  67.       borderRadius: 2,
  68.     },
  69.   }),
  70.   text: {
  71.     textAlign: 'center',
  72.     margin: 8,
  73.     ...Platform.select({
  74.       ios: {
  75.         color: '#007AFF',
  76.         fontSize: 18,
  77.       },
  78.       android: {
  79.         color: 'white',
  80.         fontWeight: '500',
  81.       },
  82.     }),
  83.   },
  84.   buttonDisabled: Platform.select({
  85.     ios: {},
  86.     android: {
  87.       elevation: 0,
  88.       backgroundColor: '#dfdfdf',
  89.     },
  90.   }),
  91.   textDisabled: Platform.select({
  92.     ios: {
  93.       color: '#cdcdcd',
  94.     },
  95.     android: {
  96.       color: '#a1a1a1',
  97.     },
  98.   }),
  99. });
  100.  
  101. export default Button
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement