UNPKG

@txdfe/at

Version:

一个设计体系组件库

361 lines (279 loc) 14.3 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _reactDom = require("react-dom"); var _classnames2 = _interopRequireDefault(require("classnames")); var _util = require("../util"); var _configProvider = _interopRequireDefault(require("../config-provider")); var _util2 = require("./util"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } 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 || 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 : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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; } /** Affix */ var Affix = /*#__PURE__*/function (_React$Component) { _inherits(Affix, _React$Component); var _super = _createSuper(Affix); function Affix(props, context) { var _this; _classCallCheck(this, Affix); _this = _super.call(this, props, context); _defineProperty(_assertThisInitialized(_this), "updatePosition", function () { _this._updateNodePosition(); }); _defineProperty(_assertThisInitialized(_this), "_updateNodePosition", function () { var _this$props = _this.props, container = _this$props.container, useAbsolute = _this$props.useAbsolute; var affixContainer = container(); if (!affixContainer) { return false; } var containerScrollTop = (0, _util2.getScroll)(affixContainer, true); // 容器在垂直位置上的滚动 offset var affixOffset = _this._getOffset(_this.affixNode, affixContainer); // 目标节点当前相对于容器的 offset var containerHeight = (0, _util2.getNodeHeight)(affixContainer); // 容器的高度 var affixHeight = _this.affixNode.offsetHeight; var containerRect = (0, _util2.getRect)(affixContainer); var affixChildHeight = _this.affixChildNode.offsetHeight; var _assertThisInitialize = _assertThisInitialized(_this), affixMode = _assertThisInitialize.affixMode; var affixStyle = { width: affixOffset.width }; var containerStyle = { width: affixOffset.width, height: affixChildHeight }; if (affixMode.top && containerScrollTop > affixOffset.top - affixMode.offset) { // affix top if (useAbsolute) { affixStyle.position = 'absolute'; affixStyle.top = containerScrollTop - (affixOffset.top - affixMode.offset); containerStyle.position = 'relative'; } else { affixStyle.position = 'fixed'; affixStyle.top = affixMode.offset + containerRect.top; } _this._setAffixStyle(affixStyle, true); _this._setContainerStyle(containerStyle); } else if (affixMode.bottom && containerScrollTop < affixOffset.top + affixHeight + affixMode.offset - containerHeight) { // affix bottom affixStyle.height = affixHeight; if (useAbsolute) { affixStyle.position = 'absolute'; affixStyle.top = containerScrollTop - (affixOffset.top + affixHeight + affixMode.offset - containerHeight); containerStyle.position = 'relative'; } else { affixStyle.position = 'fixed'; affixStyle.bottom = affixMode.offset; } _this._setAffixStyle(affixStyle, true); _this._setContainerStyle(containerStyle); } else { _this._setAffixStyle(null); _this._setContainerStyle(null); } }); _defineProperty(_assertThisInitialized(_this), "_affixNodeRefHandler", function (ref) { _this.affixNode = (0, _reactDom.findDOMNode)(ref); }); _defineProperty(_assertThisInitialized(_this), "_affixChildNodeRefHandler", function (ref) { _this.affixChildNode = (0, _reactDom.findDOMNode)(ref); }); _this.state = { style: null, containerStyle: null }; _this.affixMode = _this._getAffixMode(props); return _this; } _createClass(Affix, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; var container = this.props.container; this._updateNodePosition(); // wait for parent rendered this.timeout = setTimeout(function () { _this2._setEventHandlerForContainer(container); }); } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { if ('offsetTop' in nextProps || 'offsetBottom' in nextProps) { this.affixMode = this._getAffixMode(nextProps); this._updateNodePosition(); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.timeout) { clearTimeout(this.timeout); this.timeout = null; } var container = this.props.container; this._removeEventHandlerForContainer(container); } }, { key: "_setEventHandlerForContainer", value: function _setEventHandlerForContainer(getContainer) { var container = getContainer(); if (!container) { return; } _util.events.on(container, 'scroll', this._updateNodePosition, false); _util.events.on(container, 'resize', this._updateNodePosition, false); } }, { key: "_removeEventHandlerForContainer", value: function _removeEventHandlerForContainer(getContainer) { var container = getContainer(); if (container) { _util.events.off(container, 'scroll', this._updateNodePosition); _util.events.off(container, 'resize', this._updateNodePosition); } } }, { key: "_getAffixMode", value: function _getAffixMode(nextProps) { var props = nextProps || this.props; var offsetTop = props.offsetTop, offsetBottom = props.offsetBottom; var affixMode = { top: false, bottom: false, offset: 0 }; if (typeof offsetTop !== 'number' && typeof offsetBottom !== 'number') { // set default affixMode.top = true; } else if (typeof offsetTop === 'number') { affixMode.top = true; affixMode.bottom = false; affixMode.offset = offsetTop; } else if (typeof offsetBottom === 'number') { affixMode.bottom = true; affixMode.top = false; affixMode.offset = offsetBottom; } return affixMode; } }, { key: "_setAffixStyle", value: function _setAffixStyle(affixStyle) { var affixed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; if (_util.obj.shallowEqual(affixStyle, this.state.style)) { return; } this.setState({ style: affixStyle }); var onAffix = this.props.onAffix; if (affixed) { onAffix(true); } else if (!affixStyle) { onAffix(false); } } }, { key: "_setContainerStyle", value: function _setContainerStyle(containerStyle) { if (_util.obj.shallowEqual(containerStyle, this.state.containerStyle)) { return; } this.setState({ containerStyle: containerStyle }); } }, { key: "_getOffset", value: function _getOffset(affixNode, affixContainer) { var affixRect = affixNode.getBoundingClientRect(); // affix 元素 相对浏览器窗口的位置 var containerRect = (0, _util2.getRect)(affixContainer); // affix 容器 相对浏览器窗口的位置 var containerScrollTop = (0, _util2.getScroll)(affixContainer, true); var containerScrollLeft = (0, _util2.getScroll)(affixContainer, false); return { top: affixRect.top - containerRect.top + containerScrollTop, left: affixRect.left - containerRect.left + containerScrollLeft, width: affixRect.width, height: affixRect.height }; } }, { key: "render", value: function render() { var _classnames; var _this$props2 = this.props, prefix = _this$props2.prefix, className = _this$props2.className, style = _this$props2.style, children = _this$props2.children; var state = this.state; var classNames = (0, _classnames2["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "affix"), state.style), _defineProperty(_classnames, "".concat(prefix, "affix-top"), !state.style && this.affixMode.top), _defineProperty(_classnames, "".concat(prefix, "affix-bottom"), !state.style && this.affixMode.bottom), _defineProperty(_classnames, className, className), _classnames)); var combinedStyle = _objectSpread(_objectSpread({}, state.containerStyle), style); return /*#__PURE__*/_react["default"].createElement("div", { ref: this._affixNodeRefHandler, style: combinedStyle }, /*#__PURE__*/_react["default"].createElement("div", { ref: this._affixChildNodeRefHandler, className: classNames, style: state.style }, children)); } }]); return Affix; }(_react["default"].Component); _defineProperty(Affix, "propTypes", { prefix: _propTypes["default"].string, /** * 设置 Affix 需要监听滚动事件的容器元素 * @return {ReactElement} 目标容器元素的实例 */ container: _propTypes["default"].func, /** * 距离窗口顶部达到指定偏移量后触发 */ offsetTop: _propTypes["default"].number, /** * 距离窗口底部达到制定偏移量后触发 */ offsetBottom: _propTypes["default"].number, /** * 当元素的样式发生固钉样式变化时触发的回调函数 * @param {Boolean} 元素是否被固钉 */ onAffix: _propTypes["default"].func, /** * 是否启用绝对布局实现 affix * @param {Boolean} 是否启用绝对布局 */ useAbsolute: _propTypes["default"].bool, className: _propTypes["default"].string, style: _propTypes["default"].object, children: _propTypes["default"].any }); _defineProperty(Affix, "defaultProps", { prefix: 'next-', container: function container() { return window; }, onAffix: _util.func.noop }); var _default = _configProvider["default"].config(Affix); exports["default"] = _default;