@txdfe/at
Version:
一个设计体系组件库
244 lines (242 loc) • 12.5 kB
JavaScript
"use strict";
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(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var noop = function noop() {};
var Row = exports["default"] = /*#__PURE__*/function (_React$Component) {
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 = _callSuper(this, Row, [].concat(args));
_defineProperty(_this, "onClick", function (e) {
var _this$props = _this.props,
record = _this$props.record,
rowIndex = _this$props.rowIndex;
_this.props.onClick(record, rowIndex, e);
});
_defineProperty(_this, "onMouseEnter", function (e) {
var _this$props2 = _this.props,
record = _this$props2.record,
rowIndex = _this$props2.rowIndex;
_this.onRowHover(record, rowIndex, true, e);
});
_defineProperty(_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;
}
_inherits(Row, _React$Component);
return _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) {
/* 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"])(_defineProperty(_defineProperty({
first: lockType !== 'right' && colIndex === 0,
last: lockType !== 'left' && (colIndex === columns.length - 1 || colIndex + attrs.colSpan === columns.length)
}, child.className, child.className), cellClass, cellClass));
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() {
/* 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"])(_defineProperty(_defineProperty({}, "".concat(prefix, "table-row"), true), className, className));
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);
}
}]);
}(_react["default"].Component);
_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'])
});