1 | import _extends from "@babel/runtime/helpers/extends";
|
2 | import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
3 | import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
4 | import _objectSpread from "@babel/runtime/helpers/objectSpread2";
|
5 | import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
6 | import _createClass from "@babel/runtime/helpers/createClass";
|
7 | import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
8 | import _inherits from "@babel/runtime/helpers/inherits";
|
9 | import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
10 | import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
11 |
|
12 | function _createSuper(Derived) {
|
13 | function isNativeReflectConstruct() {
|
14 | if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
15 | if (Reflect.construct.sham) return false;
|
16 | if (typeof Proxy === "function") return true;
|
17 |
|
18 | try {
|
19 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
|
20 | return true;
|
21 | } catch (e) {
|
22 | return false;
|
23 | }
|
24 | }
|
25 |
|
26 | return function () {
|
27 | var Super = _getPrototypeOf(Derived),
|
28 | result;
|
29 |
|
30 | if (isNativeReflectConstruct()) {
|
31 | var NewTarget = _getPrototypeOf(this).constructor;
|
32 |
|
33 | result = Reflect.construct(Super, arguments, NewTarget);
|
34 | } else {
|
35 | result = Super.apply(this, arguments);
|
36 | }
|
37 |
|
38 | return _possibleConstructorReturn(this, result);
|
39 | };
|
40 | }
|
41 |
|
42 | import React, { Children, cloneElement, Component } from 'react';
|
43 | import PropTypes from 'prop-types';
|
44 | import classNames from 'classnames';
|
45 | import omit from 'lodash/omit';
|
46 | import Spin from '../spin';
|
47 | import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
48 | import defaultLocale from '../locale-provider/default';
|
49 | import { Size } from '../_util/enum';
|
50 | import Pagination from '../pagination';
|
51 | import { Row } from '../grid';
|
52 | import Item from './Item';
|
53 | import { getPrefixCls as _getPrefixCls } from '../configure';
|
54 |
|
55 | var List =
|
56 |
|
57 | function (_Component) {
|
58 | _inherits(List, _Component);
|
59 |
|
60 | var _super = _createSuper(List);
|
61 |
|
62 | function List() {
|
63 | var _this;
|
64 |
|
65 | _classCallCheck(this, List);
|
66 |
|
67 | _this = _super.apply(this, arguments);
|
68 | _this.keys = {};
|
69 |
|
70 | _this.renderItem = function (item, index) {
|
71 | var _this$props = _this.props,
|
72 | dataSource = _this$props.dataSource,
|
73 | renderItem = _this$props.renderItem,
|
74 | rowKey = _this$props.rowKey;
|
75 | var key;
|
76 |
|
77 | if (typeof rowKey === 'function') {
|
78 | key = rowKey(dataSource[index]);
|
79 | } else if (typeof rowKey === 'string') {
|
80 | key = dataSource[rowKey];
|
81 | } else {
|
82 | key = dataSource.key;
|
83 | }
|
84 |
|
85 | if (!key) {
|
86 | key = "list-item-".concat(index);
|
87 | }
|
88 |
|
89 | _this.keys[index] = key;
|
90 | return renderItem(item, index);
|
91 | };
|
92 |
|
93 | _this.renderEmpty = function (contextLocale) {
|
94 | var _assertThisInitialize = _assertThisInitialized(_this),
|
95 | props = _assertThisInitialize.props;
|
96 |
|
97 | var locale = _objectSpread({}, contextLocale, {}, props.locale);
|
98 |
|
99 | return React.createElement("div", {
|
100 | className: "".concat(_this.getPrefixCls(), "-empty-text")
|
101 | }, locale.emptyText);
|
102 | };
|
103 |
|
104 | return _this;
|
105 | }
|
106 |
|
107 | _createClass(List, [{
|
108 | key: "getChildContext",
|
109 | value: function getChildContext() {
|
110 | var grid = this.props.grid;
|
111 | return {
|
112 | grid: grid
|
113 | };
|
114 | }
|
115 | }, {
|
116 | key: "isSomethingAfterLastTtem",
|
117 | value: function isSomethingAfterLastTtem() {
|
118 | var _this$props2 = this.props,
|
119 | loadMore = _this$props2.loadMore,
|
120 | pagination = _this$props2.pagination,
|
121 | footer = _this$props2.footer;
|
122 | return !!(loadMore || pagination || footer);
|
123 | }
|
124 | }, {
|
125 | key: "getPrefixCls",
|
126 | value: function getPrefixCls() {
|
127 | var prefixCls = this.props.prefixCls;
|
128 | return _getPrefixCls('list', prefixCls);
|
129 | }
|
130 | }, {
|
131 | key: "render",
|
132 | value: function render() {
|
133 | var _classNames,
|
134 | _this2 = this;
|
135 |
|
136 | var _this$props3 = this.props,
|
137 | bordered = _this$props3.bordered,
|
138 | split = _this$props3.split,
|
139 | className = _this$props3.className,
|
140 | children = _this$props3.children,
|
141 | itemLayout = _this$props3.itemLayout,
|
142 | loadMore = _this$props3.loadMore,
|
143 | pagination = _this$props3.pagination,
|
144 | grid = _this$props3.grid,
|
145 | dataSource = _this$props3.dataSource,
|
146 | size = _this$props3.size,
|
147 | header = _this$props3.header,
|
148 | footer = _this$props3.footer,
|
149 | empty = _this$props3.empty,
|
150 | loading = _this$props3.loading,
|
151 | rest = _objectWithoutProperties(_this$props3, ["bordered", "split", "className", "children", "itemLayout", "loadMore", "pagination", "grid", "dataSource", "size", "header", "footer", "empty", "loading"]);
|
152 |
|
153 | var prefixCls = this.getPrefixCls();
|
154 | var loadingProp = loading;
|
155 |
|
156 | if (typeof loadingProp === 'boolean') {
|
157 | loadingProp = {
|
158 | spinning: loadingProp
|
159 | };
|
160 | }
|
161 |
|
162 | var isLoading = loadingProp && loadingProp.spinning;
|
163 |
|
164 |
|
165 | var sizeCls = '';
|
166 |
|
167 | switch (size) {
|
168 | case Size.large:
|
169 | sizeCls = 'lg';
|
170 | break;
|
171 |
|
172 | case Size.small:
|
173 | sizeCls = 'sm';
|
174 | break;
|
175 |
|
176 | default:
|
177 | }
|
178 |
|
179 | 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));
|
180 | var paginationContent = React.createElement("div", {
|
181 | className: "".concat(prefixCls, "-pagination")
|
182 | }, React.createElement(Pagination, _extends({}, pagination)));
|
183 | var childrenContent;
|
184 | childrenContent = isLoading && React.createElement("div", {
|
185 | style: {
|
186 | minHeight: 53
|
187 | }
|
188 | });
|
189 |
|
190 | if (dataSource.length > 0) {
|
191 | var items = dataSource.map(function (item, index) {
|
192 | return _this2.renderItem(item, index);
|
193 | });
|
194 | var childrenList = Children.map(items, function (child, index) {
|
195 | return cloneElement(child, {
|
196 | key: _this2.keys[index]
|
197 | });
|
198 | });
|
199 | childrenContent = grid ? React.createElement(Row, {
|
200 | gutter: grid.gutter
|
201 | }, childrenList) : childrenList;
|
202 | } else if (!children && !isLoading && !empty) {
|
203 | childrenContent = React.createElement(LocaleReceiver, {
|
204 | componentName: "Table",
|
205 | defaultLocale: defaultLocale.Table
|
206 | }, this.renderEmpty);
|
207 | } else {
|
208 | childrenContent = empty;
|
209 | }
|
210 |
|
211 | var content = React.createElement("div", null, React.createElement(Spin, _extends({}, loadingProp), childrenContent), loadMore, !loadMore && pagination ? paginationContent : null);
|
212 | return React.createElement("div", _extends({
|
213 | className: classString
|
214 | }, omit(rest, ['prefixCls', 'rowKey', 'renderItem', 'selectable'])), header && React.createElement("div", {
|
215 | className: "".concat(prefixCls, "-header")
|
216 | }, header), content, children, footer && React.createElement("div", {
|
217 | className: "".concat(prefixCls, "-footer")
|
218 | }, footer));
|
219 | }
|
220 | }]);
|
221 |
|
222 | return List;
|
223 | }(Component);
|
224 |
|
225 | export { List as default };
|
226 | List.displayName = 'List';
|
227 | List.Item = Item;
|
228 | List.childContextTypes = {
|
229 | grid: PropTypes.any
|
230 | };
|
231 | List.defaultProps = {
|
232 | dataSource: [],
|
233 | bordered: false,
|
234 | split: true,
|
235 | loading: false,
|
236 | pagination: false
|
237 | };
|
238 |
|