@txdfe/at
Version:
一个设计体系组件库
279 lines (278 loc) • 13.5 kB
JavaScript
;
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(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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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); }
/** Affix */
var Affix = /*#__PURE__*/function (_React$Component) {
function Affix(props, context) {
var _this;
_classCallCheck(this, Affix);
_this = _callSuper(this, Affix, [props, context]);
_defineProperty(_this, "updatePosition", function () {
_this._updateNodePosition();
});
_defineProperty(_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 _this2 = _this,
affixMode = _this2.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(_this, "_affixNodeRefHandler", function (ref) {
_this.affixNode = (0, _reactDom.findDOMNode)(ref);
});
_defineProperty(_this, "_affixChildNodeRefHandler", function (ref) {
_this.affixChildNode = (0, _reactDom.findDOMNode)(ref);
});
_this.state = {
style: null,
containerStyle: null
};
_this.affixMode = _this._getAffixMode(props);
return _this;
}
_inherits(Affix, _React$Component);
return _createClass(Affix, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this3 = this;
var container = this.props.container;
this._updateNodePosition();
// wait for parent rendered
this.timeout = setTimeout(function () {
_this3._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 _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"])(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "affix"), state.style), "".concat(prefix, "affix-top"), !state.style && this.affixMode.top), "".concat(prefix, "affix-bottom"), !state.style && this.affixMode.bottom), className, className));
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));
}
}]);
}(_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 = exports["default"] = _configProvider["default"].config(Affix);