Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

342 строки
9.4 KiB

  1. import _extends from 'babel-runtime/helpers/extends';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  4. import _inherits from 'babel-runtime/helpers/inherits';
  5. import React, { Component } from 'react';
  6. import PropTypes from 'prop-types';
  7. import ReactDOM from 'react-dom';
  8. import Align from 'rc-align';
  9. import Animate from 'rc-animate';
  10. import PopupInner from './PopupInner';
  11. import LazyRenderBox from './LazyRenderBox';
  12. import { saveRef } from './utils';
  13. var Popup = function (_Component) {
  14. _inherits(Popup, _Component);
  15. function Popup(props) {
  16. _classCallCheck(this, Popup);
  17. var _this = _possibleConstructorReturn(this, _Component.call(this, props));
  18. _initialiseProps.call(_this);
  19. _this.state = {
  20. // Used for stretch
  21. stretchChecked: false,
  22. targetWidth: undefined,
  23. targetHeight: undefined
  24. };
  25. _this.savePopupRef = saveRef.bind(_this, 'popupInstance');
  26. _this.saveAlignRef = saveRef.bind(_this, 'alignInstance');
  27. return _this;
  28. }
  29. Popup.prototype.componentDidMount = function componentDidMount() {
  30. this.rootNode = this.getPopupDomNode();
  31. this.setStretchSize();
  32. };
  33. Popup.prototype.componentDidUpdate = function componentDidUpdate() {
  34. this.setStretchSize();
  35. };
  36. // Record size if stretch needed
  37. Popup.prototype.getPopupDomNode = function getPopupDomNode() {
  38. return ReactDOM.findDOMNode(this.popupInstance);
  39. };
  40. // `target` on `rc-align` can accept as a function to get the bind element or a point.
  41. // ref: https://www.npmjs.com/package/rc-align
  42. Popup.prototype.getMaskTransitionName = function getMaskTransitionName() {
  43. var props = this.props;
  44. var transitionName = props.maskTransitionName;
  45. var animation = props.maskAnimation;
  46. if (!transitionName && animation) {
  47. transitionName = props.prefixCls + '-' + animation;
  48. }
  49. return transitionName;
  50. };
  51. Popup.prototype.getTransitionName = function getTransitionName() {
  52. var props = this.props;
  53. var transitionName = props.transitionName;
  54. if (!transitionName && props.animation) {
  55. transitionName = props.prefixCls + '-' + props.animation;
  56. }
  57. return transitionName;
  58. };
  59. Popup.prototype.getClassName = function getClassName(currentAlignClassName) {
  60. return this.props.prefixCls + ' ' + this.props.className + ' ' + currentAlignClassName;
  61. };
  62. Popup.prototype.getPopupElement = function getPopupElement() {
  63. var _this2 = this;
  64. var savePopupRef = this.savePopupRef;
  65. var _state = this.state,
  66. stretchChecked = _state.stretchChecked,
  67. targetHeight = _state.targetHeight,
  68. targetWidth = _state.targetWidth;
  69. var _props = this.props,
  70. align = _props.align,
  71. visible = _props.visible,
  72. prefixCls = _props.prefixCls,
  73. style = _props.style,
  74. getClassNameFromAlign = _props.getClassNameFromAlign,
  75. destroyPopupOnHide = _props.destroyPopupOnHide,
  76. stretch = _props.stretch,
  77. children = _props.children,
  78. onMouseEnter = _props.onMouseEnter,
  79. onMouseLeave = _props.onMouseLeave,
  80. onMouseDown = _props.onMouseDown,
  81. onTouchStart = _props.onTouchStart;
  82. var className = this.getClassName(this.currentAlignClassName || getClassNameFromAlign(align));
  83. var hiddenClassName = prefixCls + '-hidden';
  84. if (!visible) {
  85. this.currentAlignClassName = null;
  86. }
  87. var sizeStyle = {};
  88. if (stretch) {
  89. // Stretch with target
  90. if (stretch.indexOf('height') !== -1) {
  91. sizeStyle.height = targetHeight;
  92. } else if (stretch.indexOf('minHeight') !== -1) {
  93. sizeStyle.minHeight = targetHeight;
  94. }
  95. if (stretch.indexOf('width') !== -1) {
  96. sizeStyle.width = targetWidth;
  97. } else if (stretch.indexOf('minWidth') !== -1) {
  98. sizeStyle.minWidth = targetWidth;
  99. }
  100. // Delay force align to makes ui smooth
  101. if (!stretchChecked) {
  102. sizeStyle.visibility = 'hidden';
  103. setTimeout(function () {
  104. if (_this2.alignInstance) {
  105. _this2.alignInstance.forceAlign();
  106. }
  107. }, 0);
  108. }
  109. }
  110. var newStyle = _extends({}, sizeStyle, style, this.getZIndexStyle());
  111. var popupInnerProps = {
  112. className: className,
  113. prefixCls: prefixCls,
  114. ref: savePopupRef,
  115. onMouseEnter: onMouseEnter,
  116. onMouseLeave: onMouseLeave,
  117. onMouseDown: onMouseDown,
  118. onTouchStart: onTouchStart,
  119. style: newStyle
  120. };
  121. if (destroyPopupOnHide) {
  122. return React.createElement(
  123. Animate,
  124. {
  125. component: '',
  126. exclusive: true,
  127. transitionAppear: true,
  128. transitionName: this.getTransitionName()
  129. },
  130. visible ? React.createElement(
  131. Align,
  132. {
  133. target: this.getAlignTarget(),
  134. key: 'popup',
  135. ref: this.saveAlignRef,
  136. monitorWindowResize: true,
  137. align: align,
  138. onAlign: this.onAlign
  139. },
  140. React.createElement(
  141. PopupInner,
  142. _extends({
  143. visible: true
  144. }, popupInnerProps),
  145. children
  146. )
  147. ) : null
  148. );
  149. }
  150. return React.createElement(
  151. Animate,
  152. {
  153. component: '',
  154. exclusive: true,
  155. transitionAppear: true,
  156. transitionName: this.getTransitionName(),
  157. showProp: 'xVisible'
  158. },
  159. React.createElement(
  160. Align,
  161. {
  162. target: this.getAlignTarget(),
  163. key: 'popup',
  164. ref: this.saveAlignRef,
  165. monitorWindowResize: true,
  166. xVisible: visible,
  167. childrenProps: { visible: 'xVisible' },
  168. disabled: !visible,
  169. align: align,
  170. onAlign: this.onAlign
  171. },
  172. React.createElement(
  173. PopupInner,
  174. _extends({
  175. hiddenClassName: hiddenClassName
  176. }, popupInnerProps),
  177. children
  178. )
  179. )
  180. );
  181. };
  182. Popup.prototype.getZIndexStyle = function getZIndexStyle() {
  183. var style = {};
  184. var props = this.props;
  185. if (props.zIndex !== undefined) {
  186. style.zIndex = props.zIndex;
  187. }
  188. return style;
  189. };
  190. Popup.prototype.getMaskElement = function getMaskElement() {
  191. var props = this.props;
  192. var maskElement = void 0;
  193. if (props.mask) {
  194. var maskTransition = this.getMaskTransitionName();
  195. maskElement = React.createElement(LazyRenderBox, {
  196. style: this.getZIndexStyle(),
  197. key: 'mask',
  198. className: props.prefixCls + '-mask',
  199. hiddenClassName: props.prefixCls + '-mask-hidden',
  200. visible: props.visible
  201. });
  202. if (maskTransition) {
  203. maskElement = React.createElement(
  204. Animate,
  205. {
  206. key: 'mask',
  207. showProp: 'visible',
  208. transitionAppear: true,
  209. component: '',
  210. transitionName: maskTransition
  211. },
  212. maskElement
  213. );
  214. }
  215. }
  216. return maskElement;
  217. };
  218. Popup.prototype.render = function render() {
  219. return React.createElement(
  220. 'div',
  221. null,
  222. this.getMaskElement(),
  223. this.getPopupElement()
  224. );
  225. };
  226. return Popup;
  227. }(Component);
  228. Popup.propTypes = {
  229. visible: PropTypes.bool,
  230. style: PropTypes.object,
  231. getClassNameFromAlign: PropTypes.func,
  232. onAlign: PropTypes.func,
  233. getRootDomNode: PropTypes.func,
  234. align: PropTypes.any,
  235. destroyPopupOnHide: PropTypes.bool,
  236. className: PropTypes.string,
  237. prefixCls: PropTypes.string,
  238. onMouseEnter: PropTypes.func,
  239. onMouseLeave: PropTypes.func,
  240. onMouseDown: PropTypes.func,
  241. onTouchStart: PropTypes.func,
  242. stretch: PropTypes.string,
  243. children: PropTypes.node,
  244. point: PropTypes.shape({
  245. pageX: PropTypes.number,
  246. pageY: PropTypes.number
  247. })
  248. };
  249. var _initialiseProps = function _initialiseProps() {
  250. var _this3 = this;
  251. this.onAlign = function (popupDomNode, align) {
  252. var props = _this3.props;
  253. var currentAlignClassName = props.getClassNameFromAlign(align);
  254. // FIX: https://github.com/react-component/trigger/issues/56
  255. // FIX: https://github.com/react-component/tooltip/issues/79
  256. if (_this3.currentAlignClassName !== currentAlignClassName) {
  257. _this3.currentAlignClassName = currentAlignClassName;
  258. popupDomNode.className = _this3.getClassName(currentAlignClassName);
  259. }
  260. props.onAlign(popupDomNode, align);
  261. };
  262. this.setStretchSize = function () {
  263. var _props2 = _this3.props,
  264. stretch = _props2.stretch,
  265. getRootDomNode = _props2.getRootDomNode,
  266. visible = _props2.visible;
  267. var _state2 = _this3.state,
  268. stretchChecked = _state2.stretchChecked,
  269. targetHeight = _state2.targetHeight,
  270. targetWidth = _state2.targetWidth;
  271. if (!stretch || !visible) {
  272. if (stretchChecked) {
  273. _this3.setState({ stretchChecked: false });
  274. }
  275. return;
  276. }
  277. var $ele = getRootDomNode();
  278. if (!$ele) return;
  279. var height = $ele.offsetHeight;
  280. var width = $ele.offsetWidth;
  281. if (targetHeight !== height || targetWidth !== width || !stretchChecked) {
  282. _this3.setState({
  283. stretchChecked: true,
  284. targetHeight: height,
  285. targetWidth: width
  286. });
  287. }
  288. };
  289. this.getTargetElement = function () {
  290. return _this3.props.getRootDomNode();
  291. };
  292. this.getAlignTarget = function () {
  293. var point = _this3.props.point;
  294. if (point) {
  295. return point;
  296. }
  297. return _this3.getTargetElement;
  298. };
  299. };
  300. export default Popup;