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.5KB

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