zarm-web
Version:
基于 React 的桌面端UI库
523 lines (434 loc) • 20.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames3 = _interopRequireDefault(require("classnames"));
var _loading = _interopRequireDefault(require("../loading"));
var _checkbox = _interopRequireDefault(require("../checkbox"));
var _dom = _interopRequireDefault(require("../utils/dom"));
var _rAF = _interopRequireDefault(require("../utils/rAF"));
var _FixedColumn = _interopRequireDefault(require("./FixedColumn"));
var _Cell = _interopRequireDefault(require("./Cell"));
var _Head = _interopRequireDefault(require("./Head"));
var _Body = _interopRequireDefault(require("./Body"));
var _helpers = require("./helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
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; }
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); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 } }); 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); }
var Table =
/*#__PURE__*/
function (_Component) {
_inherits(Table, _Component);
function Table(props) {
var _this;
_classCallCheck(this, Table);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Table).call(this, props));
_this.scrollTable = void 0;
_this.head = void 0;
_this.body = void 0;
_this.fixedLeftColumn = void 0;
_this.fixedRightColumn = void 0;
_this.onSort = function (column) {
var stateSort = _this.state.stateSort;
var dataSource = _this.props.dataSource;
var sort = !stateSort[column.dataIndex];
sort ? dataSource.sort(column.sorter) : dataSource.reverse();
_this.setState({
sort: _defineProperty({}, "".concat(column.dataIndex), sort)
});
};
_this.onEnterRow = function (index) {
var fixedleftTbody = _this.fixedLeftColumn.fixedleftTbody;
var fixedrightTbody = _this.fixedRightColumn.fixedrightTbody;
var scrollbody = _this.body.scrollbody;
if (!fixedleftTbody && !fixedrightTbody) {
return;
}
[fixedleftTbody, fixedrightTbody, scrollbody].forEach(function (tbody) {
if (tbody) {
(0, _helpers.toggleHoverStatus)(tbody.querySelectorAll('tr'), index);
}
});
};
_this.onLeaveRow = function () {
_this.onEnterRow(-1);
};
_this.onScrollTable = function (e) {
var fixedleftCol = _this.fixedLeftColumn.fixedleftCol;
var fixedrightCol = _this.fixedRightColumn.fixedrightCol;
var getStyleComputedProperty = _dom.default.getStyleComputedProperty;
var scrollLeft = e.target.scrollLeft;
var containerWidth = parseInt(getStyleComputedProperty(e.target, 'width'), 10);
var scrollTableWidth = parseInt(getStyleComputedProperty(_this.scrollTable, 'width'), 10);
if (containerWidth < scrollTableWidth) {
if (fixedleftCol) {
if (scrollLeft > 0) {
fixedleftCol.classList.add('shadow');
} else {
fixedleftCol.classList.remove('shadow');
}
}
if (fixedrightCol) {
if (scrollLeft + containerWidth < scrollTableWidth) {
fixedrightCol.classList.add('shadow');
} else {
fixedrightCol.classList.remove('shadow');
}
}
}
};
_this.getRowKey = function (row, index) {
var rowKey = _this.props.rowKey;
var key;
if (rowKey) {
key = typeof rowKey === 'function' ? rowKey(row, index) : row[rowKey];
} else {
key = index;
}
return key;
};
_this.toggleExpandRow = function (key, row) {
var onExpand = _this.props.onExpand;
var expandedRowKeys = _this.state.expandedRowKeys;
var index = expandedRowKeys.indexOf(key);
var keys = _toConsumableArray(expandedRowKeys);
if (index > -1) {
keys.splice(index, 1);
} else {
keys.push(key);
}
if (onExpand) {
onExpand(index < 0, row);
}
_this.setState({
expandedRowKeys: keys
});
};
_this.renderSelectAll = function (rowSelection, dataSource, rowSpan) {
var height = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 50;
return _react.default.createElement("th", {
style: {
minWidth: 50,
height: height,
textAlign: 'center'
},
rowSpan: rowSpan
}, _react.default.createElement(_checkbox.default, {
checked: dataSource.length > 0 && _this.state.selectedRows.length === dataSource.length,
onChange: function onChange(e) {
var selected = e.target.checked;
var selectedRows = selected ? dataSource.map(function (data) {
return data;
}) : [];
_this.setState({
selectedRows: selectedRows
});
if (rowSelection.onSelectAll) {
rowSelection.onSelectAll(selected, selectedRows);
}
}
}));
};
_this.renderSelect = function (rowSelection, row) {
var height = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 40;
return _react.default.createElement("td", {
style: {
width: 50,
height: height,
textAlign: 'center'
}
}, _react.default.createElement(_checkbox.default, {
value: row,
checked: _this.state.selectedRows.indexOf(row) > -1,
onChange: function onChange(e) {
var selected = e.target.checked;
var selectedRows = _this.state.selectedRows;
if (selectedRows.indexOf(row) === -1) {
selectedRows.push(row);
} else {
selectedRows.splice(selectedRows.indexOf(row), 1);
}
_this.setState({
selectedRows: selectedRows
});
if (rowSelection.onSelect) {
rowSelection.onSelect(selected, row, selectedRows);
}
}
}));
};
_this.renderCell = function (column, row, rowIndex, columnIndex) {
var maxCellSize = _this.props.maxCellSize;
return _react.default.createElement(_Cell.default, {
key: column.dataIndex + rowIndex + columnIndex,
maxCellSize: maxCellSize,
row: row,
rowIndex: rowIndex,
column: column,
columnIndex: columnIndex
});
};
var _selectedRows = 'rowSelection' in props ? props.rowSelection.value || props.rowSelection.defaultValue || [] : [];
var _expandedRowKeys = _this.getExpandedRowKeys();
_this.state = {
selectedRows: _selectedRows,
sort: {},
fixedColAttrs: {},
expandedRowKeys: _expandedRowKeys
};
return _this;
}
_createClass(Table, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
_rAF.default.rAF(function () {
return _this2.getFixedColAttrs();
});
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
var _this3 = this;
var dataSource = this.props.dataSource;
if ('rowSelection' in nextProps && 'value' in nextProps.rowSelection) {
this.setState({
selectedRows: nextProps.rowSelection.value
});
}
if (nextProps.dataSource !== dataSource) {
// 数据变更重新计算固定列宽高
_rAF.default.rAF(function () {
return _this3.getFixedColAttrs();
});
}
}
}, {
key: "getExpandedRowKeys",
// 获取初始化展开行
value: function getExpandedRowKeys() {
var _this$props = this.props,
expandedRowKeys = _this$props.expandedRowKeys,
defaultExpandAllRows = _this$props.defaultExpandAllRows,
defaultExpandedRowKeys = _this$props.defaultExpandedRowKeys,
dataSource = _this$props.dataSource;
if (defaultExpandAllRows) {
return dataSource.map(this.getRowKey);
}
if (expandedRowKeys) {
return Array.isArray(expandedRowKeys) ? expandedRowKeys : [expandedRowKeys];
}
if (defaultExpandedRowKeys) {
return Array.isArray(defaultExpandedRowKeys) ? defaultExpandedRowKeys : [defaultExpandedRowKeys];
}
return [];
} // 同步单元格宽度和高度
}, {
key: "getFixedColAttrs",
value: function getFixedColAttrs() {
var _this$props2 = this.props,
columns = _this$props2.columns,
rowSelection = _this$props2.rowSelection;
var getStyleComputedProperty = _dom.default.getStyleComputedProperty;
if (!columns || columns.length < 2) {
return;
}
var firstColumn = columns[0];
var lastColumn = columns[columns.length - 1];
var _this$head = this.head,
thead = _this$head.thead,
leftCell = _this$head.leftCell,
rightCell = _this$head.rightCell;
var row = this.body.row;
if (firstColumn.fixed || lastColumn.fixed || rowSelection && rowSelection.fixed) {
var fixedColThHeight = getStyleComputedProperty(thead, 'height');
var fixedleftColWidth = getStyleComputedProperty(leftCell, 'width');
var fixedrightColWidth = getStyleComputedProperty(rightCell, 'width');
var fixedColTdHeight = '40';
if (row) {
fixedColTdHeight = getStyleComputedProperty(row, 'height');
}
this.setState({
fixedColAttrs: {
fixedColThHeight: fixedColThHeight,
fixedColTdHeight: fixedColTdHeight,
fixedleftColWidth: fixedleftColWidth,
fixedrightColWidth: fixedrightColWidth
}
});
}
} // 处理收起展开行
}, {
key: "renderFixedLeftCol",
// 渲染固定左侧的列
value: function renderFixedLeftCol() {
var _this4 = this;
var _this$props3 = this.props,
columns = _this$props3.columns,
prefixCls = _this$props3.prefixCls,
dataSource = _this$props3.dataSource,
rowSelection = _this$props3.rowSelection,
rowClassName = _this$props3.rowClassName;
var fixedColAttrs = this.state.fixedColAttrs;
return _react.default.createElement(_FixedColumn.default, {
ref: function ref(fixedColumn) {
_this4.fixedLeftColumn = fixedColumn;
},
direction: "left",
rowClassName: rowClassName,
prefixCls: prefixCls,
columns: columns,
dataSource: dataSource,
colAttrs: fixedColAttrs,
renderSelect: this.renderSelect,
renderSelectAll: this.renderSelectAll,
rowSelection: rowSelection,
onEnterRow: this.onEnterRow,
onLeaveRow: this.onLeaveRow
});
} // 渲染固定右侧的列
}, {
key: "renderFixedRightCol",
value: function renderFixedRightCol() {
var _this5 = this;
var _this$props4 = this.props,
columns = _this$props4.columns,
prefixCls = _this$props4.prefixCls,
dataSource = _this$props4.dataSource,
rowClassName = _this$props4.rowClassName;
var fixedColAttrs = this.state.fixedColAttrs;
return _react.default.createElement(_FixedColumn.default, {
ref: function ref(fixedColumn) {
_this5.fixedRightColumn = fixedColumn;
},
direction: "right",
rowClassName: rowClassName,
prefixCls: prefixCls,
columns: columns,
dataSource: dataSource,
colAttrs: fixedColAttrs,
onEnterRow: this.onEnterRow,
onLeaveRow: this.onLeaveRow
});
} // 渲染全选
}, {
key: "renderTable",
value: function renderTable() {
var _classnames,
_this6 = this;
var _this$props5 = this.props,
prefixCls = _this$props5.prefixCls,
columns = _this$props5.columns,
dataSource = _this$props5.dataSource,
rowClick = _this$props5.rowClick,
rowKey = _this$props5.rowKey,
rowSelection = _this$props5.rowSelection,
style = _this$props5.style,
width = _this$props5.width,
expandedRowRender = _this$props5.expandedRowRender,
rowClassName = _this$props5.rowClassName;
var _this$state = this.state,
sort = _this$state.sort,
expandedRowKeys = _this$state.expandedRowKeys;
var _groupColumns = (0, _helpers.groupColumns)(columns),
headRows = _groupColumns.headRows,
dataColumns = _groupColumns.dataColumns;
var cls = (0, _classnames3.default)((_classnames = {}, _defineProperty(_classnames, "".concat(prefixCls, "-scroll"), true), _defineProperty(_classnames, "".concat(prefixCls, "-multi-headrow"), headRows.length > 1), _classnames));
return _react.default.createElement("table", {
className: cls,
style: _objectSpread({}, style, {
width: width
}),
ref: function ref(scrollTable) {
_this6.scrollTable = scrollTable;
}
}, _react.default.createElement(_Head.default, {
ref: function ref(head) {
_this6.head = head;
},
prefixCls: prefixCls,
rows: headRows,
sort: sort,
expandedRowRender: expandedRowRender,
rowSelection: rowSelection,
dataSource: dataSource,
onSort: this.onSort,
renderSelectAll: this.renderSelectAll
}), _react.default.createElement(_Body.default, {
prefixCls: prefixCls,
ref: function ref(body) {
_this6.body = body;
},
rowSelection: rowSelection,
dataSource: dataSource,
dataColumns: dataColumns,
rowKey: rowKey,
expandedRowKeys: expandedRowKeys,
expandedRowRender: expandedRowRender,
toggleExpandRow: this.toggleExpandRow,
onEnterRow: this.onEnterRow,
onLeaveRow: this.onLeaveRow,
rowClassName: rowClassName,
rowClick: rowClick,
renderCell: this.renderCell,
renderSelect: this.renderSelect
}));
}
}, {
key: "render",
value: function render() {
var _classnames2;
var _this$props6 = this.props,
isBordered = _this$props6.isBordered,
isStriped = _this$props6.isStriped,
isRadius = _this$props6.isRadius,
isHover = _this$props6.isHover,
isLoading = _this$props6.isLoading,
size = _this$props6.size,
className = _this$props6.className,
prefixCls = _this$props6.prefixCls;
var cls = (0, _classnames3.default)((_classnames2 = {}, _defineProperty(_classnames2, prefixCls, true), _defineProperty(_classnames2, "".concat(prefixCls, "-bordered"), 'bordered' in this.props || isBordered), _defineProperty(_classnames2, "".concat(prefixCls, "-striped"), 'striped' in this.props || isStriped), _defineProperty(_classnames2, "".concat(prefixCls, "-radius"), 'radius' in this.props || isRadius), _defineProperty(_classnames2, "".concat(prefixCls, "-hover"), 'hover' in this.props || isHover), _defineProperty(_classnames2, "size-".concat(size), !!size), _defineProperty(_classnames2, className, !!className), _classnames2));
return _react.default.createElement("div", {
className: cls
}, _react.default.createElement("div", {
className: "".concat(prefixCls, "-body"),
onScroll: this.onScrollTable
}, _react.default.createElement(_loading.default, {
visible: isLoading
}, this.renderTable()), this.renderFixedLeftCol(), this.renderFixedRightCol()));
}
}]);
return Table;
}(_react.Component);
Table.defaultProps = {
prefixCls: 'ui-table',
size: 'md',
isBordered: false,
isStriped: false,
isRadius: false,
isHover: false,
maxCellSize: 20,
defaultExpandAllRows: false,
columns: [],
dataSource: []
};
var _default = Table;
exports.default = _default;