/**
 * @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 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, text, onTipRender, ...restProps} = this.props,
            {tipVisible} = this.state;

        if (!text) {
            return children;
        }

        return (
            <div className="tip-provider">

                <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;
@@ -229,16 +229,53 @@
 ct-dom';
+%0Aimport classNames from 'classnames';
 %0A%0Aimport
@@ -1016,16 +1016,35 @@
 hildren,
+ providerClassName,
  text, o
@@ -1232,22 +1232,108 @@
 ame=
-%22tip-provider%22
+%7BclassNames('tip-provider', %7B%0A                %5BproviderClassName%5D: providerClassName%0A            %7D)%7D
 %3E%0A%0A 
@@ -2215,32 +2215,32 @@
 es = %7B%0A%0A    /**%0A
-
      * The CSS c
@@ -2274,24 +2274,128 @@
 nt.%0A     */%0A
+    providerClassName: PropTypes.string,%0A%0A    /**%0A     * The CSS class name of the tip element.%0A     */%0A
     classNam

update TipProvider