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.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // @flow
  2. import Tooltip from '@atlaskit/tooltip';
  3. import React, { Fragment } from 'react';
  4. import AbstractToolboxItem from './AbstractToolboxItem';
  5. import type { Props } from './AbstractToolboxItem';
  6. /**
  7. * Web implementation of {@code AbstractToolboxItem}.
  8. */
  9. export default class ToolboxItem extends AbstractToolboxItem<Props> {
  10. /**
  11. * Handles rendering of the actual item. If the label is being shown, which
  12. * is controlled with the `showLabel` prop, the item is rendered for its
  13. * display in an overflow menu, otherwise it will only have an icon, which
  14. * can be displayed on any toolbar.
  15. *
  16. * @protected
  17. * @returns {ReactElement}
  18. */
  19. _renderItem() {
  20. const {
  21. disabled,
  22. elementAfter,
  23. onClick,
  24. showLabel,
  25. tooltipPosition
  26. } = this.props;
  27. const className = showLabel ? 'overflow-menu-item' : 'toolbox-button';
  28. const props = {
  29. 'aria-label': this.accessibilityLabel,
  30. className: className + (disabled ? ' disabled' : ''),
  31. onClick: disabled ? undefined : onClick
  32. };
  33. const elementType = showLabel ? 'li' : 'div';
  34. const useTooltip = this.tooltip && this.tooltip.length > 0;
  35. let children = (
  36. <Fragment>
  37. { this._renderIcon() }
  38. { showLabel && <span>
  39. { this.label }
  40. </span> }
  41. { elementAfter }
  42. </Fragment>
  43. );
  44. if (useTooltip) {
  45. children = (
  46. <Tooltip
  47. content = { this.tooltip }
  48. position = { tooltipPosition }>
  49. { children }
  50. </Tooltip>
  51. );
  52. }
  53. return React.createElement(elementType, props, children);
  54. }
  55. /**
  56. * Helper function to render the item's icon.
  57. *
  58. * @private
  59. * @returns {ReactElement}
  60. */
  61. _renderIcon() {
  62. const { iconName, showLabel } = this.props;
  63. const icon = <i className = { iconName } />;
  64. const elementType = showLabel ? 'span' : 'div';
  65. const className
  66. = showLabel ? 'overflow-menu-item-icon' : 'toolbox-icon';
  67. return React.createElement(elementType, { className }, icon);
  68. }
  69. }