shineout
Version:
Shein 前端组件库
164 lines (145 loc) • 5.83 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { Fragment } from 'react';
import { PureComponent } from '../component';
import Scroll from '../Scroll';
import { getKey } from '../utils/uid';
import { setTranslate } from '../utils/dom/translate';
var DefaultProps = {
itemsInView: 10,
lineHeight: 32,
data: [],
colNum: 1
};
var LazyList =
/*#__PURE__*/
function (_PureComponent) {
_inheritsLoose(LazyList, _PureComponent);
function LazyList(props) {
var _this;
_this = _PureComponent.call(this, props) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "optionInner", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "lastScrollTop", void 0);
_this.state = {
currentIndex: 0,
scrollTop: 0,
fixed: ''
};
_this.handleScroll = _this.handleScroll.bind(_assertThisInitialized(_assertThisInitialized(_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
});
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;
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.createElement(Fragment, {
key: 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.createElement(Scroll, {
stable: true,
className: className,
style: ms,
scroll: fixed,
onScroll: this.handleScroll,
scrollHeight: scrollHeight,
scrollTop: this.state.scrollTop
}, React.createElement("div", {
ref: function ref(el) {
_this2.optionInner = el;
},
style: gridStyle
}, React.createElement("div", {
style: {
height: currentIndex * lineHeight,
gridColumnEnd: '-1'
}
}), items));
};
return LazyList;
}(PureComponent);
_defineProperty(LazyList, "defaultProps", DefaultProps);
export default LazyList;