import $ from 'jquery';
import React, { Component, PropTypes } from 'react';
import { findDOMNode } from 'react-dom';

const propTypes = {
    children: PropTypes.node,
    title: PropTypes.string,
    className: PropTypes.string,
    delay: PropTypes.number,
};

class Tipsy extends Component {
    constructor(props) {
        super(props);
        this.tipsyProps = {
            title: props.title,
            className: props.className,
            delay: props.delay,
        };
    }

    componentDidMount() {
        const node = findDOMNode(this);
        if (this.props.delay) {
            setTimeout(() => {
                $(node).tooltip();
            }, this.props.delay);
        } else {
            $(node).tooltip();
        }
    }

    render() {
        return React.createElement('span', this.tipsyProps, this.props.children);
    }
}

export default Tipsy;

Tipsy.propTypes = propTypes;
