您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SideBarItem.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Linking, Text, TouchableOpacity, View } from 'react-native';
  4. import { Icon } from '../../base/font-icons';
  5. import { translate } from '../../base/i18n';
  6. import styles from './styles';
  7. type Props = {
  8. /**
  9. * The icon of the item.
  10. */
  11. icon: string,
  12. /**
  13. * The i18n label of the item.
  14. */
  15. label: string,
  16. /**
  17. * The function to be invoked when the item is pressed
  18. * if the item is a button.
  19. */
  20. onPress: Function,
  21. /**
  22. * The translate function.
  23. */
  24. t: Function,
  25. /**
  26. * The URL of the link, if this item is a link.
  27. */
  28. url: string
  29. };
  30. /**
  31. * A component rendering an item in the system sidebar.
  32. */
  33. class SideBarItem extends Component<Props> {
  34. /**
  35. * Initializes a new {@code SideBarItem} instance.
  36. *
  37. * @inheritdoc
  38. */
  39. constructor(props: Props) {
  40. super(props);
  41. // Bind event handlers so they are only bound once per instance.
  42. this._onOpenURL = this._onOpenURL.bind(this);
  43. }
  44. /**
  45. * Implements React's {@link Component#render()}, renders the sidebar item.
  46. *
  47. * @inheritdoc
  48. * @returns {ReactElement}
  49. */
  50. render() {
  51. const { label, onPress, t } = this.props;
  52. const onPressCalculated
  53. = typeof onPress === 'function' ? onPress : this._onOpenURL;
  54. return (
  55. <TouchableOpacity
  56. onPress = { onPressCalculated }
  57. style = { styles.sideBarItem }>
  58. <View style = { styles.sideBarItemButtonContainer }>
  59. <Icon
  60. name = { this.props.icon }
  61. style = { styles.sideBarItemIcon } />
  62. <Text style = { styles.sideBarItemText }>
  63. { t(label) }
  64. </Text>
  65. </View>
  66. </TouchableOpacity>
  67. );
  68. }
  69. _onOpenURL: () => void;
  70. /**
  71. * Opens the URL if one is provided.
  72. *
  73. * @private
  74. * @returns {void}
  75. */
  76. _onOpenURL() {
  77. const { url } = this.props;
  78. if (typeof url === 'string') {
  79. Linking.openURL(url);
  80. }
  81. }
  82. }
  83. export default translate(SideBarItem);