react-native-navigation的迁移库

Button.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const React = require('react');
  2. const { Component } = require('react');
  3. const PropTypes = require('prop-types');
  4. const { Platform, ColorPropType, StyleSheet, TouchableNativeFeedback, TouchableOpacity, View, Text } = require('react-native');
  5. class Button extends Component {
  6. static propTypes = {
  7. title: PropTypes.string.isRequired,
  8. accessibilityLabel: PropTypes.string,
  9. color: ColorPropType,
  10. disabled: PropTypes.bool,
  11. hasTVPreferredFocus: PropTypes.bool,
  12. onPress: PropTypes.func,
  13. onPressIn: PropTypes.func,
  14. testID: PropTypes.string,
  15. };
  16. render() {
  17. const {
  18. accessibilityLabel,
  19. color,
  20. onPress,
  21. onPressIn,
  22. title,
  23. hasTVPreferredFocus,
  24. disabled,
  25. testID,
  26. } = this.props;
  27. const buttonStyles = [styles.button];
  28. const textStyles = [styles.text];
  29. if (color) {
  30. if (Platform.OS === 'ios') {
  31. textStyles.push({color: color});
  32. } else {
  33. buttonStyles.push({backgroundColor: color});
  34. }
  35. }
  36. const accessibilityTraits = ['button'];
  37. if (disabled) {
  38. buttonStyles.push(styles.buttonDisabled);
  39. textStyles.push(styles.textDisabled);
  40. accessibilityTraits.push('disabled');
  41. }
  42. const formattedTitle =
  43. Platform.OS === 'android' ? title.toUpperCase() : title;
  44. const Touchable =
  45. Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
  46. return (
  47. <Touchable
  48. accessibilityComponentType='button'
  49. accessibilityLabel={accessibilityLabel}
  50. accessibilityTraits={accessibilityTraits}
  51. hasTVPreferredFocus={hasTVPreferredFocus}
  52. testID={testID}
  53. disabled={disabled}
  54. onPress={onPress}
  55. onPressIn={onPressIn}>
  56. <View style={buttonStyles}>
  57. <Text style={textStyles} disabled={disabled}>
  58. {formattedTitle}
  59. </Text>
  60. </View>
  61. </Touchable>
  62. );
  63. }
  64. }
  65. const styles = StyleSheet.create({
  66. button: Platform.select({
  67. ios: {},
  68. android: {
  69. elevation: 4,
  70. backgroundColor: '#2196F3',
  71. borderRadius: 2,
  72. },
  73. }),
  74. text: Platform.select({
  75. ios: {
  76. color: '#007AFF',
  77. textAlign: 'center',
  78. padding: 8,
  79. fontSize: 18,
  80. },
  81. android: {
  82. color: 'white',
  83. textAlign: 'center',
  84. padding: 8,
  85. fontWeight: '500',
  86. },
  87. }),
  88. buttonDisabled: Platform.select({
  89. ios: {},
  90. android: {
  91. elevation: 0,
  92. backgroundColor: '#dfdfdf',
  93. },
  94. }),
  95. textDisabled: Platform.select({
  96. ios: {
  97. color: '#cdcdcd',
  98. },
  99. android: {
  100. color: '#a1a1a1',
  101. },
  102. }),
  103. });
  104. module.exports = Button;