UNPKG

9.31 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
10
11var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
12
13var _react = require('react');
14
15var _react2 = _interopRequireDefault(_react);
16
17var _classnames = require('classnames');
18
19var _classnames2 = _interopRequireDefault(_classnames);
20
21var _lodash = require('lodash.omit');
22
23var _lodash2 = _interopRequireDefault(_lodash);
24
25var _TetherContent = require('./TetherContent');
26
27var _TetherContent2 = _interopRequireDefault(_TetherContent);
28
29var _utils = require('./utils');
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
36
37function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
38
39var propTypes = {
40 placement: _react2.default.PropTypes.oneOf(_utils.tetherAttachements),
41 target: _react.PropTypes.string.isRequired,
42 isOpen: _react.PropTypes.bool,
43 disabled: _react.PropTypes.bool,
44 tether: _react.PropTypes.object,
45 tetherRef: _react.PropTypes.func,
46 className: _react.PropTypes.string,
47 cssModule: _react.PropTypes.object,
48 toggle: _react.PropTypes.func,
49 autohide: _react.PropTypes.bool,
50 delay: _react.PropTypes.oneOfType([_react.PropTypes.shape({ show: _react.PropTypes.number, hide: _react.PropTypes.number }), _react.PropTypes.number])
51};
52
53var DEFAULT_DELAYS = {
54 show: 0,
55 hide: 250
56};
57
58var defaultProps = {
59 isOpen: false,
60 placement: 'bottom',
61 delay: DEFAULT_DELAYS,
62 autohide: true,
63 toggle: function toggle() {}
64};
65
66var defaultTetherConfig = {
67 classPrefix: 'bs-tether',
68 classes: {
69 element: false,
70 enabled: 'show'
71 },
72 constraints: [{ to: 'scrollParent', attachment: 'together none' }, { to: 'window', attachment: 'together none' }]
73};
74
75var Tooltip = function (_React$Component) {
76 _inherits(Tooltip, _React$Component);
77
78 function Tooltip(props) {
79 _classCallCheck(this, Tooltip);
80
81 var _this = _possibleConstructorReturn(this, (Tooltip.__proto__ || Object.getPrototypeOf(Tooltip)).call(this, props));
82
83 _this.addTargetEvents = _this.addTargetEvents.bind(_this);
84 _this.getTetherConfig = _this.getTetherConfig.bind(_this);
85 _this.handleDocumentClick = _this.handleDocumentClick.bind(_this);
86 _this.removeTargetEvents = _this.removeTargetEvents.bind(_this);
87 _this.toggle = _this.toggle.bind(_this);
88 _this.onMouseOverTooltip = _this.onMouseOverTooltip.bind(_this);
89 _this.onMouseLeaveTooltip = _this.onMouseLeaveTooltip.bind(_this);
90 _this.onMouseOverTooltipContent = _this.onMouseOverTooltipContent.bind(_this);
91 _this.onMouseLeaveTooltipContent = _this.onMouseLeaveTooltipContent.bind(_this);
92 _this.show = _this.show.bind(_this);
93 _this.hide = _this.hide.bind(_this);
94 return _this;
95 }
96
97 _createClass(Tooltip, [{
98 key: 'componentDidMount',
99 value: function componentDidMount() {
100 this._target = document.getElementById(this.props.target);
101 this.addTargetEvents();
102 }
103 }, {
104 key: 'componentWillUnmount',
105 value: function componentWillUnmount() {
106 this.removeTargetEvents();
107 }
108 }, {
109 key: 'onMouseOverTooltip',
110 value: function onMouseOverTooltip() {
111 if (this._hideTimeout) {
112 this.clearHideTimeout();
113 }
114 this._showTimeout = setTimeout(this.show, this.getDelay('show'));
115 }
116 }, {
117 key: 'onMouseLeaveTooltip',
118 value: function onMouseLeaveTooltip() {
119 if (this._showTimeout) {
120 this.clearShowTimeout();
121 }
122 this._hideTimeout = setTimeout(this.hide, this.getDelay('hide'));
123 }
124 }, {
125 key: 'onMouseOverTooltipContent',
126 value: function onMouseOverTooltipContent() {
127 if (this.props.autohide) {
128 return;
129 }
130 if (this._hideTimeout) {
131 this.clearHideTimeout();
132 }
133 }
134 }, {
135 key: 'onMouseLeaveTooltipContent',
136 value: function onMouseLeaveTooltipContent() {
137 if (this.props.autohide) {
138 return;
139 }
140 if (this._showTimeout) {
141 this.clearShowTimeout();
142 }
143 this._hideTimeout = setTimeout(this.hide, this.getDelay('hide'));
144 }
145 }, {
146 key: 'getDelay',
147 value: function getDelay(key) {
148 var delay = this.props.delay;
149
150 if ((typeof delay === 'undefined' ? 'undefined' : _typeof(delay)) === 'object') {
151 return isNaN(delay[key]) ? DEFAULT_DELAYS[key] : delay[key];
152 }
153 return delay;
154 }
155 }, {
156 key: 'getTetherConfig',
157 value: function getTetherConfig() {
158 var attachments = (0, _utils.getTetherAttachments)(this.props.placement);
159 return _extends({}, defaultTetherConfig, attachments, {
160 target: '#' + this.props.target
161 }, this.props.tether);
162 }
163 }, {
164 key: 'show',
165 value: function show() {
166 if (!this.props.isOpen) {
167 this.clearShowTimeout();
168 this.toggle();
169 }
170 }
171 }, {
172 key: 'hide',
173 value: function hide() {
174 if (this.props.isOpen) {
175 this.clearHideTimeout();
176 this.toggle();
177 }
178 }
179 }, {
180 key: 'clearShowTimeout',
181 value: function clearShowTimeout() {
182 clearTimeout(this._showTimeout);
183 this._showTimeout = undefined;
184 }
185 }, {
186 key: 'clearHideTimeout',
187 value: function clearHideTimeout() {
188 clearTimeout(this._hideTimeout);
189 this._hideTimeout = undefined;
190 }
191 }, {
192 key: 'handleDocumentClick',
193 value: function handleDocumentClick(e) {
194 if (e.target === this._target || this._target.contains(e.target)) {
195 if (this._hideTimeout) {
196 this.clearHideTimeout();
197 }
198
199 if (!this.props.isOpen) {
200 this.toggle();
201 }
202 }
203 }
204 }, {
205 key: 'addTargetEvents',
206 value: function addTargetEvents() {
207 this._target.addEventListener('mouseover', this.onMouseOverTooltip, true);
208 this._target.addEventListener('mouseout', this.onMouseLeaveTooltip, true);
209 document.addEventListener('click', this.handleDocumentClick, true);
210 }
211 }, {
212 key: 'removeTargetEvents',
213 value: function removeTargetEvents() {
214 this._target.removeEventListener('mouseover', this.onMouseOverTooltip, true);
215 this._target.removeEventListener('mouseout', this.onMouseLeaveTooltip, true);
216 document.removeEventListener('click', this.handleDocumentClick, true);
217 }
218 }, {
219 key: 'toggle',
220 value: function toggle(e) {
221 if (this.props.disabled) {
222 return e && e.preventDefault();
223 }
224
225 return this.props.toggle();
226 }
227 }, {
228 key: 'render',
229 value: function render() {
230 if (!this.props.isOpen) {
231 return null;
232 }
233
234 var attributes = (0, _lodash2.default)(this.props, Object.keys(propTypes));
235 var classes = (0, _utils.mapToCssModules)((0, _classnames2.default)('tooltip-inner', this.props.className), this.props.cssModule);
236
237 var tetherConfig = this.getTetherConfig();
238
239 return _react2.default.createElement(
240 _TetherContent2.default,
241 {
242 className: 'tooltip',
243 tether: tetherConfig,
244 tetherRef: this.props.tetherRef,
245 isOpen: this.props.isOpen,
246 toggle: this.toggle
247 },
248 _react2.default.createElement('div', _extends({}, attributes, {
249 className: classes,
250 onMouseOver: this.onMouseOverTooltipContent,
251 onMouseLeave: this.onMouseLeaveTooltipContent
252 }))
253 );
254 }
255 }]);
256
257 return Tooltip;
258}(_react2.default.Component);
259
260Tooltip.propTypes = propTypes;
261Tooltip.defaultProps = defaultProps;
262
263exports.default = Tooltip;
\No newline at end of file