|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- import _extends from 'babel-runtime/helpers/extends';
- import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
- import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
- import _inherits from 'babel-runtime/helpers/inherits';
- import React, { Component } from 'react';
- import PropTypes from 'prop-types';
- import ReactDOM from 'react-dom';
- import Align from 'rc-align';
- import Animate from 'rc-animate';
- import PopupInner from './PopupInner';
- import LazyRenderBox from './LazyRenderBox';
- import { saveRef } from './utils';
-
- var Popup = function (_Component) {
- _inherits(Popup, _Component);
-
- function Popup(props) {
- _classCallCheck(this, Popup);
-
- var _this = _possibleConstructorReturn(this, _Component.call(this, props));
-
- _initialiseProps.call(_this);
-
- _this.state = {
- // Used for stretch
- stretchChecked: false,
- targetWidth: undefined,
- targetHeight: undefined
- };
-
- _this.savePopupRef = saveRef.bind(_this, 'popupInstance');
- _this.saveAlignRef = saveRef.bind(_this, 'alignInstance');
- return _this;
- }
-
- Popup.prototype.componentDidMount = function componentDidMount() {
- this.rootNode = this.getPopupDomNode();
- this.setStretchSize();
- };
-
- Popup.prototype.componentDidUpdate = function componentDidUpdate() {
- this.setStretchSize();
- };
-
- // Record size if stretch needed
-
-
- Popup.prototype.getPopupDomNode = function getPopupDomNode() {
- return ReactDOM.findDOMNode(this.popupInstance);
- };
-
- // `target` on `rc-align` can accept as a function to get the bind element or a point.
- // ref: https://www.npmjs.com/package/rc-align
-
-
- Popup.prototype.getMaskTransitionName = function getMaskTransitionName() {
- var props = this.props;
- var transitionName = props.maskTransitionName;
- var animation = props.maskAnimation;
- if (!transitionName && animation) {
- transitionName = props.prefixCls + '-' + animation;
- }
- return transitionName;
- };
-
- Popup.prototype.getTransitionName = function getTransitionName() {
- var props = this.props;
- var transitionName = props.transitionName;
- if (!transitionName && props.animation) {
- transitionName = props.prefixCls + '-' + props.animation;
- }
- return transitionName;
- };
-
- Popup.prototype.getClassName = function getClassName(currentAlignClassName) {
- return this.props.prefixCls + ' ' + this.props.className + ' ' + currentAlignClassName;
- };
-
- Popup.prototype.getPopupElement = function getPopupElement() {
- var _this2 = this;
-
- var savePopupRef = this.savePopupRef;
- var _state = this.state,
- stretchChecked = _state.stretchChecked,
- targetHeight = _state.targetHeight,
- targetWidth = _state.targetWidth;
- var _props = this.props,
- align = _props.align,
- visible = _props.visible,
- prefixCls = _props.prefixCls,
- style = _props.style,
- getClassNameFromAlign = _props.getClassNameFromAlign,
- destroyPopupOnHide = _props.destroyPopupOnHide,
- stretch = _props.stretch,
- children = _props.children,
- onMouseEnter = _props.onMouseEnter,
- onMouseLeave = _props.onMouseLeave,
- onMouseDown = _props.onMouseDown,
- onTouchStart = _props.onTouchStart;
-
- var className = this.getClassName(this.currentAlignClassName || getClassNameFromAlign(align));
- var hiddenClassName = prefixCls + '-hidden';
-
- if (!visible) {
- this.currentAlignClassName = null;
- }
-
- var sizeStyle = {};
- if (stretch) {
- // Stretch with target
- if (stretch.indexOf('height') !== -1) {
- sizeStyle.height = targetHeight;
- } else if (stretch.indexOf('minHeight') !== -1) {
- sizeStyle.minHeight = targetHeight;
- }
- if (stretch.indexOf('width') !== -1) {
- sizeStyle.width = targetWidth;
- } else if (stretch.indexOf('minWidth') !== -1) {
- sizeStyle.minWidth = targetWidth;
- }
-
- // Delay force align to makes ui smooth
- if (!stretchChecked) {
- sizeStyle.visibility = 'hidden';
- setTimeout(function () {
- if (_this2.alignInstance) {
- _this2.alignInstance.forceAlign();
- }
- }, 0);
- }
- }
-
- var newStyle = _extends({}, sizeStyle, style, this.getZIndexStyle());
-
- var popupInnerProps = {
- className: className,
- prefixCls: prefixCls,
- ref: savePopupRef,
- onMouseEnter: onMouseEnter,
- onMouseLeave: onMouseLeave,
- onMouseDown: onMouseDown,
- onTouchStart: onTouchStart,
- style: newStyle
- };
- if (destroyPopupOnHide) {
- return React.createElement(
- Animate,
- {
- component: '',
- exclusive: true,
- transitionAppear: true,
- transitionName: this.getTransitionName()
- },
- visible ? React.createElement(
- Align,
- {
- target: this.getAlignTarget(),
- key: 'popup',
- ref: this.saveAlignRef,
- monitorWindowResize: true,
- align: align,
- onAlign: this.onAlign
- },
- React.createElement(
- PopupInner,
- _extends({
- visible: true
- }, popupInnerProps),
- children
- )
- ) : null
- );
- }
-
- return React.createElement(
- Animate,
- {
- component: '',
- exclusive: true,
- transitionAppear: true,
- transitionName: this.getTransitionName(),
- showProp: 'xVisible'
- },
- React.createElement(
- Align,
- {
- target: this.getAlignTarget(),
- key: 'popup',
- ref: this.saveAlignRef,
- monitorWindowResize: true,
- xVisible: visible,
- childrenProps: { visible: 'xVisible' },
- disabled: !visible,
- align: align,
- onAlign: this.onAlign
- },
- React.createElement(
- PopupInner,
- _extends({
- hiddenClassName: hiddenClassName
- }, popupInnerProps),
- children
- )
- )
- );
- };
-
- Popup.prototype.getZIndexStyle = function getZIndexStyle() {
- var style = {};
- var props = this.props;
- if (props.zIndex !== undefined) {
- style.zIndex = props.zIndex;
- }
- return style;
- };
-
- Popup.prototype.getMaskElement = function getMaskElement() {
- var props = this.props;
- var maskElement = void 0;
- if (props.mask) {
- var maskTransition = this.getMaskTransitionName();
- maskElement = React.createElement(LazyRenderBox, {
- style: this.getZIndexStyle(),
- key: 'mask',
- className: props.prefixCls + '-mask',
- hiddenClassName: props.prefixCls + '-mask-hidden',
- visible: props.visible
- });
- if (maskTransition) {
- maskElement = React.createElement(
- Animate,
- {
- key: 'mask',
- showProp: 'visible',
- transitionAppear: true,
- component: '',
- transitionName: maskTransition
- },
- maskElement
- );
- }
- }
- return maskElement;
- };
-
- Popup.prototype.render = function render() {
- return React.createElement(
- 'div',
- null,
- this.getMaskElement(),
- this.getPopupElement()
- );
- };
-
- return Popup;
- }(Component);
-
- Popup.propTypes = {
- visible: PropTypes.bool,
- style: PropTypes.object,
- getClassNameFromAlign: PropTypes.func,
- onAlign: PropTypes.func,
- getRootDomNode: PropTypes.func,
- align: PropTypes.any,
- destroyPopupOnHide: PropTypes.bool,
- className: PropTypes.string,
- prefixCls: PropTypes.string,
- onMouseEnter: PropTypes.func,
- onMouseLeave: PropTypes.func,
- onMouseDown: PropTypes.func,
- onTouchStart: PropTypes.func,
- stretch: PropTypes.string,
- children: PropTypes.node,
- point: PropTypes.shape({
- pageX: PropTypes.number,
- pageY: PropTypes.number
- })
- };
-
- var _initialiseProps = function _initialiseProps() {
- var _this3 = this;
-
- this.onAlign = function (popupDomNode, align) {
- var props = _this3.props;
- var currentAlignClassName = props.getClassNameFromAlign(align);
- // FIX: https://github.com/react-component/trigger/issues/56
- // FIX: https://github.com/react-component/tooltip/issues/79
- if (_this3.currentAlignClassName !== currentAlignClassName) {
- _this3.currentAlignClassName = currentAlignClassName;
- popupDomNode.className = _this3.getClassName(currentAlignClassName);
- }
- props.onAlign(popupDomNode, align);
- };
-
- this.setStretchSize = function () {
- var _props2 = _this3.props,
- stretch = _props2.stretch,
- getRootDomNode = _props2.getRootDomNode,
- visible = _props2.visible;
- var _state2 = _this3.state,
- stretchChecked = _state2.stretchChecked,
- targetHeight = _state2.targetHeight,
- targetWidth = _state2.targetWidth;
-
-
- if (!stretch || !visible) {
- if (stretchChecked) {
- _this3.setState({ stretchChecked: false });
- }
- return;
- }
-
- var $ele = getRootDomNode();
- if (!$ele) return;
-
- var height = $ele.offsetHeight;
- var width = $ele.offsetWidth;
-
- if (targetHeight !== height || targetWidth !== width || !stretchChecked) {
- _this3.setState({
- stretchChecked: true,
- targetHeight: height,
- targetWidth: width
- });
- }
- };
-
- this.getTargetElement = function () {
- return _this3.props.getRootDomNode();
- };
-
- this.getAlignTarget = function () {
- var point = _this3.props.point;
-
- if (point) {
- return point;
- }
- return _this3.getTargetElement;
- };
- };
-
- export default Popup;
|