UNPKG

shineout

Version:

Shein 前端组件库

193 lines (154 loc) 6.31 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = _default; var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _styles = require("./styles"); var _context = require("../Scroll/context"); var _uid = require("../utils/uid"); var _popover = require("../utils/dom/popover"); var DefaultProps = { animation: true, delay: 0, position: 'top', trigger: 'hover' }; function _default(options) { var show = options.show, hide = options.hide, move = options.move, isCurrent = options.isCurrent; var Container = /*#__PURE__*/ function (_PureComponent) { (0, _inheritsLoose2.default)(Container, _PureComponent); function Container(props) { var _this; _this = _PureComponent.call(this, props) || this; (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "id", void 0); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "placeholderElement", void 0); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "showTimer", void 0); _this.handleShow = _this.handleShow.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); _this.handleDismiss = _this.handleDismiss.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); _this.tryHide = _this.tryHide.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); _this.elementRef = _this.elementRef.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); _this.id = (0, _uid.getUidStr)(); return _this; } var _proto = Container.prototype; _proto.componentDidUpdate = function componentDidUpdate(prevProps) { if (!move || !isCurrent(this.id)) return; var _this$props = this.props, scrollLeft = _this$props.scrollLeft, scrollTop = _this$props.scrollTop; if (prevProps.scrollLeft !== scrollLeft || prevProps.scrollTop !== scrollTop) { var pos = this.getPosition(); move(this.id, pos); this.tryHide(); } }; _proto.componentWillUnmount = function componentWillUnmount() { hide(); }; _proto.getElement = function getElement() { return this.placeholderElement.nextSibling; }; _proto.getPosition = function getPosition() { var _this$props$position = this.props.position, position = _this$props$position === void 0 ? DefaultProps.position : _this$props$position; var el = this.getElement(); return (0, _popover.getPosition)(position, el); }; _proto.elementRef = function elementRef(el) { this.placeholderElement = el; }; _proto.tryHide = function tryHide() { var scrollElement = this.props.scrollElement; var rect = this.getElement().getBoundingClientRect(); var scrollRect = scrollElement ? scrollElement.getBoundingClientRect() : { top: 0, bottom: 0, left: 0, right: 0 }; if (rect.bottom < scrollRect.top || rect.top > scrollRect.bottom || rect.right < scrollRect.left || rect.left > scrollRect.right) { hide(); } }; _proto.handleShow = function handleShow() { var _this2 = this; if (this.showTimer) clearTimeout(this.showTimer); var delay = this.props.delay; if (!delay) { this.showSync(); } else { this.showTimer = setTimeout(function () { _this2.showSync(); }, delay); } }; _proto.handleDismiss = function handleDismiss() { clearTimeout(this.showTimer); hide(); }; _proto.showSync = function showSync() { var pos = this.getPosition(); var style = Object.keys(pos).reduce(function (data, key) { data[key] = pos[key]; return data; }, {}); var props = Object.assign({}, this.props, { style: style }); show(props, this.id, this.props.style); }; _proto.render = function render() { var _this3 = this; var _this$props2 = this.props, children = _this$props2.children, trigger = _this$props2.trigger, disabledChild = _this$props2.disabledChild, tip = _this$props2.tip; if (!(0, _react.isValidElement)(children)) { console.error(new Error('Tooltip children expect a single ReactElement.')); return null; } if (!tip) return children; var inner = disabledChild ? _react.default.createElement("span", { className: (0, _styles.tooltipClass)('disabled-wrapper'), style: { cursor: 'not-allowed' } }, (0, _react.cloneElement)(children, { style: (0, _objectSpread2.default)({}, children.props.style, { pointerEvents: 'none' }) })) : children; var props = { key: 'el' }; if (trigger === 'hover') { props.onMouseEnter = this.handleShow; props.onMouseLeave = this.handleDismiss; } else { props.onClick = function (e) { if (e) e.stopPropagation(); setTimeout(_this3.handleShow, 10); if (children.props.onClick) children.props.onClick(); }; } return [_react.default.createElement("noscript", { ref: this.elementRef, key: "ns" }), (0, _react.cloneElement)(inner, props)]; }; return Container; }(_react.PureComponent); (0, _defineProperty2.default)(Container, "defaultProps", DefaultProps); return (0, _context.scrollConsumer)(Container); }