You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ToolboxItem.native.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // @flow
  2. import React from 'react';
  3. import { Text, TouchableHighlight, View } from 'react-native';
  4. import { Icon } from '../../../base/font-icons';
  5. import AbstractToolboxItem from './AbstractToolboxItem';
  6. import type { Props } from './AbstractToolboxItem';
  7. /**
  8. * Native implementation of {@code AbstractToolboxItem}.
  9. */
  10. export default class ToolboxItem extends AbstractToolboxItem<Props> {
  11. /**
  12. * Transform the given (web) icon name into a name that works with
  13. * {@code Icon}.
  14. *
  15. * @private
  16. * @returns {string}
  17. */
  18. _getIconName() {
  19. const { iconName } = this.props;
  20. return iconName.replace('icon-', '').split(' ')[0];
  21. }
  22. /**
  23. * Renders the {@code Icon} part of this {@code ToolboxItem}.
  24. *
  25. * @private
  26. * @returns {ReactElement}
  27. */
  28. _renderIcon() {
  29. const { styles } = this.props;
  30. return (
  31. <Icon
  32. name = { this._getIconName() }
  33. style = { styles && styles.iconStyle } />
  34. );
  35. }
  36. /**
  37. * Renders this {@code ToolboxItem}. Invoked by {@link AbstractToolboxItem}.
  38. *
  39. * @override
  40. * @protected
  41. * @returns {ReactElement}
  42. */
  43. _renderItem() {
  44. const {
  45. accessibilityLabel,
  46. disabled,
  47. onClick,
  48. showLabel,
  49. styles
  50. } = this.props;
  51. let children = this._renderIcon();
  52. // XXX When using a wrapper View, apply the style to it instead of
  53. // applying it to the TouchableHighlight.
  54. let style = styles && styles.style;
  55. if (showLabel) {
  56. // XXX TouchableHighlight requires 1 child. If there's a need to
  57. // show both the icon and the label, then these two need to be
  58. // wrapped in a View.
  59. children = ( // eslint-disable-line no-extra-parens
  60. <View style = { style }>
  61. { children }
  62. <Text style = { styles && styles.labelStyle }>
  63. { this.label }
  64. </Text>
  65. </View>
  66. );
  67. // XXX As stated earlier, the style was applied to the wrapper View
  68. // (above).
  69. style = undefined;
  70. }
  71. return (
  72. <TouchableHighlight
  73. accessibilityLabel = { accessibilityLabel }
  74. disabled = { disabled }
  75. onPress = { onClick }
  76. style = { style }
  77. underlayColor = { styles && styles.underlayColor } >
  78. { children }
  79. </TouchableHighlight>
  80. );
  81. }
  82. }