feat(data-grid): hide selected columns from header context menu

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-09-14 12:39:06 +08:00
1 parent 974848d6e2
commit 638aa48f57
22 files changed
+460 -16

No files matched your search

@@ -232,6 +232,83 @@ describe("useDataGridColumnLayout", () => {
expect(JSON.parse(localStorage.getItem("dbx-data-grid-column-layout:visibility-null-column-layout")!)).toMatchObject({ hiddenKeys: ["empty\0\0"] });
});
it("hides a batch of columns in one commit and persists the hidden keys", () => {
const scope = effectScope();
const state = scope.run(() =>
useDataGridColumnLayoutState({
columns: ref(["id", "name", "email", "phone"]),
sourceColumns: ref(undefined),
commentByColumn: ref(new Map()),
displayableColumnIndexes: ref([0, 1, 2, 3]),
allNullColumnIndexes: ref([]),
columnOrderKeys: ref(["id\0\0", "name\0\0", "email\0\0", "phone\0\0"]),
layoutScopeKey: ref("batch-hide-layout"),
tableScopeKey: ref(""),
}),
)!;
state.hideColumns([1, 2]);
expect(state.visibleColumnIndexes.value).toEqual([0, 3]);
expect(state.hiddenColumnCount.value).toBe(2);
// 重复索引与已隐藏索引不会改变结果(幂等)
state.hideColumns([1, 2]);
expect(state.visibleColumnIndexes.value).toEqual([0, 3]);
state.showAllColumns();
expect(state.visibleColumnIndexes.value).toEqual([0, 1, 2, 3]);
state.hideColumns([3]);
state.toggleColumnVisibility(0);
scope.stop();
expect(loadDataGridColumnLayout("batch-hide-layout")?.hiddenKeys).toEqual(["id\0\0", "phone\0\0"]);
});
it("keeps at least one column visible when a batch would hide every column", () => {
const scope = effectScope();
const state = scope.run(() =>
useDataGridColumnLayoutState({
columns: ref(["id", "name", "email"]),
sourceColumns: ref(undefined),
commentByColumn: ref(new Map()),
displayableColumnIndexes: ref([0, 1, 2]),
allNullColumnIndexes: ref([]),
columnOrderKeys: ref(["id\0\0", "name\0\0", "email\0\0"]),
layoutScopeKey: ref("batch-hide-last-layout"),
tableScopeKey: ref(""),
}),
)!;
state.hideColumns([1, 2]);
expect(state.visibleColumnIndexes.value).toEqual([0]);
// 只剩一列可见时再隐藏它:保持不变,不会变成空网格
state.hideColumns([0]);
expect(state.visibleColumnIndexes.value).toEqual([0]);
scope.stop();
});
it("treats an empty batch hide as a no-op", () => {
const scope = effectScope();
const state = scope.run(() =>
useDataGridColumnLayoutState({
columns: ref(["id", "name", "email"]),
sourceColumns: ref(undefined),
commentByColumn: ref(new Map()),
displayableColumnIndexes: ref([0, 1, 2]),
allNullColumnIndexes: ref([]),
columnOrderKeys: ref(["id\0\0", "name\0\0", "email\0\0"]),
layoutScopeKey: ref("batch-hide-empty-layout"),
tableScopeKey: ref(""),
}),
)!;
state.hideColumns([]);
expect(state.visibleColumnIndexes.value).toEqual([0, 1, 2]);
scope.stop();
});
it("returns ordered layout options with visibility state and reorders hidden fields", () => {
const scope = effectScope();
const state = scope.run(() =>
@@ -9,19 +9,23 @@ function createSelection(options?: {
onUserCellSelection?: () => void;
shouldUpdateDraggedRowsImmediately?: () => boolean;
onDraggedRowSelectionChange?: () => void;
displayItemCount?: number;
}) {
const columns = computed(() => ["id", "name", "email"]);
const displayItems = computed(() =>
[1, 2, 3, 4].map((id, index) => ({
id,
sourceIndex: index,
data: [id, `name-${id}`, `user-${id}@example.com`],
isNew: false,
isDraft: false,
isDeleted: false,
isDirtyCol: [false, false, false],
status: "clean",
})),
Array.from({ length: options?.displayItemCount ?? 4 }, (_, index) => {
const id = index + 1;
return {
id,
sourceIndex: index,
data: [id, `name-${id}`, `user-${id}@example.com`],
isNew: false,
isDraft: false,
isDeleted: false,
isDirtyCol: [false, false, false],
status: "clean",
};
}),
);
return useDataGridSelection({
@@ -685,4 +689,40 @@ describe("useDataGridSelection", () => {
Object.defineProperty(globalThis, "document", { configurable: true, value: originalDocument });
}
});
it("selects a header column on a zero-row result", () => {
const selection = createSelection({ displayItemCount: 0 });
selection.selectColumn(1);
expect(selection.hasColumnSelection.value).toBe(true);
expect(selection.selectedColumnIndexes.value).toEqual(new Set([1]));
});
it("selects a header column range on a zero-row result", () => {
const selection = createSelection({ displayItemCount: 0 });
selection.selectColumns(0, 2);
expect(selection.hasColumnSelection.value).toBe(true);
expect(selection.selectedColumnIndexes.value).toEqual(new Set([0, 1, 2]));
});
it("merges a shift-click header selection on a zero-row result", () => {
const selection = createSelection({ displayItemCount: 0 });
selection.selectColumn(1);
selection.selectColumn(3, rowEvent({ shift: true }));
expect(selection.selectedColumnIndexes.value).toEqual(new Set([1, 2, 3]));
});
it("keeps discrete cell selection empty when selecting header columns on a zero-row result", () => {
const selection = createSelection({ displayItemCount: 0 });
selection.selectColumn(1);
expect(selection.selectedCellKeys.value).toEqual(new Set());
expect(selection.hasCellSelection.value).toBe(false);
});
});