UNPKG

5.08 kBJavaScriptView Raw
1import _typeof from 'babel-runtime/helpers/typeof';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _extends from 'babel-runtime/helpers/extends';
4import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
5import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
6import _inherits from 'babel-runtime/helpers/inherits';
7import React, { Children } from 'react';
8import PropTypes from 'prop-types';
9import classnames from 'classnames';
10import ListHeader from './list-header';
11import ListFooter from './list-footer';
12import RowComponent from './list/row';
13import BodyComponent from './list/body';
14import HeaderComponent from './fixed/header';
15import WrapperComponent from './fixed/wrapper';
16import { statics } from './util';
17
18export default function list(BaseComponent) {
19 var _class, _temp2;
20
21 var ListTable = (_temp2 = _class = function (_React$Component) {
22 _inherits(ListTable, _React$Component);
23
24 function ListTable() {
25 var _temp, _this, _ret;
26
27 _classCallCheck(this, ListTable);
28
29 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
30 args[_key] = arguments[_key];
31 }
32
33 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {}, _temp), _possibleConstructorReturn(_this, _ret);
34 }
35
36 ListTable.prototype.getChildContext = function getChildContext() {
37 return {
38 listHeader: this.listHeader,
39 listFooter: this.listFooter,
40 rowSelection: this.rowSelection
41 };
42 };
43
44 ListTable.prototype.normalizeDataSource = function normalizeDataSource(dataSource) {
45 var ret = [];
46 var loop = function loop(dataSource, level) {
47 dataSource.forEach(function (item) {
48 var itemCopy = _extends({}, item);
49 itemCopy.__level = level;
50 ret.push(itemCopy);
51 if (itemCopy.children) {
52 loop(itemCopy.children, level + 1);
53 }
54 });
55 };
56 loop(dataSource, 0);
57 this.ds = ret;
58 return ret;
59 };
60
61 ListTable.prototype.render = function render() {
62 var _this2 = this;
63
64 /* eslint-disable prefer-const */
65 var _props = this.props,
66 components = _props.components,
67 children = _props.children,
68 className = _props.className,
69 prefix = _props.prefix,
70 others = _objectWithoutProperties(_props, ['components', 'children', 'className', 'prefix']);
71
72 var isList = false,
73 ret = [];
74 Children.forEach(children, function (child) {
75 if (child) {
76 if (['function', 'object'].indexOf(_typeof(child.type)) > -1) {
77 if (child.type._typeMark === 'listHeader') {
78 _this2.listHeader = child.props;
79 isList = true;
80 } else if (child.type._typeMark === 'listFooter') {
81 _this2.listFooter = child.props;
82 } else {
83 ret.push(child);
84 }
85 } else {
86 ret.push(child);
87 }
88 }
89 });
90 this.rowSelection = this.props.rowSelection;
91 if (isList) {
92 var _classnames;
93
94 components = _extends({}, components);
95 components.Row = components.Row || RowComponent;
96 components.Body = components.Body || BodyComponent;
97 components.Header = components.Header || HeaderComponent;
98 components.Wrapper = components.Wrapper || WrapperComponent;
99 className = classnames((_classnames = {}, _classnames[prefix + 'table-group'] = true, _classnames[className] = className, _classnames));
100 }
101 return React.createElement(BaseComponent, _extends({}, others, {
102 components: components,
103 children: ret.length > 0 ? ret : undefined,
104 className: className,
105 prefix: prefix
106 }));
107 };
108
109 return ListTable;
110 }(React.Component), _class.ListHeader = ListHeader, _class.ListFooter = ListFooter, _class.ListRow = RowComponent, _class.ListBody = BodyComponent, _class.propTypes = _extends({}, BaseComponent.propTypes), _class.defaultProps = _extends({}, BaseComponent.defaultProps), _class.childContextTypes = {
111 listHeader: PropTypes.any,
112 listFooter: PropTypes.any,
113 rowSelection: PropTypes.object
114 }, _temp2);
115 ListTable.displayName = 'ListTable';
116
117 statics(ListTable, BaseComponent);
118 return ListTable;
119}
\No newline at end of file