UNPKG

10 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6
7var _class, _temp2;
8
9import React from 'react';
10import { findDOMNode } from 'react-dom';
11import PropTypes from 'prop-types';
12import classnames from 'classnames';
13import { obj, dom } from '../../util';
14import { fetchDataByPath } from '../util';
15
16var noop = function noop() {};
17
18var Row = (_temp2 = _class = function (_React$Component) {
19 _inherits(Row, _React$Component);
20
21 function Row() {
22 var _temp, _this, _ret;
23
24 _classCallCheck(this, Row);
25
26 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
27 args[_key] = arguments[_key];
28 }
29
30 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.onClick = function (e) {
31 var _this$props = _this.props,
32 record = _this$props.record,
33 rowIndex = _this$props.rowIndex;
34
35 _this.props.onClick(record, rowIndex, e);
36 }, _this.onMouseEnter = function (e) {
37 var _this$props2 = _this.props,
38 record = _this$props2.record,
39 rowIndex = _this$props2.rowIndex,
40 __rowIndex = _this$props2.__rowIndex;
41
42 var row = __rowIndex || rowIndex;
43 _this.onRowHover(record, row, true, e);
44 }, _this.onMouseLeave = function (e) {
45 var _this$props3 = _this.props,
46 record = _this$props3.record,
47 rowIndex = _this$props3.rowIndex,
48 __rowIndex = _this$props3.__rowIndex;
49
50 var row = __rowIndex || rowIndex;
51 _this.onRowHover(record, row, false, e);
52 }, _temp), _possibleConstructorReturn(_this, _ret);
53 }
54
55 Row.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps) {
56 if (nextProps.pure) {
57 var isEqual = obj.shallowEqual(this.props, nextProps);
58 return !isEqual;
59 }
60
61 return true;
62 };
63
64 Row.prototype.onRowHover = function onRowHover(record, index, isEnter, e) {
65 var _props = this.props,
66 onMouseEnter = _props.onMouseEnter,
67 onMouseLeave = _props.onMouseLeave,
68 currentRow = findDOMNode(this);
69
70 if (isEnter) {
71 onMouseEnter(record, index, e);
72 currentRow && dom.addClass(currentRow, 'hovered');
73 } else {
74 onMouseLeave(record, index, e);
75 currentRow && dom.removeClass(currentRow, 'hovered');
76 }
77 };
78
79 Row.prototype.renderCells = function renderCells(record, rowIndex) {
80 var _this2 = this;
81
82 var _props2 = this.props,
83 Cell = _props2.Cell,
84 columns = _props2.columns,
85 getCellProps = _props2.getCellProps,
86 cellRef = _props2.cellRef,
87 prefix = _props2.prefix,
88 primaryKey = _props2.primaryKey,
89 __rowIndex = _props2.__rowIndex,
90 pure = _props2.pure,
91 locale = _props2.locale,
92 rtl = _props2.rtl;
93
94 // use params first, it's for list
95
96 rowIndex = rowIndex !== undefined ? rowIndex : this.props.rowIndex;
97
98 var lockType = this.context.lockType;
99
100 return columns.map(function (child, index) {
101 var _classnames;
102
103 /* eslint-disable no-unused-vars, prefer-const */
104 var dataIndex = child.dataIndex,
105 align = child.align,
106 alignHeader = child.alignHeader,
107 width = child.width,
108 colSpan = child.colSpan,
109 style = child.style,
110 cellStyle = child.cellStyle,
111 __colIndex = child.__colIndex,
112 others = _objectWithoutProperties(child, ['dataIndex', 'align', 'alignHeader', 'width', 'colSpan', 'style', 'cellStyle', '__colIndex']);
113
114 var colIndex = '__colIndex' in child ? __colIndex : index;
115 // colSpan should show in body td by the way of <Table.Column colSpan={2} />
116 // tbody's cell merge should only by the way of <Table cellProps={} />
117
118 var value = fetchDataByPath(record, dataIndex);
119 var attrs = getCellProps(rowIndex, colIndex, dataIndex, record) || {};
120
121 if (_this2.context.notRenderCellIndex) {
122 var matchCellIndex = _this2.context.notRenderCellIndex.map(function (cellIndex) {
123 return cellIndex.toString();
124 }).indexOf([rowIndex, colIndex].toString());
125 if (matchCellIndex > -1) {
126 _this2.context.notRenderCellIndex.splice(matchCellIndex, 1);
127 return null;
128 }
129 }
130 if (attrs.colSpan && attrs.colSpan > 1 || attrs.rowSpan && attrs.rowSpan > 1) {
131 _this2._getNotRenderCellIndex(colIndex, rowIndex, attrs.colSpan || 1, attrs.rowSpan || 1);
132 }
133
134 var cellClass = attrs.className;
135 var className = classnames((_classnames = {
136 first: lockType !== 'right' && colIndex === 0,
137 last: lockType !== 'left' && (colIndex === columns.length - 1 || colIndex + attrs.colSpan === columns.length) }, _classnames[child.className] = child.className, _classnames[cellClass] = cellClass, _classnames));
138
139 var newStyle = _extends({}, attrs.style, cellStyle);
140
141 return React.createElement(Cell, _extends({
142 key: __rowIndex + '-' + colIndex
143 }, others, attrs, {
144 style: newStyle,
145 'data-next-table-col': colIndex,
146 'data-next-table-row': rowIndex,
147 ref: function ref(cell) {
148 return cellRef(__rowIndex, colIndex, cell);
149 },
150 prefix: prefix,
151 pure: pure,
152 primaryKey: primaryKey,
153 record: record,
154 className: className,
155 value: value,
156 colIndex: colIndex,
157 rowIndex: rowIndex,
158 align: align,
159 locale: locale,
160 rtl: rtl,
161 width: width
162 }));
163 });
164 };
165
166 Row.prototype._getNotRenderCellIndex = function _getNotRenderCellIndex(colIndex, rowIndex, colSpan, rowSpan) {
167 var maxColIndex = colSpan;
168 var maxRowIndex = rowSpan;
169 var notRenderCellIndex = [];
170 for (var i = 0; i < maxColIndex; i++) {
171 for (var j = 0; j < maxRowIndex; j++) {
172 notRenderCellIndex.push([rowIndex + j, colIndex + i]);
173 }
174 }
175 [].push.apply(this.context.notRenderCellIndex, notRenderCellIndex);
176 };
177
178 Row.prototype.render = function render() {
179 var _classnames2;
180
181 /* eslint-disable no-unused-vars*/
182 var _props3 = this.props,
183 prefix = _props3.prefix,
184 className = _props3.className,
185 onClick = _props3.onClick,
186 onMouseEnter = _props3.onMouseEnter,
187 onMouseLeave = _props3.onMouseLeave,
188 columns = _props3.columns,
189 Cell = _props3.Cell,
190 getCellProps = _props3.getCellProps,
191 rowIndex = _props3.rowIndex,
192 record = _props3.record,
193 __rowIndex = _props3.__rowIndex,
194 children = _props3.children,
195 primaryKey = _props3.primaryKey,
196 cellRef = _props3.cellRef,
197 colGroup = _props3.colGroup,
198 pure = _props3.pure,
199 locale = _props3.locale,
200 expandedIndexSimulate = _props3.expandedIndexSimulate,
201 tableEl = _props3.tableEl,
202 rtl = _props3.rtl,
203 wrapper = _props3.wrapper,
204 others = _objectWithoutProperties(_props3, ['prefix', 'className', 'onClick', 'onMouseEnter', 'onMouseLeave', 'columns', 'Cell', 'getCellProps', 'rowIndex', 'record', '__rowIndex', 'children', 'primaryKey', 'cellRef', 'colGroup', 'pure', 'locale', 'expandedIndexSimulate', 'tableEl', 'rtl', 'wrapper']);
205
206 var cls = classnames((_classnames2 = {}, _classnames2[prefix + 'table-row'] = true, _classnames2[className] = className, _classnames2));
207
208 var tr = React.createElement(
209 'tr',
210 _extends({
211 className: cls,
212 role: 'row'
213 }, others, {
214 onClick: this.onClick,
215 onMouseEnter: this.onMouseEnter,
216 onMouseLeave: this.onMouseLeave
217 }),
218 this.renderCells(record),
219 children
220 );
221
222 return wrapper(tr);
223 };
224
225 return Row;
226}(React.Component), _class.propTypes = {
227 prefix: PropTypes.string,
228 pure: PropTypes.bool,
229 primaryKey: PropTypes.oneOfType([PropTypes.symbol, PropTypes.string]),
230 className: PropTypes.string,
231 columns: PropTypes.array,
232 record: PropTypes.any,
233 Cell: PropTypes.func,
234 rowIndex: PropTypes.number,
235 getCellProps: PropTypes.func,
236 onClick: PropTypes.func,
237 onMouseEnter: PropTypes.func,
238 onMouseLeave: PropTypes.func,
239 children: PropTypes.any,
240 cellRef: PropTypes.func,
241 colGroup: PropTypes.object,
242 locale: PropTypes.object,
243 wrapper: PropTypes.func
244}, _class.defaultProps = {
245 prefix: 'next-',
246 primaryKey: 'id',
247 columns: [],
248 record: {},
249 getCellProps: noop,
250 onClick: noop,
251 onMouseEnter: noop,
252 onMouseLeave: noop,
253 cellRef: noop,
254 colGroup: {},
255 wrapper: function wrapper(row) {
256 return row;
257 }
258}, _class.contextTypes = {
259 notRenderCellIndex: PropTypes.array,
260 lockType: PropTypes.oneOf(['left', 'right'])
261}, _temp2);
262Row.displayName = 'Row';
263export { Row as default };
\No newline at end of file