shineout
Version:
Shein 前端组件库
178 lines (151 loc) • 6.47 kB
JavaScript
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
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 _component = require("../component");
var _Scroll = _interopRequireDefault(require("../Scroll"));
var _uid = require("../utils/uid");
var _translate = require("../utils/dom/translate");
var DefaultProps = {
itemsInView: 10,
lineHeight: 32,
data: [],
colNum: 1
};
var LazyList =
/*#__PURE__*/
function (_PureComponent) {
(0, _inheritsLoose2.default)(LazyList, _PureComponent);
function LazyList(props) {
var _this;
_this = _PureComponent.call(this, props) || this;
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "optionInner", void 0);
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "lastScrollTop", void 0);
_this.state = {
currentIndex: 0,
scrollTop: 0,
fixed: ''
};
_this.handleScroll = _this.handleScroll.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
return _this;
}
var _proto = LazyList.prototype;
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
if (!this.props.stay && prevProps.data.length !== this.props.data.length) {
this.resetScrollTop();
}
};
_proto.getScrollHeight = function getScrollHeight() {
var _this$props = this.props,
_this$props$lineHeigh = _this$props.lineHeight,
lineHeight = _this$props$lineHeigh === void 0 ? DefaultProps.lineHeight : _this$props$lineHeigh,
data = _this$props.data,
_this$props$colNum = _this$props.colNum,
colNum = _this$props$colNum === void 0 ? DefaultProps.colNum : _this$props$colNum;
var rows = Math.ceil(data.length / colNum);
return rows * lineHeight;
};
_proto.resetScrollTop = function resetScrollTop() {
this.setState({
currentIndex: 0,
scrollTop: 0
});
(0, _translate.setTranslate)(this.optionInner, '0px', "0px");
this.optionInner.style.marginTop = "0px";
this.lastScrollTop = 0;
};
_proto.handleScroll = function handleScroll(_x, y, _max, _bar, _v, h, _pixelX, pixelY) {
if (!this.optionInner) return;
var scrollHeight = this.getScrollHeight();
var _this$props2 = this.props,
data = _this$props2.data,
_this$props2$itemsInV = _this$props2.itemsInView,
itemsInView = _this$props2$itemsInV === void 0 ? DefaultProps.itemsInView : _this$props2$itemsInV,
_this$props2$lineHeig = _this$props2.lineHeight,
lineHeight = _this$props2$lineHeig === void 0 ? DefaultProps.lineHeight : _this$props2$lineHeig;
var fullHeight = itemsInView * lineHeight;
var contentHeight = scrollHeight - h;
var scrollTop = h > fullHeight ? 0 : y;
this.optionInner.style.marginTop = scrollTop * h + "px";
if (pixelY === undefined || pixelY === 0) {
this.lastScrollTop = scrollTop * contentHeight;
} else {
this.lastScrollTop += pixelY;
if (this.lastScrollTop < 0) this.lastScrollTop = 0; // scroll over bottom
if (this.lastScrollTop > contentHeight) this.lastScrollTop = contentHeight;
scrollTop = this.lastScrollTop / contentHeight;
this.optionInner.style.marginTop = scrollTop * h + "px";
}
var index = Math.floor(this.lastScrollTop / lineHeight) - 1;
if (data.length - itemsInView < index) index = data.length - itemsInView;
if (index < 0) index = 0;
(0, _translate.setTranslate)(this.optionInner, '0px', "-" + (this.lastScrollTop + scrollTop * h) + "px");
this.setState({
scrollTop: scrollTop,
currentIndex: index,
fixed: h < this.getScrollHeight() ? 'y' : ''
});
};
_proto.render = function render() {
var _this2 = this;
var _this$props3 = this.props,
className = _this$props3.className,
style = _this$props3.style,
height = _this$props3.height,
_this$props3$lineHeig = _this$props3.lineHeight,
lineHeight = _this$props3$lineHeig === void 0 ? DefaultProps.lineHeight : _this$props3$lineHeig,
data = _this$props3.data,
_this$props3$itemsInV = _this$props3.itemsInView,
itemsInView = _this$props3$itemsInV === void 0 ? DefaultProps.itemsInView : _this$props3$itemsInV,
renderItem = _this$props3.renderItem,
keygen = _this$props3.keygen,
_this$props3$colNum = _this$props3.colNum,
colNum = _this$props3$colNum === void 0 ? DefaultProps.colNum : _this$props3$colNum;
var _this$state = this.state,
currentIndex = _this$state.currentIndex,
fixed = _this$state.fixed;
var scrollHeight = this.getScrollHeight();
var ms = Object.assign({}, style, height && {
height: height
});
var start = currentIndex * colNum;
var items = data.slice(start, (currentIndex + itemsInView) * colNum).map(function (d, i) {
return _react.default.createElement(_react.Fragment, {
key: (0, _uid.getKey)(d, keygen, i)
}, renderItem(d, i + start));
});
var fr = Array(colNum).fill('1fr').join(' ');
var gridStyle = colNum > 1 ? {
display: 'grid',
gridTemplateColumns: fr
} : {};
return _react.default.createElement(_Scroll.default, {
stable: true,
className: className,
style: ms,
scroll: fixed,
onScroll: this.handleScroll,
scrollHeight: scrollHeight,
scrollTop: this.state.scrollTop
}, _react.default.createElement("div", {
ref: function ref(el) {
_this2.optionInner = el;
},
style: gridStyle
}, _react.default.createElement("div", {
style: {
height: currentIndex * lineHeight,
gridColumnEnd: '-1'
}
}), items));
};
return LazyList;
}(_component.PureComponent);
(0, _defineProperty2.default)(LazyList, "defaultProps", DefaultProps);
var _default = LazyList;
exports.default = _default;