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

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