Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BaseIndicator.js 2.7KB

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