Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

382 Zeilen
11 KiB

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