ze-react-component-library
Version:
ZeroETP React Component Library
314 lines (268 loc) • 8.67 kB
JavaScript
import "antd/es/table/style";
import _Table from "antd/es/table";
import "antd/es/button/style";
import _Button from "antd/es/button";
import "antd/es/tooltip/style";
import _Tooltip from "antd/es/tooltip";
import "antd/es/empty/style";
import _Empty from "antd/es/empty";
function _typeof(obj) { "@babel/helpers - typeof"; 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); }
var __assign = this && this.__assign || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
}
return t;
};
return __assign.apply(this, arguments);
};
import React, { useRef } from "react";
import numeral from "numeral";
import "./ZEGrid.less";
import { DownloadOutlined } from "@ant-design/icons";
import { exportTableToExcel } from "../util";
import formulaProcessor from "./formulaProcessor";
var processItem = function processItem(item) {
if (item && _typeof(item) === "object" && "v" in item) {
if (item.v === "-" || item.v === null) return "-";
if ("formatter" in item) {
return numeral(item.v).format(item.formatter);
}
return item.v;
}
return item;
};
var hideData = function hideData(data, hideRow, hideCol) {
if (!hideCol && !hideRow) return data;
var dataAfterHide = [];
for (var rowIndex = 0; rowIndex < data.length; rowIndex++) {
var row = data[rowIndex];
var rowData = [];
for (var colIndex = 0; colIndex < row.length; colIndex++) {
var item = row[colIndex];
if (!hideCol || !hideCol(colIndex, data)) {
rowData.push(item);
}
}
if (!hideRow || !hideRow(rowIndex, data)) {
dataAfterHide.push(rowData);
}
}
return dataAfterHide;
};
var ZEGrid = function ZEGrid(_a) {
var _data = _a.data,
controls = _a.controls,
autoMergeForIndex = _a.autoMergeForIndex,
fix = _a.fix,
width = _a.width,
height = _a.height,
loading = _a.loading,
xlsx = _a.xlsx,
_b = _a.bordered,
bordered = _b === void 0 ? true : _b,
_c = _a.exportFileName,
exportFileName = _c === void 0 ? "数据导出" : _c,
/* hyperformula */
_d = _a.showFormula,
/* hyperformula */
showFormula = _d === void 0 ? false : _d,
hideCol = _a.hideCol,
hideRow = _a.hideRow,
getCellStyle = _a.getCellStyle,
getColumnProps = _a.getColumnProps,
onCellClick = _a.onCellClick,
onRowClick = _a.onRowClick;
var rootDivRef = useRef(null);
if (!_data || _data.length === 0 || _data[0].length === 0) return /*#__PURE__*/React.createElement(_Empty, null);
var dataResolved = formulaProcessor(_data, showFormula);
var dataAfterHide = hideData(dataResolved, hideRow, hideCol); // excel下载的单元格合并信息
var mergeInfo = [];
var datasource = dataAfterHide.map(function (row, i) {
var rowMap = {};
row.forEach(function (item, j) {
var _a;
var rawItem = (_a = _data === null || _data === void 0 ? void 0 : _data[i]) === null || _a === void 0 ? void 0 : _a[j];
var node = undefined;
if (rawItem && _typeof(rawItem) === "object") {
if (typeof rawItem.node === "string") {
node = /*#__PURE__*/React.createElement("span", {
dangerouslySetInnerHTML: {
__html: rawItem.node
}
});
} else {
node = rawItem.node;
}
}
rowMap["" + j] = {
value: processItem(item),
id: "" + i + j,
node: node,
i: i,
j: j,
rowSpan: 1,
colSpan: 1
};
if (item === undefined) {
rowMap["" + j].colSpan = 0;
}
if (item !== undefined && j < row.length) {
// 不是最后一列,计算一下colSpan
var colSpan_1 = 1;
for (var jj = j + 1; jj < row.length; jj += 1) {
if (row[jj] === undefined) {
colSpan_1 += 1;
} else {
break;
}
}
rowMap["" + j].colSpan = colSpan_1;
}
if (autoMergeForIndex && autoMergeForIndex(i, j)) {
// rowspan, auto merge
if (typeof item === "string" && item.length > 0) {
if (i > 0) {
var prevItem = dataAfterHide[i - 1][j];
if (prevItem === item) {
rowMap["" + j].rowSpan = 0;
}
}
if (rowMap["" + j].rowSpan !== 0) {
for (var index = i + 1; index < dataAfterHide.length; index += 1) {
var element = dataAfterHide[index][j];
if (element === item) {
rowMap["" + j].rowSpan += 1;
} else {
break;
}
}
}
}
}
var _b = rowMap["" + j],
colSpan = _b.colSpan,
rowSpan = _b.rowSpan;
if (colSpan > 1 || rowSpan > 1) {
mergeInfo.push({
s: {
r: i,
c: j
},
e: {
r: i + rowSpan - 1,
c: j + colSpan - 1
}
});
}
});
return rowMap;
});
var columns = dataAfterHide[0].map(function (_item, j) {
var defaultCellStyle = {
whiteSpace: "normal"
};
var wid = undefined;
if (width) {
wid = width(j);
}
var col = __assign({
dataIndex: "" + j,
align: "center",
width: wid,
onCell: function onCell(v, rowIndex) {
var _a, _b, _c;
var cellStyle = (getCellStyle === null || getCellStyle === void 0 ? void 0 : getCellStyle({
rowIndex: rowIndex,
colIndex: j,
value: (_a = v[j]) === null || _a === void 0 ? void 0 : _a.value,
grid: datasource
})) || {};
return {
colSpan: (_b = v[j]) === null || _b === void 0 ? void 0 : _b.colSpan,
rowSpan: (_c = v[j]) === null || _c === void 0 ? void 0 : _c.rowSpan,
style: __assign(__assign({}, defaultCellStyle), cellStyle),
onClick: function onClick(e) {
onCellClick === null || onCellClick === void 0 ? void 0 : onCellClick(e, {
rowIndex: rowIndex,
colIndex: j,
record: v[j]
});
}
};
},
render: function render(v) {
if (!v) return "-";
if (v.node) {
if (typeof v.node === "function") {
return v.node();
}
return v.node;
}
return v.value;
}
}, getColumnProps === null || getColumnProps === void 0 ? void 0 : getColumnProps(j));
if (fix && "col" in fix && j <= fix.col) {
col.fixed = "left";
}
return col;
});
var tableProps = {};
if (fix && "col" in fix) {
tableProps.scroll = {
x: columns.length * 100
};
}
if (height) {
if (!tableProps.scroll) tableProps.scroll = {};
tableProps.scroll.y = height;
}
return /*#__PURE__*/React.createElement("div", {
"data-testid": "ZEGrid",
ref: rootDivRef
}, (xlsx || controls) && /*#__PURE__*/React.createElement("div", {
style: {
display: "flex"
}
}, /*#__PURE__*/React.createElement("div", {
style: {
flexGrow: 1,
marginRight: 12
}
}, controls), xlsx && /*#__PURE__*/React.createElement(_Tooltip, {
title: "\u5BFC\u51FAExcel"
}, /*#__PURE__*/React.createElement(_Button, {
type: "link",
size: "small",
icon: /*#__PURE__*/React.createElement(DownloadOutlined, null),
onClick: function onClick() {
rootDivRef.current && exportTableToExcel(rootDivRef.current, exportFileName, xlsx);
}
}))), /*#__PURE__*/React.createElement(_Table, __assign({}, tableProps, {
size: "small",
loading: loading,
dataSource: datasource,
columns: columns,
showHeader: false,
bordered: bordered,
rowKey: function rowKey(record) {
var _a;
return (_a = record[0]) === null || _a === void 0 ? void 0 : _a.id;
},
pagination: false,
onRow: function onRow(record, index) {
return {
onClick: function onClick(e) {
onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(e, {
record: record,
rowIndex: index
});
}
};
}
})));
};
export default ZEGrid;