|
| 1 | +import React, { PropTypes } from 'react'; |
| 2 | +import TetherContent from './TetherContent'; |
| 3 | +import { getTetherAttachments, tetherAttachements } from './utils'; |
| 4 | +const propTypes = { |
| 5 | + placement: React.PropTypes.oneOf(tetherAttachements), |
| 6 | + target: PropTypes.string.isRequired, |
| 7 | + isOpen: PropTypes.bool |
| 8 | +}; |
| 9 | + |
| 10 | +const defaultProps = { |
| 11 | + isOpen: false, |
| 12 | + placement: 'bottom' |
| 13 | +}; |
| 14 | + |
| 15 | +const defaultTetherConfig = { |
| 16 | + classPrefix: 'bs-tether', |
| 17 | + classes: { element: 'tooltip in', enabled: 'open' }, |
| 18 | + constraints: [ |
| 19 | + { to: 'scrollParent', attachment: 'together none' }, |
| 20 | + { to: 'window', attachment: 'together none' } |
| 21 | + ] |
| 22 | +}; |
| 23 | + |
| 24 | +class Tooltip extends React.Component { |
| 25 | + constructor(props) { |
| 26 | + super(props); |
| 27 | + |
| 28 | + this.addTargetEvents = this.addTargetEvents.bind(this); |
| 29 | + this.getTetherConfig = this.getTetherConfig.bind(this); |
| 30 | + this.handleDocumentClick = this.handleDocumentClick.bind(this); |
| 31 | + this.removeTargetEvents = this.removeTargetEvents.bind(this); |
| 32 | + this.toggle = this.toggle.bind(this); |
| 33 | + this.onMouseOverTooltip = this.onMouseOverTooltip.bind(this); |
| 34 | + this.onMouseLeaveTooltip = this.onMouseLeaveTooltip.bind(this); |
| 35 | + this.onTimeout = this.onTimeout.bind(this); |
| 36 | + } |
| 37 | + |
| 38 | + componentDidMount() { |
| 39 | + this._target = document.getElementById(this.props.target); |
| 40 | + this.addTargetEvents(); |
| 41 | + } |
| 42 | + |
| 43 | + componentWillUnmount() { |
| 44 | + this.removeTargetEvents(); |
| 45 | + } |
| 46 | + |
| 47 | + onMouseOverTooltip() { |
| 48 | + if (this._hoverTimeout) { |
| 49 | + clearTimeout(this._hoverTimeout); |
| 50 | + } |
| 51 | + |
| 52 | + if (!this.props.isOpen) { |
| 53 | + this.toggle(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + onMouseLeaveTooltip() { |
| 58 | + this._hoverTimeout = setTimeout(this.onTimeout, 250); |
| 59 | + } |
| 60 | + |
| 61 | + onTimeout() { |
| 62 | + if (this.props.isOpen) { |
| 63 | + this.toggle(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + getTetherConfig() { |
| 68 | + const attachments = getTetherAttachments(this.props.placement); |
| 69 | + return { |
| 70 | + ...defaultTetherConfig, |
| 71 | + ...attachments, |
| 72 | + target: '#' + this.props.target, |
| 73 | + ...this.props.tether |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + handleDocumentClick(e) { |
| 78 | + if (e.target === this._target || this._target.contains(e.target)) { |
| 79 | + if (this._hoverTimeout) { |
| 80 | + clearTimeout(this._hoverTimeout); |
| 81 | + } |
| 82 | + |
| 83 | + if (!this.props.isOpen) { |
| 84 | + this.toggle(); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + addTargetEvents() { |
| 90 | + this._target.addEventListener('mouseover', this.onMouseOverTooltip); |
| 91 | + this._target.addEventListener('mouseout', this.onMouseLeaveTooltip); |
| 92 | + document.addEventListener('click', this.handleDocumentClick); |
| 93 | + } |
| 94 | + |
| 95 | + removeTargetEvents() { |
| 96 | + this._target.removeEventListener('mouseover', this.onMouseOverTooltip); |
| 97 | + this._target.removeEventListener('mouseout', this.onMouseLeaveTooltip); |
| 98 | + document.removeEventListener('click', this.handleDocumentClick); |
| 99 | + } |
| 100 | + |
| 101 | + toggle(e) { |
| 102 | + if (this.props.disabled) { |
| 103 | + return e && e.preventDefault(); |
| 104 | + } |
| 105 | + |
| 106 | + this.props.toggle(); |
| 107 | + } |
| 108 | + |
| 109 | + render() { |
| 110 | + if (!this.props.isOpen) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + let tetherConfig = this.getTetherConfig(); |
| 115 | + |
| 116 | + return ( |
| 117 | + <TetherContent |
| 118 | + onMouseOver={this.onMouseOverTooltip} |
| 119 | + onMouseLeave={this.onMouseLeaveTooltip} |
| 120 | + arrow="tooltip" |
| 121 | + tether={tetherConfig} |
| 122 | + isOpen={this.props.isOpen} |
| 123 | + toggle={this.toggle}> |
| 124 | + <div className="tooltip-inner"> |
| 125 | + {this.props.children} |
| 126 | + </div> |
| 127 | + </TetherContent> |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +Tooltip.propTypes = propTypes; |
| 133 | +Tooltip.defaultProps = defaultProps; |
| 134 | + |
| 135 | +export default Tooltip; |
0 commit comments