UNPKG

@txdfe/at

Version:

一个设计体系组件库

175 lines (174 loc) 8.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = require("react"); var _reactDom = require("react-dom"); var _propTypes = _interopRequireDefault(require("prop-types")); var _util = require("../util"); var _position = _interopRequireDefault(require("./utils/position")); var _findNode = _interopRequireDefault(require("./utils/find-node")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var noop = _util.func.noop; var getStyle = _util.dom.getStyle; var place = _position["default"].place; var Position = exports["default"] = /*#__PURE__*/function (_Component) { function Position() { var _this; _classCallCheck(this, Position); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, Position, [].concat(args)); _defineProperty(_this, "handleResize", function () { clearTimeout(_this.resizeTimeout); _this.resizeTimeout = setTimeout(function () { _this.setPosition(); }, 200); }); return _this; } _inherits(Position, _Component); return _createClass(Position, [{ key: "componentDidMount", value: function componentDidMount() { this.setPosition(); if (this.props.needListenResize) { _util.events.on(window, 'resize', this.handleResize); } if (this.props.needListenScroll) { _util.events.on(window, 'scroll', this.handleResize); } } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { if ('align' in nextProps && nextProps.align !== this.props.align || nextProps.shouldUpdatePosition || !this.props.visible && nextProps.visible) { this.shouldUpdatePosition = true; } } }, { key: "componentDidUpdate", value: function componentDidUpdate() { if (this.shouldUpdatePosition) { this.setPosition(); this.shouldUpdatePosition = false; } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.props.needListenResize) { _util.events.off(window, 'resize', this.handleResize); } if (this.props.needListenScroll) { _util.events.off(window, 'scroll', this.handleResize); } clearTimeout(this.resizeTimeout); } }, { key: "setPosition", value: function setPosition() { var _this$props = this.props, role = _this$props.role, align = _this$props.align, offset = _this$props.offset, visible = _this$props.visible, beforePosition = _this$props.beforePosition, onPosition = _this$props.onPosition, needAdjust = _this$props.needAdjust, rtl = _this$props.rtl; if (!visible) { return; } beforePosition(); var contentNode = this.getContentNode(); var targetNode = this.getTargetNode(); var containerNode = this.getContainerNode(this.props); if (contentNode && targetNode) { var resultAlign = place({ role: role, pinElement: contentNode, baseElement: targetNode, align: align, offset: offset, needAdjust: needAdjust, isRtl: rtl, containerNode: containerNode }); var top = getStyle(contentNode, 'top'); var left = getStyle(contentNode, 'left'); onPosition({ align: resultAlign.split(' '), top: top, left: left }, contentNode); } } }, { key: "getContentNode", value: function getContentNode() { return (0, _reactDom.findDOMNode)(this); } }, { key: "getContainerNode", value: function getContainerNode(props) { var targetNode = (0, _findNode["default"])(props.target); return (0, _findNode["default"])(props.container, targetNode); } }, { key: "getTargetNode", value: function getTargetNode() { var target = this.props.target; return target === _position["default"].VIEWPORT ? _position["default"].VIEWPORT : (0, _findNode["default"])(target, this.props); } }, { key: "render", value: function render() { return _react.Children.only(this.props.children); } }]); }(_react.Component); _defineProperty(Position, "VIEWPORT", _position["default"].VIEWPORT); _defineProperty(Position, "propTypes", { role: _propTypes["default"].string, children: _propTypes["default"].node, visible: _propTypes["default"].bool, container: _propTypes["default"].any, target: _propTypes["default"].any, align: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].bool]), offset: _propTypes["default"].array, beforePosition: _propTypes["default"].func, onPosition: _propTypes["default"].func, needAdjust: _propTypes["default"].bool, needListenResize: _propTypes["default"].bool, needListenScroll: _propTypes["default"].bool, shouldUpdatePosition: _propTypes["default"].bool, rtl: _propTypes["default"].bool }); _defineProperty(Position, "defaultProps", { align: 'tl bl', offset: [0, 0], beforePosition: noop, onPosition: noop, needAdjust: true, needListenResize: true, needListenScroll: true, shouldUpdatePosition: false, rtl: false });