UNPKG

rc-table

Version:
136 lines (107 loc) 5.28 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _lodash = require('lodash.get'); var _lodash2 = _interopRequireDefault(_lodash); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); } var TableCell = function (_React$Component) { _inherits(TableCell, _React$Component); function TableCell() { var _temp, _this, _ret; _classCallCheck(this, TableCell); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleClick = function (e) { var _this$props = _this.props, record = _this$props.record, onCellClick = _this$props.column.onCellClick; if (onCellClick) { onCellClick(record, e); } }, _temp), _possibleConstructorReturn(_this, _ret); } TableCell.prototype.isInvalidRenderCellText = function isInvalidRenderCellText(text) { return text && !_react2["default"].isValidElement(text) && Object.prototype.toString.call(text) === '[object Object]'; }; TableCell.prototype.render = function render() { var _props = this.props, record = _props.record, indentSize = _props.indentSize, prefixCls = _props.prefixCls, indent = _props.indent, index = _props.index, expandIcon = _props.expandIcon, column = _props.column; var dataIndex = column.dataIndex, render = column.render, _column$className = column.className, className = _column$className === undefined ? '' : _column$className; // We should return undefined if no dataIndex is specified, but in order to // be compatible with object-path's behavior, we return the record object instead. var text = void 0; if (typeof dataIndex === 'number') { text = (0, _lodash2["default"])(record, dataIndex); } else if (!dataIndex || dataIndex.length === 0) { text = record; } else { text = (0, _lodash2["default"])(record, dataIndex); } var tdProps = void 0; var colSpan = void 0; var rowSpan = void 0; if (render) { text = render(text, record, index); if (this.isInvalidRenderCellText(text)) { tdProps = text.props || {}; colSpan = tdProps.colSpan; rowSpan = tdProps.rowSpan; text = text.children; } } // Fix https://github.com/ant-design/ant-design/issues/1202 if (this.isInvalidRenderCellText(text)) { text = null; } var indentText = expandIcon ? _react2["default"].createElement('span', { style: { paddingLeft: indentSize * indent + 'px' }, className: prefixCls + '-indent indent-level-' + indent }) : null; if (rowSpan === 0 || colSpan === 0) { return null; } return _react2["default"].createElement( 'td', _extends({ className: className }, tdProps, { onClick: this.handleClick }), indentText, expandIcon, text ); }; return TableCell; }(_react2["default"].Component); TableCell.propTypes = { record: _propTypes2["default"].object, prefixCls: _propTypes2["default"].string, index: _propTypes2["default"].number, indent: _propTypes2["default"].number, indentSize: _propTypes2["default"].number, column: _propTypes2["default"].object, expandIcon: _propTypes2["default"].node }; exports["default"] = TableCell; module.exports = exports['default'];