UNPKG

maycur-business

Version:

maycur business react components of web

348 lines (298 loc) • 13.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _tippy = _interopRequireDefault(require("./js/tippy")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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 } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var defaultProps = { html: null, position: 'bottom', animation: 'shift', animateFill: true, arrow: false, delay: 0, hideDelay: 0, trigger: 'mouseenter focus', duration: 0, hideDuration: 0, interactive: false, interactiveBorder: 2, theme: 'dark', offset: 0, hideOnClick: true, multiple: false, followCursor: false, inertia: false, popperOptions: {}, onUpdate: function onUpdate() {}, onCreate: function onCreate() {}, onShow: function onShow() {}, onShown: function onShown() {}, onHide: function onHide() {}, onHidden: function onHidden() {}, onTrigger: function onTrigger() {}, disabled: false, arrowSize: 'regular', size: 'regular', className: '', style: {}, distance: 0, onRequestClose: function onRequestClose() {}, sticky: false, stickyDuration: 0, touchHold: false, unmountHTMLWhenHide: false }; var propKeys = Object.keys(defaultProps); var detectPropsChanged = function detectPropsChanged(props, prevProps) { var result = []; propKeys.forEach(function (key) { if (props[key] !== prevProps[key]) { result.push(key); } }); return result; }; var Tooltip = /*#__PURE__*/ function (_Component) { _inherits(Tooltip, _Component); function Tooltip(props) { var _this; _classCallCheck(this, Tooltip); _this = _possibleConstructorReturn(this, _getPrototypeOf(Tooltip).call(this, props)); _this.initTippy = _this._initTippy.bind(_assertThisInitialized(_this)); _this.destroyTippy = _this._destroyTippy.bind(_assertThisInitialized(_this)); _this.updateTippy = _this._updateTippy.bind(_assertThisInitialized(_this)); _this.updateReactDom = _this._updateReactDom.bind(_assertThisInitialized(_this)); _this.showTooltip = _this._showTooltip.bind(_assertThisInitialized(_this)); _this.hideTooltip = _this._hideTooltip.bind(_assertThisInitialized(_this)); _this.updateSettings = _this._updateSettings.bind(_assertThisInitialized(_this)); return _this; } _createClass(Tooltip, [{ key: "componentDidMount", value: function componentDidMount() { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } this.initTippy(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } this.destroyTippy(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this2 = this; // enable and disabled if (typeof window === 'undefined' || typeof document === 'undefined') { return; } if (this.props.disabled === false && prevProps.disabled === true) { this.updateSettings('disabled', false); this.destroyTippy(); this.initTippy(); return; } if (this.props.disabled === true && prevProps.disabled === false) { this.updateSettings('disabled', true); this.destroyTippy(); return; } // open if (this.props.open === true && !prevProps.open) { this.updateSettings('open', true); setTimeout(function () { _this2.showTooltip(); }, 0); } if (this.props.open === false && prevProps.open === true) { this.updateSettings('open', false); this.hideTooltip(); } if (this.props.html !== prevProps.html) { this.updateReactDom(); } // Update content if (this.props.title !== prevProps.title) { this.updateTippy(); } // update otherProps var propChanges = detectPropsChanged(this.props, prevProps); propChanges.forEach(function (key) { _this2.updateSettings(key, _this2.props[key]); }); } }, { key: "_showTooltip", value: function _showTooltip() { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } if (this.tippy) { var popper = this.tippy.getPopperElement(this.tooltipDOM); this.tippy.show(popper, this.props.duration); } } }, { key: "_hideTooltip", value: function _hideTooltip() { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } if (this.tippy) { var popper = this.tippy.getPopperElement(this.tooltipDOM); this.tippy.hide(popper, this.props.hideDuration); } } }, { key: "_updateSettings", value: function _updateSettings(name, value) { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } if (this.tippy) { var popper = this.tippy.getPopperElement(this.tooltipDOM); this.tippy.updateSettings(popper, name, value); } } }, { key: "_updateReactDom", value: function _updateReactDom() { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } if (this.tippy) { this.updateSettings('reactDOM', this.props.html); var popper = this.tippy.getPopperElement(this.tooltipDOM); var isVisible = popper.style.visibility === 'visible' || this.props.open; if (isVisible) { this.tippy.updateForReact(popper, this.props.html); } } } }, { key: "_updateTippy", value: function _updateTippy() { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } if (this.tippy) { var popper = this.tippy.getPopperElement(this.tooltipDOM); this.tippy.update(popper); } } }, { key: "_initTippy", value: function _initTippy() { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } if (!this.props.disabled) { this.tooltipDOM.setAttribute('title', this.props.title); this.tippy = (0, _tippy["default"])(this.tooltipDOM, { // test: this.props.test, disabled: this.props.disabled, onTrigger: this.props.onTrigger, position: this.props.position, animation: this.props.animation, animateFill: this.props.animateFill, arrow: this.props.arrow, arrowSize: this.props.arrowSize, delay: this.props.delay, hideDelay: this.props.hideDelay, trigger: this.props.trigger, duration: this.props.duration, hideDuration: this.props.hideDuration, interactive: this.props.interactive, interactiveBorder: this.props.interactiveBorder, theme: this.props.theme, offset: this.props.offset, hideOnClick: this.props.hideOnClick, multiple: this.props.multiple, size: this.props.size, followCursor: this.props.followCursor, inertia: this.props.inertia, popperOptions: this.props.popperOptions, onUpdate: this.props.onUpdate, onCreate: this.props.onCreate, onShow: this.props.onShow, onShown: this.props.onShown, onHide: this.props.onHide, onHidden: this.props.onHidden, distance: this.props.distance, reactDOM: this.props.html, unmountHTMLWhenHide: this.props.unmountHTMLWhenHide, open: this.props.open, sticky: this.props.sticky, stickyDuration: this.props.stickyDuration, touchHold: this.props.touchHold, onRequestClose: this.props.onRequestClose, useContext: this.props.useContext, reactInstance: this.props.useContext ? this : undefined, performance: true, html: this.props.rawTemplate ? this.props.rawTemplate : undefined }); if (this.props.open) { this.showTooltip(); } } else { this.tippy = null; } } }, { key: "_destroyTippy", value: function _destroyTippy() { if (typeof window === 'undefined' || typeof document === 'undefined') { return; } if (this.tippy) { var popper = this.tippy.getPopperElement(this.tooltipDOM); this.updateSettings('open', false); this.tippy.hide(popper, 0); this.tippy.destroy(popper); this.tippy = null; } } }, { key: "render", value: function render() { var _this3 = this; return _react["default"].createElement("div", { ref: function ref(tooltip) { _this3.tooltipDOM = tooltip; }, title: this.props.title, className: this.props.className, tabIndex: this.props.tabIndex, style: _objectSpread({ display: 'inline', lineHeight: 'initial' }, this.props.style) }, this.props.children); } }]); return Tooltip; }(_react.Component); Tooltip.defaultProps = defaultProps; var _default = Tooltip; exports["default"] = _default;