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.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * Handles rendering of the actual item. If the label is being shown, which
  13. * is controlled with the `showLabel` prop, the item is rendered for its
  14. * display in an overflow menu, otherwise it will only have an icon, which
  15. * can be displayed on any toolbar.
  16. *
  17. * @protected
  18. * @returns {ReactElement}
  19. */
  20. _renderItem() {
  21. const {
  22. disabled,
  23. elementAfter,
  24. onClick,
  25. showLabel,
  26. tooltipPosition
  27. } = this.props;
  28. const className = showLabel ? 'overflow-menu-item' : 'toolbox-button';
  29. const props = {
  30. 'aria-label': this.accessibilityLabel,
  31. className: className + (disabled ? ' disabled' : ''),
  32. onClick: disabled ? undefined : onClick
  33. };
  34. const elementType = showLabel ? 'li' : 'div';
  35. const useTooltip = this.tooltip && this.tooltip.length > 0;
  36. let children = (
  37. <Fragment>
  38. { this._renderIcon() }
  39. { showLabel && <span>
  40. { this.label }
  41. </span> }
  42. { elementAfter }
  43. </Fragment>
  44. );
  45. if (useTooltip) {
  46. children = (
  47. <Tooltip
  48. content = { this.tooltip }
  49. position = { tooltipPosition }>
  50. { children }
  51. </Tooltip>
  52. );
  53. }
  54. return React.createElement(elementType, props, children);
  55. }
  56. /**
  57. * Helper function to render the item's icon.
  58. *
  59. * @private
  60. * @returns {ReactElement}
  61. */
  62. _renderIcon() {
  63. const { disabled, icon, showLabel, toggled } = this.props;
  64. const iconComponent = <Icon src = { icon } />;
  65. const elementType = showLabel ? 'span' : 'div';
  66. const className = `${showLabel ? 'overflow-menu-item-icon' : 'toolbox-icon'} ${
  67. toggled ? 'toggled' : ''} ${disabled ? 'disabled' : ''}`;
  68. return React.createElement(elementType, { className }, iconComponent);
  69. }
  70. }