UNPKG

9.65 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _typeof from 'babel-runtime/helpers/typeof';
3import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
4import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
5import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
6import _inherits from 'babel-runtime/helpers/inherits';
7
8var _class, _temp2;
9
10import React from 'react';
11import PropTypes from 'prop-types';
12import classnames from 'classnames';
13import RowComponent from './row';
14import CellComponent from './cell';
15import { dom, events } from '../../util';
16
17var noop = function noop() {};
18
19var Body = (_temp2 = _class = function (_React$Component) {
20 _inherits(Body, _React$Component);
21
22 function Body() {
23 var _temp, _this, _ret;
24
25 _classCallCheck(this, Body);
26
27 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
28 args[_key] = arguments[_key];
29 }
30
31 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.getRowRef = function (i, row) {
32 _this.props.rowRef(i, row);
33 }, _this.onRowClick = function (record, index, e) {
34 _this.props.onRowClick(record, index, e);
35 }, _this.onRowMouseEnter = function (record, index, e) {
36 _this.props.onRowMouseEnter(record, index, e);
37 }, _this.onRowMouseLeave = function (record, index, e) {
38 _this.props.onRowMouseLeave(record, index, e);
39 }, _this.onBodyMouseOver = function (e) {
40 _this.props.onBodyMouseOver(e);
41 }, _this.onBodyMouseOut = function (e) {
42 _this.props.onBodyMouseOut(e);
43 }, _this.getEmptyNode = function (ref) {
44 _this.emptyNode = ref;
45 }, _this.setEmptyDomStyle = function () {
46 var tableEl = _this.props.tableEl;
47 // getboundingclientRect 获取的是除 margin 之外的内容区,可能带小数点,不四舍五入
48
49 var borderLeftWidth = dom.getStyle(tableEl, 'borderLeftWidth');
50 var tableWidth = tableEl && tableEl.getBoundingClientRect().width;
51 var totalWidth = tableWidth - borderLeftWidth - 1 || '100%';
52
53 dom.setStyle(_this.emptyNode, { width: totalWidth });
54 }, _temp), _possibleConstructorReturn(_this, _ret);
55 }
56
57 Body.prototype.componentDidMount = function componentDidMount() {
58 events.on(window, 'resize', this.setEmptyDomStyle);
59 };
60
61 Body.prototype.componentDidUpdate = function componentDidUpdate() {
62 this.setEmptyDomStyle();
63 };
64
65 Body.prototype.componentWillUnmount = function componentWillUnmount() {
66 events.off(window, 'resize', this.setEmptyDomStyle);
67 };
68
69 Body.prototype.render = function render() {
70 var _this2 = this;
71
72 /*eslint-disable no-unused-vars */
73 var _props = this.props,
74 prefix = _props.prefix,
75 className = _props.className,
76 children = _props.children,
77 Tag = _props.component,
78 colGroup = _props.colGroup,
79 loading = _props.loading,
80 emptyContent = _props.emptyContent,
81 components = _props.components,
82 getCellProps = _props.getCellProps,
83 primaryKey = _props.primaryKey,
84 getRowProps = _props.getRowProps,
85 dataSource = _props.dataSource,
86 cellRef = _props.cellRef,
87 columns = _props.columns,
88 rowRef = _props.rowRef,
89 onRowClick = _props.onRowClick,
90 onRowMouseEnter = _props.onRowMouseEnter,
91 onRowMouseLeave = _props.onRowMouseLeave,
92 onBodyMouseOver = _props.onBodyMouseOver,
93 onBodyMouseOut = _props.onBodyMouseOut,
94 locale = _props.locale,
95 pure = _props.pure,
96 expandedIndexSimulate = _props.expandedIndexSimulate,
97 tableEl = _props.tableEl,
98 rtl = _props.rtl,
99 crossline = _props.crossline,
100 tableWidth = _props.tableWidth,
101 others = _objectWithoutProperties(_props, ['prefix', 'className', 'children', 'component', 'colGroup', 'loading', 'emptyContent', 'components', 'getCellProps', 'primaryKey', 'getRowProps', 'dataSource', 'cellRef', 'columns', 'rowRef', 'onRowClick', 'onRowMouseEnter', 'onRowMouseLeave', 'onBodyMouseOver', 'onBodyMouseOut', 'locale', 'pure', 'expandedIndexSimulate', 'tableEl', 'rtl', 'crossline', 'tableWidth']);
102
103 var totalWidth = +(tableEl && tableEl.clientWidth) - 1 || '100%';
104
105 var _components$Row = components.Row,
106 Row = _components$Row === undefined ? RowComponent : _components$Row,
107 _components$Cell = components.Cell,
108 Cell = _components$Cell === undefined ? CellComponent : _components$Cell;
109
110 var empty = loading ? React.createElement(
111 'span',
112 null,
113 '\xA0'
114 ) : emptyContent || locale.empty;
115 var rows = React.createElement(
116 'tr',
117 null,
118 React.createElement(
119 'td',
120 { colSpan: columns.length },
121 React.createElement(
122 'div',
123 {
124 ref: this.getEmptyNode,
125 className: prefix + 'table-empty',
126 style: { position: 'sticky', left: 0, overflow: 'hidden', width: totalWidth }
127 },
128 empty
129 )
130 )
131 );
132 if (Tag === 'div') {
133 rows = React.createElement(
134 'table',
135 { role: 'table' },
136 React.createElement(
137 'tbody',
138 null,
139 rows
140 )
141 );
142 }
143 if (dataSource.length) {
144 rows = dataSource.map(function (record, index) {
145 var _classnames;
146
147 var rowProps = {};
148 // record may be a string
149 var rowIndex = (typeof record === 'undefined' ? 'undefined' : _typeof(record)) === 'object' && '__rowIndex' in record ? record.__rowIndex : index;
150
151 if (expandedIndexSimulate) {
152 rowProps = record.__expanded ? {} : getRowProps(record, index / 2);
153 } else {
154 rowProps = getRowProps(record, rowIndex);
155 }
156
157 rowProps = rowProps || {};
158
159 var rowClass = rowProps.className;
160 var className = classnames((_classnames = {
161 first: index === 0,
162 last: index === dataSource.length - 1
163 }, _classnames[rowClass] = rowClass, _classnames));
164 var expanded = record.__expanded ? 'expanded' : '';
165 return React.createElement(Row, _extends({
166 key: '' + (record[primaryKey] || (record[primaryKey] === 0 ? 0 : rowIndex)) + expanded
167 }, rowProps, {
168 ref: _this2.getRowRef.bind(_this2, expanded ? rowIndex + '_expanded' : rowIndex),
169 colGroup: colGroup,
170 rtl: rtl,
171 columns: columns,
172 primaryKey: primaryKey,
173 record: record,
174 rowIndex: rowIndex,
175 __rowIndex: rowIndex,
176 prefix: prefix,
177 pure: pure,
178 cellRef: cellRef,
179 getCellProps: getCellProps,
180 className: className,
181 Cell: Cell,
182 tableEl: tableEl,
183 onClick: _this2.onRowClick,
184 locale: locale,
185 onMouseEnter: _this2.onRowMouseEnter,
186 onMouseLeave: _this2.onRowMouseLeave
187 }));
188 });
189 } else {
190 // 异步设置空数据时的宽度
191 this.setEmptyDomStyle();
192 }
193 var event = crossline ? {
194 onMouseOver: this.onBodyMouseOver,
195 onMouseOut: this.onBodyMouseOut
196 } : {};
197 return React.createElement(
198 Tag,
199 _extends({ className: className }, others, event),
200 rows,
201 children
202 );
203 };
204
205 return Body;
206}(React.Component), _class.propTypes = {
207 loading: PropTypes.bool,
208 emptyContent: PropTypes.any,
209 tableEl: PropTypes.any,
210 prefix: PropTypes.string,
211 pure: PropTypes.bool,
212 components: PropTypes.object,
213 getCellProps: PropTypes.func,
214 cellRef: PropTypes.func,
215 primaryKey: PropTypes.oneOfType([PropTypes.symbol, PropTypes.string]),
216 getRowProps: PropTypes.func,
217 rowRef: PropTypes.func,
218 dataSource: PropTypes.array,
219 children: PropTypes.any,
220 className: PropTypes.string,
221 component: PropTypes.string,
222 colGroup: PropTypes.object,
223 columns: PropTypes.array,
224 onRowClick: PropTypes.func,
225 onRowMouseEnter: PropTypes.func,
226 onRowMouseLeave: PropTypes.func,
227 onBodyMouseOver: PropTypes.func,
228 onBodyMouseOut: PropTypes.func,
229 locale: PropTypes.object,
230 crossline: PropTypes.bool,
231 tableWidth: PropTypes.number
232}, _class.defaultProps = {
233 loading: false,
234 prefix: 'next-',
235 components: {},
236 getCellProps: noop,
237 cellRef: noop,
238 primaryKey: 'id',
239 getRowProps: noop,
240 rowRef: noop,
241 dataSource: [],
242 component: 'tbody',
243 columns: []
244}, _temp2);
245Body.displayName = 'Body';
246export { Body as default };
\No newline at end of file