UNPKG

reactstrap

Version:
390 lines 16.6 kB
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); } function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } import React from 'react'; import PropTypes from 'prop-types'; import PopperContent from './PopperContent'; import { getTarget, targetPropType, omit, PopperPlacements, mapToCssModules, DOMElement } from './utils'; export var propTypes = { children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), placement: PropTypes.oneOf(PopperPlacements), target: targetPropType.isRequired, container: targetPropType, isOpen: PropTypes.bool, disabled: PropTypes.bool, hideArrow: PropTypes.bool, boundariesElement: PropTypes.oneOfType([PropTypes.string, DOMElement]), className: PropTypes.string, innerClassName: PropTypes.string, arrowClassName: PropTypes.string, popperClassName: PropTypes.string, cssModule: PropTypes.object, toggle: PropTypes.func, autohide: PropTypes.bool, placementPrefix: PropTypes.string, delay: PropTypes.oneOfType([PropTypes.shape({ show: PropTypes.number, hide: PropTypes.number }), PropTypes.number]), modifiers: PropTypes.array, strategy: PropTypes.string, offset: PropTypes.arrayOf(PropTypes.number), innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.string, PropTypes.object]), trigger: PropTypes.string, fade: PropTypes.bool, flip: PropTypes.bool }; var DEFAULT_DELAYS = { show: 0, hide: 50 }; var defaultProps = { isOpen: false, hideArrow: false, autohide: false, delay: DEFAULT_DELAYS, toggle: function toggle() {}, trigger: 'click', fade: true }; function isInDOMSubtree(element, subtreeRoot) { return subtreeRoot && (element === subtreeRoot || subtreeRoot.contains(element)); } function isInDOMSubtrees(element) { var subtreeRoots = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; return subtreeRoots && subtreeRoots.length && subtreeRoots.filter(function (subTreeRoot) { return isInDOMSubtree(element, subTreeRoot); })[0]; } var TooltipPopoverWrapper = /*#__PURE__*/function (_React$Component) { _inherits(TooltipPopoverWrapper, _React$Component); var _super = _createSuper(TooltipPopoverWrapper); function TooltipPopoverWrapper(props) { var _this; _classCallCheck(this, TooltipPopoverWrapper); _this = _super.call(this, props); _this._targets = []; _this.currentTargetElement = null; _this.addTargetEvents = _this.addTargetEvents.bind(_assertThisInitialized(_this)); _this.handleDocumentClick = _this.handleDocumentClick.bind(_assertThisInitialized(_this)); _this.removeTargetEvents = _this.removeTargetEvents.bind(_assertThisInitialized(_this)); _this.toggle = _this.toggle.bind(_assertThisInitialized(_this)); _this.showWithDelay = _this.showWithDelay.bind(_assertThisInitialized(_this)); _this.hideWithDelay = _this.hideWithDelay.bind(_assertThisInitialized(_this)); _this.onMouseOverTooltipContent = _this.onMouseOverTooltipContent.bind(_assertThisInitialized(_this)); _this.onMouseLeaveTooltipContent = _this.onMouseLeaveTooltipContent.bind(_assertThisInitialized(_this)); _this.show = _this.show.bind(_assertThisInitialized(_this)); _this.hide = _this.hide.bind(_assertThisInitialized(_this)); _this.onEscKeyDown = _this.onEscKeyDown.bind(_assertThisInitialized(_this)); _this.getRef = _this.getRef.bind(_assertThisInitialized(_this)); _this.state = { isOpen: props.isOpen }; _this._isMounted = false; return _this; } _createClass(TooltipPopoverWrapper, [{ key: "componentDidMount", value: function componentDidMount() { this._isMounted = true; this.updateTarget(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this._isMounted = false; this.removeTargetEvents(); this._targets = null; this.clearShowTimeout(); this.clearHideTimeout(); } }, { key: "handleDocumentClick", value: function handleDocumentClick(e) { var triggers = this.props.trigger.split(' '); if (triggers.indexOf('legacy') > -1 && (this.props.isOpen || isInDOMSubtrees(e.target, this._targets))) { if (this._hideTimeout) { this.clearHideTimeout(); } if (this.props.isOpen && !isInDOMSubtree(e.target, this._popover)) { this.hideWithDelay(e); } else if (!this.props.isOpen) { this.showWithDelay(e); } } else if (triggers.indexOf('click') > -1 && isInDOMSubtrees(e.target, this._targets)) { if (this._hideTimeout) { this.clearHideTimeout(); } if (!this.props.isOpen) { this.showWithDelay(e); } else { this.hideWithDelay(e); } } } }, { key: "onMouseOverTooltipContent", value: function onMouseOverTooltipContent() { if (this.props.trigger.indexOf('hover') > -1 && !this.props.autohide) { if (this._hideTimeout) { this.clearHideTimeout(); } if (this.state.isOpen && !this.props.isOpen) { this.toggle(); } } } }, { key: "onMouseLeaveTooltipContent", value: function onMouseLeaveTooltipContent(e) { if (this.props.trigger.indexOf('hover') > -1 && !this.props.autohide) { if (this._showTimeout) { this.clearShowTimeout(); } e.persist(); this._hideTimeout = setTimeout(this.hide.bind(this, e), this.getDelay('hide')); } } }, { key: "onEscKeyDown", value: function onEscKeyDown(e) { if (e.key === 'Escape') { this.hide(e); } } }, { key: "getRef", value: function getRef(ref) { var innerRef = this.props.innerRef; if (innerRef) { if (typeof innerRef === 'function') { innerRef(ref); } else if (_typeof(innerRef) === 'object') { innerRef.current = ref; } } this._popover = ref; } }, { key: "getDelay", value: function getDelay(key) { var delay = this.props.delay; if (_typeof(delay) === 'object') { return isNaN(delay[key]) ? DEFAULT_DELAYS[key] : delay[key]; } return delay; } }, { key: "getCurrentTarget", value: function getCurrentTarget(target) { if (!target) return null; var index = this._targets.indexOf(target); if (index >= 0) return this._targets[index]; return this.getCurrentTarget(target.parentElement); } }, { key: "show", value: function show(e) { if (!this.props.isOpen) { this.clearShowTimeout(); this.currentTargetElement = e ? e.currentTarget || this.getCurrentTarget(e.target) : null; if (e && e.composedPath && typeof e.composedPath === 'function') { var path = e.composedPath(); this.currentTargetElement = path && path[0] || this.currentTargetElement; } this.toggle(e); } } }, { key: "showWithDelay", value: function showWithDelay(e) { if (this._hideTimeout) { this.clearHideTimeout(); } this._showTimeout = setTimeout(this.show.bind(this, e), this.getDelay('show')); } }, { key: "hide", value: function hide(e) { if (this.props.isOpen) { this.clearHideTimeout(); this.currentTargetElement = null; this.toggle(e); } } }, { key: "hideWithDelay", value: function hideWithDelay(e) { if (this._showTimeout) { this.clearShowTimeout(); } this._hideTimeout = setTimeout(this.hide.bind(this, e), this.getDelay('hide')); } }, { key: "clearShowTimeout", value: function clearShowTimeout() { clearTimeout(this._showTimeout); this._showTimeout = undefined; } }, { key: "clearHideTimeout", value: function clearHideTimeout() { clearTimeout(this._hideTimeout); this._hideTimeout = undefined; } }, { key: "addEventOnTargets", value: function addEventOnTargets(type, handler, isBubble) { this._targets.forEach(function (target) { target.addEventListener(type, handler, isBubble); }); } }, { key: "removeEventOnTargets", value: function removeEventOnTargets(type, handler, isBubble) { this._targets.forEach(function (target) { target.removeEventListener(type, handler, isBubble); }); } }, { key: "addTargetEvents", value: function addTargetEvents() { if (this.props.trigger) { var triggers = this.props.trigger.split(' '); if (triggers.indexOf('manual') === -1) { if (triggers.indexOf('click') > -1 || triggers.indexOf('legacy') > -1) { document.addEventListener('click', this.handleDocumentClick, true); } if (this._targets && this._targets.length) { if (triggers.indexOf('hover') > -1) { this.addEventOnTargets('mouseover', this.showWithDelay, true); this.addEventOnTargets('mouseout', this.hideWithDelay, true); } if (triggers.indexOf('focus') > -1) { this.addEventOnTargets('focusin', this.show, true); this.addEventOnTargets('focusout', this.hide, true); } this.addEventOnTargets('keydown', this.onEscKeyDown, true); } } } } }, { key: "removeTargetEvents", value: function removeTargetEvents() { if (this._targets) { this.removeEventOnTargets('mouseover', this.showWithDelay, true); this.removeEventOnTargets('mouseout', this.hideWithDelay, true); this.removeEventOnTargets('keydown', this.onEscKeyDown, true); this.removeEventOnTargets('focusin', this.show, true); this.removeEventOnTargets('focusout', this.hide, true); } document.removeEventListener('click', this.handleDocumentClick, true); } }, { key: "updateTarget", value: function updateTarget() { var newTarget = getTarget(this.props.target, true); if (newTarget !== this._targets) { this.removeTargetEvents(); this._targets = newTarget ? Array.from(newTarget) : []; this.currentTargetElement = this.currentTargetElement || this._targets[0]; this.addTargetEvents(); } } }, { key: "toggle", value: function toggle(e) { if (this.props.disabled || !this._isMounted) { return e && e.preventDefault(); } return this.props.toggle(e); } }, { key: "render", value: function render() { var _this2 = this; if (this.props.isOpen) { this.updateTarget(); } var target = this.currentTargetElement || this._targets[0]; if (!target) { return null; } var _this$props = this.props, className = _this$props.className, cssModule = _this$props.cssModule, innerClassName = _this$props.innerClassName, isOpen = _this$props.isOpen, hideArrow = _this$props.hideArrow, boundariesElement = _this$props.boundariesElement, placement = _this$props.placement, placementPrefix = _this$props.placementPrefix, arrowClassName = _this$props.arrowClassName, popperClassName = _this$props.popperClassName, container = _this$props.container, modifiers = _this$props.modifiers, strategy = _this$props.strategy, offset = _this$props.offset, fade = _this$props.fade, flip = _this$props.flip, children = _this$props.children; var attributes = omit(this.props, Object.keys(propTypes)); var popperClasses = mapToCssModules(popperClassName, cssModule); var classes = mapToCssModules(innerClassName, cssModule); return /*#__PURE__*/React.createElement(PopperContent, { className: className, target: target, isOpen: isOpen, hideArrow: hideArrow, boundariesElement: boundariesElement, placement: placement, placementPrefix: placementPrefix, arrowClassName: arrowClassName, popperClassName: popperClasses, container: container, modifiers: modifiers, strategy: strategy, offset: offset, cssModule: cssModule, fade: fade, flip: flip }, function (_ref) { var update = _ref.update; return /*#__PURE__*/React.createElement("div", _extends({}, attributes, { ref: _this2.getRef, className: classes, role: "tooltip", onMouseOver: _this2.onMouseOverTooltipContent, onMouseLeave: _this2.onMouseLeaveTooltipContent, onKeyDown: _this2.onEscKeyDown }), typeof children === 'function' ? children({ update: update }) : children); }); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if (props.isOpen && !state.isOpen) { return { isOpen: props.isOpen }; } return null; } }]); return TooltipPopoverWrapper; }(React.Component); TooltipPopoverWrapper.propTypes = propTypes; TooltipPopoverWrapper.defaultProps = defaultProps; export default TooltipPopoverWrapper;