/**
 * @file TipProvider component
 * @author liangxiaojun(liangxiaojun@derbysoft.com)
 */

import React, {Component, isValidElement, cloneElement} from 'react';
import PropTypes from 'prop-types';
import {findDOMNode} from 'react-dom';
import classNames from 'classnames';

import Tip from '../Tip';
import Theme from '../Theme';

import Util from '../_vendors/Util';

class TipProvider extends Component {

    static Position = Tip.Position;
    static Theme = Theme;

    constructor(props, ...restArgs) {

        super(props, ...restArgs);

        this.state = {
            tipVisible: false
        };

    }

    showTip = () => {
        if (!this.state.tipVisible) {
            this.setState({
                tipVisible: true
            });
        }
    };

    hideTip = () => {
        this.setState({
            tipVisible: false
        });
    };

    componentDidMount() {
        this.refs.trigger && (this.triggerEl = findDOMNode(this.refs.trigger));
    }

    render() {

        const {children, className, style, text, onTipRender, ...restProps} = this.props,
            {tipVisible} = this.state,

            wrapperClassName = classNames('tip-provider', {
                [className]: className
            });

        return (
            <div className={wrapperClassName}
                 style={style}>

                <div ref="triggerWrapper"
                     className="trigger-wrapper"
                     onMouseOver={this.showTip}
                     onMouseOut={this.hideTip}>
                    {
                        isValidElement(children) ?
                            cloneElement(children, {
                                ref: 'trigger'
                            })
                            :
                            <span ref="trigger">
                                {children}
                            </span>
                    }
                </div>

                <Tip {...restProps}
                     triggerEl={this.triggerEl}
                     visible={tipVisible}
                     onRender={onTipRender}>
                    {text}
                </Tip>

            </div>
        );
    }

}

TipProvider.propTypes = {

    /**
     * The CSS class name of the root element.
     */
    className: PropTypes.string,

    /**
     * Override the styles of the root element.
     */
    style: PropTypes.object,

    /**
     * If true,the popover will have a triangle on the top of the DOM element.
     */
    hasTriangle: PropTypes.bool,

    triangle: PropTypes.element,

    /**
     * The popover theme.Can be primary,highlight,success,warning,error.
     */
    theme: PropTypes.oneOf(Util.enumerateValue(Theme)),

    /**
     * The popover alignment.
     */
    position: PropTypes.oneOf(Util.enumerateValue(Tip.Position)),

    /**
     * If true, menu will have animation effects.
     */
    isAnimated: PropTypes.bool,

    shouldPreventContainerScroll: PropTypes.bool,

    /**
     * The depth of Paper component.
     */
    depth: PropTypes.number,

    isTriggerPositionFixed: PropTypes.bool,

    /**
     * The function of tip render.
     */
    onTipRender: PropTypes.func,

    /**
     * Callback function fired when wrapper wheeled.
     */
    onWheel: PropTypes.func,

    text: PropTypes.any

};

TipProvider.defaultProps = {

    theme: Theme.DARK,

    hasTriangle: true,
    position: Tip.Position.BOTTOM,
    isAnimated: true,
    shouldPreventContainerScroll: true,
    isTriggerPositionFixed: false

};

export default TipProvider;
@@ -233,45 +233,8 @@
 om';
-%0Aimport classNames from 'classnames';
 %0A%0Aim
@@ -983,26 +983,8 @@
 ren,
- className, style,
  tex
@@ -1064,17 +1064,17 @@
 is.state
-,
+;
 %0A%0A      
@@ -1079,57 +1079,18 @@
     
-    
-wrapperClassName = classNames('tip-provider',
+if (!text)
  %7B%0A 
@@ -1104,34 +1104,24 @@
     
- 
-   %5BclassName%5D: className
+return children;
 %0A   
@@ -1125,23 +1125,17 @@
         
-    %7D);
+%7D
 %0A%0A      
@@ -1176,57 +1176,22 @@
 ame=
-%7BwrapperClassName%7D%0A                 style=%7Bstyle%7D
+%22tip-provider%22
 %3E%0A%0A 

update TipProvider