feat(grid): add autofit-all-columns action to the filter panel

This commit is contained in:
zipg authored and GitHub committed 2026-09-22 14:17:18 +08:00
1 parent 4f0f69131b
commit 98101d20af
19 files changed
+148 -5

No files matched your search

@@ -62,6 +62,56 @@ describe("useDataGridColumnResize", () => {
clearDataGridColumnWidthStates();
});
it("fits every visible column to its content when auto fitting the whole grid", () => {
const longText = "x".repeat(300);
const state = createResizeState({
columns: ["id", "note"],
rows: [
[1, longText],
[2, longText],
],
});
state.initColumnWidths();
const before = state.renderedColumnWidths.value.slice();
expect(Math.max(...before)).toBeLessThan(DATA_GRID_COL_AUTO_FIT_MAX_WIDTH);
state.autoFitAllColumns();
const after = state.renderedColumnWidths.value;
// The long text column now uses the full auto-fit allowance instead of the
// density value limit that bounds the default grow-only sizing.
expect(after[1]).toBeGreaterThan(before[1]!);
expect(after[1]).toBeLessThanOrEqual(DATA_GRID_COL_AUTO_FIT_MAX_WIDTH);
expect(after[0]).toBeGreaterThanOrEqual(DATA_GRID_COL_MIN_WIDTH);
expect(after.length).toBe(before.length);
});
it("keeps auto fitted widths when the grid re-initialises for the same columns", () => {
const longText = "y".repeat(220);
const state = createResizeState({
columns: ["id", "note", "extra"],
rows: [[1, longText, "short"]],
});
state.initColumnWidths();
state.autoFitAllColumns();
const fitted = state.renderedColumnWidths.value.slice();
state.initColumnWidths();
expect(state.renderedColumnWidths.value).toEqual(fitted);
});
it("does not change any width when the grid has no columns yet", () => {
const state = createResizeState({ columns: [], rows: [] });
state.initColumnWidths();
state.autoFitAllColumns();
expect(state.renderedColumnWidths.value).toEqual([]);
});
it("keeps compact query result columns at content width instead of filling the viewport", () => {
const state = createResizeState({
columns: ["id", "user_id"],
@@ -184,10 +184,13 @@ export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions)
document.addEventListener("mouseup", onUp);
}
function autoFitColumn(colIdx: number) {
/** Full-content width of one visible column: unlike the default grow-only
* sizing this ignores the density value truncation, so long text columns are
* sized for what the cell actually shows. */
function autoFitColumnWidth(colIdx: number): number | undefined {
const colName = columns.value[colIdx];
if (!colName) return;
columnWidths.value[colIdx] = calculateDataGridColumnWidth({
if (!colName) return undefined;
return calculateDataGridColumnWidth({
columnName: colName,
sampleValues: sampleColumnValues(colIdx),
maxWidth: DATA_GRID_COL_AUTO_FIT_MAX_WIDTH,
@@ -198,10 +201,37 @@ export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions)
headerTextWidth: measureHeaderText?.(colName),
hasIndexIndicator: options.columnIndexIndicators?.value[colIdx] ?? false,
});
}
function autoFitColumn(colIdx: number) {
const width = autoFitColumnWidth(colIdx);
if (width === undefined) return;
columnWidths.value[colIdx] = width;
markColumnUserSized(colIdx);
persistColumnWidths();
}
/** Fit every visible column to its content in one action (issue #9813), so a
* wide table does not need one double-click per column. Manual drag keeps
* working afterwards: the fitted columns are recorded as user-sized, which is
* exactly the state a drag leaves behind. */
function autoFitAllColumns() {
if (columnWidths.value.length !== columns.value.length || columns.value.length === 0) return;
const next = columnWidths.value.slice();
let changed = false;
for (let colIdx = 0; colIdx < columns.value.length; colIdx++) {
const width = autoFitColumnWidth(colIdx);
if (width === undefined) continue;
markColumnUserSized(colIdx);
if (next[colIdx] !== width) {
next[colIdx] = width;
changed = true;
}
}
if (changed) columnWidths.value = next;
persistColumnWidths();
}
const renderedColumnWidths = computed(() => columnWidths.value.slice());
const resolvedRowNumberWidth = computed(() => options.rowNumberWidth?.value ?? DATA_GRID_ROW_NUM_WIDTH);
@@ -245,6 +275,7 @@ export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions)
growColumnWidthsToFitSamples,
onResizeStart,
autoFitColumn,
autoFitAllColumns,
renderedColumnWidths,
totalWidth,
columnVars,