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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. 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. <Fragment>
  34. { this._renderIcon() }
  35. { showLabel && this.label }
  36. </Fragment>
  37. );
  38. return React.createElement(elementType, props, children);
  39. }
  40. /**
  41. * Helper function to render the item's icon.
  42. *
  43. * @private
  44. * @returns {ReactElement}
  45. */
  46. _renderIcon() {
  47. const { iconName, tooltipPosition, showLabel } = this.props;
  48. const icon = <i className = { iconName } />;
  49. const elementType = showLabel ? 'span' : 'div';
  50. const className
  51. = showLabel ? 'overflow-menu-item-icon' : 'toolbox-icon';
  52. const iconWrapper
  53. = React.createElement(elementType, { className }, icon);
  54. const tooltip = this.tooltip;
  55. const useTooltip = !showLabel && tooltip && tooltip.length > 0;
  56. if (useTooltip) {
  57. return (
  58. <Tooltip
  59. description = { tooltip }
  60. position = { tooltipPosition }>
  61. { iconWrapper }
  62. </Tooltip>
  63. );
  64. }
  65. return iconWrapper;
  66. }
  67. }