choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
239 lines (204 loc) • 8.02 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Children, cloneElement, Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import omit from 'lodash/omit';
import Spin from '../spin';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
import { Size } from '../_util/enum';
import Pagination from '../pagination';
import { Row } from '../grid';
import Item from './Item';
import { getPrefixCls as _getPrefixCls } from '../configure';
var List =
/*#__PURE__*/
function (_Component) {
_inherits(List, _Component);
var _super = _createSuper(List);
function List() {
var _this;
_classCallCheck(this, List);
_this = _super.apply(this, arguments);
_this.keys = {};
_this.renderItem = function (item, index) {
var _this$props = _this.props,
dataSource = _this$props.dataSource,
renderItem = _this$props.renderItem,
rowKey = _this$props.rowKey;
var key;
if (typeof rowKey === 'function') {
key = rowKey(dataSource[index]);
} else if (typeof rowKey === 'string') {
key = dataSource[rowKey];
} else {
key = dataSource.key;
}
if (!key) {
key = "list-item-".concat(index);
}
_this.keys[index] = key;
return renderItem(item, index);
};
_this.renderEmpty = function (contextLocale) {
var _assertThisInitialize = _assertThisInitialized(_this),
props = _assertThisInitialize.props;
var locale = _objectSpread({}, contextLocale, {}, props.locale);
return React.createElement("div", {
className: "".concat(_this.getPrefixCls(), "-empty-text")
}, locale.emptyText);
};
return _this;
}
_createClass(List, [{
key: "getChildContext",
value: function getChildContext() {
var grid = this.props.grid;
return {
grid: grid
};
}
}, {
key: "isSomethingAfterLastTtem",
value: function isSomethingAfterLastTtem() {
var _this$props2 = this.props,
loadMore = _this$props2.loadMore,
pagination = _this$props2.pagination,
footer = _this$props2.footer;
return !!(loadMore || pagination || footer);
}
}, {
key: "getPrefixCls",
value: function getPrefixCls() {
var prefixCls = this.props.prefixCls;
return _getPrefixCls('list', prefixCls);
}
}, {
key: "render",
value: function render() {
var _classNames,
_this2 = this;
var _this$props3 = this.props,
bordered = _this$props3.bordered,
split = _this$props3.split,
className = _this$props3.className,
children = _this$props3.children,
itemLayout = _this$props3.itemLayout,
loadMore = _this$props3.loadMore,
pagination = _this$props3.pagination,
grid = _this$props3.grid,
dataSource = _this$props3.dataSource,
size = _this$props3.size,
header = _this$props3.header,
footer = _this$props3.footer,
empty = _this$props3.empty,
loading = _this$props3.loading,
rest = _objectWithoutProperties(_this$props3, ["bordered", "split", "className", "children", "itemLayout", "loadMore", "pagination", "grid", "dataSource", "size", "header", "footer", "empty", "loading"]);
var prefixCls = this.getPrefixCls();
var loadingProp = loading;
if (typeof loadingProp === 'boolean') {
loadingProp = {
spinning: loadingProp
};
}
var isLoading = loadingProp && loadingProp.spinning; // large => lg
// small => sm
var sizeCls = '';
switch (size) {
case Size.large:
sizeCls = 'lg';
break;
case Size.small:
sizeCls = 'sm';
break;
default:
}
var classString = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-vertical"), itemLayout === 'vertical'), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(sizeCls), sizeCls), _defineProperty(_classNames, "".concat(prefixCls, "-split"), split), _defineProperty(_classNames, "".concat(prefixCls, "-bordered"), bordered), _defineProperty(_classNames, "".concat(prefixCls, "-loading"), isLoading), _defineProperty(_classNames, "".concat(prefixCls, "-grid"), grid), _defineProperty(_classNames, "".concat(prefixCls, "-something-after-last-item"), this.isSomethingAfterLastTtem()), _classNames));
var paginationContent = React.createElement("div", {
className: "".concat(prefixCls, "-pagination")
}, React.createElement(Pagination, _extends({}, pagination)));
var childrenContent;
childrenContent = isLoading && React.createElement("div", {
style: {
minHeight: 53
}
});
if (dataSource.length > 0) {
var items = dataSource.map(function (item, index) {
return _this2.renderItem(item, index);
});
var childrenList = Children.map(items, function (child, index) {
return cloneElement(child, {
key: _this2.keys[index]
});
});
childrenContent = grid ? React.createElement(Row, {
gutter: grid.gutter
}, childrenList) : childrenList;
} else if (!children && !isLoading && !empty) {
childrenContent = React.createElement(LocaleReceiver, {
componentName: "Table",
defaultLocale: defaultLocale.Table
}, this.renderEmpty);
} else {
childrenContent = empty;
}
var content = React.createElement("div", null, React.createElement(Spin, _extends({}, loadingProp), childrenContent), loadMore, !loadMore && pagination ? paginationContent : null);
return React.createElement("div", _extends({
className: classString
}, omit(rest, ['prefixCls', 'rowKey', 'renderItem', 'selectable'])), header && React.createElement("div", {
className: "".concat(prefixCls, "-header")
}, header), content, children, footer && React.createElement("div", {
className: "".concat(prefixCls, "-footer")
}, footer));
}
}]);
return List;
}(Component);
export { List as default };
List.displayName = 'List';
List.Item = Item;
List.childContextTypes = {
grid: PropTypes.any
};
List.defaultProps = {
dataSource: [],
bordered: false,
split: true,
loading: false,
pagination: false
};
//# sourceMappingURL=index.js.map