feat(grid): adjust column widths by density
This commit is contained in:
1 parent
b509520382
commit
ae4bbe1564
14 files changed
+623
-330
No files matched your search
@@ -1,14 +1,23 @@
|
||||
import { computed } from "vue";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { computed, nextTick, ref } from "vue";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { DATA_GRID_COL_AUTO_FIT_MAX_WIDTH, DATA_GRID_COL_MAX_WIDTH, DATA_GRID_COL_MIN_WIDTH } from "@/lib/dataGrid/dataGridColumnWidth";
|
||||
import { DATA_GRID_ROW_NUM_WIDTH, resizeDataGridColumnWidth, useDataGridColumnResize } from "@/composables/useDataGridColumnResize";
|
||||
|
||||
function createResizeState(options: { columns: string[]; rows: Array<Array<string | number | boolean | null>>; columnIndexes?: number[] }) {
|
||||
return useDataGridColumnResize({
|
||||
function createResizeState(options: { columns: string[]; rows: Array<Array<string | number | boolean | null>>; columnIndexes?: number[]; density?: "compact" | "standard" | "comfortable"; compactColumnHeaderActions?: boolean }) {
|
||||
const compact = ref(options.compactColumnHeaderActions ?? true);
|
||||
const state = useDataGridColumnResize({
|
||||
columns: computed(() => options.columns),
|
||||
sourceRows: computed(() => options.rows),
|
||||
columnIndexes: computed(() => options.columnIndexes ?? options.columns.map((_, index) => index)),
|
||||
density: ref(options.density ?? "standard"),
|
||||
compactColumnHeaderActions: computed(() => compact.value),
|
||||
});
|
||||
return {
|
||||
...state,
|
||||
setCompact(v: boolean) {
|
||||
compact.value = v;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("useDataGridColumnResize", () => {
|
||||
@@ -62,4 +71,24 @@ describe("useDataGridColumnResize", () => {
|
||||
expect(state.renderedColumnWidths.value).not.toBe(before);
|
||||
expect(state.renderedColumnWidths.value[1]).toBe(before[1] + 40);
|
||||
});
|
||||
|
||||
it("recalculates column widths when compactColumnHeaderActions changes", async () => {
|
||||
const state = createResizeState({
|
||||
columns: ["some_column_name_here"],
|
||||
rows: [["a"]],
|
||||
density: "compact",
|
||||
compactColumnHeaderActions: true,
|
||||
});
|
||||
|
||||
state.initColumnWidths();
|
||||
const widthCompact = state.renderedColumnWidths.value[0];
|
||||
|
||||
state.setCompact(false);
|
||||
await nextTick();
|
||||
|
||||
const widthNonCompact = state.renderedColumnWidths.value[0];
|
||||
// compact: charWidth*nameLen + headerControlWidthCompact(20) = 7*21+20=167
|
||||
// non-compact: 7*21+headerControlWidth(36)=183
|
||||
expect(widthNonCompact).toBeGreaterThan(widthCompact);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ref, computed, watch, type ComputedRef } from "vue";
|
||||
import { calculateDataGridColumnWidth, DATA_GRID_AUTO_FIT_VALUE_TEXT_LIMIT, DATA_GRID_COL_AUTO_FIT_MAX_WIDTH, DATA_GRID_COL_MIN_WIDTH, DATA_GRID_SAMPLE_ROWS } from "@/lib/dataGrid/dataGridColumnWidth";
|
||||
import { ref, 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 } from "@/lib/dataGrid/dataGridColumnWidth";
|
||||
import type { ColumnWidthDensity } from "@/stores/settingsStore";
|
||||
|
||||
type CellValue = string | number | boolean | null;
|
||||
|
||||
@@ -13,10 +14,12 @@ export interface UseDataGridColumnResizeOptions {
|
||||
columns: ComputedRef<string[]>;
|
||||
sourceRows: ComputedRef<CellValue[][]>;
|
||||
columnIndexes: ComputedRef<number[]>;
|
||||
density: Ref<ColumnWidthDensity>;
|
||||
compactColumnHeaderActions: ComputedRef<boolean>;
|
||||
}
|
||||
|
||||
export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions) {
|
||||
const { columns, sourceRows, columnIndexes } = options;
|
||||
const { columns, sourceRows, columnIndexes, density, compactColumnHeaderActions } = options;
|
||||
|
||||
const columnWidths = ref<number[]>([]);
|
||||
let isResizing = false;
|
||||
@@ -25,7 +28,8 @@ export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions)
|
||||
function sampleColumnValues(visibleColIdx: number): CellValue[] {
|
||||
const actualColIdx = columnIndexes.value[visibleColIdx];
|
||||
const rows = sourceRows.value;
|
||||
const end = Math.min(rows.length, DATA_GRID_SAMPLE_ROWS);
|
||||
const preset = COLUMN_WIDTH_DENSITY_PRESETS[density.value];
|
||||
const end = Math.min(rows.length, preset.sampleRows);
|
||||
const values: CellValue[] = [];
|
||||
for (let i = 0; i < end; i++) {
|
||||
values.push(rows[i][actualColIdx] ?? null);
|
||||
@@ -33,20 +37,24 @@ export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions)
|
||||
return values;
|
||||
}
|
||||
|
||||
function initColumnWidths() {
|
||||
function initColumnWidths(force = false) {
|
||||
const previousWidthsByColumnIndex = new Map<number, number>();
|
||||
previousColumnIndexes.forEach((columnIndex, visibleIndex) => {
|
||||
const width = columnWidths.value[visibleIndex];
|
||||
if (width !== undefined) previousWidthsByColumnIndex.set(columnIndex, width);
|
||||
});
|
||||
const nextColumnIndexes = [...columnIndexes.value];
|
||||
if (columnWidths.value.length !== columns.value.length || previousColumnIndexes.join("\0") !== nextColumnIndexes.join("\0")) {
|
||||
if (force || columnWidths.value.length !== columns.value.length || previousColumnIndexes.join("\0") !== nextColumnIndexes.join("\0")) {
|
||||
columnWidths.value = columns.value.map((colName, colIdx) => {
|
||||
const existingWidth = previousWidthsByColumnIndex.get(nextColumnIndexes[colIdx]);
|
||||
if (existingWidth !== undefined) return existingWidth;
|
||||
if (!force) {
|
||||
const existingWidth = previousWidthsByColumnIndex.get(nextColumnIndexes[colIdx]);
|
||||
if (existingWidth !== undefined) return existingWidth;
|
||||
}
|
||||
return calculateDataGridColumnWidth({
|
||||
columnName: colName,
|
||||
sampleValues: sampleColumnValues(colIdx),
|
||||
density: density.value,
|
||||
compactColumnHeaderActions: compactColumnHeaderActions.value,
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -103,6 +111,8 @@ export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions)
|
||||
sampleValues: sampleColumnValues(colIdx),
|
||||
maxWidth: DATA_GRID_COL_AUTO_FIT_MAX_WIDTH,
|
||||
valueTextLimit: DATA_GRID_AUTO_FIT_VALUE_TEXT_LIMIT,
|
||||
density: density.value,
|
||||
compactColumnHeaderActions: compactColumnHeaderActions.value,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -124,7 +134,11 @@ export function useDataGridColumnResize(options: UseDataGridColumnResizeOptions)
|
||||
return isResizing;
|
||||
}
|
||||
|
||||
watch(() => columnIndexes.value.join("\0"), initColumnWidths);
|
||||
watch(
|
||||
() => columnIndexes.value.join("\0"),
|
||||
() => initColumnWidths(),
|
||||
);
|
||||
watch([density, compactColumnHeaderActions], () => initColumnWidths(true));
|
||||
|
||||
return {
|
||||
columnWidths,
|
||||
|
||||
Reference in new issue
Block a user