shineout
Version:
Shein 前端组件库
214 lines (187 loc) • 7.31 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { Component, isValidElement } from 'react';
import classnames from 'classnames';
import { getLocale } from '../locale';
import LazyList from '../AnimationList/LazyList';
import { listClass } from './styles';
import { isFunc, isArray, isString } from '../utils/is';
import { getKey } from '../utils/uid';
import { removeStack, addStack } from '../utils/lazyload';
import Spin from '../Spin';
import getDataset from '../utils/dom/getDataset';
import Checkbox from '../Table/Checkbox';
import { isRTL } from '../config';
var DefaultProps = {
colNum: 1,
loading: false,
size: 'default'
};
var Index =
/*#__PURE__*/
function (_Component) {
_inheritsLoose(Index, _Component);
function Index(props) {
var _this;
_this = _Component.call(this, props) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "id", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "node", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "observer", void 0);
_this.bindNode = _this.bindNode.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.bindObserver = _this.bindObserver.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.scrollLoading = _this.scrollLoading.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.renderItem = _this.renderItem.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.id = null;
return _this;
}
var _proto = Index.prototype;
_proto.componentWillUnmount = function componentWillUnmount() {
removeStack(this.id, true);
this.node = null;
this.observer = null;
this.id = null;
};
_proto.getItemClassName = function getItemClassName(value, index, flag, isLastCol) {
var rowClassName = this.props.rowClassName;
var base = listClass('item', flag && 'checkbox', isLastCol && 'item-last');
if (isFunc(rowClassName) && rowClassName) return classnames(base, rowClassName(value, index));
if (isString(rowClassName)) return classnames(base, rowClassName);
return base;
};
_proto.getContent = function getContent(value, index) {
var renderItem = this.props.renderItem;
if (renderItem && typeof renderItem === 'function') return renderItem(value, index);
if (isString(renderItem)) return value[renderItem];
if (isString(value)) return value;
return null;
};
_proto.scrollLoading = function scrollLoading() {
var scrollLoading = this.props.scrollLoading;
if (!isFunc(scrollLoading)) return;
if (scrollLoading) scrollLoading();
};
_proto.bindNode = function bindNode(node) {
this.node = node;
};
_proto.bindObserver = function bindObserver(node) {
this.observer = node;
if (!node) return;
removeStack(this.id, true);
this.id = addStack({
container: this.node,
element: node,
render: this.scrollLoading,
offset: 20,
noRemove: true
});
};
_proto.renderCheckBox = function renderCheckBox(flag, data, index) {
if (!flag) return null;
var datum = this.props.datum;
return React.createElement(Checkbox, {
data: data,
index: index,
datum: datum,
force: datum.check(data)
});
};
_proto.renderItem = function renderItem(value, index) {
var _this$props = this.props,
keygen = _this$props.keygen,
onChange = _this$props.onChange,
data = _this$props.data,
_this$props$colNum = _this$props.colNum,
colNum = _this$props$colNum === void 0 ? 1 : _this$props$colNum;
var haveRowSelected = isFunc(onChange);
var content = this.getContent(value, index);
var lastColStart = data.length - (data.length % colNum || colNum);
var isLast = index >= lastColStart;
return React.createElement("div", {
className: this.getItemClassName(value, index, haveRowSelected, isLast),
key: getKey(value, keygen, index)
}, this.renderCheckBox(haveRowSelected, value, index), haveRowSelected ? React.createElement("div", {
className: listClass('item-meta')
}, content) : content);
};
_proto.renderList = function renderList(isEmpty) {
var _this$props2 = this.props,
data = _this$props2.data,
empty = _this$props2.empty,
keygen = _this$props2.keygen,
fixed = _this$props2.fixed,
rowsInView = _this$props2.rowsInView,
lineHeight = _this$props2.lineHeight,
value = _this$props2.value,
colNum = _this$props2.colNum;
if (isEmpty) return React.createElement("div", {
className: listClass('item', 'empty')
}, empty || getLocale('noData'));
if (!fixed) {
var items = data.map(this.renderItem);
if (colNum && colNum > 1) {
var frs = Array(colNum).fill('1fr').join(' ');
return React.createElement("div", {
style: {
display: 'grid',
gridTemplateColumns: frs
}
}, items);
}
return items;
}
return React.createElement(LazyList, {
lineHeight: lineHeight,
data: data,
keygen: keygen,
renderItem: this.renderItem,
itemsInView: rowsInView,
force: value,
colNum: colNum
});
};
_proto.renderFooter = function renderFooter() {
var footer = this.props.footer;
if (footer && typeof footer === 'function') return React.createElement("div", {
className: listClass('footer')
}, footer());
if (isValidElement(footer)) return React.createElement("div", {
className: listClass('footer')
}, footer);
return null;
};
_proto.render = function render() {
var _this$props3 = this.props,
data = _this$props3.data,
loading = _this$props3.loading,
style = _this$props3.style,
size = _this$props3.size,
bordered = _this$props3.bordered,
fixed = _this$props3.fixed,
height = _this$props3.height,
scrollLoading = _this$props3.scrollLoading;
var isEmpty = !isArray(data) || data.length <= 0;
var ms = Object.assign({}, style, height && {
height: height
});
return React.createElement("div", _extends({
className: classnames(listClass('container', size, bordered && 'bordered', fixed && 'fixed', isRTL() && 'rtl'), this.props.className),
style: ms,
ref: this.bindNode
}, getDataset(this.props)), loading && React.createElement("div", {
className: listClass('loading')
}, typeof loading === 'boolean' ? React.createElement(Spin, {
size: 40
}) : loading), React.createElement("div", {
className: listClass('list', isEmpty && 'empty')
}, this.renderList(isEmpty)), !isEmpty && isFunc(scrollLoading) && React.createElement("div", {
ref: this.bindObserver
}), this.renderFooter());
};
return Index;
}(Component);
_defineProperty(Index, "defaultProps", DefaultProps);
_defineProperty(Index, "displayName", void 0);
Index.displayName = 'ShineoutList';
export default Index;