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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. onClick,
  22. showLabel
  23. } = this.props;
  24. const props = {
  25. 'aria-label': this.accessibilityLabel,
  26. className: showLabel ? 'overflow-menu-item' : 'toolbox-button',
  27. onClick
  28. };
  29. const elementType = showLabel ? 'li' : 'div';
  30. // eslint-disable-next-line no-extra-parens
  31. const children = (
  32. <Fragment>
  33. { this._renderIcon() }
  34. { showLabel && this.label }
  35. </Fragment>
  36. );
  37. return React.createElement(elementType, props, children);
  38. }
  39. /**
  40. * Helper function to render the item's icon.
  41. *
  42. * @private
  43. * @returns {ReactElement}
  44. */
  45. _renderIcon() {
  46. const { iconName, tooltipPosition, showLabel } = this.props;
  47. const icon = <i className = { iconName } />;
  48. const elementType = showLabel ? 'span' : 'div';
  49. const className
  50. = showLabel ? 'overflow-menu-item-icon' : 'toolbox-icon';
  51. const iconWrapper
  52. = React.createElement(elementType, { className }, icon);
  53. const tooltip = this.tooltip;
  54. const useTooltip = !showLabel && tooltip && tooltip.length > 0;
  55. if (useTooltip) {
  56. return (
  57. <Tooltip
  58. content = { tooltip }
  59. position = { tooltipPosition }>
  60. { iconWrapper }
  61. </Tooltip>
  62. );
  63. }
  64. return iconWrapper;
  65. }
  66. }