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

Popover.web.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * Whether displaying of the popover should be prevented.
  64. */
  65. disablePopover: PropTypes.bool,
  66. /**
  67. * An id attribute to apply to the root of the {@code Popover}
  68. * component.
  69. */
  70. id: PropTypes.string,
  71. /**
  72. * Callback to invoke when the popover has opened.
  73. */
  74. onPopoverOpen: PropTypes.func,
  75. /**
  76. * From which side of the dialog trigger the dialog should display. The
  77. * value will be passed to {@code InlineDialog}.
  78. */
  79. position: PropTypes.string
  80. };
  81. /**
  82. * Initializes a new {@code Popover} instance.
  83. *
  84. * @param {Object} props - The read-only properties with which the new
  85. * instance is to be initialized.
  86. */
  87. constructor(props) {
  88. super(props);
  89. this.state = {
  90. /**
  91. * Whether or not the {@code InlineDialog} should be displayed.
  92. *
  93. * @type {boolean}
  94. */
  95. showDialog: false
  96. };
  97. // Bind event handlers so they are only bound once for every instance.
  98. this._onHideDialog = this._onHideDialog.bind(this);
  99. this._onShowDialog = this._onShowDialog.bind(this);
  100. }
  101. /**
  102. * Implements React's {@link Component#render()}.
  103. *
  104. * @inheritdoc
  105. * @returns {ReactElement}
  106. */
  107. render() {
  108. return (
  109. <div
  110. className = { this.props.className }
  111. id = { this.props.id }
  112. onMouseEnter = { this._onShowDialog }
  113. onMouseLeave = { this._onHideDialog }>
  114. <InlineDialog
  115. content = { this._renderContent() }
  116. isOpen = { this.state.showDialog }
  117. position = { this.props.position }>
  118. { this.props.children }
  119. </InlineDialog>
  120. </div>
  121. );
  122. }
  123. /**
  124. * Stops displaying the {@code InlineDialog}.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _onHideDialog() {
  130. this.setState({ showDialog: false });
  131. }
  132. /**
  133. * Displays the {@code InlineDialog} and calls any registered onPopoverOpen
  134. * callbacks.
  135. *
  136. * @private
  137. * @returns {void}
  138. */
  139. _onShowDialog() {
  140. if (!this.props.disablePopover) {
  141. this.setState({ showDialog: true });
  142. if (this.props.onPopoverOpen) {
  143. this.props.onPopoverOpen();
  144. }
  145. }
  146. }
  147. /**
  148. * Renders the React Element to be displayed in the {@code InlineDialog}.
  149. * Also adds padding to support moving the mouse from the trigger to the
  150. * dialog to prevent mouseleave events.
  151. *
  152. * @private
  153. * @returns {ReactElement}
  154. */
  155. _renderContent() {
  156. const { content, position } = this.props;
  157. return (
  158. <div className = 'popover'>
  159. { content }
  160. <div className = 'popover-mouse-padding-top' />
  161. <div className = { _mapPositionToPaddingClass(position) } />
  162. </div>
  163. );
  164. }
  165. }
  166. export default Popover;