Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ToolboxItem.web.js 3.5KB

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