UNPKG

@txdfe/at

Version:

一个设计体系组件库

306 lines (244 loc) 13.6 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _reactDom = require("react-dom"); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames3 = _interopRequireDefault(require("classnames")); var _util = require("../../util"); var _util2 = require("../util"); var _excluded = ["dataIndex", "align", "alignHeader", "width"], _excluded2 = ["prefix", "className", "onClick", "onMouseEnter", "onMouseLeave", "columns", "Cell", "getCellProps", "rowIndex", "record", "children", "primaryKey", "cellRef", "colGroup", "pure", "locale", "expandedIndexSimulate", "rtl", "wrapper"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _extends() { _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; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var noop = function noop() {}; var Row = /*#__PURE__*/function (_React$Component) { _inherits(Row, _React$Component); var _super = _createSuper(Row); function Row() { var _this; _classCallCheck(this, Row); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "onClick", function (e) { var _this$props = _this.props, record = _this$props.record, rowIndex = _this$props.rowIndex; _this.props.onClick(record, rowIndex, e); }); _defineProperty(_assertThisInitialized(_this), "onMouseEnter", function (e) { var _this$props2 = _this.props, record = _this$props2.record, rowIndex = _this$props2.rowIndex; _this.onRowHover(record, rowIndex, true, e); }); _defineProperty(_assertThisInitialized(_this), "onMouseLeave", function (e) { var _this$props3 = _this.props, record = _this$props3.record, rowIndex = _this$props3.rowIndex; _this.onRowHover(record, rowIndex, false, e); }); return _this; } _createClass(Row, [{ key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps) { if (nextProps.pure) { var isEqual = _util.obj.shallowEqual(this.props, nextProps); return !isEqual; } return true; } }, { key: "onRowHover", value: function onRowHover(record, index, isEnter, e) { var _this$props4 = this.props, onMouseEnter = _this$props4.onMouseEnter, onMouseLeave = _this$props4.onMouseLeave, currentRow = (0, _reactDom.findDOMNode)(this); if (isEnter) { onMouseEnter(record, index, e); currentRow && _util.dom.addClass(currentRow, 'hovered'); } else { onMouseLeave(record, index, e); currentRow && _util.dom.removeClass(currentRow, 'hovered'); } } }, { key: "renderCells", value: function renderCells(record, rowIndex) { var _this2 = this; var _this$props5 = this.props, Cell = _this$props5.Cell, columns = _this$props5.columns, getCellProps = _this$props5.getCellProps, cellRef = _this$props5.cellRef, prefix = _this$props5.prefix, primaryKey = _this$props5.primaryKey, pure = _this$props5.pure, locale = _this$props5.locale, table = _this$props5.table, rtl = _this$props5.rtl; // use params first, it's for list rowIndex = rowIndex !== undefined ? rowIndex : this.props.rowIndex; var lockType = this.context.lockType; return columns.map(function (child, colIndex) { var _classnames; /* eslint-disable no-unused-vars, prefer-const */ var dataIndex = child.dataIndex, align = child.align, alignHeader = child.alignHeader, width = child.width, others = _objectWithoutProperties(child, _excluded); var value = (0, _util2.fetchDataByPath)(record, dataIndex); var attrs = getCellProps(rowIndex, colIndex, dataIndex, record) || {}; if (_this2.context.notRenderCellIndex) { var matchCellIndex = _this2.context.notRenderCellIndex.map(function (cellIndex) { return cellIndex.toString(); }).indexOf([rowIndex, colIndex].toString()); if (matchCellIndex > -1) { _this2.context.notRenderCellIndex.splice(matchCellIndex, 1); return null; } } if (attrs.colSpan && attrs.colSpan > 1 || attrs.rowSpan && attrs.rowSpan > 1) { _this2._getNotRenderCellIndex(colIndex, rowIndex, attrs.colSpan || 1, attrs.rowSpan || 1); } var cellClass = attrs.className; var className = (0, _classnames3["default"])((_classnames = { first: lockType !== 'right' && colIndex === 0, last: lockType !== 'left' && (colIndex === columns.length - 1 || colIndex + attrs.colSpan === columns.length) }, _defineProperty(_classnames, child.className, child.className), _defineProperty(_classnames, cellClass, cellClass), _classnames)); return /*#__PURE__*/_react["default"].createElement(Cell, _extends({ key: colIndex }, others, attrs, { ref: function ref(cell) { return cellRef(rowIndex, colIndex, cell); }, prefix: prefix, pure: pure, primaryKey: primaryKey, record: record, className: className, value: value, colIndex: colIndex, rowIndex: rowIndex, align: align, locale: locale, rtl: rtl, width: width, table: table })); }); } }, { key: "_getNotRenderCellIndex", value: function _getNotRenderCellIndex(colIndex, rowIndex, colSpan, rowSpan) { var maxColIndex = colSpan; var maxRowIndex = rowSpan; var notRenderCellIndex = []; for (var i = 0; i < maxColIndex; i++) { for (var j = 0; j < maxRowIndex; j++) { notRenderCellIndex.push([rowIndex + j, colIndex + i]); } } [].push.apply(this.context.notRenderCellIndex, notRenderCellIndex); } }, { key: "render", value: function render() { var _classnames2; /* eslint-disable no-unused-vars*/ var _this$props6 = this.props, prefix = _this$props6.prefix, className = _this$props6.className, onClick = _this$props6.onClick, onMouseEnter = _this$props6.onMouseEnter, onMouseLeave = _this$props6.onMouseLeave, columns = _this$props6.columns, Cell = _this$props6.Cell, getCellProps = _this$props6.getCellProps, rowIndex = _this$props6.rowIndex, record = _this$props6.record, children = _this$props6.children, primaryKey = _this$props6.primaryKey, cellRef = _this$props6.cellRef, colGroup = _this$props6.colGroup, pure = _this$props6.pure, locale = _this$props6.locale, expandedIndexSimulate = _this$props6.expandedIndexSimulate, rtl = _this$props6.rtl, wrapper = _this$props6.wrapper, others = _objectWithoutProperties(_this$props6, _excluded2); var cls = (0, _classnames3["default"])((_classnames2 = {}, _defineProperty(_classnames2, "".concat(prefix, "table-row"), true), _defineProperty(_classnames2, className, className), _classnames2)); var tr = /*#__PURE__*/_react["default"].createElement("tr", _extends({ className: cls, role: "row" }, others, { onClick: this.onClick, onMouseEnter: this.onMouseEnter, onMouseLeave: this.onMouseLeave }), this.renderCells(record), children); return wrapper(tr); } }]); return Row; }(_react["default"].Component); exports["default"] = Row; _defineProperty(Row, "propTypes", { prefix: _propTypes["default"].string, pure: _propTypes["default"].bool, primaryKey: _propTypes["default"].string, className: _propTypes["default"].string, columns: _propTypes["default"].array, record: _propTypes["default"].any, Cell: _propTypes["default"].func, rowIndex: _propTypes["default"].number, getCellProps: _propTypes["default"].func, onClick: _propTypes["default"].func, onMouseEnter: _propTypes["default"].func, onMouseLeave: _propTypes["default"].func, children: _propTypes["default"].any, cellRef: _propTypes["default"].func, colGroup: _propTypes["default"].object, locale: _propTypes["default"].object, wrapper: _propTypes["default"].func, table: _propTypes["default"].object }); _defineProperty(Row, "defaultProps", { prefix: 'next-', primaryKey: 'id', columns: [], record: {}, getCellProps: noop, onClick: noop, onMouseEnter: noop, onMouseLeave: noop, cellRef: noop, colGroup: {}, wrapper: function wrapper(row) { return row; }, table: null }); _defineProperty(Row, "contextTypes", { notRenderCellIndex: _propTypes["default"].array, lockType: _propTypes["default"].oneOf(['left', 'right']) });