您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ToolboxItem.web.js 3.5KB

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