refactor(grid): 提取 cellValue 格式化到共享模块并增加测试 (#218)

将 CellValue 类型和 displayCellValue(原 formatCell)提取到
src/lib/cellValue.ts,DataGrid.vue 和 useDataGridExport.ts 统一引用,
移除 composable 中冗余的 formatCell 参数传递。
This commit is contained in:
Abeautifulsnow
2026-05-11 18:56:08 +08:00
committed by GitHub
parent e4c1d6639b
commit dddb813fc6
4 changed files with 74 additions and 13 deletions
+4 -7
View File
@@ -10,8 +10,7 @@ import {
type SelectionData,
} from "@/lib/gridSelection";
import { useToast } from "@/composables/useToast";
type CellValue = string | number | boolean | null;
import { displayCellValue, type CellValue } from "@/lib/cellValue";
interface RowItem {
id: number;
@@ -36,7 +35,6 @@ export interface UseDataGridExportOptions {
| Ref<{ rowId: number; rowIndex: number; col: number } | null>
| ComputedRef<{ rowId: number; rowIndex: number; col: number } | null>;
getRowItem: (rowId: number) => RowItem | undefined;
formatCell: (value: CellValue) => string;
quoteIdent: (name: string) => string;
escapeVal: (value: CellValue) => string;
}
@@ -54,7 +52,6 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
selectedCells,
contextCell,
getRowItem,
formatCell,
quoteIdent,
escapeVal,
} = options;
@@ -90,7 +87,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
if (!contextCell.value || contextCell.value.col < 0) return;
const item = getRowItem(contextCell.value.rowId);
const val = item?.data[contextCell.value.col] ?? null;
copyText(formatCell(val));
copyText(displayCellValue(val));
}
function copyRow() {
@@ -118,7 +115,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
function copyAll() {
const header = columns.value.join("\t");
const body = displayItems.value.map((item) => item.data.map((c) => formatCell(c)).join("\t")).join("\n");
const body = displayItems.value.map((item) => item.data.map((c) => displayCellValue(c)).join("\t")).join("\n");
copyText(`${header}\n${body}`);
}
@@ -184,7 +181,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
// --- Export functions ---
async function exportCsv() {
try {
const rows = displayItems.value.map((item) => item.data.map((c) => formatCell(c)));
const rows = displayItems.value.map((item) => item.data.map((c) => displayCellValue(c)));
if (await saveFileContent(formatCsv(columns.value, rows), "export.csv", "CSV", "csv")) {
toast(t("grid.exported"));
}