Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ToolboxItem.web.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 } 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._onKeyPress = this._onKeyPress.bind(this);
  19. }
  20. _onKeyPress: (Object) => void;
  21. /**
  22. * Handles 'Enter' and Space key on the button to trigger onClick for accessibility.
  23. *
  24. * @param {Object} event - The key event.
  25. * @private
  26. * @returns {void}
  27. */
  28. _onKeyPress(event) {
  29. if (event.key === 'Enter' || event.key === ' ') {
  30. event.preventDefault();
  31. this.props.onClick();
  32. }
  33. }
  34. /**
  35. * Handles rendering of the actual item. If the label is being shown, which
  36. * is controlled with the `showLabel` prop, the item is rendered for its
  37. * display in an overflow menu, otherwise it will only have an icon, which
  38. * can be displayed on any toolbar.
  39. *
  40. * @protected
  41. * @returns {ReactElement}
  42. */
  43. _renderItem() {
  44. const {
  45. disabled,
  46. elementAfter,
  47. onClick,
  48. showLabel,
  49. tooltipPosition,
  50. toggled
  51. } = this.props;
  52. const className = showLabel ? 'overflow-menu-item' : 'toolbox-button';
  53. const props = {
  54. 'aria-pressed': toggled,
  55. 'aria-disabled': disabled,
  56. 'aria-label': this.accessibilityLabel,
  57. className: className + (disabled ? ' disabled' : ''),
  58. onClick: disabled ? undefined : onClick,
  59. onKeyPress: this._onKeyPress,
  60. tabIndex: 0,
  61. role: showLabel ? 'menuitem' : 'button'
  62. };
  63. const elementType = showLabel ? 'li' : 'div';
  64. const useTooltip = this.tooltip && this.tooltip.length > 0;
  65. let children = (
  66. <Fragment>
  67. { this._renderIcon() }
  68. { showLabel && <span>
  69. { this.label }
  70. </span> }
  71. { elementAfter }
  72. </Fragment>
  73. );
  74. if (useTooltip) {
  75. children = (
  76. <Tooltip
  77. content = { this.tooltip }
  78. position = { tooltipPosition }>
  79. { children }
  80. </Tooltip>
  81. );
  82. }
  83. return React.createElement(elementType, props, children);
  84. }
  85. /**
  86. * Helper function to render the item's icon.
  87. *
  88. * @private
  89. * @returns {ReactElement}
  90. */
  91. _renderIcon() {
  92. const { customClass, disabled, icon, showLabel, toggled } = this.props;
  93. const iconComponent = <Icon src = { icon } />;
  94. const elementType = showLabel ? 'span' : 'div';
  95. const className = `${showLabel ? 'overflow-menu-item-icon' : 'toolbox-icon'} ${
  96. toggled ? 'toggled' : ''} ${disabled ? 'disabled' : ''} ${customClass ?? ''}`;
  97. return React.createElement(elementType, { className }, iconComponent);
  98. }
  99. }