react-admin-kit
Version:
A react based UI components for admin system
307 lines (288 loc) • 10 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import dayjs from 'dayjs';
import { FormStore } from "rc-field-form/es/useForm";
/**
* 根据 dataIndex 获取嵌套对象的值
* @param record 数据记录
* @param dataIndex 字段索引,可以是字符串或字符串数组
* @returns 对应的值
*/
export function _getValueByDataIndex(record, dataIndex) {
if (typeof dataIndex === 'string') {
return record[dataIndex];
}
if (Array.isArray(dataIndex)) {
return dataIndex.reduce(function (obj, key) {
return obj != null ? obj[key] : undefined;
}, record);
}
return undefined;
}
export function getFieldVisibility(field) {
var defaultHideInSearch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var type = field.type,
search = field.search,
hideInSearch = field.hideInSearch,
hideInTable = field.hideInTable,
hideInForm = field.hideInForm;
// 默认值:如果没有指定,默认出现在所有区域
var defaultVisibility = {
search: defaultHideInSearch === false,
table: true,
form: true
};
// 1. 首先处理 type 属性的优先级
if (type) {
switch (type) {
case 'search':
return {
search: true,
table: false,
form: false
};
case 'table':
return {
search: false,
table: true,
form: false
};
case 'form':
return {
search: false,
table: false,
form: true
};
default:
// 未知的 type,继续后续判断
break;
}
}
// 2. 如果指定了 type,但不在上述情况中,继续后续逻辑
var visibility = _objectSpread({}, defaultVisibility);
// 3. 处理 search 属性(过时的 API,为了兼容性保留)
if (typeof search === 'boolean') {
visibility.search = search;
}
// 4. 处理 hideInXxx 属性
if (hideInSearch === true) {
visibility.search = false;
} else if (hideInSearch === false) {
visibility.search = true;
}
if (hideInTable) {
visibility.table = false;
}
if (hideInForm) {
visibility.form = false;
}
return visibility;
}
// 只挑选,不对 column 加东西。
export function getAreaFields(fields, area, options) {
var _ref = options || {},
_ref$defaultHideInSea = _ref.defaultHideInSearch,
defaultHideInSearch = _ref$defaultHideInSea === void 0 ? false : _ref$defaultHideInSea;
var result = {
search: [],
table: [],
export: [],
form: []
};
fields.forEach(function (field) {
var visibility = getFieldVisibility(field, defaultHideInSearch);
var valueType = typeof field.valueType === 'string' ? field.valueType : '';
var tableOnlyValueType = ['index', 'option'].includes(valueType);
var formOnlyValueType = valueType === 'dependency';
// 创建字段副本,避免修改原始对象
var fieldCopy = _objectSpread({}, field);
// search区域
if (visibility.search && !tableOnlyValueType) {
result.search.push(_objectSpread(_objectSpread({}, fieldCopy), {}, {
hideInSearch: false,
hideInTable: true,
hideInForm: true,
search: true
}));
}
// table区域
if (visibility.table && !formOnlyValueType) {
result.table.push(_objectSpread(_objectSpread({}, fieldCopy), {}, {
hideInTable: false,
hideInSearch: true,
hideInForm: true
}));
}
// export区域
if (visibility.table && !tableOnlyValueType && !formOnlyValueType) {
result.export.push(fieldCopy);
}
// form区域
if (visibility.form && !tableOnlyValueType) {
result.form.push(_objectSpread(_objectSpread({}, fieldCopy), {}, {
hideInForm: false
}));
}
});
return result[area] || [];
}
export function _formatDateTypeData(text, format) {
if (!text) return '';
var parsed = dayjs(text);
if (parsed.isValid()) {
return parsed.format(format || 'YYYY-MM-DD');
} else {
if (typeof text === 'number' || typeof text === 'string') {
return text;
}
return 'Invalid Date';
}
}
export function _getTextByOptions(text, _col) {
var findOption = function findOption(options, text) {
return options.find(function (option) {
if (option.value === text) return true;
var optionValue = typeof option.value === 'number' ? option.value.toString() : option.value;
var textValue = typeof text === 'number' ? text.toString() : text;
return optionValue === textValue;
});
};
var col = _col || {};
if (col.valueEnum) {
var _col$valueEnum$text;
return (_col$valueEnum$text = col.valueEnum[text]) === null || _col$valueEnum$text === void 0 ? void 0 : _col$valueEnum$text.text;
}
if (col.fieldProps) {
if (typeof col.fieldProps === 'function') {
var _col$fieldProps, _findOption;
var options =
// @ts-ignore
((_col$fieldProps = col.fieldProps(new FormStore(function () {
return '';
}), {
current: {}
}, col)) === null || _col$fieldProps === void 0 ? void 0 : _col$fieldProps.options) || [];
return (_findOption = findOption(options, text)) === null || _findOption === void 0 ? void 0 : _findOption.label;
} else {
var _col$fieldProps2, _findOption2;
// @ts-ignore
var _options = ((_col$fieldProps2 = col.fieldProps) === null || _col$fieldProps2 === void 0 ? void 0 : _col$fieldProps2.options) || [];
return (_findOption2 = findOption(_options, text)) === null || _findOption2 === void 0 ? void 0 : _findOption2.label;
}
}
}
/**
* 获取导出的值
*/
export function _getExportValue(record, col) {
var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var text = typeof col.dataIndex === 'string' || Array.isArray(col.dataIndex) ? _getValueByDataIndex(record, col.dataIndex) : ''; // dataIndex 如果是 react node 就不导出
if (col.renderExport) {
return col.renderExport(text, record);
}
if (col.render) {
// @ts-ignore
return col.render(text, record, index, undefined, undefined);
}
if (col.renderText) {
// @ts-ignore
return col.renderText(text, record, index);
}
if (['select', 'radio', 'radioButton', 'checkbox'].includes(typeof col.valueType === 'string' ? col.valueType || '' : '')) {
return _getTextByOptions(text, col);
}
if (col.valueType === 'date') {
return _formatDateTypeData(text, 'YYYY-MM-DD');
}
if (col.valueType === 'dateTime') {
return _formatDateTypeData(text, 'YYYY-MM-DD HH:mm:ss');
}
if (col.valueType === 'dateRange') {
if (text) {
return text.map(function (itemDate) {
return _formatDateTypeData(itemDate, 'YYYY-MM-DD');
}).join(' - ');
}
}
if (col.valueType === 'dateTimeRange') {
if (text) {
return text.map(function (itemDate) {
return _formatDateTypeData(itemDate, 'YYYY-MM-DD HH:mm:ss');
}).join(' - ');
}
}
if (col.valueType === 'money') {
if (!text) return '';
return text;
}
if (!!text && _typeof(text) === 'object') {
return text.value;
}
return text;
}
/* istanbul ignore next */
export var exportTable = /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(exportColumns, rows, ExcelJS, options) {
var workbook, worksheet, rowsData, buffer, blob, url, a;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
workbook = new ExcelJS.Workbook();
worksheet = workbook.addWorksheet();
worksheet.properties.defaultColWidth = 30;
// 设置列
worksheet.columns = exportColumns.map(function (col) {
return {
header: typeof col.title === 'string' ? col.title : '',
key: typeof col.dataIndex === 'string' ? col.dataIndex : Date.now().toString(),
width: col.width ? Number(col.width) / 5 : undefined
};
});
// 处理行
rowsData = (rows || []).map(function (record, index) {
return exportColumns.map(function (col) {
return _getExportValue(record, col, index);
});
}); // 添加行
worksheet.addRows(rowsData);
// 处理合并问题
(rows || []).forEach(function (record, recordIndex) {
exportColumns.forEach(function (col, colIdx) {
//@ts-ignore
var _ref3 = col.onCell ? col.onCell(record) || {} : {},
_ref3$rowSpan = _ref3.rowSpan,
rowSpan = _ref3$rowSpan === void 0 ? 0 : _ref3$rowSpan;
if (rowSpan > 1) {
// 向下合并
var rowIndex = recordIndex + 2;
var colIndex = colIdx + 1;
worksheet.mergeCells(rowIndex, colIndex, rowIndex + rowSpan - 1, colIndex);
}
});
});
// 导出文件
_context.next = 9;
return workbook.xlsx.writeBuffer();
case 9:
buffer = _context.sent;
blob = new Blob([buffer], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
url = URL.createObjectURL(blob);
a = document.createElement('a');
a.href = url;
a.download = "".concat(options.filename, ".xlsx");
a.click();
URL.revokeObjectURL(url);
case 17:
case "end":
return _context.stop();
}
}, _callee);
}));
return function exportTable(_x, _x2, _x3, _x4) {
return _ref2.apply(this, arguments);
};
}();