shineout
Version:
Shein 前端组件库
78 lines (63 loc) • 2.2 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 from 'react';
import { PureComponent } from '../component';
import { lazyloadClass } from './styles';
import { addStack, removeStack } from '../utils/lazyload';
var DefaultProps = {
offset: 0
};
var Lazyload =
/*#__PURE__*/
function (_PureComponent) {
_inheritsLoose(Lazyload, _PureComponent);
function Lazyload(props) {
var _this;
_this = _PureComponent.call(this, props) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "placeholder", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "lazyId", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "placeholderRef", function (el) {
_this.placeholder = el;
});
_this.state = {
ready: false
};
return _this;
}
var _proto = Lazyload.prototype;
_proto.componentDidMount = function componentDidMount() {
var _this2 = this;
var _this$props = this.props,
container = _this$props.container,
_this$props$offset = _this$props.offset,
offset = _this$props$offset === void 0 ? DefaultProps.offset : _this$props$offset;
this.lazyId = addStack({
offset: offset,
container: container,
element: this.placeholder,
render: function render() {
return _this2.setState({
ready: true
});
}
});
};
_proto.componentWillUnmount = function componentWillUnmount() {
removeStack(this.lazyId);
};
_proto.render = function render() {
var ready = this.state.ready;
var _this$props2 = this.props,
children = _this$props2.children,
placeholder = _this$props2.placeholder;
if (ready) return children;
return React.createElement("span", {
ref: this.placeholderRef,
className: lazyloadClass('_')
}, placeholder);
};
return Lazyload;
}(PureComponent);
_defineProperty(Lazyload, "defaultProps", DefaultProps);
export default Lazyload;