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.web.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // @flow
  2. import Tooltip from '@atlaskit/tooltip';
  3. import React, { Fragment } from 'react';
  4. import { Icon } from '../../icons';
  5. import AbstractToolboxItem from './AbstractToolboxItem';
  6. import type { Props } from './AbstractToolboxItem';
  7. /**
  8. * Web implementation of {@code AbstractToolboxItem}.
  9. */
  10. export default class ToolboxItem extends AbstractToolboxItem<Props> {
  11. /**
  12. * Initializes a new {@code ToolboxItem} instance.
  13. *
  14. * @inheritdoc
  15. */
  16. constructor(props: Props) {
  17. super(props);
  18. this._onKeyDown = this._onKeyDown.bind(this);
  19. this._onKeyUp = this._onKeyUp.bind(this);
  20. }
  21. _onKeyDown: (Object) => void;
  22. /**
  23. * Handles 'Enter' key on the button to trigger onClick for accessibility.
  24. *
  25. * @param {Object} event - The key event.
  26. * @private
  27. * @returns {void}
  28. */
  29. _onKeyDown(event) {
  30. // If the event coming to the dialog has been subject to preventDefault
  31. // we don't handle it here.
  32. if (event.defaultPrevented) {
  33. return;
  34. }
  35. if (event.key === 'Enter') {
  36. event.preventDefault();
  37. event.stopPropagation();
  38. this.props.onClick();
  39. } else if (event.key === ' ') {
  40. // Space triggers button onKeyUp but we need to prevent PTT here
  41. event.preventDefault();
  42. event.stopPropagation();
  43. }
  44. }
  45. _onKeyUp: (Object) => void;
  46. /**
  47. * Handles ' ' (Space) key on the button to trigger onClick for
  48. * accessibility.
  49. *
  50. * @param {Object} event - The key event.
  51. * @private
  52. * @returns {void}
  53. */
  54. _onKeyUp(event) {
  55. // If the event coming to the dialog has been subject to preventDefault
  56. // we don't handle it here.
  57. if (event.defaultPrevented) {
  58. return;
  59. }
  60. if (event.key === ' ') {
  61. event.preventDefault();
  62. event.stopPropagation();
  63. this.props.onClick();
  64. }
  65. }
  66. /**
  67. * Handles rendering of the actual item. If the label is being shown, which
  68. * is controlled with the `showLabel` prop, the item is rendered for its
  69. * display in an overflow menu, otherwise it will only have an icon, which
  70. * can be displayed on any toolbar.
  71. *
  72. * @protected
  73. * @returns {ReactElement}
  74. */
  75. _renderItem() {
  76. const {
  77. disabled,
  78. elementAfter,
  79. onClick,
  80. showLabel,
  81. tooltipPosition,
  82. toggled
  83. } = this.props;
  84. const className = showLabel ? 'overflow-menu-item' : 'toolbox-button';
  85. const props = {
  86. 'aria-pressed': toggled,
  87. 'aria-disabled': disabled,
  88. 'aria-label': this.accessibilityLabel,
  89. className: className + (disabled ? ' disabled' : ''),
  90. onClick: disabled ? undefined : onClick,
  91. onKeyDown: this._onKeyDown,
  92. onKeyUp: this._onKeyUp,
  93. tabIndex: 0,
  94. role: 'button'
  95. };
  96. const elementType = showLabel ? 'li' : 'div';
  97. const useTooltip = this.tooltip && this.tooltip.length > 0;
  98. let children = (
  99. <Fragment>
  100. { this._renderIcon() }
  101. { showLabel && <span>
  102. { this.label }
  103. </span> }
  104. { elementAfter }
  105. </Fragment>
  106. );
  107. if (useTooltip) {
  108. children = (
  109. <Tooltip
  110. content = { this.tooltip }
  111. position = { tooltipPosition }>
  112. { children }
  113. </Tooltip>
  114. );
  115. }
  116. return React.createElement(elementType, props, children);
  117. }
  118. /**
  119. * Helper function to render the item's icon.
  120. *
  121. * @private
  122. * @returns {ReactElement}
  123. */
  124. _renderIcon() {
  125. const { disabled, icon, showLabel, toggled } = this.props;
  126. const iconComponent = <Icon src = { icon } />;
  127. const elementType = showLabel ? 'span' : 'div';
  128. const className = `${showLabel ? 'overflow-menu-item-icon' : 'toolbox-icon'} ${
  129. toggled ? 'toggled' : ''} ${disabled ? 'disabled' : ''}`;
  130. return React.createElement(elementType, { className }, iconComponent);
  131. }
  132. }