Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ToolboxItem.web.js 4.1KB

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