Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ToolboxItem.web.tsx 4.7KB

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