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

Popover.web.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import InlineDialog from '@atlaskit/inline-dialog';
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. /**
  5. * A map of dialog positions, relative to trigger, to css classes used to
  6. * manipulate elements for handling mouse events.
  7. *
  8. * @private
  9. * @type {object}
  10. */
  11. const DIALOG_TO_PADDING_POSITION = {
  12. 'left': 'popover-mousemove-padding-right',
  13. 'top': 'popover-mousemove-padding-bottom'
  14. };
  15. /**
  16. * Takes the position expected by {@code InlineDialog} and maps it to a CSS
  17. * class that can be used styling the elements used for preventing mouseleave
  18. * events when moving from the trigger to the dialog.
  19. *
  20. * @param {string} position - From which position the dialog will display.
  21. * @private
  22. * @returns {string}
  23. */
  24. function _mapPositionToPaddingClass(position = 'left') {
  25. return DIALOG_TO_PADDING_POSITION[position.split(' ')[0]];
  26. }
  27. /**
  28. * Implements a React {@code Component} for showing an {@code InlineDialog} on
  29. * mouseenter of the trigger and contents, and hiding the dialog on mouseleave.
  30. *
  31. * @extends Component
  32. */
  33. class Popover extends Component {
  34. /**
  35. * Default values for {@code Popover} component's properties.
  36. *
  37. * @static
  38. */
  39. static defaultProps = {
  40. className: '',
  41. id: ''
  42. };
  43. /**
  44. * {@code Popover} component's property types.
  45. *
  46. * @static
  47. */
  48. static propTypes = {
  49. /**
  50. * A child React Element to use as the trigger for showing the dialog.
  51. */
  52. children: PropTypes.object,
  53. /**
  54. * Additional CSS classnames to apply to the root of the {@code Popover}
  55. * component.
  56. */
  57. className: PropTypes.string,
  58. /**
  59. * The ReactElement to display within the dialog.
  60. */
  61. content: PropTypes.object,
  62. /**
  63. * An id attribute to apply to the root of the {@code Popover}
  64. * component.
  65. */
  66. id: PropTypes.string,
  67. /**
  68. * Callback to invoke when the popover has opened.
  69. */
  70. onPopoverOpen: PropTypes.func,
  71. /**
  72. * From which side of the dialog trigger the dialog should display. The
  73. * value will be passed to {@code InlineDialog}.
  74. */
  75. position: PropTypes.string
  76. };
  77. /**
  78. * Initializes a new {@code Popover} instance.
  79. *
  80. * @param {Object} props - The read-only properties with which the new
  81. * instance is to be initialized.
  82. */
  83. constructor(props) {
  84. super(props);
  85. this.state = {
  86. /**
  87. * Whether or not the {@code InlineDialog} should be displayed.
  88. *
  89. * @type {boolean}
  90. */
  91. showDialog: false
  92. };
  93. // Bind event handlers so they are only bound once for every instance.
  94. this._onHideDialog = this._onHideDialog.bind(this);
  95. this._onShowDialog = this._onShowDialog.bind(this);
  96. }
  97. /**
  98. * Implements React's {@link Component#render()}.
  99. *
  100. * @inheritdoc
  101. * @returns {ReactElement}
  102. */
  103. render() {
  104. return (
  105. <div
  106. className = { this.props.className }
  107. id = { this.props.id }
  108. onMouseEnter = { this._onShowDialog }
  109. onMouseLeave = { this._onHideDialog }>
  110. <InlineDialog
  111. content = { this._renderContent() }
  112. isOpen = { this.state.showDialog }
  113. position = { this.props.position }>
  114. { this.props.children }
  115. </InlineDialog>
  116. </div>
  117. );
  118. }
  119. /**
  120. * Stops displaying the {@code InlineDialog}.
  121. *
  122. * @private
  123. * @returns {void}
  124. */
  125. _onHideDialog() {
  126. this.setState({ showDialog: false });
  127. }
  128. /**
  129. * Displays the {@code InlineDialog} and calls any registered onPopoverOpen
  130. * callbacks.
  131. *
  132. * @private
  133. * @returns {void}
  134. */
  135. _onShowDialog() {
  136. this.setState({ showDialog: true });
  137. if (this.props.onPopoverOpen) {
  138. this.props.onPopoverOpen();
  139. }
  140. }
  141. /**
  142. * Renders the React Element to be displayed in the {@code InlineDialog}.
  143. * Also adds padding to support moving the mouse from the trigger to the
  144. * dialog to prevent mouseleave events.
  145. *
  146. * @private
  147. * @returns {ReactElement}
  148. */
  149. _renderContent() {
  150. const { content, position } = this.props;
  151. return (
  152. <div className = 'popover'>
  153. { content }
  154. <div className = 'popover-mouse-padding-top' />
  155. <div className = { _mapPositionToPaddingClass(position) } />
  156. </div>
  157. );
  158. }
  159. }
  160. export default Popover;