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.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import React, { Fragment } from 'react';
  2. import Icon from '../../icons/components/Icon';
  3. import Tooltip from '../../tooltip/components/Tooltip';
  4. import ContextMenuItem from '../../ui/components/web/ContextMenuItem';
  5. import AbstractToolboxItem from './AbstractToolboxItem';
  6. import type { IProps as AbstractToolboxItemProps } from './AbstractToolboxItem';
  7. interface IProps extends AbstractToolboxItemProps {
  8. /**
  9. * Whether or not the item is displayed in a context menu.
  10. */
  11. contextMenu?: boolean;
  12. /**
  13. * On key down handler.
  14. */
  15. onKeyDown: (e?: React.KeyboardEvent) => void;
  16. }
  17. /**
  18. * Web implementation of {@code AbstractToolboxItem}.
  19. */
  20. export default class ToolboxItem extends AbstractToolboxItem<IProps> {
  21. /**
  22. * Initializes a new {@code ToolboxItem} instance.
  23. *
  24. * @inheritdoc
  25. */
  26. constructor(props: IProps) {
  27. super(props);
  28. this._onKeyPress = this._onKeyPress.bind(this);
  29. }
  30. /**
  31. * Handles 'Enter' and Space key on the button to trigger onClick for accessibility.
  32. *
  33. * @param {Object} event - The key event.
  34. * @private
  35. * @returns {void}
  36. */
  37. _onKeyPress(event?: React.KeyboardEvent) {
  38. if (event?.key === 'Enter') {
  39. event.preventDefault();
  40. this.props.onClick();
  41. }
  42. }
  43. /**
  44. * Handles rendering of the actual item. If the label is being shown, which
  45. * is controlled with the `showLabel` prop, the item is rendered for its
  46. * display in an overflow menu, otherwise it will only have an icon, which
  47. * can be displayed on any toolbar.
  48. *
  49. * @protected
  50. * @returns {ReactElement}
  51. */
  52. _renderItem() {
  53. const {
  54. contextMenu,
  55. disabled,
  56. elementAfter,
  57. icon,
  58. onClick,
  59. onKeyDown,
  60. showLabel,
  61. tooltipPosition,
  62. toggled
  63. } = this.props;
  64. const className = showLabel ? 'overflow-menu-item' : 'toolbox-button';
  65. const props = {
  66. 'aria-pressed': toggled,
  67. 'aria-disabled': disabled,
  68. 'aria-label': this.accessibilityLabel,
  69. className: className + (disabled ? ' disabled' : ''),
  70. onClick: disabled ? undefined : onClick,
  71. onKeyDown: disabled ? undefined : onKeyDown,
  72. onKeyPress: this._onKeyPress,
  73. tabIndex: 0,
  74. role: 'button'
  75. };
  76. const elementType = showLabel ? 'li' : 'div';
  77. const useTooltip = this.tooltip && this.tooltip.length > 0;
  78. if (contextMenu) {
  79. return (
  80. <ContextMenuItem
  81. accessibilityLabel = { this.accessibilityLabel }
  82. disabled = { disabled }
  83. icon = { icon }
  84. onClick = { onClick }
  85. onKeyDown = { onKeyDown }
  86. onKeyPress = { this._onKeyPress }
  87. text = { this.label } />
  88. );
  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. }