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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. disabled,
  46. onClick,
  47. showLabel,
  48. styles
  49. } = this.props;
  50. let children = this._renderIcon();
  51. // XXX When using a wrapper View, apply the style to it instead of
  52. // applying it to the TouchableHighlight.
  53. let style = styles && styles.style;
  54. if (showLabel) {
  55. // XXX TouchableHighlight requires 1 child. If there's a need to
  56. // show both the icon and the label, then these two need to be
  57. // wrapped in a View.
  58. children = ( // eslint-disable-line no-extra-parens
  59. <View style = { style }>
  60. { children }
  61. <Text style = { styles && styles.labelStyle }>
  62. { this.label }
  63. </Text>
  64. </View>
  65. );
  66. // XXX As stated earlier, the style was applied to the wrapper View
  67. // (above).
  68. style = undefined;
  69. }
  70. return (
  71. <TouchableHighlight
  72. accessibilityLabel = { this.accessibilityLabel }
  73. disabled = { disabled }
  74. onPress = { onClick }
  75. style = { style }
  76. underlayColor = { styles && styles.underlayColor } >
  77. { children }
  78. </TouchableHighlight>
  79. );
  80. }
  81. }