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

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