feat(data-grid): hide selected columns from header context menu
This commit is contained in:
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);
|
||||
});
|
||||
});
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
type TableDataGridColumnOrderChangedDetail,
|
||||
} from "@/lib/dataGrid/dataGridColumnLayoutStorage";
|
||||
import { buildDataGridColumnLookupItems, filterDataGridColumnLookupItems, type DataGridColumnLookupItem } from "@/lib/dataGrid/dataGridColumnLookup";
|
||||
import { hiddenColumnIndexesForKeys, hiddenColumnIndexesWithAllNullColumns, hiddenColumnKeysForIndexes, invertedHiddenColumnIndexes, nextHiddenColumnIndexes, removeAutoHiddenColumnIndexes, visibleColumnIndexesForFilter } from "@/lib/dataGrid/dataGridColumnVisibility";
|
||||
import { hiddenColumnIndexesAfterHiding, hiddenColumnIndexesForKeys, hiddenColumnIndexesWithAllNullColumns, hiddenColumnKeysForIndexes, invertedHiddenColumnIndexes, nextHiddenColumnIndexes, removeAutoHiddenColumnIndexes, visibleColumnIndexesForFilter } from "@/lib/dataGrid/dataGridColumnVisibility";
|
||||
|
||||
export type RenderedDataGridColumn = {
|
||||
visibleColIdx: number;
|
||||
@@ -212,6 +212,24 @@ export function useDataGridColumnLayoutState(options: {
|
||||
persistHiddenColumnKeys();
|
||||
}
|
||||
|
||||
// 批量隐藏:一次 hiddenColumnIndexes 提交、一次持久化,供表头右键菜单使用。
|
||||
function hideColumns(columnIndexes: Iterable<number>) {
|
||||
const requestedIndexes = [...columnIndexes].filter((index) => Number.isInteger(index) && index >= 0);
|
||||
if (requestedIndexes.length === 0) return;
|
||||
hiddenColumnIndexes.value = hiddenColumnIndexesAfterHiding({
|
||||
columnIndexes: requestedIndexes,
|
||||
hiddenIndexes: hiddenColumnIndexes.value,
|
||||
availableIndexes: toValue(options.displayableColumnIndexes),
|
||||
});
|
||||
// hiddenColumnIndexesAfterHiding 只做加法(绝不删列),且 invariant
|
||||
// autoHiddenNullColumnIndexes 始终是 hiddenColumnIndexes 的子集,由
|
||||
// applyNullColumnVisibility / showColumn / toggleColumnVisibility 共同维护,
|
||||
// 所以这里不需要清理 autoHiddenNullColumnIndexes(与之等价的剪枝循环恒不可达)。
|
||||
// 将来若允许传入「已隐藏」的列,正确做法与剪枝相反:该列属于手动隐藏,
|
||||
// 必须从 autoHiddenNullColumnIndexes 中移除,而不是保留。
|
||||
persistHiddenColumnKeys();
|
||||
}
|
||||
|
||||
function showAllColumns() {
|
||||
hiddenColumnIndexes.value = new Set();
|
||||
autoHiddenNullColumnIndexes.value = new Set();
|
||||
@@ -410,6 +428,7 @@ export function useDataGridColumnLayoutState(options: {
|
||||
filteredColumnLayoutOptions,
|
||||
isColumnVisible,
|
||||
toggleColumnVisibility,
|
||||
hideColumns,
|
||||
showAllColumns,
|
||||
invertColumnVisibility,
|
||||
showColumn,
|
||||
|
||||
@@ -224,7 +224,9 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
|
||||
|
||||
function selectColumns(startCol: number, endCol: number, options?: { merge?: boolean }) {
|
||||
const normalizedColumns = normalizeSelectedColumnIndexes(Array.from({ length: Math.abs(endCol - startCol) + 1 }, (_, index) => Math.min(startCol, endCol) + index));
|
||||
if (normalizedColumns.length === 0 || displayItems.value.length <= 0) return;
|
||||
// Column selection is column-based and deliberately independent of the row count, so an
|
||||
// empty result (0 rows, N columns) keeps header multi-select working.
|
||||
if (normalizedColumns.length === 0) return;
|
||||
clearRowSelection();
|
||||
selectedCellKeys.value = new Set();
|
||||
selectionAnchor.value = null;
|
||||
|
||||
Reference in new issue
Block a user