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.

OverflowMenuItem.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Icon } from '../../../icons';
  4. import { Tooltip } from '../../../tooltip';
  5. /**
  6. * The type of the React {@code Component} props of {@link OverflowMenuItem}.
  7. */
  8. type Props = {
  9. /**
  10. * A succinct description of what the item does. Used by accessibility tools
  11. * and torture tests.
  12. */
  13. accessibilityLabel: string,
  14. /**
  15. * Whether menu item is disabled or not.
  16. */
  17. disabled: boolean,
  18. /**
  19. * A React Element to display at the end of {@code OverflowMenuItem}.
  20. */
  21. elementAfter?: React$Node,
  22. /**
  23. * The icon class to use for displaying an icon before the link text.
  24. */
  25. icon: Object,
  26. /**
  27. * Id of the icon to be rendered.
  28. */
  29. iconId?: string,
  30. /**
  31. * The callback to invoke when {@code OverflowMenuItem} is clicked.
  32. */
  33. onClick: Function,
  34. /**
  35. * The text to display in the {@code OverflowMenuItem}.
  36. */
  37. text: string,
  38. /**
  39. * The text to display in the tooltip.
  40. */
  41. tooltip?: string,
  42. /**
  43. * From which direction the tooltip should appear, relative to the button.
  44. */
  45. tooltipPosition: string
  46. };
  47. /**
  48. * A React {@code Component} for displaying a link to interact with other
  49. * features of the application.
  50. *
  51. * @extends Component
  52. */
  53. class OverflowMenuItem extends Component<Props> {
  54. /**
  55. * Default values for {@code OverflowMenuItem} component's properties.
  56. *
  57. * @static
  58. */
  59. static defaultProps = {
  60. tooltipPosition: 'left',
  61. disabled: false
  62. };
  63. /**
  64. * Initializes a new {@code OverflowMenuItem} instance.
  65. *
  66. * @param {*} props - The read-only properties with which the new instance
  67. * is to be initialized.
  68. */
  69. constructor(props: Props) {
  70. super(props);
  71. // Bind event handler so it is only bound once for every instance.
  72. this._onKeyPress = this._onKeyPress.bind(this);
  73. }
  74. _onKeyPress: (Object) => void;
  75. /**
  76. * KeyPress handler for accessibility.
  77. *
  78. * @param {Object} e - The key event to handle.
  79. *
  80. * @returns {void}
  81. */
  82. _onKeyPress(e) {
  83. if (!this.props.disabled && this.props.onClick && (e.key === ' ' || e.key === 'Enter')) {
  84. e.preventDefault();
  85. this.props.onClick();
  86. }
  87. }
  88. /**
  89. * Implements React's {@link Component#render()}.
  90. *
  91. * @inheritdoc
  92. * @returns {ReactElement}
  93. */
  94. render() {
  95. const { accessibilityLabel, disabled, elementAfter, icon, iconId, onClick } = this.props;
  96. let className = 'overflow-menu-item';
  97. className += this.props.disabled ? ' disabled' : '';
  98. return (
  99. <li
  100. aria-disabled = { disabled }
  101. aria-label = { accessibilityLabel }
  102. className = { className }
  103. onClick = { disabled ? null : onClick }
  104. onKeyPress = { this._onKeyPress }
  105. role = 'menuitem'
  106. tabIndex = { 0 }>
  107. <span className = 'overflow-menu-item-icon'>
  108. <Icon
  109. id = { iconId }
  110. src = { icon } />
  111. </span>
  112. { this._renderText() }
  113. {
  114. elementAfter || null
  115. }
  116. </li>
  117. );
  118. }
  119. /**
  120. * Renders the text label to display in the {@code OverflowMenuItem}.
  121. *
  122. * @private
  123. * @returns {ReactElement}
  124. */
  125. _renderText() {
  126. const textElement = (
  127. <span className = 'overflow-menu-item-text'>
  128. { this.props.text }
  129. </span>
  130. );
  131. if (this.props.tooltip) {
  132. return (
  133. <Tooltip
  134. content = { this.props.tooltip }
  135. position = { this.props.tooltipPosition }>
  136. { textElement }
  137. </Tooltip>
  138. );
  139. }
  140. return textElement;
  141. }
  142. }
  143. export default OverflowMenuItem;