feat(grid): add configurable column width layout

Closes #10366
This commit is contained in:
t8y2 authored and t8y2 committed 2026-09-27 12:04:10 +08:00
1 parent 72f8dfeb7c
commit 4a7b8f0f27
21 files changed
+165 -2

No files matched your search

@@ -1,11 +1,12 @@
// @vitest-environment happy-dom
import { computed, isRef, nextTick, ref } from "vue";
import { computed, isRef, nextTick, ref, type Ref } from "vue";
import { beforeEach, describe, expect, it } from "vitest";
import { DATA_GRID_COL_AUTO_FIT_MAX_WIDTH, DATA_GRID_COL_MIN_WIDTH, sampleDataGridColumnValues } from "@/lib/dataGrid/dataGridColumnWidth";
import { clearDataGridColumnWidthStates, createDataGridColumnMeasurementSignature, createDataGridColumnStructureSignature, DATA_GRID_COLUMN_WIDTH_STATE_LIMIT, dataGridColumnWidthStateCount, loadDataGridColumnWidthState, saveDataGridColumnWidthState } from "@/lib/dataGrid/dataGridColumnWidthState";
import { DATA_GRID_ROW_NUM_WIDTH, dataGridRowNumberColumnWidth, resizeDataGridColumnWidth, useDataGridColumnResize } from "@/composables/useDataGridColumnResize";
import { resultGridColumnWidthCacheKey } from "@/lib/tabs/tabPresentation";
import type { DataGridColumnWidthMode } from "@/stores/settingsStore";
function createResizeState(options: {
columns: string[];
@@ -18,6 +19,7 @@ function createResizeState(options: {
indexIndicatorColumnIndexes?: number[] | ReturnType<typeof ref<number[]>>;
headerTextWidth?: number;
viewportWidth?: number | ReturnType<typeof ref<number>>;
widthMode?: DataGridColumnWidthMode | Ref<DataGridColumnWidthMode>;
displayValue?: (value: string | number | boolean | null, columnIndex: number) => string | number | boolean | null;
}) {
const compact = ref(options.compactColumnHeaderActions ?? true);
@@ -27,11 +29,13 @@ function createResizeState(options: {
const rows = isRef(options.rows) ? options.rows : ref(options.rows);
const indexIndicatorColumnIndexes = isRef(options.indexIndicatorColumnIndexes) ? options.indexIndicatorColumnIndexes : ref(options.indexIndicatorColumnIndexes ?? []);
const viewportWidth = isRef(options.viewportWidth) ? options.viewportWidth : ref(options.viewportWidth ?? 0);
const widthMode = isRef(options.widthMode) ? options.widthMode : ref(options.widthMode ?? "fill");
const state = useDataGridColumnResize({
columns: computed(() => options.columns),
sourceRows: computed(() => rows.value),
columnIndexes: computed(() => options.columnIndexes ?? options.columns.map((_, index) => index)),
density,
widthMode,
compactColumnHeaderActions: computed(() => compact.value),
columnIndexIndicators: computed(() => options.columns.map((_, index) => indexIndicatorColumnIndexes.value.includes(index))),
cacheKey: computed(() => options.cacheKey),
@@ -50,6 +54,9 @@ function createResizeState(options: {
setDensity(value: "compact" | "standard" | "comfortable") {
density.value = value;
},
setWidthMode(value: DataGridColumnWidthMode) {
widthMode.value = value;
},
setHeaderTextWidth(width: number) {
headerTextWidth.value = width;
headerMeasurementKey.value += 1;
@@ -108,6 +115,39 @@ describe("useDataGridColumnResize", () => {
expect(state.renderedColumnWidths.value[1]).toBeGreaterThan(intrinsic[1]);
});
it("keeps measured content widths without distributing spare viewport space", () => {
const state = createResizeState({
columns: ["id", "name"],
rows: [[1, "a"]],
viewportWidth: 400,
widthMode: "content",
});
state.initColumnWidths();
expect(state.renderedColumnWidths.value).toEqual(state.columnWidths.value);
expect(state.totalWidth.value).toBeLessThan(400);
});
it("switches the open grid layout immediately without changing a manual width", () => {
const state = createResizeState({
columns: ["id", "name"],
rows: [[1, "a"]],
viewportWidth: 600,
});
state.initColumnWidths();
state.onResizeStart(0, new MouseEvent("mousedown", { clientX: 100, cancelable: true }));
document.dispatchEvent(new MouseEvent("mouseup", { clientX: 140 }));
const manualWidth = state.columnWidths.value[0];
state.setWidthMode("content");
expect(state.renderedColumnWidths.value).toEqual(state.columnWidths.value);
expect(state.renderedColumnWidths.value[0]).toBe(manualWidth);
state.setWidthMode("fill");
expect(state.renderedColumnWidths.value[0]).toBe(manualWidth);
expect(state.totalWidth.value).toBe(600);
});
it("tracks the cursor while dragging under spare width and stores the final width without stale rendering", async () => {
const state = createResizeState({
columns: ["id", "name"],
@@ -1,7 +1,7 @@
import { ref, shallowRef, computed, watch, type ComputedRef, type Ref } from "vue";
import { calculateDataGridColumnWidth, DATA_GRID_AUTO_FIT_VALUE_TEXT_LIMIT, DATA_GRID_COL_AUTO_FIT_MAX_WIDTH, DATA_GRID_COL_MIN_WIDTH, COLUMN_WIDTH_DENSITY_PRESETS, sampleDataGridColumnValues } from "@/lib/dataGrid/dataGridColumnWidth";
import { createDataGridColumnMeasurementSignature, loadDataGridColumnWidthState, removeDataGridColumnWidthState, saveDataGridColumnWidthState } from "@/lib/dataGrid/dataGridColumnWidthState";
import type { ColumnWidthDensity } from "@/stores/settingsStore";
import type { ColumnWidthDensity, DataGridColumnWidthMode } from "@/stores/settingsStore";
type CellValue = string | number | boolean | null;
@@ -35,6 +35,7 @@ export interface UseDataGridColumnResizeOptions {
sourceRows: ComputedRef<CellValue[][]>;
columnIndexes: ComputedRef<number[]>;
density: Ref<ColumnWidthDensity>;
widthMode: Ref<DataGridColumnWidthMode>;
compactColumnHeaderActions: ComputedRef<boolean>;
columnIndexIndicators?: ComputedRef<readonly boolean[]>;
cacheKey?: ComputedRef<string | undefined>;
@@ -250,6 +251,7 @@ export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions)
const renderedColumnWidths = computed(() => {
const widths = columnWidths.value.slice();
if (options.widthMode.value === "content") return widths;
const viewportWidth = options.viewportWidth?.value ?? 0;
const surplus = viewportWidth - resolvedRowNumberWidth.value - widths.reduce((sum, width) => sum + width, 0);
if (surplus <= 0) return widths;