fix(grid): size columns by sampled values and distribute spare width

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-24 12:16:20 +08:00
1 parent e8e2625801
commit 81f963fe60
3 files changed
+97 -13

No files matched your search

@@ -17,6 +17,7 @@ function createResizeState(options: {
compactColumnHeaderActions?: boolean;
indexIndicatorColumnIndexes?: number[] | ReturnType<typeof ref<number[]>>;
headerTextWidth?: number;
viewportWidth?: number | ReturnType<typeof ref<number>>;
displayValue?: (value: string | number | boolean | null, columnIndex: number) => string | number | boolean | null;
}) {
const compact = ref(options.compactColumnHeaderActions ?? true);
@@ -25,6 +26,7 @@ function createResizeState(options: {
const density = ref(options.density ?? "standard");
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 state = useDataGridColumnResize({
columns: computed(() => options.columns),
sourceRows: computed(() => rows.value),
@@ -36,6 +38,7 @@ function createResizeState(options: {
columnStructureSignature: computed(() => createDataGridColumnStructureSignature(options.columns, options.columnTypes)),
measureHeaderText: () => headerTextWidth.value,
headerMeasurementKey,
viewportWidth,
displayValue: options.displayValue,
});
return {
@@ -87,6 +90,52 @@ describe("useDataGridColumnResize", () => {
expect(after.length).toBe(before.length);
});
it("distributes spare viewport width across non-user-sized columns", () => {
const state = createResizeState({
columns: ["id", "name"],
rows: [[1, "a"]],
viewportWidth: 400,
});
state.initColumnWidths();
const intrinsic = state.columnWidths.value.slice();
expect(state.renderedColumnWidths.value.reduce((sum, width) => sum + width, 0) + DATA_GRID_ROW_NUM_WIDTH).toBe(400);
expect(state.renderedColumnWidths.value[0]).toBeGreaterThan(intrinsic[0]);
expect(state.renderedColumnWidths.value[1]).toBeGreaterThan(intrinsic[1]);
state.onResizeStart(0, new MouseEvent("mousedown", { clientX: 100, cancelable: true }));
document.dispatchEvent(new MouseEvent("mouseup", { clientX: 100 }));
expect(state.renderedColumnWidths.value[0]).toBeGreaterThan(intrinsic[0]);
expect(state.renderedColumnWidths.value[1]).toBeGreaterThan(intrinsic[1]);
});
it("tracks the cursor while dragging under spare width and stores the final width without stale rendering", async () => {
const state = createResizeState({
columns: ["id", "name"],
rows: [[1, "a"]],
viewportWidth: 1000,
});
state.initColumnWidths();
const intrinsic = state.columnWidths.value.slice();
const startWidth = state.renderedColumnWidths.value[0] ?? 0;
// The grabbed column initially renders wider than its stored intrinsic width.
expect(startWidth).toBeGreaterThan(intrinsic[0] ?? 0);
state.onResizeStart(0, new MouseEvent("mousedown", { clientX: 100, cancelable: true }));
// Grabbing the handle must not shift the rendered edge.
expect(state.renderedColumnWidths.value[0]).toBe(startWidth);
document.dispatchEvent(new MouseEvent("mousemove", { clientX: 150 }));
await new Promise((resolve) => requestAnimationFrame(resolve));
// Mid-drag the rendered width is exactly startWidth + delta: the resizing
// column is not re-stretched with the remaining surplus.
expect(state.renderedColumnWidths.value[0]).toBe(startWidth + 50);
document.dispatchEvent(new MouseEvent("mouseup", { clientX: 150 }));
// After mouseup the rendered width equals the stored user width (no stale render).
expect(state.columnWidths.value[0]).toBe(startWidth + 50);
expect(state.renderedColumnWidths.value[0]).toBe(state.columnWidths.value[0]);
});
it("keeps auto fitted widths when the grid re-initialises for the same columns", () => {
const longText = "y".repeat(220);
const state = createResizeState({
@@ -112,13 +161,17 @@ describe("useDataGridColumnResize", () => {
expect(state.renderedColumnWidths.value).toEqual([]);
});
it("keeps compact query result columns at content width instead of filling the viewport", () => {
it("keeps compact query result columns at content width when no viewport width is reported", () => {
// viewportWidth unset (0) means no spare width is distributed, so compact
// columns keep their content width; with a real viewport they fill it — see
// "distributes spare viewport width across non-user-sized columns" above.
const state = createResizeState({
columns: ["id", "user_id"],
rows: [
[1, 10],
[2, 20],
],
viewportWidth: 0,
});
state.initColumnWidths();