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 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. onClick,
  23. showLabel,
  24. tooltipPosition
  25. } = this.props;
  26. const className = showLabel ? 'overflow-menu-item' : 'toolbox-button';
  27. const props = {
  28. 'aria-label': this.accessibilityLabel,
  29. className: className + (disabled ? ' disabled' : ''),
  30. onClick: disabled ? undefined : onClick
  31. };
  32. const elementType = showLabel ? 'li' : 'div';
  33. const useTooltip = this.tooltip && this.tooltip.length > 0;
  34. // eslint-disable-next-line no-extra-parens
  35. let children = (
  36. <Fragment>
  37. { this._renderIcon() }
  38. { showLabel && <span>
  39. { this.label }
  40. </span> }
  41. </Fragment>
  42. );
  43. if (useTooltip) {
  44. // eslint-disable-next-line no-extra-parens
  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. const iconWrapper
  68. = React.createElement(elementType, { className }, icon);
  69. return iconWrapper;
  70. }
  71. }