您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BaseIndicator.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React, { Component } from 'react';
  2. import { setTooltip } from '../../../../../modules/UI/util/Tooltip';
  3. /**
  4. * React {@code Component} for showing an icon with a tooltip.
  5. *
  6. * @extends Component
  7. */
  8. class BaseIndicator extends Component {
  9. static defaultProps = {
  10. className: '',
  11. iconClassName: '',
  12. iconSize: 'auto',
  13. id: ''
  14. };
  15. static propTypes = {
  16. /**
  17. * The CSS class names to set on the root element of the component.
  18. */
  19. className: React.PropTypes.string,
  20. /**
  21. * The CSS classnames to set on the icon element of the component.
  22. */
  23. iconClassName: React.PropTypes.string,
  24. /**
  25. * The front size for the icon.
  26. */
  27. iconSize: React.PropTypes.string,
  28. /**
  29. * The ID attribue to set on the root element of the component.
  30. */
  31. id: React.PropTypes.string,
  32. /**
  33. * The translation key to use for displaying a tooltip when hovering
  34. * over the component.
  35. */
  36. tooltipKey: React.PropTypes.string
  37. };
  38. /**
  39. * Initializes a new {@code BaseIndicator} instance.
  40. *
  41. * @param {Object} props - The read-only properties with which the new
  42. * instance is to be initialized.
  43. */
  44. constructor(props) {
  45. super(props);
  46. /**
  47. * An internal reference to the HTML element at the top of the
  48. * component's DOM hierarchy. The reference is needed for attaching a
  49. * tooltip.
  50. *
  51. * @type {HTMLElement}
  52. */
  53. this._rootElement = null;
  54. // Bind event handler so it is only bound once for every instance.
  55. this._setRootElementRef = this._setRootElementRef.bind(this);
  56. }
  57. /**
  58. * Sets a tooltip which will display when hovering over the component.
  59. *
  60. * @inheritdoc
  61. * @returns {void}
  62. */
  63. componentDidMount() {
  64. this._setTooltip();
  65. }
  66. /**
  67. * Implements React's {@link Component#render()}.
  68. *
  69. * @inheritdoc
  70. * @returns {ReactElement}
  71. */
  72. render() {
  73. return (
  74. <span
  75. className = { this.props.className }
  76. id = { this.props.id }
  77. ref = { this._setRootElementRef }>
  78. <i
  79. className = { this.props.iconClassName }
  80. style = {{ fontSize: this.props.iconSize }} />
  81. </span>
  82. );
  83. }
  84. /**
  85. * Sets the internal reference to the root HTML element for the component.
  86. *
  87. * @param {HTMLIconElement} element - The root HTML element of the
  88. * component.
  89. * @private
  90. * @returns {void}
  91. */
  92. _setRootElementRef(element) {
  93. this._rootElement = element;
  94. }
  95. /**
  96. * Associate the component as a tooltip trigger so a tooltip may display on
  97. * hover.
  98. *
  99. * @private
  100. * @returns {void}
  101. */
  102. _setTooltip() {
  103. // TODO Replace UIUtil with an AtlasKit component when a suitable one
  104. // becomes available for tooltips.
  105. setTooltip(
  106. this._rootElement,
  107. this.props.tooltipKey,
  108. 'top'
  109. );
  110. }
  111. }
  112. export default BaseIndicator;